Skip to content

Commit 8884128

Browse files
committed
feat(sdk-trace-base): add configurable depth option to ConsoleSpanExporter
Add optional depth parameter to ConsoleSpanExporter constructor to control console.dir output depth. Defaults to 3 for backward compatibility. - Add ConsoleSpanOptions interface with depth parameter - Add constructor to accept options - Use configurable depth instead of hardcoded value - Add tests for default, custom numeric, and null depth values
1 parent 941a9c5 commit 8884128

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

packages/opentelemetry-sdk-trace-base/src/export/ConsoleSpanExporter.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ import {
2222
hrTimeToMicroseconds,
2323
} from '@opentelemetry/core';
2424

25+
interface ConsoleSpanOptions {
26+
depth?: number | null;
27+
}
28+
2529
/**
2630
* This is implementation of {@link SpanExporter} that prints spans to the
2731
* console. This class can be used for diagnostic purposes.
@@ -31,6 +35,12 @@ import {
3135

3236
/* eslint-disable no-console */
3337
export class ConsoleSpanExporter implements SpanExporter {
38+
protected _depth: number | null;
39+
40+
constructor(options?: ConsoleSpanOptions) {
41+
this._depth = typeof options?.depth !== 'undefined' ? options?.depth : 3;
42+
}
43+
3444
/**
3545
* Export spans.
3646
* @param spans
@@ -93,7 +103,7 @@ export class ConsoleSpanExporter implements SpanExporter {
93103
done?: (result: ExportResult) => void
94104
): void {
95105
for (const span of spans) {
96-
console.dir(this._exportInfo(span), { depth: 3 });
106+
console.dir(this._exportInfo(span), { depth: this._depth });
97107
}
98108
if (done) {
99109
return done({ code: ExportResultCode.SUCCESS });

packages/opentelemetry-sdk-trace-base/test/common/export/ConsoleSpanExporter.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,57 @@ describe('ConsoleSpanExporter', () => {
116116
await consoleExporter.forceFlush();
117117
});
118118
});
119+
120+
describe('constructor', () => {
121+
it('should use default depth of 3', () => {
122+
consoleExporter = new ConsoleSpanExporter();
123+
const basicTracerProvider = new BasicTracerProvider({
124+
sampler: new AlwaysOnSampler(),
125+
spanProcessors: [new SimpleSpanProcessor(consoleExporter)],
126+
});
127+
128+
const spyConsole = sinon.spy(console, 'dir');
129+
const tracer = basicTracerProvider.getTracer('test');
130+
const span = tracer.startSpan('test-span');
131+
span.end();
132+
133+
const consoleArgs = spyConsole.args[0];
134+
const options = consoleArgs[1];
135+
assert.strictEqual(options?.depth, 3);
136+
});
137+
138+
it('should use custom depth when provided', () => {
139+
consoleExporter = new ConsoleSpanExporter({ depth: 5 });
140+
const basicTracerProvider = new BasicTracerProvider({
141+
sampler: new AlwaysOnSampler(),
142+
spanProcessors: [new SimpleSpanProcessor(consoleExporter)],
143+
});
144+
145+
const spyConsole = sinon.spy(console, 'dir');
146+
const tracer = basicTracerProvider.getTracer('test');
147+
const span = tracer.startSpan('test-span');
148+
span.end();
149+
150+
const consoleArgs = spyConsole.args[0];
151+
const options = consoleArgs[1];
152+
assert.strictEqual(options?.depth, 5);
153+
});
154+
155+
it('should use null depth when explicitly provided', () => {
156+
consoleExporter = new ConsoleSpanExporter({ depth: null });
157+
const basicTracerProvider = new BasicTracerProvider({
158+
sampler: new AlwaysOnSampler(),
159+
spanProcessors: [new SimpleSpanProcessor(consoleExporter)],
160+
});
161+
162+
const spyConsole = sinon.spy(console, 'dir');
163+
const tracer = basicTracerProvider.getTracer('test');
164+
const span = tracer.startSpan('test-span');
165+
span.end();
166+
167+
const consoleArgs = spyConsole.args[0];
168+
const options = consoleArgs[1];
169+
assert.strictEqual(options?.depth, null);
170+
});
171+
});
119172
});

0 commit comments

Comments
 (0)