Skip to content

Instantly share code, notes, and snippets.

@clutchski
Last active June 7, 2019 20:24
Show Gist options
  • Save clutchski/7c8d132a872cd69db23fb0cb43615834 to your computer and use it in GitHub Desktop.
Save clutchski/7c8d132a872cd69db23fb0cb43615834 to your computer and use it in GitHub Desktop.
two methods for adding custom headers in ruby / rails
#
# If you wna to copy headers directly onto tags as spans, there's a short hand method
# that works via configuration.
#
Datadog.configure do |c|
c.use :rack, headers: {
request: [
'x-customer-mobile-screen',
'x-customer-mobile-userflow'
]
}
end
#
# If you want to map the headers to a more human readable names, use custom middleware like this.
# Note: it has to be run _after_ our Standard Rack/Rails tracing middleware.
#
class AddMobileHeaders
def initialize(app)
@app = app
end
def call(env)
request = Rack::Request.new(env)
span = Datadog.tracer.active_root_span
if not span.nil?
screen = request.headers["x-customer-mobile-screen"]
userflow = request.headers["x-customer-mobile-userflow"]
span.set_tag("mobile.screen", screen)
span.set_tag("mobile.userflow", userflow)
end
return @app.call(env)
end
end
YourRailsApp::Application.configure do
config.middleware.use "AddMobileHeaders"
# ...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment