Skip to content

Commit db7089b

Browse files
committed
add specific types for default and custom sample context data
1 parent f4ac5f8 commit db7089b

File tree

8 files changed

+114
-21
lines changed

8 files changed

+114
-21
lines changed

packages/hub/src/hub.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import {
33
Breadcrumb,
44
BreadcrumbHint,
55
Client,
6+
CustomSampleContext,
67
Event,
78
EventHint,
89
Extra,
910
Extras,
1011
Hub as HubInterface,
1112
Integration,
1213
IntegrationClass,
13-
SampleContext,
1414
Severity,
1515
Span,
1616
SpanContext,
@@ -370,8 +370,8 @@ export class Hub implements HubInterface {
370370
/**
371371
* @inheritDoc
372372
*/
373-
public startTransaction(context: TransactionContext, sampleContext?: SampleContext): Transaction {
374-
return this._callExtensionMethod('startTransaction', context, sampleContext);
373+
public startTransaction(context: TransactionContext, customSampleContext?: CustomSampleContext): Transaction {
374+
return this._callExtensionMethod('startTransaction', context, customSampleContext);
375375
}
376376

377377
/**

packages/minimal/src/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import { getCurrentHub, Hub, Scope } from '@sentry/hub';
22
import {
33
Breadcrumb,
44
CaptureContext,
5+
CustomSampleContext,
56
Event,
67
Extra,
78
Extras,
8-
SampleContext,
99
Severity,
1010
Transaction,
1111
TransactionContext,
@@ -206,8 +206,9 @@ export function _callOnClient(method: string, ...args: any[]): void {
206206
* Sentry.
207207
*
208208
* @param context Properties of the new `Transaction`.
209-
* @param sampleContext Information given to the transaction sampling function
209+
* @param customSampleContext Information given to the transaction sampling function (along with context-dependent
210+
* default values)
210211
*/
211-
export function startTransaction(context: TransactionContext, sampleContext?: SampleContext): Transaction {
212-
return callOnHub('startTransaction', { ...context }, sampleContext);
212+
export function startTransaction(context: TransactionContext, customSampleContext?: CustomSampleContext): Transaction {
213+
return callOnHub('startTransaction', { ...context }, customSampleContext);
213214
}

packages/tracing/src/hubextensions.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getActiveDomain, getMainCarrier, Hub } from '@sentry/hub';
2-
import { SampleContext, TransactionContext } from '@sentry/types';
2+
import { CustomSampleContext, DefaultSampleContext, SampleContext, TransactionContext } from '@sentry/types';
33
import {
44
dynamicRequire,
55
extractNodeRequestData,
@@ -98,10 +98,10 @@ function sample<T extends Transaction>(hub: Hub, transaction: T, sampleContext:
9898
/**
9999
* Gets the correct context to pass to the tracesSampler, based on the environment (i.e., which SDK is being used)
100100
*
101-
* @returns A SampleContext object
101+
* @returns The default sample context
102102
*/
103-
function getDefaultSampleContext(): SampleContext {
104-
const defaultSampleContext: SampleContext = {};
103+
function getDefaultSampleContext(): DefaultSampleContext {
104+
const defaultSampleContext: DefaultSampleContext = {};
105105

106106
if (isNodeEnv()) {
107107
const domain = getActiveDomain();
@@ -171,9 +171,13 @@ function isValidSampleRate(rate: unknown): boolean {
171171
*
172172
* @see {@link Hub.startTransaction}
173173
*/
174-
function _startTransaction(this: Hub, context: TransactionContext, sampleContext?: SampleContext): Transaction {
174+
function _startTransaction(
175+
this: Hub,
176+
context: TransactionContext,
177+
customSampleContext?: CustomSampleContext,
178+
): Transaction {
175179
const transaction = new Transaction(context, this);
176-
return sample(this, transaction, { ...getDefaultSampleContext(), ...sampleContext });
180+
return sample(this, transaction, { ...getDefaultSampleContext(), ...customSampleContext });
177181
}
178182

179183
/**

packages/types/src/hub.ts

Lines changed: 4 additions & 3 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 { SampleContext, Transaction, TransactionContext } from './transaction';
9+
import { CustomSampleContext, Transaction, TransactionContext } from './transaction';
1010
import { User } from './user';
1111

1212
/**
@@ -198,7 +198,8 @@ export interface Hub {
198198
* Sentry.
199199
*
200200
* @param context Properties of the new `Transaction`.
201-
* @param sampleContext Information given to the transaction sampling function
201+
* @param customSampleContext Information given to the transaction sampling function (along with context-dependent
202+
* default values)
202203
*/
203-
startTransaction(context: TransactionContext, sampleContext?: SampleContext): Transaction;
204+
startTransaction(context: TransactionContext, customSampleContext?: CustomSampleContext): Transaction;
204205
}

packages/types/src/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export { Hub } from './hub';
1010
export { Integration, IntegrationClass } from './integration';
1111
export { LogLevel } from './loglevel';
1212
export { Mechanism } from './mechanism';
13+
export { ExtractedNodeRequestData, WorkerLocation } from './misc';
1314
export { Options } from './options';
1415
export { Package } from './package';
1516
export { Request } from './request';
@@ -22,7 +23,13 @@ export { Span, SpanContext } from './span';
2223
export { StackFrame } from './stackframe';
2324
export { Stacktrace } from './stacktrace';
2425
export { Status } from './status';
25-
export { SampleContext, Transaction, TransactionContext } from './transaction';
26+
export {
27+
CustomSampleContext,
28+
DefaultSampleContext,
29+
SampleContext,
30+
Transaction,
31+
TransactionContext,
32+
} from './transaction';
2633
export { Thread } from './thread';
2734
export { Transport, TransportOptions, TransportClass } from './transport';
2835
export { User } from './user';

packages/types/src/misc.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Data extracted from an incoming request to a node server
3+
*/
4+
export interface ExtractedNodeRequestData {
5+
[key: string]: any;
6+
7+
/** Specific headers from the request */
8+
headers?: { [key: string]: string };
9+
10+
/** The request's method */
11+
method?: string;
12+
13+
/** The request's URL, including query string */
14+
url?: string;
15+
16+
/** String representing the cookies sent along with the request */
17+
cookies?: { [key: string]: string };
18+
19+
/** The request's query string, without the leading '?' */
20+
query_string?: string;
21+
22+
/** Any data sent in the request's body, as a JSON string */
23+
data?: string;
24+
}
25+
26+
/**
27+
* Location object on a service worker's `self` object.
28+
*
29+
* See https://developer.mozilla.org/en-US/docs/Web/API/WorkerLocation.
30+
*/
31+
export interface WorkerLocation {
32+
/** The protocol scheme of the URL of the script executed in the Worker, including the final ':'. */
33+
readonly protocol: string;
34+
35+
/** The host, that is the hostname, a ':', and the port of the URL of the script executed in the Worker. */
36+
readonly host: string;
37+
38+
/** The domain of the URL of the script executed in the Worker. */
39+
readonly hostname: string;
40+
41+
/** The canonical form of the origin of the specific location. */
42+
readonly origin: string;
43+
44+
/** The port number of the URL of the script executed in the Worker. */
45+
readonly port: string;
46+
47+
/** The path of the URL of the script executed in the Worker, beginning with a '/'. */
48+
readonly pathname: string;
49+
50+
/** The parameters (query string) of the URL of the script executed in the Worker, beginning with a '?'. */
51+
readonly search: string;
52+
53+
/** The fragment identifier of the URL of the script executed in the Worker, beginning with a '#'. */
54+
readonly hash: string;
55+
56+
/** Stringifier that returns the whole URL of the script executed in the Worker. */
57+
readonly href: string;
58+
59+
/** Synonym for `href` attribute */
60+
toString(): string;
61+
}

packages/types/src/transaction.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ExtractedNodeRequestData, WorkerLocation } from './misc';
12
import { Span, SpanContext } from './span';
23

34
/**
@@ -50,9 +51,25 @@ export interface Transaction extends TransactionContext, Span {
5051
}
5152

5253
/**
53-
* The data passed to the `tracesSampler` function, which forms the basis for whatever decisions it might make.
54-
* Combination of default values (which differ per SDK/integration) and data passed to `startTransaction`.
54+
* The context passed to the tracesSampler function by default.
55+
*/
56+
export interface DefaultSampleContext {
57+
/** Object representing the URL of the current page or woker script. Passed in a browser or service worker context */
58+
location?: Location | WorkerLocation;
59+
60+
/** Object representing the incoming request to a node server. Passed when in a node server context. */
61+
request?: ExtractedNodeRequestData;
62+
}
63+
64+
/**
65+
* Context data passed by the user when starting a transaction, to be used by the tracesSampler method.
5566
*/
56-
export interface SampleContext {
67+
export interface CustomSampleContext {
5768
[key: string]: any;
5869
}
70+
71+
/**
72+
* The data passed to the `tracesSampler` function, which forms the basis for whatever decisions it might make.
73+
* Combination of default values (which differ per SDK/integration) and data passed to `startTransaction`.
74+
*/
75+
export type SampleContext = DefaultSampleContext & CustomSampleContext;

packages/utils/src/node.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
2+
import { ExtractedNodeRequestData } from '@sentry/types';
3+
24
import { isString } from './is';
35
import { normalize } from './object';
46

@@ -36,7 +38,7 @@ const DEFAULT_REQUEST_KEYS = ['cookies', 'data', 'headers', 'method', 'query_str
3638
export function extractNodeRequestData(
3739
req: { [key: string]: any },
3840
keys: string[] = DEFAULT_REQUEST_KEYS,
39-
): { [key: string]: string } {
41+
): ExtractedNodeRequestData {
4042
// make sure we can safely use dynamicRequire below
4143
if (!isNodeEnv()) {
4244
throw new Error("Can't get node request data outside of a node environment");

0 commit comments

Comments
 (0)