Repository navigation
feat(core): add experimental SDK config integration #26291
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
Changes from all commits
f7ed7df
6833e61
2dda507
276363a
4e4e059
d4d6e80
81cdd0e
bb8bd32
845d54b
a40fab7
f53a5da
6147663
9f0e7c8
78560ad
1c07d88
4130d5a
847b8c8
086b056
1c09a73
59146e7
a18248f
61c0d9d
d6b4e6d
5f04568
5f73c1f
0c08353
9789074
1ebfe67
cc31fb4
c94dd20
e68e4ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| import type { Client } from '../client'; | ||
| import { DEBUG_BUILD } from '../debug-build'; | ||
| import { defineIntegration } from '../integration'; | ||
| import { _INTERNAL_flushMetricsBuffer } from '../metrics/internal'; | ||
| import { count } from '../metrics/public-api'; | ||
| import { Scope } from '../scope'; | ||
| import type { IntegrationFn } from '../types/integration'; | ||
| import { debug } from '../utils/debug-logger'; | ||
| import { fnv1a32 } from '../utils/hash'; | ||
| import { isPlainObject } from '../utils/is'; | ||
| import { timestampInSeconds } from '../utils/time'; | ||
| import { safeUnref } from '../utils/timer'; | ||
|
|
||
| const _experimentalReportSdkConfigIntegration = (() => ({ | ||
| name: 'ExperimentalReportSdkConfig', | ||
| afterAllSetup(client: Client) { | ||
| const scope = new Scope(); | ||
| scope.setClient(client); | ||
| let reported = false; | ||
|
|
||
| const report = (): void => { | ||
| // The timer and flush hook can both request capture; only report once. | ||
| if (reported) { | ||
| return; | ||
| } | ||
|
|
||
| reported = true; | ||
| clearTimeout(timer); | ||
|
|
||
| try { | ||
| const attributes = buildConfigAttributes(client); | ||
| count('sdk.config', 1, { attributes, scope }); | ||
| } catch (error) { | ||
| DEBUG_BUILD && debug.warn('Failed to report SDK configuration', error); | ||
| } | ||
| }; | ||
|
|
||
| // Allow configuration to settle unless the client flushes earlier. | ||
| const timer = safeUnref(setTimeout(report, 5000)); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the RFC also suggest re-sending at a slow interval for server SDKs, but I guess for the initial implementation this might not be needed. |
||
|
|
||
| // Close, tab hiding, and natural Node process exit trigger flushing. | ||
| client.on('flush', () => { | ||
| report(); | ||
| // report() may register the first metric flusher too late for this flush, | ||
| // or run after an existing flusher. Drain explicitly to cover both cases. | ||
| _INTERNAL_flushMetricsBuffer(client); | ||
|
nicohrubec marked this conversation as resolved.
Resolved
|
||
| }); | ||
| }, | ||
| })) satisfies IntegrationFn; | ||
|
|
||
| /** | ||
| * Reports effective SDK configuration once after initialization. | ||
| * | ||
| * @experimental This integration may change or be removed without notice. | ||
| */ | ||
| export const experimentalReportSdkConfigIntegration = defineIntegration(_experimentalReportSdkConfigIntegration); | ||
|
|
||
| // Sort object keys recursively, preserving array order. Replace functions | ||
| // and unsupported values with fingerprints before hashing the config. | ||
| function serialize(value: unknown, ancestors = new Set<object>()): unknown { | ||
|
nicohrubec marked this conversation as resolved.
Resolved
|
||
| if (typeof value === 'function') { | ||
| return `[Function:${fnv1a32(Function.prototype.toString.call(value))}]`; | ||
| } | ||
|
|
||
| // initialScope extras can contain bigints or symbols; JSON throws on bigints and omits symbols. | ||
| if (typeof value === 'bigint' || typeof value === 'symbol') { | ||
| return `[${typeof value}:${fnv1a32(String(value))}]`; | ||
| } | ||
|
|
||
| // Pass through primitives and null unchanged. | ||
| if (value === null || typeof value !== 'object') { | ||
| return value; | ||
| } | ||
|
|
||
| if (value instanceof RegExp) { | ||
| return value.toString(); | ||
| } | ||
|
|
||
| // Stop recursion if an object references itself or an ancestor. | ||
| if (ancestors.has(value)) { | ||
| return '[Circular]'; | ||
| } | ||
|
|
||
| if (!Array.isArray(value) && !isPlainObject(value)) { | ||
| const type = String(Object.prototype.toString.call(value)).slice(8, -1); | ||
| return `[${type}:${fnv1a32(String(value))}]`; | ||
| } | ||
|
|
||
| const nextAncestors = new Set(ancestors).add(value); | ||
| if (Array.isArray(value)) { | ||
| return value.map(item => serialize(item, nextAncestors) ?? null); | ||
| } | ||
|
|
||
| const result: Record<string, unknown> = {}; | ||
| for (const key of Object.keys(value).sort()) { | ||
| const item: unknown = value[key]; | ||
| result[key] = item === undefined ? '[undefined]' : serialize(item, nextAncestors); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| function buildConfigAttributes(client: Client): Record<string, string> { | ||
| // Integrations are reported separately; SDK metadata lives under `sdk`. | ||
| const { runtime, ...options } = Object.fromEntries( | ||
| Object.entries(client.getOptions()).filter( | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. another interesting problem is that this will not have all resolved options no matter how late we call this, because we set options in different ways:
1-3 will show up in |
||
| ([key]) => !['integrations', 'defaultIntegrations', '_metadata'].includes(key), | ||
| ), | ||
| ); | ||
|
|
||
| const config = { | ||
| version: 1, | ||
| sdk: client.getSdkMetadata()?.sdk, | ||
| runtime, | ||
| options: serialize({ | ||
| ...options, | ||
| dataCollection: client.getDataCollectionOptions(), | ||
| }) as Record<string, unknown>, | ||
| options_set_by_user: client.getSdkMetadata()?.optionsSetByUser, | ||
| integrations: client.getIntegrationNames().map(name => ({ name })), | ||
| }; | ||
|
nicohrubec marked this conversation as resolved.
Resolved
|
||
|
|
||
| const metadata = { | ||
| timestamp: timestampInSeconds(), | ||
| release: options.release, | ||
| environment: options.environment, | ||
| dist: options.dist, | ||
| }; | ||
|
|
||
| // Exclude reporting metadata and instance-specific defaults from config identity. | ||
| const hashExclusions = new Set(['serverName', ...Object.keys(metadata)]); | ||
|
|
||
| const hashInput = { | ||
| ...config, | ||
| options: Object.fromEntries(Object.entries(config.options).filter(([key]) => !hashExclusions.has(key))), | ||
| }; | ||
|
|
||
| const payload = { | ||
| ...config, | ||
| ...metadata, | ||
| }; | ||
|
|
||
| return { | ||
| 'sentry.sdk_config.hash': fnv1a32(JSON.stringify(hashInput)), | ||
| 'sentry.sdk_config.payload': JSON.stringify(payload), | ||
| }; | ||
|
Check warning on line 145 in packages/core/src/integrations/reportSdkConfig.ts
|
||
| } | ||
|
sentry-warden[bot] marked this conversation as resolved.
Resolved
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| /** | ||
| * 32-bit FNV-1a. Keep this algorithm stable: tunnel paths must match across SDK versions. | ||
| */ | ||
| export function fnv1a32(input: string): string { | ||
| let hash = 0x811c9dc5; | ||
| for (let i = 0; i < input.length; i++) { | ||
| // eslint-disable-next-line no-bitwise | ||
| hash ^= input.charCodeAt(i); | ||
| hash = Math.imul(hash, 0x01000193); | ||
| } | ||
|
|
||
| // eslint-disable-next-line no-bitwise | ||
| return (hash >>> 0).toString(36); | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.