ExLLM.Infrastructure.Telemetry.Instrumentation (ex_llm v0.8.1)

View Source

Helper module for adding telemetry instrumentation to ExLLM operations.

This module provides macros and functions to easily add telemetry to existing code with minimal changes. It follows the patterns established in the centralized telemetry module.

Usage

Basic Instrumentation

defmodule MyModule do
  import ExLLM.Infrastructure.Telemetry.Instrumentation

  def my_function(args) do
    instrument [:ex_llm, :my_module, :my_function], %{args: args} do
      # Your code here
      {:ok, result}
    end
  end
end

Provider Instrumentation

defmodule MyAdapter do
  import ExLLM.Infrastructure.Telemetry.Instrumentation

  def call(model, messages, opts) do
    instrument_provider :my_provider, model, opts do
      # API call here
      {:ok, response}
    end
  end
end

HTTP Instrumentation

def post_json(url, body, headers, opts) do
  instrument_http :post, url do
    HTTPClient.post(url, body, headers)
  end
end

Summary

Functions

Helper to check if a cost threshold has been exceeded.

Helper to extract usage information from various response formats.

Instrument a block of code with telemetry events.

Instrument cache operations.

Instrument cache store operations.

Instrument HTTP requests with standard metadata.

Instrument a provider API call with standard metadata.

Instrument streaming operations.

Functions

check_cost_threshold(cost_cents, threshold_cents)

Helper to check if a cost threshold has been exceeded.

extract_usage_metadata(response)

Helper to extract usage information from various response formats.

instrument(event_prefix, metadata \\ %{}, list)

(macro)

Instrument a block of code with telemetry events.

Automatically emits start/stop/exception events and measures duration.

instrument_cache_lookup(key, lookup_fn)

Instrument cache operations.

instrument_cache_store(key, value, ttl, store_fn)

Instrument cache store operations.

instrument_context_truncation(messages_before, messages_after, tokens_removed)

Instrument context truncation.

instrument_http(method, url, opts \\ [], list)

(macro)

Instrument HTTP requests with standard metadata.

instrument_provider(provider, model, opts \\ [], list)

(macro)

Instrument a provider API call with standard metadata.

instrument_session_add_message(session_id, message, tokens)

Instrument session operations.

instrument_stream_chunk(provider, model, chunk_size)

instrument_stream_complete(provider, model, total_chunks, duration)

instrument_stream_start(provider, model)

Instrument streaming operations.