-
-
Save xxx/3b9f6d8ab057507df608f390ed0ec394 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Tracer::Utils | |
def push_span(span) | |
RequestStore.store[:tracer_spans] ||= [] | |
RequestStore.store[:tracer_spans].push(span) | |
end | |
def pop_span | |
RequestStore.store[:tracer_spans] ||= [] | |
RequestStore.store[:tracer_spans].pop | |
end | |
def peek_span | |
RequestStore.store[:tracer_spans] ||= [] | |
RequestStore.store[:tracer_spans][-1] | |
end | |
def trace_block(name, tags: {}, &_block) | |
parent = peek_span | |
# TODO: if parent is nil here, check to see if we have a request to extract | |
# headers from | |
span = OpenTracing.start_span( | |
name, | |
child_of: parent, | |
tags: tags | |
) | |
push_span span | |
yield span | |
ensure | |
pop_span | |
span&.finish | |
end | |
# This sets a bunch tags that end up being backend-specific. | |
def extract_request_tags(req) | |
base = { | |
'http.method' => req.request_method, | |
'http.host' => req.host_with_port, | |
'http.request.size' => req.content_length, | |
'http.path' => req.path, | |
# 'http.url' => req.url, # Can reveal secrets, so not included by default. | |
'http.request.user_agent' => req.user_agent, | |
'http.request.xhr' => req.xhr? | |
} | |
req.params.each do |key, val| | |
v = case key | |
when 'password', 'old_password', 'new_password', 'api_key' | |
'<FILTERED>' # Zipkin doesn't escape its input?! | |
else | |
val_s = val.to_s | |
CGI.escape(val_s) # Srsly, why doesn't zipkin html escape its input? | |
end | |
base["param.#{key}"] = v | |
end | |
base | |
end | |
module_function :push_span, :pop_span, :peek_span, :trace_block, :extract_request_tags | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment