Skip to content
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
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
152 changes: 152 additions & 0 deletions src/__tests__/dataCollection.probe.cjs
Original file line number Diff line number Diff line change
@@ -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
})
95 changes: 95 additions & 0 deletions src/__tests__/dataCollection.test.ts
Original file line number Diff line number Diff line change
@@ -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,
)
})
})
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export function resolveConfig(config: ObservabilityConfig): ResolvedConfig {
}
: undefined,
integrations: config.sentry?.integrations ?? [],
dataCollection: config.sentry?.dataCollection,
},

pyroscope: {
Expand Down
1 change: 1 addition & 0 deletions src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ function initializeSentry(config: ResolvedConfig): void {
profileSessionSampleRate: config.sentry.profileSampleRate,
profileLifecycle: 'trace',
sendDefaultPii: true,
dataCollection: config.sentry.dataCollection,
skipOpenTelemetrySetup: config.enableOtel,
})
}
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -164,6 +166,7 @@ export interface ResolvedConfig {
profileSampleRate: number
tracesSampler?: (context: SamplingContext) => number | undefined
integrations: SentryIntegration[]
dataCollection?: Sentry.NodeOptions['dataCollection']
}

pyroscope: {
Expand Down
Loading