Skip to content

Commit 4a1a3b5

Browse files
committed
allow startTransaction to take a sampleContext argument which gets passed to tracesSampler
1 parent 88045c6 commit 4a1a3b5

File tree

5 files changed

+19
-12
lines changed

5 files changed

+19
-12
lines changed

packages/hub/src/hub.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
Hub as HubInterface,
1111
Integration,
1212
IntegrationClass,
13+
SampleContext,
1314
Severity,
1415
Span,
1516
SpanContext,
@@ -369,8 +370,8 @@ export class Hub implements HubInterface {
369370
/**
370371
* @inheritDoc
371372
*/
372-
public startTransaction(context: TransactionContext): Transaction {
373-
return this._callExtensionMethod('startTransaction', context);
373+
public startTransaction(context: TransactionContext, sampleContext?: SampleContext): Transaction {
374+
return this._callExtensionMethod('startTransaction', context, sampleContext);
374375
}
375376

376377
/**

packages/minimal/src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
Event,
66
Extra,
77
Extras,
8+
SampleContext,
89
Severity,
910
Transaction,
1011
TransactionContext,
@@ -205,7 +206,8 @@ export function _callOnClient(method: string, ...args: any[]): void {
205206
* Sentry.
206207
*
207208
* @param context Properties of the new `Transaction`.
209+
* @param sampleContext Information given to the transaction sampling function
208210
*/
209-
export function startTransaction(context: TransactionContext): Transaction {
210-
return callOnHub('startTransaction', { ...context });
211+
export function startTransaction(context: TransactionContext, sampleContext?: SampleContext): Transaction {
212+
return callOnHub('startTransaction', { ...context }, sampleContext);
211213
}

packages/tracing/src/hubextensions.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function traceHeaders(this: Hub): { [key: string]: string } {
3232
*
3333
* Mutates the given Transaction object and then returns the mutated object.
3434
*/
35-
function sample<T extends Transaction>(hub: Hub, transaction: T): T {
35+
function sample<T extends Transaction>(hub: Hub, transaction: T, sampleContext: SampleContext = {}): T {
3636
const client = hub.getClient();
3737
const options = (client && client.getOptions()) || {};
3838

@@ -47,8 +47,6 @@ function sample<T extends Transaction>(hub: Hub, transaction: T): T {
4747
// we have to test for a pre-existsing sampling decision, in case this transaction is a child transaction and has
4848
// inherited its parent's decision
4949
if (transaction.sampled === undefined) {
50-
const sampleContext: SampleContext = {}; // TODO (kmclb) build context object
51-
5250
// we would have bailed at the beginning if neither `tracesSampler` nor `tracesSampleRate` were defined, so one of
5351
// these should work; prefer the hook if so
5452
const sampleRate =
@@ -82,9 +80,9 @@ function sample<T extends Transaction>(hub: Hub, transaction: T): T {
8280
/**
8381
* {@see Hub.startTransaction}
8482
*/
85-
function startTransaction(this: Hub, context: TransactionContext): Transaction {
83+
function startTransaction(this: Hub, context: TransactionContext, sampleContext?: SampleContext): Transaction {
8684
const transaction = new Transaction(context, this);
87-
return sample(this, transaction);
85+
return sample(this, transaction, sampleContext);
8886
}
8987

9088
/**
@@ -95,9 +93,10 @@ export function startIdleTransaction(
9593
context: TransactionContext,
9694
idleTimeout?: number,
9795
onScope?: boolean,
96+
sampleContext?: SampleContext,
9897
): IdleTransaction {
9998
const transaction = new IdleTransaction(context, hub, idleTimeout, onScope);
100-
return sample(hub, transaction);
99+
return sample(hub, transaction, sampleContext);
101100
}
102101

103102
/**

packages/types/src/hub.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Integration, IntegrationClass } from './integration';
66
import { Scope } from './scope';
77
import { Severity } from './severity';
88
import { Span, SpanContext } from './span';
9-
import { Transaction, TransactionContext } from './transaction';
9+
import { SampleContext, Transaction, TransactionContext } from './transaction';
1010
import { User } from './user';
1111

1212
/**
@@ -198,6 +198,7 @@ export interface Hub {
198198
* Sentry.
199199
*
200200
* @param context Properties of the new `Transaction`.
201+
* @param sampleContext Information given to the transaction sampling function
201202
*/
202-
startTransaction(context: TransactionContext): Transaction;
203+
startTransaction(context: TransactionContext, sampleContext?: SampleContext): Transaction;
203204
}

packages/types/src/options.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ export interface Options {
151151
*
152152
* Can be defined in place of `tracesSampleRate`. If neither is defined, tracing is disabled.
153153
*
154+
* `startTransaction` or `startIdleTransaction` accept a `sampleContext` argument, which is then passed to this
155+
* function, meant to provide information on which to make a sampling decision. Additional information may be included
156+
* automatically, depending on the SDK and any included integrations.
157+
*
154158
* @returns A sample rate between 0 and 1 (0 drops the trace, 1 guarantees it will be sent).
155159
*/
156160
tracesSampler?(traceContext: SampleContext): number;

0 commit comments

Comments
 (0)