Class: OpenCensus::Trace::Integrations::RackMiddleware
- Inherits:
-
Object
- Object
- OpenCensus::Trace::Integrations::RackMiddleware
- Defined in:
- lib/opencensus/trace/integrations/rack_middleware.rb
Overview
# Rack integration
This is a middleware for Rack applications:
-
It wraps all incoming requests in a root span
-
It exports the captured spans at the end of the request.
Example:
require "opencensus/trace/integrations/rack_middleware"
use OpenCensus::Trace::Integrations::RackMiddleware
Constant Summary collapse
- AUTODETECTABLE_FORMATTERS =
List of trace context formatters we use to parse the parent span context.
[ Formatters::CloudTrace.new, Formatters::TraceContext.new ].freeze
Instance Method Summary collapse
-
#call(env) ⇒ Array
Run the Rack middleware.
-
#initialize(app, exporter: nil) ⇒ RackMiddleware
constructor
Create the Rack middleware.
Constructor Details
#initialize(app, exporter: nil) ⇒ RackMiddleware
Create the Rack middleware.
54 55 56 57 |
# File 'lib/opencensus/trace/integrations/rack_middleware.rb', line 54 def initialize app, exporter: nil @app = app @exporter = exporter || OpenCensus::Trace.config.exporter end |
Instance Method Details
#call(env) ⇒ Array
Run the Rack middleware.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/opencensus/trace/integrations/rack_middleware.rb', line 67 def call env formatter = AUTODETECTABLE_FORMATTERS.detect do |f| env.key? f.rack_header_name end if formatter context = formatter.deserialize env[formatter.rack_header_name] end Trace.start_request_trace \ trace_context: context, same_process_as_parent: false do |span_context| begin span_context.in_span get_path(env) do |span| start_request span, env @app.call(env).tap do |response| finish_request span, response end end ensure @exporter.export span_context.build_contained_spans end end end |