Skip to content

Commit c3db968

Browse files
committed
record transaction sampling method and rate
1 parent 928e96a commit c3db968

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

packages/core/src/request.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ export function sessionToSentryRequest(session: Session, api: API): SentryReques
2020

2121
/** Creates a SentryRequest from an event. */
2222
export function eventToSentryRequest(event: Event, api: API): SentryRequest {
23+
// since JS has no Object.prototype.pop()
24+
const { __sentry_samplingMethod: samplingMethod, __sentry_sampleRate: sampleRate, ...otherTags } = event.tags || {};
25+
event.tags = otherTags;
26+
2327
const useEnvelope = event.type === 'transaction';
2428

2529
const req: SentryRequest = {
@@ -41,6 +45,11 @@ export function eventToSentryRequest(event: Event, api: API): SentryRequest {
4145
});
4246
const itemHeaders = JSON.stringify({
4347
type: event.type,
48+
49+
// TODO: Right now, sampleRate may or may not be defined (it won't be in the cases of inheritance and
50+
// explicitly-set sampling decisions). Are we good with that?
51+
sample_rates: [{ id: samplingMethod, rate: sampleRate }],
52+
4453
// The content-type is assumed to be 'application/json' and not part of
4554
// the current spec for transaction items, so we don't bloat the request
4655
// body with it.

packages/tracing/src/hubextensions.ts

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,6 @@ function traceHeaders(this: Hub): { [key: string]: string } {
2828
return {};
2929
}
3030

31-
/**
32-
* Implements sampling inheritance and falls back to user-provided static rate if no parent decision is available.
33-
*
34-
* @param parentSampled: The parent transaction's sampling decision, if any.
35-
* @param givenRate: The rate to use if no parental decision is available.
36-
*
37-
* @returns The parent's sampling decision (if one exists), or the provided static rate
38-
*/
39-
function _inheritOrUseGivenRate(parentSampled: boolean | undefined, givenRate: unknown): boolean | unknown {
40-
return parentSampled !== undefined ? parentSampled : givenRate;
41-
}
42-
4331
/**
4432
* Makes a sampling decision for the given transaction and stores it on the transaction.
4533
*
@@ -64,15 +52,33 @@ function sample<T extends Transaction>(hub: Hub, transaction: T, samplingContext
6452

6553
// if the user has forced a sampling decision by passing a `sampled` value in their transaction context, go with that
6654
if (transaction.sampled !== undefined) {
55+
transaction.tags = { ...transaction.tags, __sentry_samplingMethod: 'explicitly_set' };
6756
return transaction;
6857
}
6958

7059
// we would have bailed already if neither `tracesSampler` nor `tracesSampleRate` were defined, so one of these should
7160
// work; prefer the hook if so
72-
const sampleRate =
73-
typeof options.tracesSampler === 'function'
74-
? options.tracesSampler(samplingContext)
75-
: _inheritOrUseGivenRate(samplingContext.parentSampled, options.tracesSampleRate);
61+
let sampleRate;
62+
if (typeof options.tracesSampler === 'function') {
63+
sampleRate = options.tracesSampler(samplingContext);
64+
// cast the rate to a number first in case it's a boolean
65+
transaction.tags = {
66+
...transaction.tags,
67+
__sentry_samplingMethod: 'client_sampler',
68+
__sentry_sampleRate: String(Number(sampleRate)),
69+
};
70+
} else if (samplingContext.parentSampled !== undefined) {
71+
sampleRate = samplingContext.parentSampled;
72+
transaction.tags = { ...transaction.tags, __sentry_samplingMethod: 'inheritance' };
73+
} else {
74+
sampleRate = options.tracesSampleRate;
75+
// cast the rate to a number first in case it's a boolean
76+
transaction.tags = {
77+
...transaction.tags,
78+
__sentry_samplingMethod: 'client_rate',
79+
__sentry_sampleRate: String(Number(sampleRate)),
80+
};
81+
}
7682

7783
// Since this is coming from the user (or from a function provided by the user), who knows what we might get. (The
7884
// only valid values are booleans or numbers between 0 and 1.)
@@ -88,7 +94,7 @@ function sample<T extends Transaction>(hub: Hub, transaction: T, samplingContext
8894
`[Tracing] Discarding transaction because ${
8995
typeof options.tracesSampler === 'function'
9096
? 'tracesSampler returned 0 or false'
91-
: 'tracesSampleRate is set to 0'
97+
: 'a negative sampling decision was inherited or tracesSampleRate is set to 0'
9298
}`,
9399
);
94100
transaction.sampled = false;

0 commit comments

Comments
 (0)