Module: Anthropic::Internal::Tracing Private

Defined in:
lib/anthropic/internal/tracing.rb,
sig/anthropic/internal/tracing.rbs

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

One CLIENT span per API call: every retry of an SDK method's request, and reading the stream it returns. The SDK emits no span per HTTP attempt.

Defined Under Namespace

Modules: SpanName, _Recorder

Constant Summary collapse

INSTRUMENTATION_SCOPE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • ("com.anthropic.sdk.ruby")
"com.anthropic.sdk.ruby"
ERROR_TYPE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • ("error.type")
"error.type"
HTTP_REQUEST_METHOD =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • ("http.request.method")
"http.request.method"
HTTP_REQUEST_RESEND_COUNT =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • ("http.request.resend_count")
"http.request.resend_count"
HTTP_RESPONSE_STATUS_CODE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • ("http.response.status_code")
"http.response.status_code"
HTTP_ROUTE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • ("http.route")
"http.route"
SERVER_ADDRESS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • ("server.address")
"server.address"
SERVER_PORT =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • ("server.port")
"server.port"
URL_FULL =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • ("url.full")
"url.full"
URL_PATH =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • ("url.path")
"url.path"
URL_QUERY =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • ("url.query")
"url.query"
URL_SCHEME =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • ("url.scheme")
"url.scheme"
GEN_AI_PROVIDER_NAME =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • ("gen_ai.provider.name")
"gen_ai.provider.name"
ANTHROPIC_REQUEST_ID =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • ("anthropic.request.id")
"anthropic.request.id"
ANTHROPIC_ORGANIZATION_ID =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • ("anthropic.organization.id")
"anthropic.organization.id"
ANTHROPIC_WORKSPACE_ID =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • ("anthropic.workspace.id")
"anthropic.workspace.id"
GEN_AI_OPERATION_NAME =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • ("gen_ai.operation.name")
"gen_ai.operation.name"
GEN_AI_REQUEST_MODEL =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • ("gen_ai.request.model")
"gen_ai.request.model"
GEN_AI_REQUEST_MAX_TOKENS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • ("gen_ai.request.max_tokens")
"gen_ai.request.max_tokens"
GEN_AI_REQUEST_TEMPERATURE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • ("gen_ai.request.temperature")
"gen_ai.request.temperature"
GEN_AI_REQUEST_TOP_P =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • ("gen_ai.request.top_p")
"gen_ai.request.top_p"
GEN_AI_REQUEST_TOP_K =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • ("gen_ai.request.top_k")
"gen_ai.request.top_k"
GEN_AI_REQUEST_REASONING_LEVEL =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • ("gen_ai.request.reasoning.level")
"gen_ai.request.reasoning.level"
ANTHROPIC_REQUEST_SERVICE_TIER =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • ("anthropic.request.service_tier")
"anthropic.request.service_tier"
ANTHROPIC_THINKING_TYPE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • ("anthropic.thinking.type")
"anthropic.thinking.type"
ANTHROPIC_THINKING_BUDGET_TOKENS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • ("anthropic.thinking.budget_tokens")
"anthropic.thinking.budget_tokens"
ANTHROPIC_BETA =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • ("anthropic.beta")
"anthropic.beta"
GEN_AI_REQUEST_STREAM =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • ("gen_ai.request.stream")
"gen_ai.request.stream"
GEN_AI_RESPONSE_ID =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • ("gen_ai.response.id")
"gen_ai.response.id"
GEN_AI_RESPONSE_MODEL =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • ("gen_ai.response.model")
"gen_ai.response.model"
GEN_AI_USAGE_CACHE_WRITE_INPUT_TOKENS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • ("gen_ai.usage.cache_write.input_tokens")
"gen_ai.usage.cache_write.input_tokens"
GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • ("gen_ai.usage.cache_read.input_tokens")
"gen_ai.usage.cache_read.input_tokens"
ANTHROPIC_USAGE_CACHE_CREATION_EPHEMERAL_5M_INPUT_TOKENS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • ("anthropic.usage.cache_creation.ephemeral_5m_input_tokens")
"anthropic.usage.cache_creation.ephemeral_5m_input_tokens"
ANTHROPIC_USAGE_CACHE_CREATION_EPHEMERAL_1H_INPUT_TOKENS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • ("anthropic.usage.cache_creation.ephemeral_1h_input_tokens")
"anthropic.usage.cache_creation.ephemeral_1h_input_tokens"
ANTHROPIC_RESPONSE_SERVICE_TIER =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • ("anthropic.response.service_tier")
"anthropic.response.service_tier"
GEN_AI_USAGE_OUTPUT_TOKENS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • ("gen_ai.usage.output_tokens")
"gen_ai.usage.output_tokens"
GEN_AI_USAGE_REASONING_OUTPUT_TOKENS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • ("gen_ai.usage.reasoning.output_tokens")
"gen_ai.usage.reasoning.output_tokens"
ANTHROPIC_MESSAGE_STOP_REASON =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • ("anthropic.message.stop_reason")
"anthropic.message.stop_reason"
ANTHROPIC_MESSAGE_COUNT_TOKENS_INPUT_TOKENS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • ("anthropic.message.count_tokens.input_tokens")
"anthropic.message.count_tokens.input_tokens"
ANTHROPIC_SESSION_ENVIRONMENT_ID =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • ("anthropic.session.environment.id")
"anthropic.session.environment.id"
GEN_AI_AGENT_NAME =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • ("gen_ai.agent.name")
"gen_ai.agent.name"
CLIENT_ATTRIBUTES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • (::Hash[String, String])
{GEN_AI_PROVIDER_NAME => "anthropic"}.freeze
RESPONSE_HEADER_ATTRIBUTES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Each attribute with the response header it comes from.

