Skip to content

Commit

Permalink
feat(instrumentation): add new setMeterInstruments method (#3267)
Browse files Browse the repository at this point in the history
Co-authored-by: Chengzhong Wu <legendecas@gmail.com>
Fixes #3249
  • Loading branch information
osherv authored Nov 20, 2022
1 parent e315f53 commit b29deee
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 10 deletions.
1 change: 1 addition & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -74,15 +73,9 @@ export class HttpInstrumentation extends InstrumentationBase<Http> {
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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {
InstrumentationConfig,
} from '../../src';

import { MeterProvider } from '@opentelemetry/sdk-metrics';

interface TestInstrumentationConfig extends InstrumentationConfig {
isActive?: boolean;
}
Expand Down Expand Up @@ -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);
});
});
Expand Down

0 comments on commit b29deee

Please sign in to comment.