Skip to content

Commit 7196b7f

Browse files
andyflemingdyladan
andauthored
fix(metrics): updates unit option behavior to be spec-compliant (#2983)
Co-authored-by: Daniel Dyla <[email protected]>
1 parent 7149f6c commit 7196b7f

File tree

7 files changed

+48
-16
lines changed

7 files changed

+48
-16
lines changed

experimental/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ All notable changes to experimental packages in this project will be documented
2727
* fix(otlp-exporter-base): include esm and esnext in package files #2952 @dyladan
2828
* fix(otlp-http-exporter): update endpoint to match spec #2895 @svetlanabrennan
2929
* fix(otlp-transformer): include esm and esnext in package files and update README #2992 @pichlermarc
30+
* fix(metrics): specification compliant default metric unit #2983 @andyfleming
3031

3132
### :books: (Refine Doc)
3233

experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/metricsHelper.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export function ensureExportedCounterIsCorrect(
131131
assert.deepStrictEqual(metric, {
132132
name: 'int-counter',
133133
description: 'sample counter description',
134-
unit: '1',
134+
unit: '',
135135
data: 'sum',
136136
sum: {
137137
dataPoints: [
@@ -159,7 +159,7 @@ export function ensureExportedObservableGaugeIsCorrect(
159159
assert.deepStrictEqual(metric, {
160160
name: 'double-observable-gauge',
161161
description: 'sample observable gauge description',
162-
unit: '1',
162+
unit: '',
163163
data: 'gauge',
164164
gauge: {
165165
dataPoints: [
@@ -187,7 +187,7 @@ export function ensureExportedHistogramIsCorrect(
187187
assert.deepStrictEqual(metric, {
188188
name: 'int-histogram',
189189
description: 'sample histogram description',
190-
unit: '1',
190+
unit: '',
191191
data: 'histogram',
192192
histogram: {
193193
dataPoints: [

experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/metricsHelper.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ export function ensureCounterIsCorrect(
237237
assert.deepStrictEqual(metric, {
238238
name: 'int-counter',
239239
description: 'sample counter description',
240-
unit: '1',
240+
unit: '',
241241
sum: {
242242
dataPoints: [
243243
{
@@ -261,7 +261,7 @@ export function ensureDoubleCounterIsCorrect(
261261
assert.deepStrictEqual(metric, {
262262
name: 'double-counter',
263263
description: 'sample counter description',
264-
unit: '1',
264+
unit: '',
265265
doubleSum: {
266266
dataPoints: [
267267
{
@@ -287,7 +287,7 @@ export function ensureObservableGaugeIsCorrect(
287287
assert.deepStrictEqual(metric, {
288288
name,
289289
description: 'sample observable gauge description',
290-
unit: '1',
290+
unit: '',
291291
gauge: {
292292
dataPoints: [
293293
{
@@ -311,7 +311,7 @@ export function ensureObservableCounterIsCorrect(
311311
assert.deepStrictEqual(metric, {
312312
name,
313313
description: 'sample observable counter description',
314-
unit: '1',
314+
unit: '',
315315
doubleSum: {
316316
isMonotonic: true,
317317
dataPoints: [
@@ -337,7 +337,7 @@ export function ensureObservableUpDownCounterIsCorrect(
337337
assert.deepStrictEqual(metric, {
338338
name,
339339
description: 'sample observable up down counter description',
340-
unit: '1',
340+
unit: '',
341341
doubleSum: {
342342
isMonotonic: false,
343343
dataPoints: [
@@ -363,7 +363,7 @@ export function ensureHistogramIsCorrect(
363363
assert.deepStrictEqual(metric, {
364364
name: 'int-histogram',
365365
description: 'sample histogram description',
366-
unit: '1',
366+
unit: '',
367367
histogram: {
368368
dataPoints: [
369369
{

experimental/packages/opentelemetry-exporter-metrics-otlp-proto/test/metricsHelper.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export function ensureExportedCounterIsCorrect(
135135
assert.deepStrictEqual(metric, {
136136
name: 'int-counter',
137137
description: 'sample counter description',
138-
unit: '1',
138+
unit: '',
139139
sum: {
140140
dataPoints: [
141141
{
@@ -158,7 +158,7 @@ export function ensureExportedObservableGaugeIsCorrect(
158158
assert.deepStrictEqual(metric, {
159159
name: 'double-observable-gauge',
160160
description: 'sample observable gauge description',
161-
unit: '1',
161+
unit: '',
162162
gauge: {
163163
dataPoints: [
164164
{
@@ -181,7 +181,7 @@ export function ensureExportedHistogramIsCorrect(
181181
assert.deepStrictEqual(metric, {
182182
name: 'int-histogram',
183183
description: 'sample histogram description',
184-
unit: '1',
184+
unit: '',
185185
histogram: {
186186
dataPoints: [
187187
{

experimental/packages/opentelemetry-sdk-metrics-base/src/InstrumentDescriptor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export function createInstrumentDescriptor(name: string, type: InstrumentType, o
4242
name,
4343
type,
4444
description: options?.description ?? '',
45-
unit: options?.unit ?? '1',
45+
unit: options?.unit ?? '',
4646
valueType: options?.valueType ?? ValueType.DOUBLE,
4747
};
4848
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import * as assert from 'assert';
18+
import {createInstrumentDescriptor, InstrumentType} from '../src/InstrumentDescriptor';
19+
20+
describe('InstrumentDescriptor', () => {
21+
describe('createInstrumentDescriptor', () => {
22+
for (const val of [null, undefined]) {
23+
it(`should interpret an empty unit value as a blank string (${val})`, () => {
24+
const result = createInstrumentDescriptor('example', InstrumentType.COUNTER, {
25+
unit: val as any,
26+
});
27+
assert.strictEqual(result.unit, '');
28+
});
29+
}
30+
});
31+
});

experimental/packages/opentelemetry-sdk-metrics-base/test/Instruments.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ describe('Instruments', () => {
6464
descriptor: {
6565
name: 'test',
6666
description: '',
67-
unit: '1',
67+
unit: '',
6868
type: InstrumentType.COUNTER,
6969
valueType: ValueType.INT,
7070
},
@@ -180,7 +180,7 @@ describe('Instruments', () => {
180180
descriptor: {
181181
name: 'test',
182182
description: '',
183-
unit: '1',
183+
unit: '',
184184
type: InstrumentType.UP_DOWN_COUNTER,
185185
valueType: ValueType.INT,
186186
},
@@ -265,7 +265,7 @@ describe('Instruments', () => {
265265
descriptor: {
266266
name: 'test',
267267
description: '',
268-
unit: '1',
268+
unit: '',
269269
type: InstrumentType.HISTOGRAM,
270270
valueType: ValueType.INT,
271271
},

0 commit comments

Comments
 (0)