Skip to content

Commit

Permalink
Deprecate metric/unit and use a string for units in the metric AP…
Browse files Browse the repository at this point in the history
…I/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
  • Loading branch information
MrAlias authored Feb 27, 2023
1 parent 17e5d0f commit fe6856e
Show file tree
Hide file tree
Showing 31 changed files with 148 additions and 212 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- [bridge/ot] Fall-back to TextMap carrier when it's not ot.HttpHeaders. (#3679)
- 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)
- The `WithUnit` option in `go.opentelemetry.io/otel/sdk/metric/instrument` is updated to accept a `string` for the unit value. (#3776)

### Fixed

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

### Deprecated

- The `go.opentelemetry.io/otel/metric/unit` package is deprecated.
Use the equivalent unit string instead. (#3776)
- Use `"1"` instead of `unit.Dimensionless`
- Use `"By"` instead of `unit.Bytes`
- Use `"ms"` instead of `unit.Milliseconds`

## [1.13.0/0.36.0] 2023-02-07

### Added
Expand Down
2 changes: 1 addition & 1 deletion bridge/opencensus/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
github.com/stretchr/testify v1.8.2
go.opencensus.io v0.24.0
go.opentelemetry.io/otel v1.13.0
go.opentelemetry.io/otel/metric v0.36.0
go.opentelemetry.io/otel/sdk v1.13.0
go.opentelemetry.io/otel/sdk/metric v0.36.0
go.opentelemetry.io/otel/trace v1.13.0
Expand All @@ -19,6 +18,7 @@ require (
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/otel/metric v0.36.0 // indirect
golang.org/x/sys v0.5.0 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
16 changes: 1 addition & 15 deletions bridge/opencensus/internal/ocmetric/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
ocmetricdata "go.opencensus.io/metric/metricdata"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric/unit"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
)

Expand Down Expand Up @@ -52,7 +51,7 @@ func ConvertMetrics(ocmetrics []*ocmetricdata.Metric) ([]metricdata.Metrics, err
otelMetrics = append(otelMetrics, metricdata.Metrics{
Name: ocm.Descriptor.Name,
Description: ocm.Descriptor.Description,
Unit: convertUnit(ocm.Descriptor.Unit),
Unit: string(ocm.Descriptor.Unit),
Data: agg,
})
}
Expand Down Expand Up @@ -201,16 +200,3 @@ func convertAttrs(keys []ocmetricdata.LabelKey, values []ocmetricdata.LabelValue
}
return attribute.NewSet(attrs...), nil
}

// convertUnit converts from the OpenCensus unit to OpenTelemetry unit.
func convertUnit(u ocmetricdata.Unit) unit.Unit {
switch u {
case ocmetricdata.UnitDimensionless:
return unit.Dimensionless
case ocmetricdata.UnitBytes:
return unit.Bytes
case ocmetricdata.UnitMilliseconds:
return unit.Milliseconds
}
return unit.Unit(string(u))
}
49 changes: 8 additions & 41 deletions bridge/opencensus/internal/ocmetric/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
ocmetricdata "go.opencensus.io/metric/metricdata"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric/unit"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest"
)
Expand Down Expand Up @@ -214,7 +213,7 @@ func TestConvertMetrics(t *testing.T) {
{
Name: "foo.com/histogram-a",
Description: "a testing histogram",
Unit: unit.Dimensionless,
Unit: "1",
Data: metricdata.Histogram{
DataPoints: []metricdata.HistogramDataPoint{
{
Expand Down Expand Up @@ -252,7 +251,7 @@ func TestConvertMetrics(t *testing.T) {
}, {
Name: "foo.com/gauge-a",
Description: "an int testing gauge",
Unit: unit.Bytes,
Unit: "By",
Data: metricdata.Gauge[int64]{
DataPoints: []metricdata.DataPoint[int64]{
{
Expand Down Expand Up @@ -281,7 +280,7 @@ func TestConvertMetrics(t *testing.T) {
}, {
Name: "foo.com/gauge-b",
Description: "a float testing gauge",
Unit: unit.Bytes,
Unit: "By",
Data: metricdata.Gauge[float64]{
DataPoints: []metricdata.DataPoint[float64]{
{
Expand Down Expand Up @@ -310,7 +309,7 @@ func TestConvertMetrics(t *testing.T) {
}, {
Name: "foo.com/sum-a",
Description: "an int testing sum",
Unit: unit.Milliseconds,
Unit: "ms",
Data: metricdata.Sum[int64]{
IsMonotonic: true,
Temporality: metricdata.CumulativeTemporality,
Expand Down Expand Up @@ -341,7 +340,7 @@ func TestConvertMetrics(t *testing.T) {
}, {
Name: "foo.com/sum-b",
Description: "a float testing sum",
Unit: unit.Milliseconds,
Unit: "ms",
Data: metricdata.Sum[float64]{
IsMonotonic: true,
Temporality: metricdata.CumulativeTemporality,
Expand Down Expand Up @@ -387,7 +386,7 @@ func TestConvertMetrics(t *testing.T) {
{
Name: "foo.com/histogram-a",
Description: "a testing histogram",
Unit: unit.Dimensionless,
Unit: "1",
Data: metricdata.Histogram{
Temporality: metricdata.CumulativeTemporality,
DataPoints: []metricdata.HistogramDataPoint{},
Expand All @@ -410,7 +409,7 @@ func TestConvertMetrics(t *testing.T) {
{
Name: "foo.com/sum-a",
Description: "a testing sum",
Unit: unit.Dimensionless,
Unit: "1",
Data: metricdata.Sum[float64]{
IsMonotonic: true,
Temporality: metricdata.CumulativeTemporality,
Expand All @@ -434,7 +433,7 @@ func TestConvertMetrics(t *testing.T) {
{
Name: "foo.com/gauge-a",
Description: "a testing gauge",
Unit: unit.Dimensionless,
Unit: "1",
Data: metricdata.Gauge[int64]{
DataPoints: []metricdata.DataPoint[int64]{},
},
Expand Down Expand Up @@ -580,38 +579,6 @@ func TestConvertMetrics(t *testing.T) {
}
}

func TestConvertUnits(t *testing.T) {
var noUnit unit.Unit
for _, tc := range []struct {
desc string
input ocmetricdata.Unit
expected unit.Unit
}{{
desc: "unspecified unit",
expected: noUnit,
}, {
desc: "dimensionless",
input: ocmetricdata.UnitDimensionless,
expected: unit.Dimensionless,
}, {
desc: "milliseconds",
input: ocmetricdata.UnitMilliseconds,
expected: unit.Milliseconds,
}, {
desc: "bytes",
input: ocmetricdata.UnitBytes,
expected: unit.Bytes,
},
} {
t.Run(tc.desc, func(t *testing.T) {
output := convertUnit(tc.input)
if output != tc.expected {
t.Errorf("convertUnit(%v) = %q, want %q", tc.input, output, tc.expected)
}
})
}
}

func TestConvertAttributes(t *testing.T) {
setWithMultipleKeys := attribute.NewSet(
attribute.KeyValue{Key: attribute.Key("first"), Value: attribute.StringValue("1")},
Expand Down
2 changes: 1 addition & 1 deletion exporters/otlp/otlpmetric/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ require (
github.com/stretchr/testify v1.8.2
go.opentelemetry.io/otel v1.13.0
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.13.0
go.opentelemetry.io/otel/metric v0.36.0
go.opentelemetry.io/otel/sdk v1.13.0
go.opentelemetry.io/otel/sdk/metric v0.36.0
go.opentelemetry.io/proto/otlp v0.19.0
Expand All @@ -23,6 +22,7 @@ require (
github.com/golang/protobuf v1.5.2 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/otel/metric v0.36.0 // indirect
go.opentelemetry.io/otel/trace v1.13.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.5.0 // indirect
Expand Down
11 changes: 5 additions & 6 deletions exporters/otlp/otlpmetric/internal/otest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/internal"
"go.opentelemetry.io/otel/metric/unit"
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
collpb "go.opentelemetry.io/proto/otlp/collector/metrics/v1"
cpb "go.opentelemetry.io/proto/otlp/common/v1"
Expand Down Expand Up @@ -118,31 +117,31 @@ var (
{
Name: "int64-gauge",
Description: "Gauge with int64 values",
Unit: string(unit.Dimensionless),
Unit: "1",
Data: &mpb.Metric_Gauge{Gauge: gaugeInt64},
},
{
Name: "float64-gauge",
Description: "Gauge with float64 values",
Unit: string(unit.Dimensionless),
Unit: "1",
Data: &mpb.Metric_Gauge{Gauge: gaugeFloat64},
},
{
Name: "int64-sum",
Description: "Sum with int64 values",
Unit: string(unit.Dimensionless),
Unit: "1",
Data: &mpb.Metric_Sum{Sum: sumInt64},
},
{
Name: "float64-sum",
Description: "Sum with float64 values",
Unit: string(unit.Dimensionless),
Unit: "1",
Data: &mpb.Metric_Sum{Sum: sumFloat64},
},
{
Name: "histogram",
Description: "Histogram",
Unit: string(unit.Dimensionless),
Unit: "1",
Data: &mpb.Metric_Histogram{Histogram: hist},
},
}
Expand Down
27 changes: 13 additions & 14 deletions exporters/otlp/otlpmetric/internal/transform/metricdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/stretchr/testify/require"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric/unit"
"go.opentelemetry.io/otel/sdk/instrumentation"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
"go.opentelemetry.io/otel/sdk/resource"
Expand Down Expand Up @@ -188,49 +187,49 @@ var (
{
Name: "int64-gauge",
Description: "Gauge with int64 values",
Unit: unit.Dimensionless,
Unit: "1",
Data: otelGaugeInt64,
},
{
Name: "float64-gauge",
Description: "Gauge with float64 values",
Unit: unit.Dimensionless,
Unit: "1",
Data: otelGaugeFloat64,
},
{
Name: "int64-sum",
Description: "Sum with int64 values",
Unit: unit.Dimensionless,
Unit: "1",
Data: otelSumInt64,
},
{
Name: "float64-sum",
Description: "Sum with float64 values",
Unit: unit.Dimensionless,
Unit: "1",
Data: otelSumFloat64,
},
{
Name: "invalid-sum",
Description: "Sum with invalid temporality",
Unit: unit.Dimensionless,
Unit: "1",
Data: otelSumInvalid,
},
{
Name: "histogram",
Description: "Histogram",
Unit: unit.Dimensionless,
Unit: "1",
Data: otelHist,
},
{
Name: "invalid-histogram",
Description: "Invalid histogram",
Unit: unit.Dimensionless,
Unit: "1",
Data: otelHistInvalid,
},
{
Name: "unknown",
Description: "Unknown aggregation",
Unit: unit.Dimensionless,
Unit: "1",
Data: unknownAgg,
},
}
Expand All @@ -239,31 +238,31 @@ var (
{
Name: "int64-gauge",
Description: "Gauge with int64 values",
Unit: string(unit.Dimensionless),
Unit: "1",
Data: &mpb.Metric_Gauge{Gauge: pbGaugeInt64},
},
{
Name: "float64-gauge",
Description: "Gauge with float64 values",
Unit: string(unit.Dimensionless),
Unit: "1",
Data: &mpb.Metric_Gauge{Gauge: pbGaugeFloat64},
},
{
Name: "int64-sum",
Description: "Sum with int64 values",
Unit: string(unit.Dimensionless),
Unit: "1",
Data: &mpb.Metric_Sum{Sum: pbSumInt64},
},
{
Name: "float64-sum",
Description: "Sum with float64 values",
Unit: string(unit.Dimensionless),
Unit: "1",
Data: &mpb.Metric_Sum{Sum: pbSumFloat64},
},
{
Name: "histogram",
Description: "Histogram",
Unit: string(unit.Dimensionless),
Unit: "1",
Data: &mpb.Metric_Histogram{Histogram: pbHist},
},
}
Expand Down
9 changes: 4 additions & 5 deletions exporters/prometheus/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/internal/global"
"go.opentelemetry.io/otel/metric/unit"
"go.opentelemetry.io/otel/sdk/instrumentation"
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
Expand Down Expand Up @@ -309,10 +308,10 @@ func sanitizeRune(r rune) rune {
return '_'
}

var unitSuffixes = map[unit.Unit]string{
unit.Dimensionless: "_ratio",
unit.Bytes: "_bytes",
unit.Milliseconds: "_milliseconds",
var unitSuffixes = map[string]string{
"1": "_ratio",
"By": "_bytes",
"ms": "_milliseconds",
}

// getName returns the sanitized name, including unit suffix.
Expand Down
Loading

0 comments on commit fe6856e

Please sign in to comment.