Class: Sashiko::Adapters::Faraday::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/sashiko/adapters/faraday.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, tracer: nil) ⇒ Middleware

Returns a new instance of Middleware.



18
19
20
21
# File 'lib/sashiko/adapters/faraday.rb', line 18

def initialize(app, tracer: nil)
  @app    = app
  @tracer = tracer
end

Instance Method Details

#call(env) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/sashiko/adapters/faraday.rb', line 23

def call(env)
  method = env.method.to_s.upcase
  url    = env.url
  attrs = {
    "http.request.method" => method,
    "url.full"            => url.to_s,
    "server.address"      => url.host,
    "server.port"         => url.port,
  }

  # OTel HTTP semconv (stable): client span name is "{METHOD}",
  # e.g. "GET". The previous "HTTP GET" prefix was non-standard.
  (@tracer || Sashiko.tracer).in_span(method, attributes: attrs, kind: :client) do |span|
    response = @app.call(env)
    span.set_attribute("http.response.status_code", response.status)

    case response.status
    in 100..399 # ok, no-op
    in Integer => code
      span.set_attribute("error.type", code.to_s)
      span.status = OpenTelemetry::Trace::Status.error("HTTP #{code}")
    end

    response
  rescue => e
    span.set_attribute("error.type", e.class.name)
    span.record_exception(e)
    span.status = OpenTelemetry::Trace::Status.error(e.message)
    raise
  end
end