-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(sdk-trace-base): add util.inspect support for SpanImpl, Tracer, BasicTracerProvider #6690
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
Merged
trentm
merged 5 commits into
open-telemetry:main
from
mcollina:feat/sdk-trace-base-inspect-symbol
May 15, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a659d84
feat(sdk-trace-base): add util.inspect support for SpanImpl, Tracer, …
mcollina 72ffcf0
docs(changelog): add entry for sdk-trace-base util.inspect support
mcollina b60c455
test(sdk-trace-base): move util.inspect tests to test/node
mcollina 34539e6
fix(sdk-trace-base): avoid diag noise when inspecting Resource with p…
mcollina 2680da7
lint:fix
trentm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import type { Attributes, AttributeValue } from '@opentelemetry/api'; | ||
| import type { Resource } from '@opentelemetry/resources'; | ||
|
|
||
| /** | ||
| * Well-known symbol used by Node.js `util.inspect` (and `console.*`) to | ||
| * render an object via a custom representation. Defined as a global Symbol | ||
| * so it works without importing from `node:util`, keeping this module safe | ||
| * for browser builds (where the symbol is simply never looked up). | ||
| */ | ||
| export const inspectCustom = Symbol.for('nodejs.util.inspect.custom'); | ||
|
|
||
| /** | ||
| * Collect a Resource's settled attributes without touching the | ||
| * `attributes` getter, which emits diag.error/debug entries when async | ||
| * attribute detectors are still pending. Promise-like (unsettled) | ||
| * entries are silently skipped so logging a Span/Tracer/Provider during | ||
| * startup doesn't recurse through the diag pipeline. | ||
| */ | ||
| export function settledResourceAttributes(resource: Resource): Attributes { | ||
| const attrs: Attributes = {}; | ||
| for (const [k, v] of resource.getRawAttributes()) { | ||
| if (typeof (v as Partial<PromiseLike<unknown>>)?.then === 'function') { | ||
| continue; | ||
| } | ||
| if (v != null) { | ||
| attrs[k] ??= v as AttributeValue; | ||
| } | ||
| } | ||
| return attrs; | ||
| } | ||
|
|
||
| export type InspectFn = (value: unknown, options: unknown) => string; | ||
|
|
||
| export interface InspectStylizeOptions { | ||
| depth?: number | null; | ||
| stylize?: (text: string, styleType: string) => string; | ||
| } | ||
|
|
||
| /** | ||
| * Build a class-tagged inspect representation. Returns a stub like | ||
| * `[ClassName]` once the recursion budget is exhausted, otherwise returns | ||
| * `ClassName <inspected payload>` so nested fields keep proper coloring, | ||
| * indentation, and depth handling. In environments that don't supply an | ||
| * `inspect` callback (e.g. browsers), falls back to returning the raw | ||
| * payload object. | ||
| */ | ||
| export function formatInspect( | ||
| className: string, | ||
| payload: object, | ||
| depth: number, | ||
| options: InspectStylizeOptions | undefined, | ||
| inspect: InspectFn | undefined | ||
| ): unknown { | ||
| if (typeof depth === 'number' && depth < 0) { | ||
| const tag = `[${className}]`; | ||
| return options?.stylize ? options.stylize(tag, 'special') : tag; | ||
| } | ||
| if (typeof inspect !== 'function' || !options) { | ||
| return payload; | ||
| } | ||
| const childOptions = { | ||
| ...options, | ||
| depth: options.depth == null ? options.depth : options.depth - 1, | ||
| }; | ||
| return `${className} ${inspect(payload, childOptions)}`; | ||
| } |
153 changes: 153 additions & 0 deletions
153
packages/opentelemetry-sdk-trace-base/test/node/inspect.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import type { SpanContext } from '@opentelemetry/api'; | ||
| import { | ||
| diag, | ||
| DiagLogLevel, | ||
| ROOT_CONTEXT, | ||
| SpanKind, | ||
| TraceFlags, | ||
| } from '@opentelemetry/api'; | ||
| import { resourceFromAttributes } from '@opentelemetry/resources'; | ||
| import * as assert from 'assert'; | ||
| import * as sinon from 'sinon'; | ||
| import * as util from 'util'; | ||
| import { BasicTracerProvider } from '../../src'; | ||
| import { SpanImpl } from '../../src/Span'; | ||
| import type { Tracer } from '../../src/Tracer'; | ||
|
|
||
| describe('util.inspect', () => { | ||
| const tracerProvider = new BasicTracerProvider(); | ||
| const tracer = tracerProvider.getTracer('default') as Tracer; | ||
| const spanContext: SpanContext = { | ||
| traceId: 'd4cda95b652f4a1592b449d5929fda1b', | ||
| spanId: '6e0c63257de34c92', | ||
| traceFlags: TraceFlags.SAMPLED, | ||
| }; | ||
|
|
||
| describe('SpanImpl', () => { | ||
| it('should render with class tag and key fields', () => { | ||
| const span = new SpanImpl({ | ||
| scope: tracer.instrumentationScope, | ||
| resource: tracer['_resource'], | ||
| context: ROOT_CONTEXT, | ||
| spanContext, | ||
| name: 'span1', | ||
| kind: SpanKind.CLIENT, | ||
| spanLimits: tracer.getSpanLimits(), | ||
| spanProcessor: tracer['_spanProcessor'], | ||
| attributes: { foo: 'bar' }, | ||
| }); | ||
|
|
||
| const out = util.inspect(span, { depth: 5, colors: false }); | ||
| assert.ok(out.startsWith('SpanImpl '), `unexpected prefix: ${out}`); | ||
| assert.ok(out.includes("name: 'span1'")); | ||
| assert.ok(out.includes(spanContext.traceId)); | ||
| assert.ok(out.includes(spanContext.spanId)); | ||
| assert.ok(out.includes("foo: 'bar'")); | ||
| }); | ||
|
|
||
| it('should collapse to a stub when depth budget is exhausted', () => { | ||
| const span = new SpanImpl({ | ||
| scope: tracer.instrumentationScope, | ||
| resource: tracer['_resource'], | ||
| context: ROOT_CONTEXT, | ||
| spanContext, | ||
| name: 'span1', | ||
| kind: SpanKind.CLIENT, | ||
| spanLimits: tracer.getSpanLimits(), | ||
| spanProcessor: tracer['_spanProcessor'], | ||
| }); | ||
|
|
||
| const out = util.inspect({ span }, { depth: 0, colors: false }); | ||
| assert.ok(out.includes('[SpanImpl]'), `unexpected output: ${out}`); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Tracer', () => { | ||
| it('should render with scope and resource', () => { | ||
| const t = tracerProvider.getTracer('debug-scope', '1.2.3'); | ||
| const out = util.inspect(t, { depth: 4, colors: false }); | ||
| assert.ok(out.startsWith('Tracer '), `unexpected prefix: ${out}`); | ||
| assert.ok(out.includes("name: 'debug-scope'")); | ||
| assert.ok(out.includes("version: '1.2.3'")); | ||
| assert.ok(out.includes('spanLimits')); | ||
| }); | ||
| }); | ||
|
|
||
| describe('BasicTracerProvider', () => { | ||
| it('should render with tracer keys', () => { | ||
| const provider = new BasicTracerProvider(); | ||
| provider.getTracer('a'); | ||
| provider.getTracer('b', '0.0.1'); | ||
| const out = util.inspect(provider, { depth: 4, colors: false }); | ||
| assert.ok( | ||
| out.startsWith('BasicTracerProvider '), | ||
| `unexpected prefix: ${out}` | ||
| ); | ||
| assert.ok(out.includes("'a@:'")); | ||
| assert.ok(out.includes("'b@0.0.1:'")); | ||
| }); | ||
| }); | ||
|
|
||
| describe('resource with unsettled async attributes', () => { | ||
| let diagError: sinon.SinonSpy; | ||
| let diagDebug: sinon.SinonSpy; | ||
|
|
||
| beforeEach(() => { | ||
| diagError = sinon.spy(); | ||
| diagDebug = sinon.spy(); | ||
| diag.setLogger( | ||
| { | ||
| error: diagError, | ||
| warn: () => {}, | ||
| info: () => {}, | ||
| debug: diagDebug, | ||
| verbose: () => {}, | ||
| }, | ||
| DiagLogLevel.DEBUG | ||
| ); | ||
| }); | ||
| afterEach(() => diag.disable()); | ||
|
|
||
| it('should not emit diag warnings and should skip unsettled attributes', () => { | ||
| const resource = resourceFromAttributes({ | ||
| 'service.name': 'svc', | ||
| 'cloud.region': Promise.resolve('us-east-1'), | ||
| }); | ||
| const provider = new BasicTracerProvider({ resource }); | ||
| const t = provider.getTracer('default') as Tracer; | ||
| const span = new SpanImpl({ | ||
| scope: t.instrumentationScope, | ||
| resource: t['_resource'], | ||
| context: ROOT_CONTEXT, | ||
| spanContext, | ||
| name: 'span1', | ||
| kind: SpanKind.INTERNAL, | ||
| spanLimits: t.getSpanLimits(), | ||
| spanProcessor: t['_spanProcessor'], | ||
| }); | ||
|
|
||
| const out = util.inspect(span, { depth: 5, colors: false }); | ||
|
|
||
| assert.strictEqual( | ||
| diagError.callCount, | ||
| 0, | ||
| `unexpected diag.error calls: ${diagError.args.map(a => a.join(' ')).join('; ')}` | ||
| ); | ||
| assert.ok( | ||
| !diagDebug.args.some( | ||
| ([msg]) => | ||
| typeof msg === 'string' && | ||
| msg.startsWith('Unsettled resource attribute') | ||
| ), | ||
| 'inspect should not emit "Unsettled resource attribute" debug logs' | ||
| ); | ||
| assert.ok(out.includes("'service.name': 'svc'")); | ||
| assert.ok(!out.includes('cloud.region')); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.