From b29deeed5029598385aedd30f9b540b1248bea52 Mon Sep 17 00:00:00 2001 From: Osher Vaknin <81672378+osherv@users.noreply.github.com> Date: Sun, 20 Nov 2022 17:28:26 +0200 Subject: [PATCH] feat(instrumentation): add new setMeterInstruments method (#3267) Co-authored-by: Chengzhong Wu Fixes https://github.com/open-telemetry/opentelemetry-js/issues/3249 --- experimental/CHANGELOG.md | 1 + .../src/http.ts | 11 ++------ .../package.json | 1 + .../src/instrumentation.ts | 10 +++++++ .../test/common/Instrumentation.test.ts | 27 ++++++++++++++++++- 5 files changed, 40 insertions(+), 10 deletions(-) diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index f0e7bf5ab3..7d39f3e228 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -9,6 +9,7 @@ All notable changes to experimental packages in this project will be documented ### :rocket: (Enhancement) * feat(instrumentation-http): monitor error events with events.errorMonitor [#3402](https://github.com/open-telemetry/opentelemetry-js/pull/3402) @legendecas +* feat(instrumentation): add new `_setMeterInstruments` protected method that update the meter instruments every meter provider update. ### :bug: (Bug Fix) diff --git a/experimental/packages/opentelemetry-instrumentation-http/src/http.ts b/experimental/packages/opentelemetry-instrumentation-http/src/http.ts index 71f3363f2d..0f19ffd867 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/src/http.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/src/http.ts @@ -26,9 +26,8 @@ import { SpanStatusCode, trace, Histogram, - MeterProvider, MetricAttributes, - ValueType, + ValueType } from '@opentelemetry/api'; import { hrTime, hrTimeDuration, hrTimeToMilliseconds, suppressTracing } from '@opentelemetry/core'; import type * as http from 'http'; @@ -74,15 +73,9 @@ export class HttpInstrumentation extends InstrumentationBase { config ); this._headerCapture = this._createHeaderCapture(); - this._updateMetricInstruments(); } - override setMeterProvider(meterProvider: MeterProvider) { - super.setMeterProvider(meterProvider); - this._updateMetricInstruments(); - } - - private _updateMetricInstruments() { + protected override _updateMetricInstruments() { this._httpServerDurationHistogram = this.meter.createHistogram('http.server.duration', { description: 'measures the duration of the inbound HTTP requests', unit: 'ms', diff --git a/experimental/packages/opentelemetry-instrumentation/package.json b/experimental/packages/opentelemetry-instrumentation/package.json index 29b186d510..8dc3dfb9d2 100644 --- a/experimental/packages/opentelemetry-instrumentation/package.json +++ b/experimental/packages/opentelemetry-instrumentation/package.json @@ -76,6 +76,7 @@ "@opentelemetry/api": "^1.3.0" }, "devDependencies": { + "@opentelemetry/sdk-metrics": "^1.8.0", "@babel/core": "7.16.0", "@opentelemetry/api": "^1.3.0", "@types/mocha": "10.0.0", diff --git a/experimental/packages/opentelemetry-instrumentation/src/instrumentation.ts b/experimental/packages/opentelemetry-instrumentation/src/instrumentation.ts index 5024b5d536..d562ed9056 100644 --- a/experimental/packages/opentelemetry-instrumentation/src/instrumentation.ts +++ b/experimental/packages/opentelemetry-instrumentation/src/instrumentation.ts @@ -56,6 +56,7 @@ implements types.Instrumentation { this._tracer = trace.getTracer(instrumentationName, instrumentationVersion); this._meter = metrics.getMeter(instrumentationName, instrumentationVersion); + this._updateMetricInstruments(); } /* Api to wrap instrumented method */ @@ -81,6 +82,15 @@ implements types.Instrumentation { this.instrumentationName, this.instrumentationVersion ); + + this._updateMetricInstruments(); + } + + /** + * Sets the new metric instruments with the current Meter. + */ + protected _updateMetricInstruments(): void { + return; } /* Returns InstrumentationConfig */ diff --git a/experimental/packages/opentelemetry-instrumentation/test/common/Instrumentation.test.ts b/experimental/packages/opentelemetry-instrumentation/test/common/Instrumentation.test.ts index ac0c5b0a90..be53d1ca59 100644 --- a/experimental/packages/opentelemetry-instrumentation/test/common/Instrumentation.test.ts +++ b/experimental/packages/opentelemetry-instrumentation/test/common/Instrumentation.test.ts @@ -21,6 +21,8 @@ import { InstrumentationConfig, } from '../../src'; +import { MeterProvider } from '@opentelemetry/sdk-metrics'; + interface TestInstrumentationConfig extends InstrumentationConfig { isActive?: boolean; } @@ -54,13 +56,36 @@ describe('BaseInstrumentation', () => { describe('constructor', () => { it('should enable instrumentation by default', () => { - let called = false; + let enableCalled = false; + let updateMetricInstrumentsCalled = false; class TestInstrumentation2 extends TestInstrumentation { override enable() { + enableCalled = true; + } + override _updateMetricInstruments() { + updateMetricInstrumentsCalled = true; + } + } + instrumentation = new TestInstrumentation2(); + assert.strictEqual(enableCalled, true); + assert.strictEqual(updateMetricInstrumentsCalled, true); + }); + }); + + describe('setMeterProvider', () => { + let otelTestingMeterProvider: MeterProvider; + beforeEach(() => { + otelTestingMeterProvider = new MeterProvider(); + }); + it('should call _updateMetricInstruments', () => { + let called = true; + class TestInstrumentation2 extends TestInstrumentation { + override _updateMetricInstruments() { called = true; } } instrumentation = new TestInstrumentation2(); + instrumentation.setMeterProvider(otelTestingMeterProvider); assert.strictEqual(called, true); }); });