Skip to content

Commit

Permalink
Add abstract route to span name openzipkin#96
Browse files Browse the repository at this point in the history
  • Loading branch information
jhonyam committed Jun 20, 2018
1 parent e2f4299 commit 7bfbc03
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
11 changes: 11 additions & 0 deletions lib/zipkin-tracer/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ def self.routable_request?(path_info, http_method)
false
end

def self.get_route(path_info, http_method)
return "" unless defined?(Rails)
req = Rack::Request.new("PATH_INFO" => path_info, "REQUEST_METHOD" => http_method)
# Returns a string like /some/path/:id
Rails.application.routes.router.recognize(req) { |route|
return route.path.spec.to_s
}
rescue
""
end

def self.logger
if defined?(Rails) # If we happen to be inside a Rails app, use its logger
Rails.logger
Expand Down
8 changes: 6 additions & 2 deletions lib/zipkin-tracer/rack/zipkin-tracer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) do |span|
@tracer.with_new_span(trace_id, "#{env[REQUEST_METHOD].to_s.downcase} #{route(env)}".strip) do |span|
trace!(span, zipkin_env) { @app.call(env) }
end
end
Expand All @@ -40,7 +40,11 @@ def call(env)
private

def routable_request?(env)
Application.routable_request?(env[PATH_INFO], env[REQUEST_METHOD])
Application.routable_request?(env[PATH_INFO], env[REQUEST_METHOD])
end

def route(env)
Application.get_route(env[PATH_INFO], env[REQUEST_METHOD])
end

def annotate_plugin(span, env, status, response_headers, response_body)
Expand Down
37 changes: 37 additions & 0 deletions spec/lib/application_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,43 @@ module ZipkinTracer
end
end

describe '.get_route' do
subject { Application.get_route("path", "METHOD") }

context 'Rails available' do
before do
stub_const('Rails', Class.new)
end

context 'route for /api/v1/messages/123 is found' do
before do
route = "/api/v1/messages/:id"
allow(Rails).to receive_message_chain('application.routes.router.recognize') { route }
end

it 'returns route' do
expect(subject).to eq("/api/v1/messages/:id")
end
end

context 'route is not found' do
before do
allow(Rails).to receive_message_chain('application.routes.router.recognize') { nil }
end

it 'returns nil' do
expect(subject).to eq(nil)
end
end
end

context 'Rails not available' do
it 'returns empty string' do
expect(subject).to eq("")
end
end
end

describe '.logger' do
subject { Application.logger }

Expand Down

0 comments on commit 7bfbc03

Please sign in to comment.