Returns:

  • (::Hash[String, String])
{
  ANTHROPIC_REQUEST_ID => "request-id",
  ANTHROPIC_ORGANIZATION_ID => "anthropic-organization-id",
  ANTHROPIC_WORKSPACE_ID => "anthropic-workspace-id"
}.freeze
CLIENT_OPTION_KEYS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • (::Array[Symbol])
[:tracer_provider, :propagation, :traces].freeze
TRACES_KEYS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • (::Array[Symbol])
[:enabled, :content_mode, :max_content_bytes].freeze
CONTENT_MODES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • (::Array[Symbol])
[:metadata_only, :content].freeze
DEFAULT_MAX_CONTENT_BYTES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The most bytes the content attributes of one span hold when max_content_bytes sets no other bound.

Returns:

  • (Integer)
750_000
REQUEST_OPTION_KEYS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • (::Array[Symbol])
[:conversation_id].freeze
CONTENT_QUERY_PARAMETERS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Query parameters whose value is content on every endpoint that defines them.

Returns:

  • (::Array[String])
%w[email path_prefix expected_content_sha256].freeze
SPAN_ATTRIBUTES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

By span name. :request and :response map an attribute to [path, type] in that body, :request_headers to [header, type].

Returns:

  • (::Hash[String, ::Hash[Symbol, top]])
{
  SpanName::MESSAGES_CREATE => {
    constants: {
      GEN_AI_OPERATION_NAME => "chat"
    },
    request: {
      GEN_AI_REQUEST_MODEL => [[:model], :string],
      GEN_AI_REQUEST_MAX_TOKENS => [[:max_tokens], :integer],
      GEN_AI_REQUEST_TEMPERATURE => [[:temperature], :any],
      GEN_AI_REQUEST_TOP_P => [[:top_p], :any],
      GEN_AI_REQUEST_TOP_K => [[:top_k], :any],
      GEN_AI_REQUEST_REASONING_LEVEL => [[:output_config, :effort], :string],
      ANTHROPIC_REQUEST_SERVICE_TIER => [[:service_tier], :string],
      ANTHROPIC_THINKING_TYPE => [[:thinking, :type], :string],
      ANTHROPIC_THINKING_BUDGET_TOKENS => [[:thinking, :budget_tokens], :integer]
    },
    request_headers: {
      ANTHROPIC_BETA => ["anthropic-beta", [:string]]
    },
    stream: GEN_AI_REQUEST_STREAM,
    response: {
      GEN_AI_RESPONSE_ID => [[:id], :string],
      GEN_AI_RESPONSE_MODEL => [[:model], :string],
      GEN_AI_USAGE_CACHE_WRITE_INPUT_TOKENS => [[:usage, :cache_creation_input_tokens], :integer],
      GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS => [[:usage, :cache_read_input_tokens], :integer],
      ANTHROPIC_USAGE_CACHE_CREATION_EPHEMERAL_5M_INPUT_TOKENS => [
        [:usage, :cache_creation, :ephemeral_5m_input_tokens], :integer
      ],
      ANTHROPIC_USAGE_CACHE_CREATION_EPHEMERAL_1H_INPUT_TOKENS => [
        [:usage, :cache_creation, :ephemeral_1h_input_tokens], :integer
      ],
      ANTHROPIC_RESPONSE_SERVICE_TIER => [[:usage, :service_tier], :string],
      GEN_AI_USAGE_OUTPUT_TOKENS => [[:usage, :output_tokens], :integer],
      GEN_AI_USAGE_REASONING_OUTPUT_TOKENS => [
        [:usage, :output_tokens_details, :thinking_tokens],
        :integer
      ],
      ANTHROPIC_MESSAGE_STOP_REASON => [[:stop_reason], :string]
    }
  },
  SpanName::MESSAGES_COUNT_TOKENS => {
    request: {
      GEN_AI_REQUEST_MODEL => [[:model], :string]
    },
    response: {
      ANTHROPIC_MESSAGE_COUNT_TOKENS_INPUT_TOKENS => [[:input_tokens], :integer]
    }
  },
  SpanName::SESSIONS_CREATE => {
    request: {
      ANTHROPIC_SESSION_ENVIRONMENT_ID => [[:environment_id], :string]
    },
    response: {
      GEN_AI_AGENT_NAME => [[:agent, :name], :string],
      GEN_AI_REQUEST_MODEL => [[:agent, :model, :id], :string]
    }
  }
}.freeze

