From 7c504946e6498a67cb8e773a3547c3fe251535c9 Mon Sep 17 00:00:00 2001 From: Yohei Kitamura Date: Tue, 11 Sep 2018 17:31:15 -0400 Subject: [PATCH] Update version and changelog for 'abstract route to span name' (#127) --- CHANGELOG.md | 4 ++++ lib/zipkin-tracer/application.rb | 10 +++++----- lib/zipkin-tracer/rack/zipkin-tracer.rb | 8 ++++++-- lib/zipkin-tracer/version.rb | 2 +- spec/lib/application_spec.rb | 3 ++- 5 files changed, 18 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 282acc3..0df2d3a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 0.29.0 +* Add abstract route to span name +* Fix not to raise "NoMethodError" for Rails:Module + # 0.28.0 * Add the `:trace_id_128bit` configuration option. diff --git a/lib/zipkin-tracer/application.rb b/lib/zipkin-tracer/application.rb index f52162f..cdb7aba 100644 --- a/lib/zipkin-tracer/application.rb +++ b/lib/zipkin-tracer/application.rb @@ -11,19 +11,19 @@ def self.routable_request?(path_info, http_method) false end - def self.get_route(path_info, http_method) + def self.get_route(env) return nil unless defined?(Rails) - req = Rack::Request.new("PATH_INFO" => path_info, "REQUEST_METHOD" => http_method) + req = Rack::Request.new(env) # Returns a string like /some/path/:id - Rails.application.routes.router.recognize(req) { |route| + Rails.application.routes.router.recognize(req) do |route| return route.path.spec.to_s - } + end rescue nil end def self.logger - if defined?(Rails) # If we happen to be inside a Rails app, use its logger + if defined?(Rails.logger) # If we happen to be inside a Rails app, use its logger Rails.logger else Logger.new(STDOUT) diff --git a/lib/zipkin-tracer/rack/zipkin-tracer.rb b/lib/zipkin-tracer/rack/zipkin-tracer.rb index f213e39..eaf691d 100644 --- a/lib/zipkin-tracer/rack/zipkin-tracer.rb +++ b/lib/zipkin-tracer/rack/zipkin-tracer.rb @@ -30,7 +30,7 @@ def call(env) if !trace_id.sampled? || !routable_request?(env) @app.call(env) else - @tracer.with_new_span(trace_id, "#{env[REQUEST_METHOD].to_s.downcase} #{route(env)}".strip) do |span| + @tracer.with_new_span(trace_id, span_name(env)) do |span| trace!(span, zipkin_env) { @app.call(env) } end end @@ -44,7 +44,11 @@ def routable_request?(env) end def route(env) - Application.get_route(env[PATH_INFO], env[REQUEST_METHOD]) + Application.get_route(env) + end + + def span_name(env) + "#{env[REQUEST_METHOD].to_s.downcase} #{route(env)}".strip end def annotate_plugin(span, env, status, response_headers, response_body) diff --git a/lib/zipkin-tracer/version.rb b/lib/zipkin-tracer/version.rb index 52d4293..d177495 100644 --- a/lib/zipkin-tracer/version.rb +++ b/lib/zipkin-tracer/version.rb @@ -1,3 +1,3 @@ module ZipkinTracer - VERSION = '0.28.0'.freeze + VERSION = '0.29.0'.freeze end diff --git a/spec/lib/application_spec.rb b/spec/lib/application_spec.rb index 9c6670e..f2eef3a 100644 --- a/spec/lib/application_spec.rb +++ b/spec/lib/application_spec.rb @@ -39,7 +39,8 @@ module ZipkinTracer end describe '.get_route' do - subject { Application.get_route("path", "METHOD") } + let(:env) { { "PATH_INFO" => "path", "REQUEST_METHOD" => "METHOD" } } + subject { Application.get_route(env) } context 'Rails available' do before do