Class: OpenCensus::Trace::Integrations::FaradayMiddleware
- Inherits:
-
Faraday::Middleware
- Object
- Faraday::Middleware
- OpenCensus::Trace::Integrations::FaradayMiddleware
- Defined in:
- lib/opencensus/trace/integrations/faraday_middleware.rb
Overview
Faraday integration
This is a middleware for the Faraday HTTP client:
- It wraps all outgoing requests in spans
- It adds the trace context to outgoing requests.
Example:
conn = Faraday.new(url: "http://www.example.com") do |c|
c.use OpenCensus::Trace::Integrations::FaradayMiddleware,
span_name: "http request"
c.adapter Faraday.default_adapter
end
conn.get "/"
Configuring spans
By default, spans are added to the thread-local span context, as if
by calling OpenCensus::Trace.start_span. If there is no span context,
then no span is added and this middleware effectively disables itself.
You may also provide a span context, by passing it in the middleware options hash. For example:
conn = Faraday.new(url: "http://www.example.com") do |c|
c.use OpenCensus::Trace::Integrations::FaradayMiddleware,
span_context: my_span_context
c.adapter Faraday.default_adapter
end
You may also override the span context for a particular request by including it in the options:
conn.get do |req|
req.url "/"
req..context = { span_context: my_span_context }
end
By default, all spans are given a default name. You may also override
this by passing a :span_name in the middleware options hash and/or
the request options.
Trace context
This currently adds a header to each outgoing request, propagating the trace context for distributed tracing. By default, this uses the formatter in the current config.
You may provide your own implementation of the formatter by configuring it in the middleware options hash. For example:
conn = Faraday.new(url: "http://www.example.com") do |c|
c.use OpenCensus::Trace::Integrations::FaradayMiddleware,
formatter: OpenCensus::Trace::Formatters::CloudTrace.new
c.adapter Faraday.default_adapter
end
You many also override the formatter for a particular request by including it in the options:
conn.get do |req|
req.url "/"
req..context = {
formatter: OpenCensus::Trace::Formatters::CloudTrace.new
}
end
Constant Summary collapse
- DEFAULT_SPAN_NAME =
The default name for Faraday spans
"faraday_request".freeze
Instance Method Summary collapse
-
#initialize(app, span_context: nil, span_name: nil, sampler: nil, formatter: nil) ⇒ FaradayMiddleware
constructor
Create a FaradayMiddleware.
Constructor Details
#initialize(app, span_context: nil, span_name: nil, sampler: nil, formatter: nil) ⇒ FaradayMiddleware
Create a FaradayMiddleware.
112 113 114 115 116 117 118 119 |
# File 'lib/opencensus/trace/integrations/faraday_middleware.rb', line 112 def initialize app, span_context: nil, span_name: nil, sampler: nil, formatter: nil @app = app @span_context = span_context || OpenCensus::Trace @span_name = span_name || DEFAULT_SPAN_NAME @sampler = sampler @formatter = formatter || OpenCensus::Trace.config.http_formatter end |