Skip to content

Commit 3b56060

Browse files
authored
feat(sdk-metrics)!: drop deprecated type field on MetricDescriptor (#5291)
1 parent 04e74d7 commit 3b56060

File tree

21 files changed

+61
-82
lines changed

21 files changed

+61
-82
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ For semantic convention package changes, see the [semconv CHANGELOG](packages/se
1111

1212
### :boom: Breaking Change
1313

14-
* feat(sdk-metrics)!: drop deprecated `InstrumentDescriptor` type; use `MetricDescriptor` instead [#5277](https://github.com/open-telemetry/opentelemetry-js/pull/5266)
14+
* feat(sdk-metrics)!: drop deprecated `type` field on `MetricDescriptor` [#5291](https://github.com/open-telemetry/opentelemetry-js/pull/5291) @chancancode
15+
* feat(sdk-metrics)!: drop deprecated `InstrumentDescriptor` type; use `MetricDescriptor` instead [#5277](https://github.com/open-telemetry/opentelemetry-js/pull/5266) @chancancode
1516
* feat(sdk-metrics)!: bump minimum version of `@opentelemetry/api` peer dependency to 1.9.0 [#5254](https://github.com/open-telemetry/opentelemetry-js/pull/5254) @chancancode
1617
* chore(shim-opentracing): replace deprecated SpanAttributes [#4430](https://github.com/open-telemetry/opentelemetry-js/pull/4430) @JamieDanielson
1718
* chore(otel-core): replace deprecated SpanAttributes [#4408](https://github.com/open-telemetry/opentelemetry-js/pull/4408) @JamieDanielson

experimental/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ All notable changes to experimental packages in this project will be documented
77

88
### :boom: Breaking Change
99

10+
* feat(exporter-prometheus)!: stop the using `type` field to enforce naming conventions [#5291](https://github.com/open-telemetry/opentelemetry-js/pull/5291) @chancancode
11+
* Any non-monotonic sums will now be treated as counters and will therefore include the `_total` suffix.
12+
* feat(shim-opencenus)!: stop mapping removed Instrument `type` to OpenTelemetry metrics [#5291](https://github.com/open-telemetry/opentelemetry-js/pull/5291) @chancancode
13+
* The internal OpenTelemetry data model dropped the concept of instrument type on exported metrics, therefore mapping it is not necessary anymore.
1014
* feat(instrumentation-fetch)!: passthrough original response to `applyCustomAttributes` hook [#5281](https://github.com/open-telemetry/opentelemetry-js/pull/5281) @chancancode
1115
* Previously, the fetch instrumentation code unconditionally clones every `fetch()` response in order to preserve the ability for the `applyCustomAttributes` hook to consume the response body. This is fundamentally unsound, as it forces the browser to buffer and retain the response body until it is fully received and read, which crates unnecessary memory pressure on large or long-running response streams. In extreme cases, this is effectively a memory leak and can cause the browser tab to crash. If your use case for `applyCustomAttributes` requires access to the response body, please chime in on [#5293](https://github.com/open-telemetry/opentelemetry-js/issues/5293).
1216

experimental/packages/opentelemetry-exporter-prometheus/src/PrometheusSerializer.ts

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import { diag, Attributes, AttributeValue } from '@opentelemetry/api';
1818
import {
1919
ResourceMetrics,
20-
InstrumentType,
2120
DataPointType,
2221
ScopeMetrics,
2322
MetricData,
@@ -90,10 +89,14 @@ function sanitizePrometheusMetricName(name: string): string {
9089
*/
9190
function enforcePrometheusNamingConvention(
9291
name: string,
93-
type: InstrumentType
92+
data: MetricData
9493
): string {
9594
// Prometheus requires that metrics of the Counter kind have "_total" suffix
96-
if (!name.endsWith('_total') && type === InstrumentType.COUNTER) {
95+
if (
96+
!name.endsWith('_total') &&
97+
data.dataPointType === DataPointType.SUM &&
98+
data.isMonotonic
99+
) {
97100
name = name + '_total';
98101
}
99102

@@ -210,7 +213,7 @@ export class PrometheusSerializer {
210213
}
211214
const dataPointType = metricData.dataPointType;
212215

213-
name = enforcePrometheusNamingConvention(name, metricData.descriptor.type);
216+
name = enforcePrometheusNamingConvention(name, metricData);
214217

215218
const help = `# HELP ${name} ${escapeString(
216219
metricData.descriptor.description || 'description missing'
@@ -225,25 +228,13 @@ export class PrometheusSerializer {
225228
case DataPointType.SUM:
226229
case DataPointType.GAUGE: {
227230
results = metricData.dataPoints
228-
.map(it =>
229-
this._serializeSingularDataPoint(
230-
name,
231-
metricData.descriptor.type,
232-
it
233-
)
234-
)
231+
.map(it => this._serializeSingularDataPoint(name, metricData, it))
235232
.join('');
236233
break;
237234
}
238235
case DataPointType.HISTOGRAM: {
239236
results = metricData.dataPoints
240-
.map(it =>
241-
this._serializeHistogramDataPoint(
242-
name,
243-
metricData.descriptor.type,
244-
it
245-
)
246-
)
237+
.map(it => this._serializeHistogramDataPoint(name, metricData, it))
247238
.join('');
248239
break;
249240
}
@@ -259,12 +250,12 @@ export class PrometheusSerializer {
259250

260251
private _serializeSingularDataPoint(
261252
name: string,
262-
type: InstrumentType,
253+
data: MetricData,
263254
dataPoint: DataPoint<number>
264255
): string {
265256
let results = '';
266257

267-
name = enforcePrometheusNamingConvention(name, type);
258+
name = enforcePrometheusNamingConvention(name, data);
268259
const { value, attributes } = dataPoint;
269260
const timestamp = hrTimeToMilliseconds(dataPoint.endTime);
270261
results += stringify(
@@ -279,12 +270,12 @@ export class PrometheusSerializer {
279270

280271
private _serializeHistogramDataPoint(
281272
name: string,
282-
type: InstrumentType,
273+
data: MetricData,
283274
dataPoint: DataPoint<Histogram>
284275
): string {
285276
let results = '';
286277

287-
name = enforcePrometheusNamingConvention(name, type);
278+
name = enforcePrometheusNamingConvention(name, data);
288279
const attributes = dataPoint.attributes;
289280
const histogram = dataPoint.value;
290281
const timestamp = hrTimeToMilliseconds(dataPoint.endTime);

experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusExporter.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,9 @@ describe('PrometheusExporter', () => {
389389

390390
assert.deepStrictEqual(lines, [
391391
...serializedDefaultResourceLines,
392-
'# HELP metric_observable_counter a test description',
393-
'# TYPE metric_observable_counter counter',
394-
'metric_observable_counter{key1="attributeValue1"} 20',
392+
'# HELP metric_observable_counter_total a test description',
393+
'# TYPE metric_observable_counter_total counter',
394+
'metric_observable_counter_total{key1="attributeValue1"} 20',
395395
'',
396396
]);
397397
});

experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ describe('PrometheusSerializer', () => {
107107

108108
const result = serializer['_serializeSingularDataPoint'](
109109
metric.descriptor.name,
110-
metric.descriptor.type,
110+
metric,
111111
pointData[0]
112112
);
113113
return result;
@@ -162,7 +162,7 @@ describe('PrometheusSerializer', () => {
162162

163163
const result = serializer['_serializeHistogramDataPoint'](
164164
metric.descriptor.name,
165-
metric.descriptor.type,
165+
metric,
166166
pointData[0]
167167
);
168168
return result;
@@ -511,7 +511,7 @@ describe('PrometheusSerializer', () => {
511511
} else {
512512
const result = serializer['_serializeSingularDataPoint'](
513513
metric.descriptor.name,
514-
metric.descriptor.type,
514+
metric,
515515
pointData[0]
516516
);
517517
return result;
@@ -598,7 +598,7 @@ describe('PrometheusSerializer', () => {
598598

599599
const result = serializer['_serializeSingularDataPoint'](
600600
metric.descriptor.name,
601-
metric.descriptor.type,
601+
metric,
602602
pointData[0]
603603
);
604604
return result;

experimental/packages/otlp-transformer/test/metrics.test.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import { Resource } from '@opentelemetry/resources';
1818
import {
1919
AggregationTemporality,
2020
DataPointType,
21-
InstrumentType,
2221
MetricData,
2322
ResourceMetrics,
2423
} from '@opentelemetry/sdk-metrics';
@@ -110,7 +109,6 @@ describe('Metrics', () => {
110109
return {
111110
descriptor: {
112111
description: 'this is a description',
113-
type: InstrumentType.COUNTER,
114112
name: 'counter',
115113
unit: '1',
116114
valueType: ValueType.INT,
@@ -136,7 +134,6 @@ describe('Metrics', () => {
136134
return {
137135
descriptor: {
138136
description: 'this is a description',
139-
type: InstrumentType.UP_DOWN_COUNTER,
140137
name: 'up-down-counter',
141138
unit: '1',
142139
valueType: ValueType.INT,
@@ -162,7 +159,6 @@ describe('Metrics', () => {
162159
return {
163160
descriptor: {
164161
description: 'this is a description',
165-
type: InstrumentType.OBSERVABLE_COUNTER,
166162
name: 'observable-counter',
167163
unit: '1',
168164
valueType: ValueType.INT,
@@ -188,7 +184,6 @@ describe('Metrics', () => {
188184
return {
189185
descriptor: {
190186
description: 'this is a description',
191-
type: InstrumentType.OBSERVABLE_UP_DOWN_COUNTER,
192187
name: 'observable-up-down-counter',
193188
unit: '1',
194189
valueType: ValueType.INT,
@@ -211,7 +206,6 @@ describe('Metrics', () => {
211206
return {
212207
descriptor: {
213208
description: 'this is a description',
214-
type: InstrumentType.OBSERVABLE_GAUGE,
215209
name: 'gauge',
216210
unit: '1',
217211
valueType: ValueType.DOUBLE,
@@ -241,7 +235,6 @@ describe('Metrics', () => {
241235
return {
242236
descriptor: {
243237
description: 'this is a description',
244-
type: InstrumentType.HISTOGRAM,
245238
name: 'hist',
246239
unit: '1',
247240
valueType: ValueType.INT,
@@ -282,7 +275,6 @@ describe('Metrics', () => {
282275
return {
283276
descriptor: {
284277
description: 'this is a description',
285-
type: InstrumentType.HISTOGRAM,
286278
name: 'xhist',
287279
unit: '1',
288280
valueType: ValueType.INT,

experimental/packages/shim-opencensus/src/metric-transform.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@ import {
2222
DataPointType,
2323
GaugeMetricData,
2424
HistogramMetricData,
25-
InstrumentType,
2625
MetricData,
2726
SumMetricData,
2827
} from '@opentelemetry/sdk-metrics';
2928

3029
type BaseMetric = Omit<MetricData, 'dataPoints' | 'dataPointType'>;
3130
interface MappedType {
32-
type: InstrumentType;
3331
valueType: ValueType;
3432
dataPointType:
3533
| DataPointType.GAUGE
@@ -51,7 +49,6 @@ export function mapOcMetric(metric: oc.Metric): MetricData | null {
5149
description,
5250
name,
5351
unit,
54-
type: mappedType.type,
5552
valueType: mappedType.valueType,
5653
},
5754
};
@@ -72,33 +69,28 @@ function mapOcMetricDescriptorType(
7269
switch (type) {
7370
case oc.MetricDescriptorType.GAUGE_INT64:
7471
return {
75-
type: InstrumentType.OBSERVABLE_GAUGE,
7672
valueType: ValueType.INT,
7773
dataPointType: DataPointType.GAUGE,
7874
};
7975
case oc.MetricDescriptorType.GAUGE_DOUBLE:
8076
return {
81-
type: InstrumentType.OBSERVABLE_GAUGE,
8277
valueType: ValueType.DOUBLE,
8378
dataPointType: DataPointType.GAUGE,
8479
};
8580

8681
case oc.MetricDescriptorType.CUMULATIVE_INT64:
8782
return {
88-
type: InstrumentType.COUNTER,
8983
valueType: ValueType.INT,
9084
dataPointType: DataPointType.SUM,
9185
};
9286
case oc.MetricDescriptorType.CUMULATIVE_DOUBLE:
9387
return {
94-
type: InstrumentType.COUNTER,
9588
valueType: ValueType.DOUBLE,
9689
dataPointType: DataPointType.SUM,
9790
};
9891

9992
case oc.MetricDescriptorType.CUMULATIVE_DISTRIBUTION:
10093
return {
101-
type: InstrumentType.HISTOGRAM,
10294
valueType: ValueType.DOUBLE,
10395
dataPointType: DataPointType.HISTOGRAM,
10496
};

experimental/packages/shim-opencensus/test/OpenCensusMetricProducer.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ describe('OpenCensusMetricProducer', () => {
100100
assert.deepStrictEqual(ocMetric.descriptor, {
101101
description: 'Test OC description',
102102
name: 'measure',
103-
type: 'COUNTER',
104103
unit: 'ms',
105104
valueType: ValueType.DOUBLE,
106105
});

experimental/packages/shim-opencensus/test/metric-transform.test.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import {
2323
DataPointType,
2424
GaugeMetricData,
2525
HistogramMetricData,
26-
InstrumentType,
2726
SumMetricData,
2827
} from '@opentelemetry/sdk-metrics';
2928
import * as assert from 'assert';
@@ -64,7 +63,6 @@ describe('metric-transform', () => {
6463
descriptor: {
6564
description: 'ocDescription',
6665
name: 'ocMetricName',
67-
type: InstrumentType.COUNTER,
6866
unit: 'ocUnit',
6967
valueType: ValueType.INT,
7068
},
@@ -107,7 +105,6 @@ describe('metric-transform', () => {
107105
descriptor: {
108106
description: 'ocDescription',
109107
name: 'ocMetricName',
110-
type: InstrumentType.COUNTER,
111108
unit: 'ocUnit',
112109
valueType: ValueType.DOUBLE,
113110
},
@@ -177,7 +174,6 @@ describe('metric-transform', () => {
177174
descriptor: {
178175
description: 'ocDescription',
179176
name: 'ocMetricName',
180-
type: InstrumentType.HISTOGRAM,
181177
unit: 'ocUnit',
182178
valueType: ValueType.DOUBLE,
183179
},
@@ -219,7 +215,6 @@ describe('metric-transform', () => {
219215
descriptor: {
220216
description: 'ocDescription',
221217
name: 'ocMetricName',
222-
type: InstrumentType.OBSERVABLE_GAUGE,
223218
unit: 'ocUnit',
224219
valueType: ValueType.INT,
225220
},
@@ -261,7 +256,6 @@ describe('metric-transform', () => {
261256
descriptor: {
262257
description: 'ocDescription',
263258
name: 'ocMetricName',
264-
type: InstrumentType.OBSERVABLE_GAUGE,
265259
unit: 'ocUnit',
266260
valueType: ValueType.DOUBLE,
267261
},

packages/sdk-metrics/src/InstrumentDescriptor.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ import { InstrumentType, MetricDescriptor } from './export/MetricData';
3131
* which may not contains internal fields like metric advice.
3232
*/
3333
export interface InstrumentDescriptor extends MetricDescriptor {
34+
/**
35+
* For internal use; exporter should avoid depending on the type of the
36+
* instrument as their resulting aggregator can be re-mapped with views.
37+
*/
38+
readonly type: InstrumentType;
39+
3440
/**
3541
* See {@link MetricAdvice}
3642
*

packages/sdk-metrics/src/aggregator/ExponentialHistogram.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ import {
2525
DataPointType,
2626
ExponentialHistogramMetricData,
2727
InstrumentType,
28-
MetricDescriptor,
2928
} from '../export/MetricData';
3029
import { diag, HrTime } from '@opentelemetry/api';
3130
import { Maybe } from '../utils';
3231
import { AggregationTemporality } from '../export/AggregationTemporality';
32+
import { InstrumentDescriptor } from '../InstrumentDescriptor';
3333
import { Buckets } from './exponential-histogram/Buckets';
3434
import { getMapping } from './exponential-histogram/mapping/getMapping';
3535
import { Mapping } from './exponential-histogram/mapping/types';
@@ -566,7 +566,7 @@ export class ExponentialHistogramAggregator
566566
}
567567

568568
toMetricData(
569-
descriptor: MetricDescriptor,
569+
descriptor: InstrumentDescriptor,
570570
aggregationTemporality: AggregationTemporality,
571571
accumulationByAttributes: AccumulationRecord<ExponentialHistogramAccumulation>[],
572572
endTime: HrTime

packages/sdk-metrics/src/aggregator/Histogram.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ import {
2424
DataPointType,
2525
HistogramMetricData,
2626
InstrumentType,
27-
MetricDescriptor,
2827
} from '../export/MetricData';
2928
import { HrTime } from '@opentelemetry/api';
3029
import { binarySearchUB, Maybe } from '../utils';
3130
import { AggregationTemporality } from '../export/AggregationTemporality';
31+
import { InstrumentDescriptor } from '../InstrumentDescriptor';
3232

3333
/**
3434
* Internal value type for HistogramAggregation.
@@ -217,7 +217,7 @@ export class HistogramAggregator implements Aggregator<HistogramAccumulation> {
217217
}
218218

219219
toMetricData(
220-
descriptor: MetricDescriptor,
220+
descriptor: InstrumentDescriptor,
221221
aggregationTemporality: AggregationTemporality,
222222
accumulationByAttributes: AccumulationRecord<HistogramAccumulation>[],
223223
endTime: HrTime

0 commit comments

Comments
 (0)