Module: OpenTelemetry::Instrumentation::Ethon::Patches::Old::Easy

Defined in:
lib/opentelemetry/instrumentation/ethon/patches/old/easy.rb

Overview

Ethon::Easy patch for instrumentation

Constant Summary collapse

HTTP_STATUS_SUCCESS_RANGE =

Constant for the HTTP status range

(100..399)

Instance Method Summary collapse

Instance Method Details

#completeObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/opentelemetry/instrumentation/ethon/patches/old/easy.rb', line 42

def complete
  begin
    response_options = mirror.options
    response_code = (response_options[:response_code] || response_options[:code]).to_i
    if response_code.zero?
      return_code = response_options[:return_code]
      message = return_code ? ::Ethon::Curl.easy_strerror(return_code) : 'unknown reason'
      @otel_span.status = OpenTelemetry::Trace::Status.error("Request has failed: #{message}")
    else
      @otel_span.set_attribute('http.status_code', response_code)
      @otel_span.status = OpenTelemetry::Trace::Status.error unless HTTP_STATUS_SUCCESS_RANGE.cover?(response_code.to_i)
    end
  ensure
    @otel_span&.finish
    @otel_span = nil
  end
  super
end

#headers=(headers) ⇒ Object



23
24
25
26
27
# File 'lib/opentelemetry/instrumentation/ethon/patches/old/easy.rb', line 23

def headers=(headers)
  # Store headers to call this method again when span is ready
  @otel_original_headers = headers
  super
end

#http_request(url, action_name, options = {}) ⇒ Object



18
19
20
21
# File 'lib/opentelemetry/instrumentation/ethon/patches/old/easy.rb', line 18

def http_request(url, action_name, options = {})
  @otel_method = action_name
  super
end

#otel_before_requestObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/opentelemetry/instrumentation/ethon/patches/old/easy.rb', line 69

def otel_before_request
  span_data = HttpHelper.span_attrs_for(@otel_method, semconv: :old)

  @otel_span = tracer.start_span(
    span_data.span_name,
    attributes: span_creation_attributes(span_data),
    kind: :client
  )

  @otel_original_headers ||= {}
  OpenTelemetry::Trace.with_span(@otel_span) do
    OpenTelemetry.propagation.inject(@otel_original_headers)
  end
  self.headers = @otel_original_headers
end

#otel_span_started?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/opentelemetry/instrumentation/ethon/patches/old/easy.rb', line 85

def otel_span_started?
  instance_variable_defined?(:@otel_span) && !@otel_span.nil?
end

#performObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/opentelemetry/instrumentation/ethon/patches/old/easy.rb', line 29

def perform
  otel_before_request
  super
rescue StandardError => e
  # If an exception occurs before we can call `complete`
  # we should add an error status and close the span
  # and raise the original error
  @otel_span&.status = OpenTelemetry::Trace::Status.error("Request threw an exception: #{e.message}")
  @otel_span&.finish
  @otel_span = nil
  raise e
end

#resetObject



61
62
63
64
65
66
67
# File 'lib/opentelemetry/instrumentation/ethon/patches/old/easy.rb', line 61

def reset
  super
ensure
  @otel_span = nil
  @otel_method = nil
  @otel_original_headers = nil
end