Class Method Summary collapse

Class Method Details

.api_call_span_attributes(client, req) ⇒ Hash{String=>Object}

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

A sampler reads only the attributes a span starts with, so everything known before the first attempt is here.

Parameters:

Returns:

  • (Hash{String=>Object})


654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
# File 'lib/anthropic/internal/tracing.rb', line 654

def api_call_span_attributes(client, req)
  url = client.base_url
  name = req[:span_name]
  attributes = {
    SERVER_ADDRESS => url.hostname,
    SERVER_PORT => url.port || {"https" => 443, "http" => 80}[url.scheme],
    HTTP_REQUEST_METHOD => req[:method].to_s.upcase,
    HTTP_ROUTE => req[:span_route],
    **client.open_telemetry_attributes,
    **recording(name) { request_attributes(client, req) }.to_h,
    **req[:span_attributes].to_h
  }
  recorders(name).each do |recorder|
    attributes.update(recording(name) { recorder.start_attributes(client, req) }.to_h)
  end
  attributes.compact
end

.client_option(option) ⇒ Hash{Symbol=>Object}

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The settings of a client's spans, frozen: its open_telemetry option with Symbol keys, where :traces is a Hash that has every one of TRACES_KEYS. true and false are {traces: <it>}, and a :traces of true or false is {enabled: <it>}. nil, for a client that is given no option, is what the environment says, and the environment is read here and nowhere else. Any other option is complete: what it leaves unset has the default, and no variable is read.

Parameters:

  • option (Hash{Symbol=>Object}, Boolean, nil)

Returns:

  • (Hash{Symbol=>Object})

Raises:

  • (ArgumentError)


529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
# File 'lib/anthropic/internal/tracing.rb', line 529

