Class: EpsagonRackMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/instrumentation/epsagon_rails_middleware.rb

Constant Summary collapse

EMPTY_HASH =
{}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ EpsagonRackMiddleware

Returns a new instance of EpsagonRackMiddleware.



73
74
75
# File 'lib/instrumentation/epsagon_rails_middleware.rb', line 73

def initialize(app)
  @app = app
end

Class Method Details

.allowed_rack_request_headersObject



42
43
44
45
46
# File 'lib/instrumentation/epsagon_rails_middleware.rb', line 42

def allowed_rack_request_headers
  @allowed_rack_request_headers ||= Array(config[:allowed_request_headers]).each_with_object({}) do |header, memo|
    memo["HTTP_#{header.to_s.upcase.gsub(/[-\s]/, '_')}"] = build_attribute_name('http.request.headers.', header)
  end
end

.allowed_response_headersObject



48
49
50
51
52
53
# File 'lib/instrumentation/epsagon_rails_middleware.rb', line 48

def allowed_response_headers
  @allowed_response_headers ||= Array(config[:allowed_response_headers]).each_with_object({}) do |header, memo|
    memo[header] = build_attribute_name('http.response.headers.', header)
    memo[header.to_s.upcase] = build_attribute_name('http.response.headers.', header)
  end
end

.build_attribute_name(prefix, suffix) ⇒ Object



55
56
57
# File 'lib/instrumentation/epsagon_rails_middleware.rb', line 55

def build_attribute_name(prefix, suffix)
  prefix + suffix.to_s.downcase.gsub(/[-\s]/, '_')
end

.configObject



59
60
61
# File 'lib/instrumentation/epsagon_rails_middleware.rb', line 59

def config
  EpsagonRailsInstrumentation.instance.config
end

Instance Method Details

#call(env) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/instrumentation/epsagon_rails_middleware.rb', line 77

def call(env)
  original_env = env.dup

  # restore extracted context in this process:
  OpenTelemetry::Context.with_current(OpenTelemetry.propagation.http.extract(env)) do
    request_span_name = create_request_span_name(env['REQUEST_URI'] || original_env['PATH_INFO'])
    tracer.in_span(env['HTTP_HOST'] || 'unknown',
                   attributes: request_span_attributes(env: env),
                   kind: :server) do |http_span|
      RackExtension.with_span(http_span) do
        tracer.in_span(
            env['HTTP_HOST'],
            kind: :server,
            attributes: {type: 'rails'}
          ) do |framework_span|
          @app.call(env).tap do |status, headers, response|
            set_attributes_after_request(http_span, framework_span, status, headers, response)
          end
        end
      end
    end
  end
end