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

feat(sdk-metrics-base): meter identity #2901

Merged
merged 2 commits into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ All notable changes to experimental packages in this project will be documented
* feat(instrumentation-xhr): add applyCustomAttributesOnSpan hook #2134 @mhennoch
* feat(proto): add @opentelemetry/otlp-transformer package with hand-rolled transformation #2746 @dyladan
* feat(sdk-metrics-base): shutdown and forceflush on MeterProvider #2890 @legendecas
* feat(sdk-metrics-base): meter identity #2901 @legendecas
dyladan marked this conversation as resolved.
Show resolved Hide resolved

### :bug: (Bug Fix)

Expand Down
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) {
dyladan marked this conversation as resolved.
Show resolved Hide resolved
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));
Comment on lines +38 to +39
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Random trivia: Array.from(states.values(), state => state.collect(this, collectionTime)); would be shorter, but benched it for fun and found that Array.from(states.values()).map(state => state.collect(this, collectionTime)); is ~4x faster on Node.js 🤷‍♂️

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