Skip to content

Commit 4c83b27

Browse files
authored
chore: collection from observers when using batch observer (#1470)
1 parent c70dabf commit 4c83b27

File tree

5 files changed

+46
-20
lines changed

5 files changed

+46
-20
lines changed

packages/opentelemetry-metrics/src/BatchObserverMetric.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export class BatchObserverMetric
4444
super(
4545
name,
4646
options,
47-
MetricKind.VALUE_OBSERVER,
47+
MetricKind.BATCH_OBSERVER,
4848
resource,
4949
instrumentationLibrary
5050
);

packages/opentelemetry-metrics/src/Meter.ts

+23-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { ConsoleLogger, InstrumentationLibrary } from '@opentelemetry/core';
1919
import { Resource } from '@opentelemetry/resources';
2020
import { BatchObserverMetric } from './BatchObserverMetric';
2121
import { BaseBoundInstrument } from './BoundInstrument';
22+
import { MetricKind } from './export/types';
2223
import { UpDownCounterMetric } from './UpDownCounterMetric';
2324
import { CounterMetric } from './CounterMetric';
2425
import { UpDownSumObserverMetric } from './UpDownSumObserverMetric';
@@ -295,9 +296,29 @@ export class Meter implements api.Meter {
295296
* meter instance.
296297
*/
297298
async collect(): Promise<void> {
298-
const metrics = Array.from(this._metrics.values()).map(metric => {
299-
return metric.getMetricRecord();
299+
// call batch observers first
300+
const batchObservers = Array.from(this._metrics.values())
301+
.filter(metric => {
302+
return metric.getKind() === MetricKind.BATCH_OBSERVER;
303+
})
304+
.map(metric => {
305+
return metric.getMetricRecord();
306+
});
307+
await Promise.all(batchObservers).then(records => {
308+
records.forEach(metrics => {
309+
metrics.forEach(metric => this._batcher.process(metric));
310+
});
300311
});
312+
313+
// after this all remaining metrics can be run
314+
const metrics = Array.from(this._metrics.values())
315+
.filter(metric => {
316+
return metric.getKind() !== MetricKind.BATCH_OBSERVER;
317+
})
318+
.map(metric => {
319+
return metric.getMetricRecord();
320+
});
321+
301322
await Promise.all(metrics).then(records => {
302323
records.forEach(metrics => {
303324
metrics.forEach(metric => this._batcher.process(metric));

packages/opentelemetry-metrics/src/Metric.ts

+7
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ export abstract class Metric<T extends BaseBoundInstrument>
7777
this._instruments.clear();
7878
}
7979

80+
/**
81+
* Returns kind of metric
82+
*/
83+
getKind(): MetricKind {
84+
return this._kind;
85+
}
86+
8087
getMetricRecord(): Promise<MetricRecord[]> {
8188
return new Promise(resolve => {
8289
resolve(

packages/opentelemetry-metrics/src/export/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export enum MetricKind {
2626
SUM_OBSERVER,
2727
UP_DOWN_SUM_OBSERVER,
2828
VALUE_OBSERVER,
29+
BATCH_OBSERVER,
2930
}
3031

3132
/** The kind of aggregator. */

packages/opentelemetry-metrics/test/Meter.test.ts

+14-17
Original file line numberDiff line numberDiff line change
@@ -1180,16 +1180,13 @@ describe('Meter', () => {
11801180
);
11811181

11821182
await meter.collect();
1183+
const records = meter.getBatcher().checkPointSet();
1184+
assert.strictEqual(records.length, 8);
11831185

1184-
const tempMetricRecords: MetricRecord[] = await tempMetric.getMetricRecord();
1185-
const cpuUsageMetricRecords: MetricRecord[] = await cpuUsageMetric.getMetricRecord();
1186-
assert.strictEqual(tempMetricRecords.length, 4);
1187-
assert.strictEqual(cpuUsageMetricRecords.length, 4);
1188-
1189-
const metric1 = tempMetricRecords[0];
1190-
const metric2 = tempMetricRecords[1];
1191-
const metric3 = tempMetricRecords[2];
1192-
const metric4 = tempMetricRecords[3];
1186+
const metric1 = records[0];
1187+
const metric2 = records[1];
1188+
const metric3 = records[2];
1189+
const metric4 = records[3];
11931190
assert.strictEqual(hashLabels(metric1.labels), '|#app:app1,core:1');
11941191
assert.strictEqual(hashLabels(metric2.labels), '|#app:app1,core:2');
11951192
assert.strictEqual(hashLabels(metric3.labels), '|#app:app2,core:1');
@@ -1224,14 +1221,14 @@ describe('Meter', () => {
12241221
sum: 69,
12251222
});
12261223

1227-
const metric5 = cpuUsageMetricRecords[0];
1228-
const metric6 = cpuUsageMetricRecords[1];
1229-
const metric7 = cpuUsageMetricRecords[2];
1230-
const metric8 = cpuUsageMetricRecords[3];
1231-
assert.strictEqual(hashLabels(metric1.labels), '|#app:app1,core:1');
1232-
assert.strictEqual(hashLabels(metric2.labels), '|#app:app1,core:2');
1233-
assert.strictEqual(hashLabels(metric3.labels), '|#app:app2,core:1');
1234-
assert.strictEqual(hashLabels(metric4.labels), '|#app:app2,core:2');
1224+
const metric5 = records[4];
1225+
const metric6 = records[5];
1226+
const metric7 = records[6];
1227+
const metric8 = records[7];
1228+
assert.strictEqual(hashLabels(metric5.labels), '|#app:app1,core:1');
1229+
assert.strictEqual(hashLabels(metric6.labels), '|#app:app1,core:2');
1230+
assert.strictEqual(hashLabels(metric7.labels), '|#app:app2,core:1');
1231+
assert.strictEqual(hashLabels(metric8.labels), '|#app:app2,core:2');
12351232

12361233
ensureMetric(metric5, 'cpu_usage_per_app', {
12371234
count: 1,

0 commit comments

Comments
 (0)