Skip to content
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

IMPORTANT - Fixing collecting data from observers when using batch observer in first run #1470

Merged
merged 3 commits into from
Sep 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/opentelemetry-metrics/src/BatchObserverMetric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class BatchObserverMetric
super(
name,
options,
MetricKind.VALUE_OBSERVER,
MetricKind.BATCH_OBSERVER,
resource,
instrumentationLibrary
);
Expand Down
25 changes: 23 additions & 2 deletions packages/opentelemetry-metrics/src/Meter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { ConsoleLogger, InstrumentationLibrary } from '@opentelemetry/core';
import { Resource } from '@opentelemetry/resources';
import { BatchObserverMetric } from './BatchObserverMetric';
import { BaseBoundInstrument } from './BoundInstrument';
import { MetricKind } from './export/types';
import { UpDownCounterMetric } from './UpDownCounterMetric';
import { CounterMetric } from './CounterMetric';
import { UpDownSumObserverMetric } from './UpDownSumObserverMetric';
Expand Down Expand Up @@ -295,9 +296,29 @@ export class Meter implements api.Meter {
* meter instance.
*/
async collect(): Promise<void> {
const metrics = Array.from(this._metrics.values()).map(metric => {
return metric.getMetricRecord();
// call batch observers first
const batchObservers = Array.from(this._metrics.values())
.filter(metric => {
return metric.getKind() === MetricKind.BATCH_OBSERVER;
})
.map(metric => {
return metric.getMetricRecord();
});
await Promise.all(batchObservers).then(records => {
records.forEach(metrics => {
metrics.forEach(metric => this._batcher.process(metric));
});
});

// after this all remaining metrics can be run
const metrics = Array.from(this._metrics.values())
.filter(metric => {
return metric.getKind() !== MetricKind.BATCH_OBSERVER;
})
.map(metric => {
return metric.getMetricRecord();
});

await Promise.all(metrics).then(records => {
records.forEach(metrics => {
metrics.forEach(metric => this._batcher.process(metric));
Expand Down
7 changes: 7 additions & 0 deletions packages/opentelemetry-metrics/src/Metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ export abstract class Metric<T extends BaseBoundInstrument>
this._instruments.clear();
}

/**
* Returns kind of metric
*/
getKind(): MetricKind {
return this._kind;
}

getMetricRecord(): Promise<MetricRecord[]> {
return new Promise(resolve => {
resolve(
Expand Down
1 change: 1 addition & 0 deletions packages/opentelemetry-metrics/src/export/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export enum MetricKind {
SUM_OBSERVER,
UP_DOWN_SUM_OBSERVER,
VALUE_OBSERVER,
BATCH_OBSERVER,
}

/** The kind of aggregator. */
Expand Down
31 changes: 14 additions & 17 deletions packages/opentelemetry-metrics/test/Meter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1180,16 +1180,13 @@ describe('Meter', () => {
);

await meter.collect();
const records = meter.getBatcher().checkPointSet();
assert.strictEqual(records.length, 8);

const tempMetricRecords: MetricRecord[] = await tempMetric.getMetricRecord();
const cpuUsageMetricRecords: MetricRecord[] = await cpuUsageMetric.getMetricRecord();
assert.strictEqual(tempMetricRecords.length, 4);
assert.strictEqual(cpuUsageMetricRecords.length, 4);

const metric1 = tempMetricRecords[0];
const metric2 = tempMetricRecords[1];
const metric3 = tempMetricRecords[2];
const metric4 = tempMetricRecords[3];
const metric1 = records[0];
const metric2 = records[1];
const metric3 = records[2];
const metric4 = records[3];
assert.strictEqual(hashLabels(metric1.labels), '|#app:app1,core:1');
assert.strictEqual(hashLabels(metric2.labels), '|#app:app1,core:2');
assert.strictEqual(hashLabels(metric3.labels), '|#app:app2,core:1');
Expand Down Expand Up @@ -1224,14 +1221,14 @@ describe('Meter', () => {
sum: 69,
});

const metric5 = cpuUsageMetricRecords[0];
const metric6 = cpuUsageMetricRecords[1];
const metric7 = cpuUsageMetricRecords[2];
const metric8 = cpuUsageMetricRecords[3];
assert.strictEqual(hashLabels(metric1.labels), '|#app:app1,core:1');
assert.strictEqual(hashLabels(metric2.labels), '|#app:app1,core:2');
assert.strictEqual(hashLabels(metric3.labels), '|#app:app2,core:1');
assert.strictEqual(hashLabels(metric4.labels), '|#app:app2,core:2');
const metric5 = records[4];
const metric6 = records[5];
const metric7 = records[6];
const metric8 = records[7];
assert.strictEqual(hashLabels(metric5.labels), '|#app:app1,core:1');
assert.strictEqual(hashLabels(metric6.labels), '|#app:app1,core:2');
assert.strictEqual(hashLabels(metric7.labels), '|#app:app2,core:1');
assert.strictEqual(hashLabels(metric8.labels), '|#app:app2,core:2');

ensureMetric(metric5, 'cpu_usage_per_app', {
count: 1,
Expand Down