Class: OpenAI::Internal::Logging::Context Private

Inherits:
Object
  • Object
show all
Defined in:
lib/openai/internal/logging.rb,
sig/openai/internal/logging.rbs

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.

Instance Method Summary collapse

Constructor Details

#initialize(logger:, log_level:, on_retry:, method:, url:) ⇒ Context

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 Context.



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/openai/internal/logging.rb', line 40

def initialize(logger:, log_level:, on_retry:, method:, url:)
  @logger = logger
  @log_level = log_level
  @on_retry = on_retry
  @id = nil
  @method = method.to_s.upcase
  @url = url
  @started_at = OpenAI::Internal::Util.monotonic_secs
  @attempt_started_at = @started_at
  @attempts = 0
end

Instance Method Details

#attempt_failed(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.



86
87
88
89
90
91
# File 'lib/openai/internal/logging.rb', line 86

def attempt_failed(error)
  log(:debug) do
    "[openai] request attempt failed log_id=#{id} attempt=#{@attempts} " \
      "error=#{error.class} duration_ms=#{attempt_duration_ms}"
  end
end

#completed(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.

Parameters:



112
113
114
115
116
117
118
119
# File 'lib/openai/internal/logging.rb', line 112

def completed(response)
  log(:info) do
    "[openai] request complete log_id=#{id} method=#{@method} " \
      "path=#{OpenAI::Internal::Logging.safe_path(@url)} " \
      "status=#{response.status} request_id=#{safe_field(response.headers['x-request-id'])} " \
      "attempts=#{@attempts} duration_ms=#{duration_ms}"
  end
end

#observe_stream(stream, 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.



121
122
123
124
125
# File 'lib/openai/internal/logging.rb', line 121

def observe_stream(stream, response:)
  return stream unless enabled?(:error)

  stream.observe(context: self, response: response)
end

#request_failed(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.

Parameters:

  • error (StandardError)


127
128
129
130
131
132
133
134
135
136
# File 'lib/openai/internal/logging.rb', line 127

def request_failed(error)
  status = error.is_a?(OpenAI::Errors::APIError) ? error.status : nil
  request_id = error.is_a?(OpenAI::Errors::APIError) ? error.request_id : nil
  log(:error) do
    "[openai] request failed log_id=#{id} method=#{@method} " \
      "path=#{OpenAI::Internal::Logging.safe_path(@url)} " \
      "status=#{status || 'none'} request_id=#{safe_field(request_id)} " \
      "error=#{error.class} attempts=#{@attempts} duration_ms=#{duration_ms}"
  end
end

#request_started(request, redirect_count:) ⇒ 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.



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/openai/internal/logging.rb', line 52

def request_started(request, redirect_count:)
  @attempts += 1
  log(:debug) do
    "[openai] request started log_id=#{id} attempt=#{@attempts} " \
      "redirect=#{redirect_count} method=#{request.method.to_s.upcase} " \
      "url=#{OpenAI::Internal::Logging.safe_url(request.url)} " \
      "headers=#{OpenAI::Internal::Logging.format_headers(request.headers)} " \
      "body=#{OpenAI::Internal::Logging.format_body(request.body, headers: request.headers)}"
  end
  @attempt_started_at = OpenAI::Internal::Util.monotonic_secs
end

#response_body(body, headers:, complete:, total_bytes:, attempt:) ⇒ 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.



138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/openai/internal/logging.rb', line 138

def response_body(body, headers:, complete:, total_bytes:, attempt:)
  log(:debug) do
    formatted = OpenAI::Internal::Logging.format_observed_body(
      body,
      headers: headers,
      complete: complete,
      total_bytes: total_bytes
    )
    "[openai] response body log_id=#{id} " \
      "attempt=#{attempt} duration_ms=#{duration_ms} body=#{formatted}"
  end
end

#response_received(response) ⇒ 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.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/openai/internal/logging.rb', line 64

def response_received(response)
  log(:debug) do
    "[openai] response received log_id=#{id} attempt=#{@attempts} " \
      "status=#{response.status} request_id=#{safe_field(response.headers['x-request-id'])} " \
      "duration_ms=#{attempt_duration_ms} " \
      "headers=#{OpenAI::Internal::Logging.format_headers(response.headers)}"
  end
  return response unless enabled?(:debug)

  observed = ObservedBody.new(
    body: response.body,
    headers: response.headers,
    context: self,
    attempt: @attempts
  )
  OpenAI::HTTPClient::Response.new(
    status: response.status,
    headers: response.headers,
    body: observed
  )
end

#retry_scheduled(cause, delay:, response:, retry_count:, max_retries:) ⇒ 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.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/openai/internal/logging.rb', line 93

def retry_scheduled(cause, delay:, response:, retry_count:, max_retries:)
  reason = cause.is_a?(Integer) ? "status=#{cause}" : "error=#{cause.class}"
  log(:warn) do
    "[openai] request retry log_id=#{id} attempt=#{@attempts} " \
      "#{reason} delay_seconds=#{delay}"
  end

  event = OpenAI::RetryEvent.new(
    attempt: retry_count + 2,
    max_attempts: max_retries + 1,
    delay: delay,
    response: response,
    error: cause.is_a?(OpenAI::Errors::APIConnectionError) ? cause : nil
  )
  @on_retry&.call(event)
rescue StandardError
  nil
end