Skip to content

Commit 041b048

Browse files
committed
feat: pipe instrumentation library from Meter to MetricRecord
1 parent 70a1d1e commit 041b048

5 files changed

Lines changed: 83 additions & 14 deletions

File tree

packages/opentelemetry-metrics/src/Meter.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import * as api from '@opentelemetry/api';
18-
import { ConsoleLogger } from '@opentelemetry/core';
18+
import { ConsoleLogger, InstrumentationLibrary } from '@opentelemetry/core';
1919
import { Resource } from '@opentelemetry/resources';
2020
import { BaseBoundInstrument } from './BoundInstrument';
2121
import {
@@ -42,14 +42,19 @@ export class Meter implements api.Meter {
4242
private readonly _metrics = new Map<string, Metric<BaseBoundInstrument>>();
4343
private readonly _batcher: Batcher;
4444
private readonly _resource: Resource;
45+
private readonly _instrumentationLibrary: InstrumentationLibrary;
4546

4647
/**
4748
* Constructs a new Meter instance.
4849
*/
49-
constructor(config: MeterConfig = DEFAULT_CONFIG) {
50+
constructor(
51+
instrumentationLibrary: InstrumentationLibrary,
52+
config: MeterConfig = DEFAULT_CONFIG
53+
) {
5054
this._logger = config.logger || new ConsoleLogger(config.logLevel);
5155
this._batcher = config.batcher ?? new UngroupedBatcher();
5256
this._resource = config.resource || Resource.createTelemetrySDKResource();
57+
this._instrumentationLibrary = instrumentationLibrary;
5358
// start the push controller
5459
const exporter = config.exporter || new NoopExporter();
5560
const interval = config.interval;
@@ -83,7 +88,8 @@ export class Meter implements api.Meter {
8388
name,
8489
opt,
8590
this._batcher,
86-
this._resource
91+
this._resource,
92+
this._instrumentationLibrary
8793
);
8894
this._registerMetric(name, valueRecorder);
8995
return valueRecorder;
@@ -110,7 +116,13 @@ export class Meter implements api.Meter {
110116
...DEFAULT_METRIC_OPTIONS,
111117
...options,
112118
};
113-
const counter = new CounterMetric(name, opt, this._batcher, this._resource);
119+
const counter = new CounterMetric(
120+
name,
121+
opt,
122+
this._batcher,
123+
this._resource,
124+
this._instrumentationLibrary
125+
);
114126
this._registerMetric(name, counter);
115127
return counter;
116128
}
@@ -138,7 +150,8 @@ export class Meter implements api.Meter {
138150
name,
139151
opt,
140152
this._batcher,
141-
this._resource
153+
this._resource,
154+
this._instrumentationLibrary
142155
);
143156
this._registerMetric(name, observer);
144157
return observer;

packages/opentelemetry-metrics/src/MeterProvider.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ export class MeterProvider implements api.MeterProvider {
4646
getMeter(name: string, version = '*', config?: MeterConfig): Meter {
4747
const key = `${name}@${version}`;
4848
if (!this._meters.has(key)) {
49-
this._meters.set(key, new Meter(config || this._config));
49+
this._meters.set(
50+
key,
51+
new Meter({ name, version }, config || this._config)
52+
);
5053
}
5154

5255
return this._meters.get(key)!;

packages/opentelemetry-metrics/src/Metric.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { MetricOptions } from './types';
2727
import { MetricKind, MetricDescriptor, MetricRecord } from './export/types';
2828
import { Batcher } from './export/Batcher';
2929
import { hashLabels } from './Utils';
30+
import { InstrumentationLibrary } from '@opentelemetry/core';
3031

3132
/** This is a SDK implementation of {@link Metric} interface. */
3233
export abstract class Metric<T extends BaseBoundInstrument>
@@ -42,7 +43,8 @@ export abstract class Metric<T extends BaseBoundInstrument>
4243
private readonly _name: string,
4344
private readonly _options: MetricOptions,
4445
private readonly _kind: MetricKind,
45-
readonly resource: Resource
46+
readonly resource: Resource,
47+
readonly instrumentationLibrary: InstrumentationLibrary
4648
) {
4749
this._monotonic = _options.monotonic;
4850
this._disabled = _options.disabled;
@@ -88,6 +90,7 @@ export abstract class Metric<T extends BaseBoundInstrument>
8890
labels: instrument.getLabels(),
8991
aggregator: instrument.getAggregator(),
9092
resource: this.resource,
93+
instrumentationLibrary: this.instrumentationLibrary,
9194
}));
9295
}
9396

@@ -111,9 +114,10 @@ export class CounterMetric extends Metric<BoundCounter> implements api.Counter {
111114
name: string,
112115
options: MetricOptions,
113116
private readonly _batcher: Batcher,
114-
resource: Resource
117+
resource: Resource,
118+
instrumentationLibrary: InstrumentationLibrary
115119
) {
116-
super(name, options, MetricKind.COUNTER, resource);
120+
super(name, options, MetricKind.COUNTER, resource, instrumentationLibrary);
117121
}
118122
protected _makeInstrument(labels: api.Labels): BoundCounter {
119123
return new BoundCounter(
@@ -146,9 +150,16 @@ export class ValueRecorderMetric extends Metric<BoundValueRecorder>
146150
name: string,
147151
options: MetricOptions,
148152
private readonly _batcher: Batcher,
149-
resource: Resource
153+
resource: Resource,
154+
instrumentationLibrary: InstrumentationLibrary
150155
) {
151-
super(name, options, MetricKind.VALUE_RECORDER, resource);
156+
super(
157+
name,
158+
options,
159+
MetricKind.VALUE_RECORDER,
160+
resource,
161+
instrumentationLibrary
162+
);
152163

153164
this._absolute = options.absolute !== undefined ? options.absolute : true; // Absolute default is true
154165
}
@@ -178,9 +189,10 @@ export class ObserverMetric extends Metric<BoundObserver>
178189
name: string,
179190
options: MetricOptions,
180191
private readonly _batcher: Batcher,
181-
resource: Resource
192+
resource: Resource,
193+
instrumentationLibrary: InstrumentationLibrary
182194
) {
183-
super(name, options, MetricKind.OBSERVER, resource);
195+
super(name, options, MetricKind.OBSERVER, resource, instrumentationLibrary);
184196
}
185197

186198
protected _makeInstrument(labels: api.Labels): BoundObserver {

packages/opentelemetry-metrics/src/export/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import { ValueType, HrTime, Labels } from '@opentelemetry/api';
18-
import { ExportResult } from '@opentelemetry/core';
18+
import { ExportResult, InstrumentationLibrary } from '@opentelemetry/core';
1919
import { Resource } from '@opentelemetry/resources';
2020

2121
/** The kind of metric. */
@@ -74,6 +74,7 @@ export interface MetricRecord {
7474
readonly labels: Labels;
7575
readonly aggregator: Aggregator;
7676
readonly resource: Resource;
77+
readonly instrumentationLibrary: InstrumentationLibrary;
7778
}
7879

7980
export interface MetricDescriptor {

packages/opentelemetry-metrics/test/Meter.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,18 @@ describe('Meter', () => {
115115
assert.ok(record.resource instanceof Resource);
116116
});
117117

118+
it('should pipe through instrumentation library', () => {
119+
const counter = meter.createCounter('name') as CounterMetric;
120+
assert.ok(counter.instrumentationLibrary);
121+
122+
counter.add(1, { foo: 'bar' });
123+
124+
const [record] = counter.getMetricRecord();
125+
const { name, version } = record.instrumentationLibrary;
126+
assert.strictEqual(name, 'test-meter');
127+
assert.strictEqual(version, '*');
128+
});
129+
118130
describe('.bind()', () => {
119131
it('should create a counter instrument', () => {
120132
const counter = meter.createCounter('name') as CounterMetric;
@@ -319,6 +331,20 @@ describe('Meter', () => {
319331
assert.ok(record.resource instanceof Resource);
320332
});
321333

334+
it('should pipe through instrumentation library', () => {
335+
const valueRecorder = meter.createValueRecorder(
336+
'name'
337+
) as ValueRecorderMetric;
338+
assert.ok(valueRecorder.instrumentationLibrary);
339+
340+
valueRecorder.record(1, { foo: 'bar' });
341+
342+
const [record] = valueRecorder.getMetricRecord();
343+
const { name, version } = record.instrumentationLibrary;
344+
assert.strictEqual(name, 'test-meter');
345+
assert.strictEqual(version, '*');
346+
});
347+
322348
describe('names', () => {
323349
it('should return no op metric if name is an empty string', () => {
324350
const valueRecorder = meter.createValueRecorder('');
@@ -537,6 +563,20 @@ describe('Meter', () => {
537563
const [record] = observer.getMetricRecord();
538564
assert.ok(record.resource instanceof Resource);
539565
});
566+
567+
it('should pipe through instrumentation library', () => {
568+
const observer = meter.createObserver('name') as ObserverMetric;
569+
assert.ok(observer.instrumentationLibrary);
570+
571+
observer.setCallback(result => {
572+
result.observe(() => 42, { foo: 'bar' });
573+
});
574+
575+
const [record] = observer.getMetricRecord();
576+
const { name, version } = record.instrumentationLibrary;
577+
assert.strictEqual(name, 'test-meter');
578+
assert.strictEqual(version, '*');
579+
});
540580
});
541581

542582
describe('#getMetrics', () => {

0 commit comments

Comments
 (0)