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.
Instance Method Summary collapse
- #attempt_failed(error) ⇒ void private
- #completed(response) ⇒ void private
-
#initialize(logger:, log_level:, on_retry:, method:, url:) ⇒ 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:) ⇒ 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 |
# File 'lib/openai/internal/logging.rb', line 29 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.
77 78 79 80 81 82 |
# File 'lib/openai/internal/logging.rb', line 77 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.
103 104 105 106 107 108 109 110 |
# File 'lib/openai/internal/logging.rb', line 103 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.
112 113 114 115 116 |
# File 'lib/openai/internal/logging.rb', line 112 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.
118 119 120 121 122 123 124 125 126 127 |
# File 'lib/openai/internal/logging.rb', line 118 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.
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/openai/internal/logging.rb', line 41 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.
129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/openai/internal/logging.rb', line 129 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.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/openai/internal/logging.rb', line 54 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.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/openai/internal/logging.rb', line 84 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 |