def client_option(option)
  option = environment_option if option.nil?
  option = {traces: option} if [true, false].include?(option)
  option = option_hash(option, CLIENT_OPTION_KEYS, "`open_telemetry`", "true, false or a Hash")
  propagation = option[:propagation]
  unless [true, false, nil].include?(propagation)
    raise ArgumentError.new("`open_telemetry` [:propagation] must be true or false, got #{plain(propagation)}")
  end
  traces = option[:traces].nil? ? {} : option[:traces]
  traces = {enabled: traces} if [true, false].include?(traces)
  traces = option_hash(traces, TRACES_KEYS, "`open_telemetry` [:traces]", "true, false or a Hash")
  enabled, mode, bytes = traces.values_at(*TRACES_KEYS)
  unless [true, false, nil].include?(enabled)
    raise ArgumentError.new("`open_telemetry` [:traces][:enabled] must be true or false, got #{plain(enabled)}")
  end
  unless [nil, *CONTENT_MODES, *CONTENT_MODES.map(&:to_s)].include?(mode)
    modes = CONTENT_MODES.map(&:inspect).join(" or ")
    raise ArgumentError.new("`open_telemetry` [:traces][:content_mode] must be #{modes}, got #{plain(mode)}")
  end
  unless bytes.nil? || (bytes.is_a?(Integer) && bytes >= 1)
    raise ArgumentError.new(
      "`open_telemetry` [:traces][:max_content_bytes] must be an Integer of 1 or more, got #{plain(bytes)}"
    )
  end
  {
    tracer_provider: option[:tracer_provider],
    propagation: propagation.nil? || propagation,
    traces: {
      enabled: enabled.nil? || enabled,
      content_mode: mode&.to_sym || :metadata_only,
      max_content_bytes: bytes || DEFAULT_MAX_CONTENT_BYTES
    }.freeze
  }.freeze
end

.content?(option) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Whether a client's spans have the content attributes.

Parameters:

Returns:



593
# File 'lib/anthropic/internal/tracing.rb', line 593

def content?(option) = option[:traces][:content_mode] == :content

.end_api_call_span(span, result) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

The status is ERROR for result[:error], OK for result[:data], and unset when the call has neither because the application stopped it, such as with Interrupt.

