Class: Anthropic::Helpers::Tracing::StreamedMessage Private

Inherits:
Object
  • Object
show all
Defined in:
lib/anthropic/helpers/tracing/streamed_message.rb

Overview

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

What a span records of a streamed Message, as far as the stream has been read. None of its methods raises. The content is not rebuilt here: it comes from the Message that a stream helper accumulates (accumulate_streamed_message), so a raw stream has none.

Constant Summary collapse

PROVISIONAL_USAGE =

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 counts of message_start that a later message_delta replaces with the final ones.

[:output_tokens, :output_tokens_details, :iterations].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(span, params:, start_time:, content:, content_limit: CONTENT_LIMIT) ⇒ StreamedMessage

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 a new instance of StreamedMessage.

Parameters:

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

    the request's body

  • start_time (Float) —

    when the API call started, on the monotonic clock

  • content (Boolean) —

    whether the span has the content attributes

  • content_limit (Integer) (defaults to: CONTENT_LIMIT) —

    the most bytes the content attributes hold



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/anthropic/helpers/tracing/streamed_message.rb', line 95

def initialize(span, params:, start_time:, content:, content_limit: CONTENT_LIMIT)
  @span = span
  @params = params
  @start_time = start_time
  @content = content
  @content_limit = content_limit
  @timed = false
  @message = nil
  @snapshot = nil
  @stopped = false
  @recorded = false
end

Instance Attribute Details

#snapshot=(value) ⇒ void (writeonly)

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.



88
89
90
# File 'lib/anthropic/helpers/tracing/streamed_message.rb', line 88

def snapshot=(value)
  @snapshot = value
end

Instance Method Details

#ended_by_server ⇒ 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.

The server ending the stream is the only end that sets a status here: OK after message_stop, and api_error before it, where the server cut the stream short.



150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/anthropic/helpers/tracing/streamed_message.rb', line 150

def ended_by_server
  # A second read of the stream ends at once, after the span.
  return if @recorded

  Tracing.recording("a streamed message") do
    if @stopped
      @span.status = OpenTelemetry::Trace::Status.ok
    else
      @span.status = OpenTelemetry::Trace::Status.error
      @span.set_attribute(Anthropic::Internal::Tracing::ERROR_TYPE, "api_error")
    end
  end
end

#observe(event) ⇒ 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.



109
110
111
# File 'lib/anthropic/helpers/tracing/streamed_message.rb', line 109

def observe(event)
  Tracing.recording("a streamed message") { fold(event) }
end

#set_attributes ⇒ 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.

Only the first call does anything.



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/anthropic/helpers/tracing/streamed_message.rb', line 165

def set_attributes
  return if @recorded

  @recorded = true
  Tracing.recording("a streamed message") do
    unless @message.nil?
      Anthropic::Internal::Tracing.set_response_attributes(
        @span,
        Anthropic::Internal::Tracing::SpanName::MESSAGES_CREATE,
        @message
      )
      Tracing.set_computed_usage_attributes(@span, @message[:usage])
    end
  end
  set_content
end