Skip to content

Commit fe6856e

Browse files
authored
Deprecate metric/unit and use a string for units in the metric API/SDK (#3776)
* Replace Unit from metric/unit with string Deprecate the units package. This package will not be included in the metric GA. * Add changes to changelog
1 parent 17e5d0f commit fe6856e

31 files changed

+148
-212
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
4141

4242
- [bridge/ot] Fall-back to TextMap carrier when it's not ot.HttpHeaders. (#3679)
4343
- The `Collect` method of the `"go.opentelemetry.io/otel/sdk/metric".Reader` interface is updated to accept the `metricdata.ResourceMetrics` value the collection will be made into. This change is made to enable memory reuse by SDK users. (#3732)
44+
- The `WithUnit` option in `go.opentelemetry.io/otel/sdk/metric/instrument` is updated to accept a `string` for the unit value. (#3776)
4445

4546
### Fixed
4647

@@ -51,6 +52,14 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
5152
- Data race issue in OTLP exporter retry mechanism. (#3756)
5253
- Fixes wrapping a nil error in some cases (#????)
5354

55+
### Deprecated
56+
57+
- The `go.opentelemetry.io/otel/metric/unit` package is deprecated.
58+
Use the equivalent unit string instead. (#3776)
59+
- Use `"1"` instead of `unit.Dimensionless`
60+
- Use `"By"` instead of `unit.Bytes`
61+
- Use `"ms"` instead of `unit.Milliseconds`
62+
5463
## [1.13.0/0.36.0] 2023-02-07
5564

5665
### Added

bridge/opencensus/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ require (
66
github.com/stretchr/testify v1.8.2
77
go.opencensus.io v0.24.0
88
go.opentelemetry.io/otel v1.13.0
9-
go.opentelemetry.io/otel/metric v0.36.0
109
go.opentelemetry.io/otel/sdk v1.13.0
1110
go.opentelemetry.io/otel/sdk/metric v0.36.0
1211
go.opentelemetry.io/otel/trace v1.13.0
@@ -19,6 +18,7 @@ require (
1918
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
2019
github.com/kr/pretty v0.1.0 // indirect
2120
github.com/pmezard/go-difflib v1.0.0 // indirect
21+
go.opentelemetry.io/otel/metric v0.36.0 // indirect
2222
golang.org/x/sys v0.5.0 // indirect
2323
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
2424
gopkg.in/yaml.v3 v3.0.1 // indirect

bridge/opencensus/internal/ocmetric/metric.go

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
ocmetricdata "go.opencensus.io/metric/metricdata"
2222

2323
"go.opentelemetry.io/otel/attribute"
24-
"go.opentelemetry.io/otel/metric/unit"
2524
"go.opentelemetry.io/otel/sdk/metric/metricdata"
2625
)
2726

@@ -52,7 +51,7 @@ func ConvertMetrics(ocmetrics []*ocmetricdata.Metric) ([]metricdata.Metrics, err
5251
otelMetrics = append(otelMetrics, metricdata.Metrics{
5352
Name: ocm.Descriptor.Name,
5453
Description: ocm.Descriptor.Description,
55-
Unit: convertUnit(ocm.Descriptor.Unit),
54+
Unit: string(ocm.Descriptor.Unit),
5655
Data: agg,
5756
})
5857
}
@@ -201,16 +200,3 @@ func convertAttrs(keys []ocmetricdata.LabelKey, values []ocmetricdata.LabelValue
201200
}
202201
return attribute.NewSet(attrs...), nil
203202
}
204-
205-
// convertUnit converts from the OpenCensus unit to OpenTelemetry unit.
206-
func convertUnit(u ocmetricdata.Unit) unit.Unit {
207-
switch u {
208-
case ocmetricdata.UnitDimensionless:
209-
return unit.Dimensionless
210-
case ocmetricdata.UnitBytes:
211-
return unit.Bytes
212-
case ocmetricdata.UnitMilliseconds:
213-
return unit.Milliseconds
214-
}
215-
return unit.Unit(string(u))
216-
}

bridge/opencensus/internal/ocmetric/metric_test.go

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
ocmetricdata "go.opencensus.io/metric/metricdata"
2323

2424
"go.opentelemetry.io/otel/attribute"
25-
"go.opentelemetry.io/otel/metric/unit"
2625
"go.opentelemetry.io/otel/sdk/metric/metricdata"
2726
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest"
2827
)
@@ -214,7 +213,7 @@ func TestConvertMetrics(t *testing.T) {
214213
{
215214
Name: "foo.com/histogram-a",
216215
Description: "a testing histogram",
217-
Unit: unit.Dimensionless,
216+
Unit: "1",
218217
Data: metricdata.Histogram{
219218
DataPoints: []metricdata.HistogramDataPoint{
220219
{
@@ -252,7 +251,7 @@ func TestConvertMetrics(t *testing.T) {
252251
}, {
253252
Name: "foo.com/gauge-a",
254253
Description: "an int testing gauge",
255-
Unit: unit.Bytes,
254+
Unit: "By",
256255
Data: metricdata.Gauge[int64]{
257256
DataPoints: []metricdata.DataPoint[int64]{
258257
{
@@ -281,7 +280,7 @@ func TestConvertMetrics(t *testing.T) {
281280
}, {
282281
Name: "foo.com/gauge-b",
283282
Description: "a float testing gauge",
284-
Unit: unit.Bytes,
283+
Unit: "By",
285284
Data: metricdata.Gauge[float64]{
286285
DataPoints: []metricdata.DataPoint[float64]{
287286
{
@@ -310,7 +309,7 @@ func TestConvertMetrics(t *testing.T) {
310309
}, {
311310
Name: "foo.com/sum-a",
312311
Description: "an int testing sum",
313-
Unit: unit.Milliseconds,
312+
Unit: "ms",
314313
Data: metricdata.Sum[int64]{
315314
IsMonotonic: true,
316315
Temporality: metricdata.CumulativeTemporality,
@@ -341,7 +340,7 @@ func TestConvertMetrics(t *testing.T) {
341340
}, {
342341
Name: "foo.com/sum-b",
343342
Description: "a float testing sum",
344-
Unit: unit.Milliseconds,
343+
Unit: "ms",
345344
Data: metricdata.Sum[float64]{
346345
IsMonotonic: true,
347346
Temporality: metricdata.CumulativeTemporality,
@@ -387,7 +386,7 @@ func TestConvertMetrics(t *testing.T) {
387386
{
388387
Name: "foo.com/histogram-a",
389388
Description: "a testing histogram",
390-
Unit: unit.Dimensionless,
389+
Unit: "1",
391390
Data: metricdata.Histogram{
392391
Temporality: metricdata.CumulativeTemporality,
393392
DataPoints: []metricdata.HistogramDataPoint{},
@@ -410,7 +409,7 @@ func TestConvertMetrics(t *testing.T) {
410409
{
411410
Name: "foo.com/sum-a",
412411
Description: "a testing sum",
413-
Unit: unit.Dimensionless,
412+
Unit: "1",
414413
Data: metricdata.Sum[float64]{
415414
IsMonotonic: true,
416415
Temporality: metricdata.CumulativeTemporality,
@@ -434,7 +433,7 @@ func TestConvertMetrics(t *testing.T) {
434433
{
435434
Name: "foo.com/gauge-a",
436435
Description: "a testing gauge",
437-
Unit: unit.Dimensionless,
436+
Unit: "1",
438437
Data: metricdata.Gauge[int64]{
439438
DataPoints: []metricdata.DataPoint[int64]{},
440439
},
@@ -580,38 +579,6 @@ func TestConvertMetrics(t *testing.T) {
580579
}
581580
}
582581

583-
func TestConvertUnits(t *testing.T) {
584-
var noUnit unit.Unit
585-
for _, tc := range []struct {
586-
desc string
587-
input ocmetricdata.Unit
588-
expected unit.Unit
589-
}{{
590-
desc: "unspecified unit",
591-
expected: noUnit,
592-
}, {
593-
desc: "dimensionless",
594-
input: ocmetricdata.UnitDimensionless,
595-
expected: unit.Dimensionless,
596-
}, {
597-
desc: "milliseconds",
598-
input: ocmetricdata.UnitMilliseconds,
599-
expected: unit.Milliseconds,
600-
}, {
601-
desc: "bytes",
602-
input: ocmetricdata.UnitBytes,
603-
expected: unit.Bytes,
604-
},
605-
} {
606-
t.Run(tc.desc, func(t *testing.T) {
607-
output := convertUnit(tc.input)
608-
if output != tc.expected {
609-
t.Errorf("convertUnit(%v) = %q, want %q", tc.input, output, tc.expected)
610-
}
611-
})
612-
}
613-
}
614-
615582
func TestConvertAttributes(t *testing.T) {
616583
setWithMultipleKeys := attribute.NewSet(
617584
attribute.KeyValue{Key: attribute.Key("first"), Value: attribute.StringValue("1")},

exporters/otlp/otlpmetric/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ require (
77
github.com/stretchr/testify v1.8.2
88
go.opentelemetry.io/otel v1.13.0
99
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.13.0
10-
go.opentelemetry.io/otel/metric v0.36.0
1110
go.opentelemetry.io/otel/sdk v1.13.0
1211
go.opentelemetry.io/otel/sdk/metric v0.36.0
1312
go.opentelemetry.io/proto/otlp v0.19.0
@@ -23,6 +22,7 @@ require (
2322
github.com/golang/protobuf v1.5.2 // indirect
2423
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
2524
github.com/pmezard/go-difflib v1.0.0 // indirect
25+
go.opentelemetry.io/otel/metric v0.36.0 // indirect
2626
go.opentelemetry.io/otel/trace v1.13.0 // indirect
2727
golang.org/x/net v0.7.0 // indirect
2828
golang.org/x/sys v0.5.0 // indirect

exporters/otlp/otlpmetric/internal/otest/client.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727

2828
"go.opentelemetry.io/otel"
2929
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/internal"
30-
"go.opentelemetry.io/otel/metric/unit"
3130
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
3231
collpb "go.opentelemetry.io/proto/otlp/collector/metrics/v1"
3332
cpb "go.opentelemetry.io/proto/otlp/common/v1"
@@ -118,31 +117,31 @@ var (
118117
{
119118
Name: "int64-gauge",
120119
Description: "Gauge with int64 values",
121-
Unit: string(unit.Dimensionless),
120+
Unit: "1",
122121
Data: &mpb.Metric_Gauge{Gauge: gaugeInt64},
123122
},
124123
{
125124
Name: "float64-gauge",
126125
Description: "Gauge with float64 values",
127-
Unit: string(unit.Dimensionless),
126+
Unit: "1",
128127
Data: &mpb.Metric_Gauge{Gauge: gaugeFloat64},
129128
},
130129
{
131130
Name: "int64-sum",
132131
Description: "Sum with int64 values",
133-
Unit: string(unit.Dimensionless),
132+
Unit: "1",
134133
Data: &mpb.Metric_Sum{Sum: sumInt64},
135134
},
136135
{
137136
Name: "float64-sum",
138137
Description: "Sum with float64 values",
139-
Unit: string(unit.Dimensionless),
138+
Unit: "1",
140139
Data: &mpb.Metric_Sum{Sum: sumFloat64},
141140
},
142141
{
143142
Name: "histogram",
144143
Description: "Histogram",
145-
Unit: string(unit.Dimensionless),
144+
Unit: "1",
146145
Data: &mpb.Metric_Histogram{Histogram: hist},
147146
},
148147
}

exporters/otlp/otlpmetric/internal/transform/metricdata_test.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"github.com/stretchr/testify/require"
2323

2424
"go.opentelemetry.io/otel/attribute"
25-
"go.opentelemetry.io/otel/metric/unit"
2625
"go.opentelemetry.io/otel/sdk/instrumentation"
2726
"go.opentelemetry.io/otel/sdk/metric/metricdata"
2827
"go.opentelemetry.io/otel/sdk/resource"
@@ -188,49 +187,49 @@ var (
188187
{
189188
Name: "int64-gauge",
190189
Description: "Gauge with int64 values",
191-
Unit: unit.Dimensionless,
190+
Unit: "1",
192191
Data: otelGaugeInt64,
193192
},
194193
{
195194
Name: "float64-gauge",
196195
Description: "Gauge with float64 values",
197-
Unit: unit.Dimensionless,
196+
Unit: "1",
198197
Data: otelGaugeFloat64,
199198
},
200199
{
201200
Name: "int64-sum",
202201
Description: "Sum with int64 values",
203-
Unit: unit.Dimensionless,
202+
Unit: "1",
204203
Data: otelSumInt64,
205204
},
206205
{
207206
Name: "float64-sum",
208207
Description: "Sum with float64 values",
209-
Unit: unit.Dimensionless,
208+
Unit: "1",
210209
Data: otelSumFloat64,
211210
},
212211
{
213212
Name: "invalid-sum",
214213
Description: "Sum with invalid temporality",
215-
Unit: unit.Dimensionless,
214+
Unit: "1",
216215
Data: otelSumInvalid,
217216
},
218217
{
219218
Name: "histogram",
220219
Description: "Histogram",
221-
Unit: unit.Dimensionless,
220+
Unit: "1",
222221
Data: otelHist,
223222
},
224223
{
225224
Name: "invalid-histogram",
226225
Description: "Invalid histogram",
227-
Unit: unit.Dimensionless,
226+
Unit: "1",
228227
Data: otelHistInvalid,
229228
},
230229
{
231230
Name: "unknown",
232231
Description: "Unknown aggregation",
233-
Unit: unit.Dimensionless,
232+
Unit: "1",
234233
Data: unknownAgg,
235234
},
236235
}
@@ -239,31 +238,31 @@ var (
239238
{
240239
Name: "int64-gauge",
241240
Description: "Gauge with int64 values",
242-
Unit: string(unit.Dimensionless),
241+
Unit: "1",
243242
Data: &mpb.Metric_Gauge{Gauge: pbGaugeInt64},
244243
},
245244
{
246245
Name: "float64-gauge",
247246
Description: "Gauge with float64 values",
248-
Unit: string(unit.Dimensionless),
247+
Unit: "1",
249248
Data: &mpb.Metric_Gauge{Gauge: pbGaugeFloat64},
250249
},
251250
{
252251
Name: "int64-sum",
253252
Description: "Sum with int64 values",
254-
Unit: string(unit.Dimensionless),
253+
Unit: "1",
255254
Data: &mpb.Metric_Sum{Sum: pbSumInt64},
256255
},
257256
{
258257
Name: "float64-sum",
259258
Description: "Sum with float64 values",
260-
Unit: string(unit.Dimensionless),
259+
Unit: "1",
261260
Data: &mpb.Metric_Sum{Sum: pbSumFloat64},
262261
},
263262
{
264263
Name: "histogram",
265264
Description: "Histogram",
266-
Unit: string(unit.Dimensionless),
265+
Unit: "1",
267266
Data: &mpb.Metric_Histogram{Histogram: pbHist},
268267
},
269268
}

exporters/prometheus/exporter.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"go.opentelemetry.io/otel"
3232
"go.opentelemetry.io/otel/attribute"
3333
"go.opentelemetry.io/otel/internal/global"
34-
"go.opentelemetry.io/otel/metric/unit"
3534
"go.opentelemetry.io/otel/sdk/instrumentation"
3635
"go.opentelemetry.io/otel/sdk/metric"
3736
"go.opentelemetry.io/otel/sdk/metric/metricdata"
@@ -309,10 +308,10 @@ func sanitizeRune(r rune) rune {
309308
return '_'
310309
}
311310

312-
var unitSuffixes = map[unit.Unit]string{
313-
unit.Dimensionless: "_ratio",
314-
unit.Bytes: "_bytes",
315-
unit.Milliseconds: "_milliseconds",
311+
var unitSuffixes = map[string]string{
312+
"1": "_ratio",
313+
"By": "_bytes",
314+
"ms": "_milliseconds",
316315
}
317316

318317
// getName returns the sanitized name, including unit suffix.

0 commit comments

Comments
 (0)