Class: LogBrew::NetHttpTracingClient

Inherits:
Object
  • Object
show all
Defined in:
lib/logbrew/http_client_tracing.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http, client:, on_capture_error: nil) ⇒ NetHttpTracingClient

Returns a new instance of NetHttpTracingClient.



82
83
84
85
86
# File 'lib/logbrew/http_client_tracing.rb', line 82

def initialize(http, client:, on_capture_error: nil)
  @http = http
  @client = client
  @on_capture_error = on_capture_error
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *arguments, &block) ⇒ Object



129
130
131
132
133
# File 'lib/logbrew/http_client_tracing.rb', line 129

def method_missing(name, *arguments, &block)
  return super unless @http.respond_to?(name)

  @http.public_send(name, *arguments, &block)
end

Instance Attribute Details

#httpObject (readonly)

Returns the value of attribute http.



80
81
82
# File 'lib/logbrew/http_client_tracing.rb', line 80

def http
  @http
end

Instance Method Details

#request(request, body = nil, &block) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/logbrew/http_client_tracing.rb', line 88

def request(request, body = nil, &block)
  prepared = HttpClientTracing.prepare(
    client: @client,
    source: "net_http",
    on_capture_error: @on_capture_error
  ) do
    [request.method, address, HttpClientTracing::NetHttpHeaderSnapshot.new(request)]
  end
  return @http.request(request, body, &block) unless prepared

  operation, header = prepared
  begin
    header.inject(operation.traceparent)
  rescue StandardError => error
    operation.capture_error(error)
    HttpClientTracing.reset_header(header, operation)
    return @http.request(request, body, &block)
  end

  response = nil
  begin
    response = operation.around { @http.request(request, body, &block) }
  rescue StandardError => error
    operation.finish(error: error)
    raise
  ensure
    HttpClientTracing.reset_header(header, operation)
  end
  operation.finish(status_code: HttpClientTracing.read_status(response, :code, operation))
  response
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/logbrew/http_client_tracing.rb', line 135

def respond_to_missing?(name, include_private = false)
  @http.respond_to?(name, include_private) || super
end

#start(*arguments) ⇒ Object



120
121
122
123
124
125
126
127
# File 'lib/logbrew/http_client_tracing.rb', line 120

def start(*arguments)
  unless block_given?
    started = @http.start(*arguments)
    return started.equal?(@http) ? self : started
  end

  @http.start(*arguments) { yield self }
end