Skip to content

Commit e159e4b

Browse files
committed
feat(sdk-metrics): add configurable depth option to ConsoleMetricExporter
Add optional depth parameter to ConsoleMetricExporter options to control console.dir output depth. Defaults to null for backward compatibility. - Add depth parameter to ConsoleMetricExporterOptions interface - Convert _sendMetrics from static to instance method to access depth - Use configurable depth instead of hardcoded null value - Add tests for default null depth and custom numeric depth values
1 parent 8884128 commit e159e4b

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

packages/sdk-metrics/src/export/ConsoleMetricExporter.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424

2525
interface ConsoleMetricExporterOptions {
2626
temporalitySelector?: AggregationTemporalitySelector;
27+
depth?: number | null;
2728
}
2829

2930
/**
@@ -37,10 +38,12 @@ interface ConsoleMetricExporterOptions {
3738
export class ConsoleMetricExporter implements PushMetricExporter {
3839
protected _shutdown = false;
3940
protected _temporalitySelector: AggregationTemporalitySelector;
41+
protected _depth: number | null;
4042

4143
constructor(options?: ConsoleMetricExporterOptions) {
4244
this._temporalitySelector =
4345
options?.temporalitySelector ?? DEFAULT_AGGREGATION_TEMPORALITY_SELECTOR;
46+
this._depth = typeof options?.depth !== 'undefined' ? options?.depth : null;
4447
}
4548

4649
export(
@@ -53,7 +56,7 @@ export class ConsoleMetricExporter implements PushMetricExporter {
5356
return;
5457
}
5558

56-
return ConsoleMetricExporter._sendMetrics(metrics, resultCallback);
59+
return this._sendMetrics(metrics, resultCallback);
5760
}
5861

5962
forceFlush(): Promise<void> {
@@ -71,7 +74,7 @@ export class ConsoleMetricExporter implements PushMetricExporter {
7174
return Promise.resolve();
7275
}
7376

74-
private static _sendMetrics(
77+
private _sendMetrics(
7578
metrics: ResourceMetrics,
7679
done: (result: ExportResult) => void
7780
): void {
@@ -83,7 +86,7 @@ export class ConsoleMetricExporter implements PushMetricExporter {
8386
dataPointType: metric.dataPointType,
8487
dataPoints: metric.dataPoints,
8588
},
86-
{ depth: null }
89+
{ depth: this._depth }
8790
);
8891
}
8992
}

packages/sdk-metrics/test/export/ConsoleMetricExporter.test.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,5 +174,67 @@ describe('ConsoleMetricExporter', () => {
174174
});
175175
assertAggregationTemporalitySelector(exporter, selector);
176176
});
177+
178+
it('should use default depth of null', async () => {
179+
const previousConsoleDir = console.dir;
180+
console.dir = () => {};
181+
182+
const exporter = new ConsoleMetricExporter();
183+
const metricReader = new PeriodicExportingMetricReader({
184+
exporter: exporter,
185+
exportIntervalMillis: 100,
186+
exportTimeoutMillis: 100,
187+
});
188+
const meterProvider = new MeterProvider({
189+
resource: testResource,
190+
readers: [metricReader],
191+
});
192+
const meter = meterProvider.getMeter('test', '1.0.0');
193+
const counter = meter.createCounter('test_counter');
194+
counter.add(1);
195+
196+
const spyConsole = sinon.spy(console, 'dir');
197+
const spyExport = sinon.spy(exporter, 'export');
198+
199+
await waitForNumberOfExports(spyExport, 1);
200+
201+
const consoleArgs = spyConsole.args[0];
202+
const options = consoleArgs[1];
203+
assert.strictEqual(options?.depth, null);
204+
205+
console.dir = previousConsoleDir;
206+
await metricReader.shutdown();
207+
});
208+
209+
it('should use custom depth when provided', async () => {
210+
const previousConsoleDir = console.dir;
211+
console.dir = () => {};
212+
213+
const exporter = new ConsoleMetricExporter({ depth: 5 });
214+
const metricReader = new PeriodicExportingMetricReader({
215+
exporter: exporter,
216+
exportIntervalMillis: 100,
217+
exportTimeoutMillis: 100,
218+
});
219+
const meterProvider = new MeterProvider({
220+
resource: testResource,
221+
readers: [metricReader],
222+
});
223+
const meter = meterProvider.getMeter('test', '1.0.0');
224+
const counter = meter.createCounter('test_counter');
225+
counter.add(1);
226+
227+
const spyConsole = sinon.spy(console, 'dir');
228+
const spyExport = sinon.spy(exporter, 'export');
229+
230+
await waitForNumberOfExports(spyExport, 1);
231+
232+
const consoleArgs = spyConsole.args[0];
233+
const options = consoleArgs[1];
234+
assert.strictEqual(options?.depth, 5);
235+
236+
console.dir = previousConsoleDir;
237+
await metricReader.shutdown();
238+
});
177239
});
178240
});

0 commit comments

Comments
 (0)