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
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.
"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.
"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.
"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.
"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.
"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.
"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.
"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.
"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.
"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.
"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.
"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.
"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.
"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.
"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.
"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.
"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.
"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.
"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.
"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.
"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.
"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.
"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.
"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.
"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.
"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.
"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.
"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.
"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.
"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.
"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.
"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.
"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.
"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.
"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.
"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.
"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.
"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.
"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.
"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.
"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.
"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.
{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.
{ 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.
[: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.
[: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.
[: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
contentattributes of one span hold whenmax_content_bytessets no other bound. 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.
[: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.
%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.
:requestand:responsemap an attribute to[path, type]in that body,:request_headersto[header, type]. { 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
-
.api_call_span_attributes(client, req) ⇒ Hash{String=>Object}
private
A sampler reads only the attributes a span starts with, so everything known before the first attempt is here.
-
.client_option(option) ⇒ Hash{Symbol=>Object}
private
The settings of a client's spans, frozen: its
open_telemetryoption with Symbol keys, where:tracesis a Hash that has every one of TRACES_KEYS. -
.content?(option) ⇒ Boolean
private
Whether a client's spans have the
contentattributes. -
.end_api_call_span(span, result) ⇒ void
private
The status is
ERRORforresult[:error],OKforresult[:data], and unset when the call has neither because the application stopped it, such as withInterrupt. -
.end_api_call_span_when_stream_ends(span, stream, result) ⇒ Anthropic::Internal::Type::BaseStream
private
Returns the stream that the application gets in place of
stream: it endsspanwhen the read ends (Tracing.iterate_and_end_span). -
.inject(span) ⇒ Hash{String=>String}
private
The trace context of
spanas request headers, with lowercase names: none when the propagator fails. -
.max_content_bytes(option) ⇒ Integer
private
The most bytes the
contentattributes of one of a client's spans hold. -
.propagation?(option) ⇒ Boolean
private
Whether a client's requests carry the trace context of their span.
-
.record_send(span, client, request, url) ⇒ void
private
The first send sets the URL attributes (Tracing.url_attributes).
-
.register_recorder(span_name, recorder) ⇒ void
private
span_nameisnilfor every API call. -
.request_option(option) ⇒ Hash{Symbol=>Object}
private
The
open_telemetryrequest option with Symbol keys. -
.set_error_status(span, error) ⇒ void
private
Anything but a
StandardError, such asInterrupt, is the application stopping the operation, which leaves the status unset. -
.set_response_attributes(span, span_name, response) ⇒ void
private
A field that
responselacks, or whose type is not the attribute's, sets nothing. -
.start_api_call_span(tracer, req, propagation:) ⇒ OpenTelemetry::Trace::Span?
private
Returns
nilwhenreqhas nospan_name, or when the span neither records nor propagates a trace context: withpropagationoff, a span whose context is valid is kept, for the application's HTTP instrumentation to propagate. -
.trace_header_names(span) ⇒ Array<String>
private
The lowercase names of the headers that carry a trace context.
-
.tracer(option, scope = INSTRUMENTATION_SCOPE) ⇒ OpenTelemetry::Trace::Tracer?
private
The tracer for a client, or
nilwhen its spans are disabled.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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 |