From c31f0c4d8d24c6c2eee16481085f80f6b1ac965a Mon Sep 17 00:00:00 2001 From: Ben Miner Date: Fri, 13 Nov 2026 17:02:03 +0000 Subject: [PATCH] feat: expose Sentry data collection controls (2.5.0) --- README.md | 18 ++- package-lock.json | 4 +- package.json | 2 +- src/__tests__/dataCollection.probe.cjs | 152 +++++++++++++++++++++++++ src/__tests__/dataCollection.test.ts | 95 ++++++++++++++++ src/config.ts | 1 + src/provider.ts | 1 + src/types.ts | 3 + 8 files changed, 272 insertions(+), 4 deletions(-) create mode 100644 src/__tests__/dataCollection.probe.cjs create mode 100644 src/__tests__/dataCollection.test.ts diff --git a/README.md b/README.md index cf2ab1f..06fa63b 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,22 @@ All options are passed to `init()` as an `ObservabilityConfig` object. | `enabled` | `boolean` | auto | Override automatic enable/disable logic. By default Sentry is enabled when a DSN is present and the environment is `production` or `staging`. | | `sampleRate` | `number` | `0.25` (prod), `1.0` (other) | Fraction of transactions sent to Sentry, between 0 and 1. | | `profileSampleRate` | `number` | `sampleRate` | Fraction of sampled transactions to profile, between 0 and 1. | +| `dataCollection` | `Sentry.NodeOptions['dataCollection']` | omitted | Data collection controls passed to Sentry unchanged, including HTTP request and response header controls. | + +For example, to restrict automatic capture of a request header: + +```ts +init({ + serviceName: 'my-service', + sentry: { + dataCollection: { + httpHeaders: { request: { deny: ['x-scope3-origin-ref'] } }, + }, + }, +}) +``` + +Omitting `dataCollection` preserves the existing Sentry configuration. When supplied, the SDK gives it precedence over `sendDefaultPii` and applies its own defaults to unspecified categories. Header controls govern the SDK's automatic collection; they do not filter attributes added manually to spans or data captured by other instrumentation. ### `PyroscopeConfig` @@ -308,7 +324,7 @@ npm test ### Releasing -Releases are automated. When a pull request is merged to `main` with a bumped version in `package.json`, the release workflow will: +After a pull request with a bumped version in `package.json` is merged to `main`, run the release workflow manually. It will: 1. Compare the new version against the latest git tag using semver 2. If the version is greater, create a git tag, a GitHub Release, and publish to npm diff --git a/package-lock.json b/package-lock.json index 2079b73..db22a73 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@scope3/observability-js", - "version": "2.4.1", + "version": "2.5.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@scope3/observability-js", - "version": "2.4.1", + "version": "2.5.0", "license": "MIT", "dependencies": { "@opentelemetry/api": "^1.9.0", diff --git a/package.json b/package.json index 893fafe..2326ae2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@scope3/observability-js", - "version": "2.4.1", + "version": "2.5.0", "description": "Unified observability (Sentry, OpenTelemetry, Pyroscope) for Node.js services", "keywords": [ "observability", diff --git a/src/__tests__/dataCollection.probe.cjs b/src/__tests__/dataCollection.probe.cjs new file mode 100644 index 0000000..82106cf --- /dev/null +++ b/src/__tests__/dataCollection.probe.cjs @@ -0,0 +1,152 @@ +const http = require('node:http') + +const { init } = require('../../dist/index.cjs') +const Sentry = require('@sentry/node') + +const PRIVATE_REQUEST = 'ENG559_PRIVATE_REQUEST' +const SAFE_REQUEST = 'ENG559_SAFE_REQUEST' + +function listen(server) { + return new Promise((resolve, reject) => { + server.once('error', reject) + server.listen(0, '127.0.0.1', () => { + server.removeListener('error', reject) + resolve(server.address().port) + }) + }) +} + +function request(port, path, headers = {}) { + return new Promise((resolve, reject) => { + const call = http.request( + { host: '127.0.0.1', port, path, headers }, + (response) => { + response.resume() + response.once('end', () => resolve(response)) + }, + ) + call.once('error', reject) + call.end() + }) +} + +async function main() { + const mode = process.argv[2] + if (mode !== 'sentry' && mode !== 'otel') { + throw new Error(`Unexpected probe mode: ${mode}`) + } + + const exports = [] + const startedSpans = [] + let appReceivedPrivateRequestHeader + + const collector = http.createServer((req, res) => { + const chunks = [] + req.on('data', (chunk) => chunks.push(chunk)) + req.on('end', () => { + exports.push({ + path: req.url, + body: Buffer.concat(chunks).toString('utf8'), + }) + res.writeHead(200, { 'content-type': 'application/json' }) + res.end('{}') + }) + }) + const collectorPort = await listen(collector) + + const app = http.createServer((req, res) => { + if (req.url !== '/exercise') { + res.writeHead(404) + res.end() + return + } + + appReceivedPrivateRequestHeader = req.headers['x-scope3-origin-ref'] + res.end('ok') + }) + const appPort = await listen(app) + + try { + const dataCollection = { + httpHeaders: { + request: { deny: ['x-scope3-origin-ref'] }, + response: { deny: ['x-private-response'] }, + }, + } + + init({ + serviceName: 'eng559-data-collection-test', + environment: 'production', + enableOtel: mode === 'otel', + sentry: { + dsn: `http://local@127.0.0.1:${collectorPort}/1`, + enabled: true, + sampleRate: 1, + dataCollection, + integrations: [ + Sentry.requestDataIntegration({ include: { headers: false } }), + Sentry.httpIntegration(), + ], + }, + pyroscope: { enabled: false }, + ...(mode === 'otel' + ? { + otlp: { + endpoint: `http://127.0.0.1:${collectorPort}`, + sampleRate: 1, + }, + } + : {}), + }) + + const client = Sentry.getClient() + if (!client) throw new Error('Compiled init did not initialize Sentry') + client.on('spanStart', (span) => + startedSpans.push(JSON.stringify(Sentry.spanToJSON(span).data)), + ) + + await request(appPort, '/exercise', { + 'X-Scope3-Origin-Ref': PRIVATE_REQUEST, + 'X-Safe-Request': SAFE_REQUEST, + }) + + if (mode === 'otel') await client.traceProvider.forceFlush() + await Sentry.flush(3000) + + const clientDataCollection = client.getOptions().dataCollection + const sentryExports = exports.filter((entry) => + entry.path.includes('/api/1/envelope/'), + ) + const otlpExports = exports.filter((entry) => entry.path === '/v1/traces') + const sentryText = JSON.stringify(sentryExports) + const otlpText = JSON.stringify(otlpExports) + const startedText = startedSpans.join('\n') + + process.stdout.write( + `DATA_COLLECTION_RESULT ${JSON.stringify({ + dataCollection: { + request: clientDataCollection.httpHeaders.request, + response: clientDataCollection.httpHeaders.response, + }, + appReceivedPrivateRequestHeader, + requestMarkerAtSpanStart: startedText.includes(PRIVATE_REQUEST), + safeRequestAtSpanStart: startedText.includes(SAFE_REQUEST), + sentryExportCount: sentryExports.length, + sentryExportContainsPrivateRequest: + sentryText.includes(PRIVATE_REQUEST), + sentryExportContainsSafeRequest: sentryText.includes(SAFE_REQUEST), + otlpExportCount: otlpExports.length, + otlpExportContainsPrivateRequest: otlpText.includes(PRIVATE_REQUEST), + otlpExportContainsSafeRequest: otlpText.includes(SAFE_REQUEST), + })}\n`, + ) + } finally { + await new Promise((resolve) => app.close(resolve)) + await new Promise((resolve) => collector.close(resolve)) + } +} + +main().catch((error) => { + console.error(error) + process.exitCode = 1 +}) diff --git a/src/__tests__/dataCollection.test.ts b/src/__tests__/dataCollection.test.ts new file mode 100644 index 0000000..e4f84f8 --- /dev/null +++ b/src/__tests__/dataCollection.test.ts @@ -0,0 +1,95 @@ +import { execFileSync } from 'node:child_process' +import { resolve } from 'node:path' +import { fileURLToPath } from 'node:url' +import { beforeAll, describe, expect, it } from 'vitest' + +import { resolveConfig } from '../config' + +const projectRoot = resolve(fileURLToPath(new URL('../..', import.meta.url))) +const integrationProbe = resolve( + fileURLToPath(new URL('./dataCollection.probe.cjs', import.meta.url)), +) + +describe('Sentry dataCollection configuration', () => { + it('preserves existing defaults when no dataCollection option is supplied', () => { + const resolved = resolveConfig({ serviceName: 'test-service' }) + + expect(resolved.sentry.dataCollection).toBeUndefined() + expect(resolved.sentry.enabled).toBe(false) + expect(resolved.sentry.sampleRate).toBe(1) + }) + + it('resolves caller supplied request and response controls unchanged', () => { + const dataCollection = { + httpHeaders: { + request: { deny: ['x-scope3-origin-ref'] }, + response: { deny: ['x-private-response'] }, + }, + } + + const resolved = resolveConfig({ + serviceName: 'test-service', + sentry: { dataCollection }, + }) + + expect(resolved.sentry.dataCollection).toEqual(dataCollection) + expect(resolved.sentry.dataCollection).toBe(dataCollection) + }) + + describe('compiled package initialization and HTTP instrumentation', () => { + beforeAll(() => { + execFileSync('npm', ['run', 'build'], { + cwd: projectRoot, + stdio: 'pipe', + timeout: 60_000, + }) + }, 65_000) + + it.each([ + ['Sentry-only', 'sentry'], + ['OpenTelemetry-enabled', 'otel'], + ])( + 'forwards controls through the compiled package in %s mode', + (_label, mode) => { + const output = execFileSync( + process.execPath, + [integrationProbe, mode], + { + cwd: projectRoot, + encoding: 'utf8', + timeout: 30_000, + env: { ...process.env, NODE_ENV: 'production' }, + }, + ) + const resultLine = output + .split('\n') + .find((line) => line.startsWith('DATA_COLLECTION_RESULT ')) + + if (!resultLine) throw new Error(output) + const result = JSON.parse( + resultLine.slice('DATA_COLLECTION_RESULT '.length), + ) + + expect(result.dataCollection).toEqual({ + request: { deny: ['x-scope3-origin-ref'] }, + response: { deny: ['x-private-response'] }, + }) + expect(result.appReceivedPrivateRequestHeader).toBe( + 'ENG559_PRIVATE_REQUEST', + ) + expect(result.requestMarkerAtSpanStart).toBe(false) + expect(result.safeRequestAtSpanStart).toBe(true) + expect(result.sentryExportCount).toBeGreaterThan(0) + expect(result.sentryExportContainsPrivateRequest).toBe(false) + expect(result.sentryExportContainsSafeRequest).toBe(true) + + if (mode === 'otel') { + expect(result.otlpExportCount).toBeGreaterThan(0) + expect(result.otlpExportContainsPrivateRequest).toBe(false) + expect(result.otlpExportContainsSafeRequest).toBe(true) + } + }, + 40_000, + ) + }) +}) diff --git a/src/config.ts b/src/config.ts index c87b407..375154a 100644 --- a/src/config.ts +++ b/src/config.ts @@ -54,6 +54,7 @@ export function resolveConfig(config: ObservabilityConfig): ResolvedConfig { } : undefined, integrations: config.sentry?.integrations ?? [], + dataCollection: config.sentry?.dataCollection, }, pyroscope: { diff --git a/src/provider.ts b/src/provider.ts index b860eaf..2839079 100644 --- a/src/provider.ts +++ b/src/provider.ts @@ -154,6 +154,7 @@ function initializeSentry(config: ResolvedConfig): void { profileSessionSampleRate: config.sentry.profileSampleRate, profileLifecycle: 'trace', sendDefaultPii: true, + dataCollection: config.sentry.dataCollection, skipOpenTelemetrySetup: config.enableOtel, }) } diff --git a/src/types.ts b/src/types.ts index 2e33073..465706e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -42,6 +42,8 @@ export interface SentryConfig { * (expressIntegration, nodeProfilingIntegration). */ integrations?: SentryIntegration[] + /** Sentry data collection controls, passed through unchanged to the SDK. */ + dataCollection?: Sentry.NodeOptions['dataCollection'] } /** Pyroscope continuous profiling configuration. */ @@ -164,6 +166,7 @@ export interface ResolvedConfig { profileSampleRate: number tracesSampler?: (context: SamplingContext) => number | undefined integrations: SentryIntegration[] + dataCollection?: Sentry.NodeOptions['dataCollection'] } pyroscope: {