From ac17e4d74cf193b807ad94e697f02ef4c88df862 Mon Sep 17 00:00:00 2001 From: iaintshine Date: Wed, 9 Aug 2017 23:07:32 +0200 Subject: [PATCH] Allow to pass custom span name provider 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. --- lib/faraday/tracer.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/faraday/tracer.rb b/lib/faraday/tracer.rb index 7d7c270..72bf8c5 100644 --- a/lib/faraday/tracer.rb +++ b/lib/faraday/tracer.rb @@ -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. @@ -13,15 +17,16 @@ class Tracer < Faraday::Middleware # is called. # @param errors [Array] 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',