Class: OpenAI::Internal::Logging::Context Private
- Inherits:
-
Object
- Object
- OpenAI::Internal::Logging::Context
- 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.
Constant Summary collapse
- URI =
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.
Instance Method Summary collapse
- #attempt_failed(error) ⇒ void private
- #completed(response) ⇒ void private
-
#initialize(logger:, log_level:, on_retry:, method:, url:, on_response: nil) ⇒ Context
constructor
private
A new instance of Context.
- #observe_stream(stream, response:) ⇒ void private
- #request_failed(error) ⇒ void private
- #request_started(request, redirect_count:) ⇒ Object private
- #response_body(body, headers:, complete:, total_bytes:, attempt:) ⇒ Object private
- #response_received(response) ⇒ Object private
- #retry_scheduled(cause, delay:, response:, retry_count:, max_retries:) ⇒ Object private
Constructor Details
#initialize(logger:, log_level:, on_retry:, method:, url:, on_response: nil) ⇒ 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.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/openai/internal/logging.rb', line 29 def initialize(logger:, log_level:, on_retry:, method:, url:, on_response: nil) @logger = logger @log_level = log_level @on_retry = on_retry @on_response = on_response @id = nil @method = method.to_s.upcase @url = url @request_url = url @first_request_url = nil @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.
83 84 85 86 87 88 |
# File 'lib/openai/internal/logging.rb', line 83 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.
109 110 111 112 113 114 115 116 |
# File 'lib/openai/internal/logging.rb', line 109 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.
118 119 120 121 122 |
# File 'lib/openai/internal/logging.rb', line 118 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.
124 125 126 127 128 129 130 131 132 133 |
# File 'lib/openai/internal/logging.rb', line 124 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.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/openai/internal/logging.rb', line 44 def request_started(request, redirect_count:) @request_url = request.url @first_request_url ||= request.url @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.
135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/openai/internal/logging.rb', line 135 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.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/openai/internal/logging.rb', line 59 def response_received(response) @on_response&.call(response, @first_request_url || @url, @request_url) 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.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/openai/internal/logging.rb', line 90 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 |