Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add:HTTP header tagging with DD_TRACE_HEADER_TAGS for servers #2935

Merged
merged 26 commits into from
Jul 25, 2023

Conversation

marcotc
Copy link
Member

@marcotc marcotc commented Jun 29, 2023

What does this PR do?

This PR adds support for saving the value of HTTP headers as span tags by configuring the environment variable DD_TRACE_HEADER_TAGS.

This PR covers HTTP servers: Rack and Sinatra in our case.

A follow up PR will cover HTTP clients.

Motivation

Saving HTTP headers as span tags has been supported for Rack and Sinatra, but there was no standard configuration at application-level.

At Datadog, we have the DD_TRACE_HEADER_TAGS variable defined across all tracers as the recommended way to configure HTTP header capture.

This PR brings the Ruby tracer on par with other languages that already support this configuration.

Also, this is one of the features supported by Remote Configuration.

How to test the change?

rake spec:main covers the utility methods.
Rack and Sinatra test suites cover the integration testing for this feature.

@github-actions github-actions bot added integrations Involves tracing integrations tracing labels Jun 29, 2023
@marcotc marcotc changed the title wip Support DD_TRACE_HEADER_TAGS configuration Jun 29, 2023
@@ -0,0 +1,55 @@
# frozen_string_literal: true
Copy link
Member Author

@marcotc marcotc Jun 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code was moved here from lib/datadog/tracing/contrib/sinatra/env.rb and lib/datadog/tracing/contrib/sinatra/headers.rb, with some refactoring taking place.

Sinatra already had helpers to perform header matching for Rack-style headers.
Rack was also performing header matching, but in a less organized fashion.

Because the DD_TRACE_HEADER_TAGS option has to be added to both Rack and Sinatra, this PR merges both header matching implementations into this file as they are the exact same: header matching for Rack-style headers.

@marcotc marcotc changed the title Support DD_TRACE_HEADER_TAGS configuration Added:HTTP header tagging with DD_TRACE_HEADER_TAGS Jun 30, 2023
@marcotc marcotc removed the integrations Involves tracing integrations label Jun 30, 2023
@marcotc marcotc self-assigned this Jun 30, 2023
@github-actions github-actions bot added the integrations Involves tracing integrations label Jun 30, 2023
@github-actions github-actions bot added the core Involves Datadog core libraries label Jul 4, 2023
@marcotc marcotc changed the title Added:HTTP header tagging with DD_TRACE_HEADER_TAGS Add:HTTP header tagging with DD_TRACE_HEADER_TAGS for servers Jul 4, 2023
@marcotc marcotc marked this pull request as ready for review July 4, 2023 22:31
@marcotc marcotc requested a review from a team July 4, 2023 22:31
@codecov-commenter
Copy link

codecov-commenter commented Jul 4, 2023

Codecov Report

Merging #2935 (d627531) into fix-en-var (85399ea) will increase coverage by 0.00%.
The diff coverage is 99.64%.

@@             Coverage Diff              @@
##           fix-en-var    #2935    +/-   ##
============================================
  Coverage       98.09%   98.09%            
============================================
  Files            1305     1309     +4     
  Lines           72745    72972   +227     
  Branches         3357     3367    +10     
============================================
+ Hits            71356    71585   +229     
+ Misses           1389     1387     -2     
Files Changed Coverage Δ
lib/datadog/tracing/contrib/sinatra/env.rb 100.00% <ø> (+3.57%) ⬆️
lib/datadog/core/utils/hash.rb 90.90% <95.00%> (+6.29%) ⬆️
lib/datadog/tracing/configuration/ext.rb 100.00% <100.00%> (ø)
lib/datadog/tracing/configuration/http.rb 100.00% <100.00%> (ø)
lib/datadog/tracing/configuration/settings.rb 94.54% <100.00%> (+0.31%) ⬆️
.../datadog/tracing/contrib/rack/header_collection.rb 94.11% <100.00%> (+0.36%) ⬆️
lib/datadog/tracing/contrib/rack/header_tagging.rb 100.00% <100.00%> (ø)
lib/datadog/tracing/contrib/rack/middlewares.rb 96.06% <100.00%> (-0.54%) ⬇️
...tadog/tracing/contrib/sinatra/tracer_middleware.rb 94.54% <100.00%> (-0.37%) ⬇️
lib/datadog/tracing/metadata/ext.rb 100.00% <100.00%> (ø)
... and 7 more

... and 3 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

header_value = headers[header_name]

[span_tag, header_value] if header_value
end.compact
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of map and compact, can we reduce?

@request_headers.reduce([]) do |array, (header_name, span_tag)|
  header_value = headers[header_name]
  array << [span_tag, header_value] if header_value
  array
end

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure, do you think the reduce easier to read?

@request_headers.reduce([]) do |array, (header_name, span_tag)|
  # Case-insensitive search
  header_value = headers[header_name]

  array << [span_tag, header_value] if header_value
  array
end

Comment on lines 65 to 69
# Returns false if this class was explicitly configured
# or left without configuration.
def configured?
!@header_tags.equal?(EMPTY)
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would be able to remove this logic once we have #2970 in master

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, I think so! 👍
I'll work on this when both are merged!

@marcotc marcotc requested a review from GustavoCaso July 17, 2023 20:48
@GustavoCaso
Copy link
Member

@marcotc code LGTM, but I believe the system failures are related to these changes. We should probably investigated those before merging

@marcotc marcotc added this to the 1.13.0 milestone Jul 20, 2023
def initialize(header_tags)
@request_headers = {}
@response_headers = {}
@header_tags = header_tags || EMPTY
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After a01cde6 we can get rid of EMPTY now 😄

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This suggestion kind of worked, but I need to fix a small thing in Options for it to work: #2994

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I rebased this PR on that fix, but everything here remains unchanged.

@marcotc marcotc changed the base branch from master to fix-en-var July 24, 2023 22:33
@marcotc marcotc requested a review from GustavoCaso July 24, 2023 22:34
Base automatically changed from fix-en-var to master July 25, 2023 18:10
@marcotc marcotc merged commit e9fe820 into master Jul 25, 2023
199 of 203 checks passed
@marcotc marcotc deleted the DD_TRACE_HEADER_TAGS branch July 25, 2023 18:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Involves Datadog core libraries integrations Involves tracing integrations tracing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants