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

✨ [RUM-3542] Add trace context injection control in rum configuration #2656

Merged
merged 18 commits into from
Mar 25, 2024

Conversation

cy-moi
Copy link
Contributor

@cy-moi cy-moi commented Mar 18, 2024

Motivation

APM is working on building more granular sampling control on the backend side which can't be applied if the sampling decision is already taken on the client side.

Changes

Added a field traceContextInjection in the configuration to control context injection.

This new configuration allows more granular sampling at the backend and does not bring any break changes. It is available along with sampling rate configuration.

traceContextInjection is an opt-in configuration defaults to "all" which means no behavior changes. If opt-in for the "sampled" option, you would see only sampled requests having trace context attached to them.

Testing

Sampling Decision traceContextInjection Expected Behavior
Sampled all Inject trace context with sampling priority 1
Sampled sampled Inject trace context with sampling priority 1
Not Sampled all Inject trace context with sampling priority 0
Not Sampled sampled Do not inject trace context
  • Local
  • Staging
  • Unit
  • End to end

I have gone over the contributing documentation.

Copy link

cit-pr-commenter bot commented Mar 18, 2024

Bundles Sizes Evolution

📦 Bundle Name Base Size Local Size 𝚫% Status
Rum 155.96 kB 156.17 kB +0.14%
Logs 55.15 kB 55.15 kB 0.00%
Rum Slim 101.53 kB 101.74 kB +0.21%
Worker 25.21 kB 25.21 kB 0.00%

Comment on lines 140 to 142
if (configuration.traceContextInjection === TraceContextInjection.All) {
inject(makeTracingHeaders(context.traceId, context.spanId, context.traceSampled, tracingOption.propagatorTypes))
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

FMU, the check should more look like this

 if (
    (context.traceSampled && configuration.traceContextInjection === TraceContextInjection.Sampled) ||
    configuration.traceContextInjection === TraceContextInjection.All
  ) {
    inject(makeTracingHeaders(context.traceId, context.spanId, context.traceSampled, tracingOption.propagatorTypes))
  }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you! I updated this and added tests accordingly.

Comment on lines 26 to 36
export const enum TraceContextInjection {
/*
Default. Inject trace context to all network requests as per the sampling rate.
*/
All,

/*
Inject trace context only if the trace is sampled.
*/
Sampled,
}
Copy link
Member

Choose a reason for hiding this comment

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

💬 suggestion: ‏user-facing enums should be defined as a plain object, like so:

export const DefaultPrivacyLevel = {
ALLOW: 'allow',
MASK: 'mask',
MASK_USER_INPUT: 'mask-user-input',
} as const
export type DefaultPrivacyLevel = (typeof DefaultPrivacyLevel)[keyof typeof DefaultPrivacyLevel]

This allows using either the object (ex: TraceContextInjection.All) or the value directly (ex: "all") without facing typing issues, from JS or TS.

In any case, const enum should never be customer-facing because non-TS users won't have access to it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it. I updated it with an object like this and moved it to the same configuration file.

@codecov-commenter
Copy link

codecov-commenter commented Mar 20, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.90%. Comparing base (734c28e) to head (2ecef9f).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2656      +/-   ##
==========================================
- Coverage   92.91%   92.90%   -0.01%     
==========================================
  Files         239      239              
  Lines        6934     6939       +5     
  Branches     1517     1520       +3     
==========================================
+ Hits         6443     6447       +4     
- Misses        491      492       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@cy-moi cy-moi marked this pull request as ready for review March 20, 2024 14:56
@cy-moi cy-moi requested a review from a team as a code owner March 20, 2024 14:56
@cy-moi
Copy link
Contributor Author

cy-moi commented Mar 21, 2024

/to-staging

@dd-devflow
Copy link
Contributor

dd-devflow bot commented Mar 21, 2024

🚂 Branch Integration: starting soon, merge in < 10m

Commit ec7d3c5825 will soon be integrated into staging-12.

This build is going to start soon! (estimated merge in less than 10m)

Use /to-staging -c to cancel this operation!

dd-mergequeue bot added a commit that referenced this pull request Mar 21, 2024
@dd-devflow
Copy link
Contributor

dd-devflow bot commented Mar 21, 2024

🚂 Branch Integration: This commit was successfully integrated

Commit ec7d3c5825 has been merged into staging-12 in merge commit a155b4d01e.

Check out the triggered pipeline on Gitlab 🦊

@cy-moi cy-moi requested a review from bcaudan March 22, 2024 12:32
packages/rum-core/src/domain/configuration.ts Outdated Show resolved Hide resolved
packages/rum-core/src/domain/tracing/tracer.spec.ts Outdated Show resolved Hide resolved
packages/rum-core/src/domain/configuration.ts Outdated Show resolved Hide resolved
packages/rum-core/src/domain/tracing/tracer.spec.ts Outdated Show resolved Hide resolved
packages/rum-core/src/domain/tracing/tracer.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@bcaudan bcaudan left a comment

Choose a reason for hiding this comment

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

Nice!

@cy-moi cy-moi merged commit 6a2a0d5 into main Mar 25, 2024
17 checks passed
@cy-moi cy-moi deleted the congyao/trace-context-injection-control branch March 25, 2024 10:47
@hong823
Copy link

hong823 commented Jul 12, 2024

@cy-moi Any idea if this would support when only using W3C propagatorTypes?

          allowedTracingUrls: [
            { match: "https://example.com", propagatorTypes: ['tracecontext'] }
          ]

From what I can tell, it might not be. Can you confirm?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants