Skip to content

Commit

Permalink
Allow to pass custom span name provider
Browse files Browse the repository at this point in the history
In current implementation the generated operation name
corresponds to the request's http method e.g. GET, POST etc. It's not
perfect when you look at UI and want to quickly understand what was
called. You need to dig into the trace to understand with what url it's
related.

This change introduces a capability to specify a custom span name
provider, and defaults to http method and host of the url.
  • Loading branch information
iaintshine committed Aug 9, 2017
1 parent 2a74dc8 commit ede0849
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/faraday/tracer.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
require 'uri'
require 'faraday'
require 'opentracing'

module Faraday
class Tracer < Faraday::Middleware
MethodOnly = ->(env) { env[:method].to_s.upcase }
MethodWithHost = ->(env) { "#{env[:method].to_s.upcase} #{URI(env[:url].to_s).host}".strip }

# Create a new Faraday Tracer middleware.
#
# @param app The faraday application/middlewares stack.
Expand All @@ -13,15 +17,16 @@ class Tracer < Faraday::Middleware
# is called.
# @param errors [Array<Class>] An array of error classes to be captured by the tracer
# as errors. Errors are **not** muted by the middleware.
def initialize(app, span: nil, tracer: OpenTracing.global_tracer, errors: [StandardError])
def initialize(app, span: nil, tracer: OpenTracing.global_tracer, errors: [StandardError], span_name_provider: MethodWithHost)
super(app)
@tracer = tracer
@parent_span = span
@errors = errors
@span_name_provider = span_name_provider
end

def call(env)
span = @tracer.start_span(env[:method].to_s.upcase,
span = @tracer.start_span(@span_name_provider.call(env),
child_of: @parent_span.respond_to?(:call) ? @parent_span.call : @parent_span,
tags: {
'component' => 'faraday',
Expand Down
1 change: 1 addition & 0 deletions spec/faraday/tracer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
it 'uses upcase HTTP method as span operation name' do
call(method: :post)
span = tracer.finished_spans.first
puts span.operation_name
expect(span.operation_name).to eq('POST')
end

Expand Down

0 comments on commit ede0849

Please sign in to comment.