Class: Google::Cloud::Trace::FaradayMiddleware

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/google/cloud/trace/faraday_middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, opts = {}) ⇒ FaradayMiddleware

Trace FaradayMiddleware

A faraday middleware that setup request/response labels for trace.

Installing

To use this middleware, simply install it in your middleware stack. Here is an example configuration enable the Trace middleware:

conn = Faraday.new(:url => 'http://example.com') do |faraday|
  # enable cross project tracing with option to true
  faraday.use Google::Cloud::Trace::FaradayMiddleware, enable_cross_project_tracing: true
  faraday.request  :url_encoded             # form-encode POST params
  faraday.response :logger                  # log requests to $stdout
  faraday.adapter  Faraday.default_adapter  # use Net::HTTP adapter
end


42
43
44
45
# File 'lib/google/cloud/trace/faraday_middleware.rb', line 42

def initialize app, opts = {}
  @enable_cross_project_tracing = opts[:enable_cross_project_tracing] || false
  super app
end

Instance Method Details

#call(env) ⇒ Object

Create a Trace span with the HTTP request/response information.



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/google/cloud/trace/faraday_middleware.rb', line 49

def call env
  Google::Cloud::Trace.in_span "faraday_request" do |span|
    add_request_labels span, env if span
    add_trace_context_header env if @enable_cross_project_tracing

    response = @app.call env

    add_response_labels span, env if span

    response
  end
end