Skip to content

Commit

Permalink
feat(sdk-metrics-base): meter identity
Browse files Browse the repository at this point in the history
  • Loading branch information
legendecas committed Apr 18, 2022
1 parent ed2f033 commit 569a402
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,21 @@ export class MeterProviderSharedState {

metricCollectors: MetricCollector[] = [];

meterSharedStates: MeterSharedState[] = [];
meterSharedStates: Map<string, MeterSharedState> = new Map();

constructor(public resource: Resource) {}

getMeterSharedState(instrumentationLibrary: InstrumentationLibrary) {
// TODO: meter identity
// https://github.com/open-telemetry/opentelemetry-js/issues/2593
const meterSharedState = new MeterSharedState(this, instrumentationLibrary);
this.meterSharedStates.push(meterSharedState);
const id = this.instrumentationLibraryId(instrumentationLibrary);
let meterSharedState = this.meterSharedStates.get(id);
if (meterSharedState == null) {
meterSharedState = new MeterSharedState(this, instrumentationLibrary);
this.meterSharedStates.set(id, meterSharedState);
}
return meterSharedState;
}

instrumentationLibraryId(instrumentationLibrary: InstrumentationLibrary) {
return `${instrumentationLibrary.name}:${instrumentationLibrary.version ?? ''}:${instrumentationLibrary.schemaUrl ?? ''}`;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ export class MetricCollector implements MetricProducer {

async collect(): Promise<ResourceMetrics> {
const collectionTime = hrTime();
const instrumentationLibraryMetrics = (await Promise.all(this._sharedState.meterSharedStates
.map(meterSharedState => meterSharedState.collect(this, collectionTime))));
const meterCollectionPromises = Array.from(this._sharedState.meterSharedStates.values())
.map(meterSharedState => meterSharedState.collect(this, collectionTime));
const instrumentationLibraryMetrics = await Promise.all(meterCollectionPromises);

return {
resource: this._sharedState.resource,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,51 @@ describe('MeterProvider', () => {
const meter = meterProvider.getMeter('meter1', '1.0.0');
assert.strictEqual(meter, NOOP_METER);
});

it('get meter with same identity', async () => {
const meterProvider = new MeterProvider({ resource: defaultResource });
const reader = new TestMetricReader();
meterProvider.addMetricReader(reader);

// Create meter and instrument.
// name+version pair 1
meterProvider.getMeter('meter1', 'v1.0.0');
meterProvider.getMeter('meter1', 'v1.0.0');
// name+version pair 2
meterProvider.getMeter('meter2', 'v1.0.0');
meterProvider.getMeter('meter2', 'v1.0.0');
// name+version pair 3
meterProvider.getMeter('meter1', 'v1.0.1');
meterProvider.getMeter('meter1', 'v1.0.1');
// name+version+schemaUrl pair 4
meterProvider.getMeter('meter1', 'v1.0.1', { schemaUrl: 'https://opentelemetry.io/schemas/1.4.0' });
meterProvider.getMeter('meter1', 'v1.0.1', { schemaUrl: 'https://opentelemetry.io/schemas/1.4.0' });

// Perform collection.
const result = await reader.collect();

// Results came only from de-duplicated meters.
assert.strictEqual(result?.instrumentationLibraryMetrics.length, 4);

// InstrumentationLibrary matches from de-duplicated meters.
assertInstrumentationLibraryMetrics(result?.instrumentationLibraryMetrics[0], {
name: 'meter1',
version: 'v1.0.0'
});
assertInstrumentationLibraryMetrics(result?.instrumentationLibraryMetrics[1], {
name: 'meter2',
version: 'v1.0.0'
});
assertInstrumentationLibraryMetrics(result?.instrumentationLibraryMetrics[2], {
name: 'meter1',
version: 'v1.0.1'
});
assertInstrumentationLibraryMetrics(result?.instrumentationLibraryMetrics[3], {
name: 'meter1',
version: 'v1.0.1',
schemaUrl: 'https://opentelemetry.io/schemas/1.4.0',
});
});
});

describe('addView', () => {
Expand Down

0 comments on commit 569a402

Please sign in to comment.