Parameters:

  • span (OpenTelemetry::Trace::Span)
  • result (Hash{Symbol=>Object}) —

    the result of the API call, as far as the call got

    @option result [HashSymbol=>Object] :req the request, as the SDK method gave it to BaseClient#request

    @option result [Anthropic::Internal::Transport::BaseClient] :client the client that makes the call

    @option result [Float] :start_time when the call started, on the monotonic clock

    @option result [Integer, nil] :status the status of the final response, when an attempt received one

    @option result [HashString=>String, nil] :headers the headers of the final response

    @option result [Object] :data the decoded body, or a stream that reads it, when the call returned

    @option result [StandardError, nil] :error what the call failed with

    @option result [Enumerable, nil] :iterator the enumerator of a stream in :data: a recorder that follows the stream replaces it with one that reads through it

    @option result [Object] :streamed_message what such a recorder keeps of the stream, for the SDK's stream helpers

    
    
    774
    775
    776
    777
    778
    779
    780
    781
    782
    783
    784
    785
    786
    787
    788
    789
    # File 'lib/anthropic/internal/tracing.rb', line 774
    
    def end_api_call_span(span, result)
      recording(result[:req][:span_name]) do
        next unless span.recording?
    
        set_response_status_code(span, result)
        set_response_header_attributes(span, result)
        set_created_id(span, result)
        record_result(span, result)
        if result[:error]
          set_error_status(span, result[:error])
        elsif result.key?(:data)
          span.status = OpenTelemetry::Trace::Status.ok
        end
        span.finish
      end
    end

    .end_api_call_span_when_stream_ends(span, stream, result) ⇒ Anthropic::Internal::Type::BaseStream

    This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

    Returns the stream that the application gets in place of stream: it ends span when the read ends (iterate_and_end_span). result is as for end_api_call_span.

    Parameters:

    Returns:

    
    
    800
    801
    802
    803
    804
    805
    806
    807
    808
    809
    810
    811
    812
    813
    # File 'lib/anthropic/internal/tracing.rb', line 800
    
    def end_api_call_span_when_stream_ends(span, stream, result)
      recording(result[:req][:span_name]) do
        next stream unless span.recording?
    
        set_response_status_code(span, result)
        set_response_header_attributes(span, result)
        # A recorder that follows the stream wraps `result[:iterator]` first, so its read ends before the span does.
        result[:iterator] = stream.to_enum
        record_result(span, result)
        sse = stream.is_a?(Anthropic::Internal::Stream)
        iterator = iterate_and_end_span(result[:iterator], span, sse)
        Anthropic::Internal::TracedStream.new(stream, iterator, result[:streamed_message])
      end || stream
    end

    .inject(span) ⇒ Hash{String=>String}

    This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

    The trace context of span as request headers, with lowercase names: none when the propagator fails.

    Parameters:

    • span (OpenTelemetry::Trace::Span)

    Returns:

    • (Hash{String=>String})
    
    
    720
    721
    722
    723
    724
    725
    726
    # File 'lib/anthropic/internal/tracing.rb', line 720
    
    def inject(span)
      carrier = {}
      recording(nil) do
        OpenTelemetry.propagation.inject(carrier, context: OpenTelemetry::Trace.context_with_span(span))
        carrier.transform_keys { _1.to_s.downcase }
      end || {}
    end

    .max_content_bytes(option) ⇒ Integer

    This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

    The most bytes the content attributes of one of a client's spans hold.

    Parameters:

    Returns:

    • (Integer)
    
    
    600
    # File 'lib/anthropic/internal/tracing.rb', line 600
    
    def max_content_bytes(option) = option[:traces][:max_content_bytes]

    .propagation?(option) ⇒ Boolean

    This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

    Whether a client's requests carry the trace context of their span.

    Parameters:

    Returns:

    
    
    607
    # File 'lib/anthropic/internal/tracing.rb', line 607
    
    def propagation?(option) = option[:propagation]

    .record_send(span, client, request, url) ⇒ void

    This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

    This method returns an undefined value.

    The first send sets the URL attributes (url_attributes). Every later one, a retry or a followed redirect, counts toward http.request.resend_count.

    Parameters:

    
    
    684
    685
    686
    687
    688
    689
    690
    691
    692
    693
    694
    695
    696
    697
    # File 'lib/anthropic/internal/tracing.rb', line 684
    
    def record_send(span, client, request, url)
      sends = request[:sends] = request[:sends].to_i + 1
      recording(nil) do
        next unless span.recording?
    
        if sends == 1
          span.add_attributes(
            url_attributes(client, url, request[:span_query].to_a, request[:span_content_query].to_a)
          )
        else
          span.set_attribute(HTTP_REQUEST_RESEND_COUNT, sends - 1)
        end
      end
    end

    .register_recorder(span_name, recorder) ⇒ void

    This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

    This method returns an undefined value.

    span_name is nil for every API call. The span starts with what recorder.start_attributes(client, req) returns, without its nil values, and recorder.record(span, result) runs right before the span ends, or before a streamed body takes the span with it. An error that either raises is logged, and does not fail the API call.

    Parameters:

    • span_name (String, nil) —

      one of SpanName

    • recorder (#start_attributes, #record)
    
    
    642
    643
    644
    # File 'lib/anthropic/internal/tracing.rb', line 642
    
    def register_recorder(span_name, recorder)
      @recorders = {**@recorders, span_name => [*@recorders[span_name], recorder].freeze}.freeze
    end

    .request_option(option) ⇒ Hash{Symbol=>Object}

    This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

    The open_telemetry request option with Symbol keys.

    Parameters:

    • option (Hash{Symbol=>Object}, nil)

    Returns:

    • (Hash{Symbol=>Object})

    Raises:

    • (ArgumentError)
    
    
    570
    571
    572
    # File 'lib/anthropic/internal/tracing.rb', line 570
    
    def request_option(option)
      option_hash(option.nil? ? {} : option, REQUEST_OPTION_KEYS, "Request `open_telemetry`", "a Hash")
    end

    .set_error_status(span, error) ⇒ void

    This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

    This method returns an undefined value.

    Anything but a StandardError, such as Interrupt, is the application stopping the operation, which leaves the status unset.

    Parameters:

    • span (OpenTelemetry::Trace::Span)
    • error (Exception)
    
    
    822
    823
    824
    825
    826
    827
    # File 'lib/anthropic/internal/tracing.rb', line 822
    
    def set_error_status(span, error)
      return unless error.is_a?(StandardError)
    
      span.status = OpenTelemetry::Trace::Status.error
      span.set_attribute(ERROR_TYPE, error_type(error))
    end

    .set_response_attributes(span, span_name, response) ⇒ void

    This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

    This method returns an undefined value.

    A field that response lacks, or whose type is not the attribute's, sets nothing.

    Parameters:

    
    
    707
    708
    709
    710
    711
    712
    # File 'lib/anthropic/internal/tracing.rb', line 707
    
    def set_response_attributes(span, span_name, response)
      SPAN_ATTRIBUTES.dig(span_name, :response).to_h.each do |attribute, (path, type)|
        value = attribute_value(dig(response, path), type)
        span.set_attribute(attribute, value) unless value.nil?
      end
    end

    .start_api_call_span(tracer, req, propagation:) ⇒ OpenTelemetry::Trace::Span?

    This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

    Returns nil when req has no span_name, or when the span neither records nor propagates a trace context: with propagation off, a span whose context is valid is kept, for the application's HTTP instrumentation to propagate. A span that cannot start is nil too, and the call goes on without one.

    Parameters:

    • tracer (OpenTelemetry::Trace::Tracer)
    • req (Hash{Symbol=>Object}) —

      the request, as the SDK method gave it to BaseClient#request

    • propagation (Boolean) —

      whether the client's requests carry the trace context

    • propagation: (Boolean)

    Returns:

    • (OpenTelemetry::Trace::Span, nil)
    
    
    621
    622
    623
    624
    625
    626
    627
    628
    629
    # File 'lib/anthropic/internal/tracing.rb', line 621
    
    def start_api_call_span(tracer, req, propagation:)
      name = req[:span_name]
      return nil if name.nil?
    
      recording(name) do
        span = tracer.start_span(name, attributes: req[:span_attributes], kind: :client)
        span if span.recording? || (propagation ? !inject(span).empty? : span.context.valid?)
      end
    end

    .trace_header_names(span) ⇒ Array<String>

    This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

    The lowercase names of the headers that carry a trace context. A propagator may write headers that it does not list, so those inject writes for span count too. A propagator that fails to list its headers counts as listing the standard ones.

    Parameters:

    • span (OpenTelemetry::Trace::Span, nil)

    Returns:

    • (Array<String>)
    
    
    736
    737
    738
    739
    740
    741
    742
    743
    744
    # File 'lib/anthropic/internal/tracing.rb', line 736
    
    def trace_header_names(span)
      standard = %w[traceparent tracestate baggage]
      names = recording(nil) do
        propagation = OpenTelemetry.propagation
        fields = propagation.respond_to?(:fields) ? propagation.fields : standard
        fields.map { _1.to_s.downcase }
      end || standard
      span ? names | inject(span).keys : names
    end

    .tracer(option, scope = INSTRUMENTATION_SCOPE) ⇒ OpenTelemetry::Trace::Tracer?

    This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

    The tracer for a client, or nil when its spans are disabled.

    Parameters:

    • option (Hash{Symbol=>Object}) —
    • scope (String) (defaults to: INSTRUMENTATION_SCOPE)

    Returns:

    • (OpenTelemetry::Trace::Tracer, nil)
    
    
    581
    582
    583
    584
    585
    586
    # File 'lib/anthropic/internal/tracing.rb', line 581
    
    def tracer(option, scope = INSTRUMENTATION_SCOPE)
      return nil unless option[:traces][:enabled]
    
      provider = option[:tracer_provider] || OpenTelemetry.tracer_provider
      provider.tracer(scope, Anthropic::VERSION)
    end