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

feat(core)!: Add normalizedRequest to samplingContext #14902

Merged
merged 4 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const { loggingTransport } = require('@sentry-internal/node-integration-tests');
const Sentry = require('@sentry/node');

Sentry.init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
release: '1.0',
transport: loggingTransport,
tracesSampler: samplingContext => {
// The sampling decision is based on whether the data in `normalizedRequest` is available --> this is what we want to test for
return (
samplingContext.normalizedRequest.url.includes('/test-normalized-request?query=123') &&
samplingContext.normalizedRequest.method &&
samplingContext.normalizedRequest.query_string === 'query=123' &&
!!samplingContext.normalizedRequest.headers
);
},
});

// express must be required after Sentry is initialized
const express = require('express');
const cors = require('cors');
const { startExpressServerAndSendPortToRunner } = require('@sentry-internal/node-integration-tests');

const app = express();

app.use(cors());

app.get('/test-normalized-request', (_req, res) => {
res.send('Success');
});

Sentry.setupExpressErrorHandler(app);

startExpressServerAndSendPortToRunner(app);
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ Sentry.init({
samplingContext.attributes['http.method'] === 'GET'
);
},
debug: true,
});

// express must be required after Sentry is initialized
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,23 @@ describe('express tracesSampler', () => {
});
});
});

describe('express tracesSampler includes normalizedRequest data', () => {
afterAll(() => {
cleanupChildProcesses();
});

describe('CJS', () => {
test('correctly samples & passes data to tracesSampler', done => {
const runner = createRunner(__dirname, 'scenario-normalizedRequest.js')
.expect({
transaction: {
transaction: 'GET /test-normalized-request',
},
})
.start(done);

runner.makeRequest('get', '/test-normalized-request?query=123');
});
});
});
1 change: 1 addition & 0 deletions docs/migration/v8-to-v9.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ Since v9, the types have been merged into `@sentry/core`, which removed some of
- The `TransactionNamingScheme` type has been removed. There is no replacement.
- The `Request` type has been removed. Use `RequestEventData` type instead.
- The `IntegrationClass` type is no longer exported - it was not used anymore. Instead, use `Integration` or `IntegrationFn`.
- The `samplingContext.request` attribute in the `tracesSampler` has been removed. Use `samplingContext.normalizedRequest` instead. Note that the type of `normalizedRequest` differs from `request`.

# No Version Support Timeline

Expand Down
14 changes: 11 additions & 3 deletions packages/core/src/tracing/sampling.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Options, SamplingContext } from '../types-hoist';

import { getIsolationScope } from '../currentScopes';
import { DEBUG_BUILD } from '../debug-build';
import { logger } from '../utils-hoist/logger';
import { hasTracingEnabled } from '../utils/hasTracingEnabled';
Expand All @@ -20,13 +21,20 @@ export function sampleSpan(
return [false];
}

const normalizedRequest = getIsolationScope().getScopeData().sdkProcessingMetadata.normalizedRequest;

const enhancedSamplingContext = {
...samplingContext,
normalizedRequest: samplingContext.normalizedRequest || normalizedRequest,
};

// we would have bailed already if neither `tracesSampler` nor `tracesSampleRate` nor `enableTracing` were defined, so one of these should
// work; prefer the hook if so
let sampleRate;
if (typeof options.tracesSampler === 'function') {
sampleRate = options.tracesSampler(samplingContext);
} else if (samplingContext.parentSampled !== undefined) {
sampleRate = samplingContext.parentSampled;
sampleRate = options.tracesSampler(enhancedSamplingContext);
} else if (enhancedSamplingContext.parentSampled !== undefined) {
sampleRate = enhancedSamplingContext.parentSampled;
} else if (typeof options.tracesSampleRate !== 'undefined') {
sampleRate = options.tracesSampleRate;
} else {
Expand Down
7 changes: 4 additions & 3 deletions packages/core/src/types-hoist/samplingcontext.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ExtractedNodeRequestData, WorkerLocation } from './misc';
import type { RequestEventData } from '../types-hoist/request';
import type { WorkerLocation } from './misc';
import type { SpanAttributes } from './span';

/**
Expand Down Expand Up @@ -35,9 +36,9 @@ export interface SamplingContext extends CustomSamplingContext {
location?: WorkerLocation;

/**
* Object representing the incoming request to a node server.
* Object representing the incoming request to a node server in a normalized format.
*/
request?: ExtractedNodeRequestData;
normalizedRequest?: RequestEventData;

s1gr1d marked this conversation as resolved.
Show resolved Hide resolved
/** The name of the span being sampled. */
name: string;
Expand Down
Loading