Skip to content

Commit 18f4cb8

Browse files
authored
[API EPIC 4/4] Fix tests and examples (#2587)
* Empty All of the metrics dir * Add instrument api with documentation * Add a NoOp implementation. * Updated to the new config standard * Address PR comments * This change moves components to sdk/metrics The Moved components are: - metric/metrictest - metric/number - metric/internal/registry - metric/sdkapi * The SDK changes necessary to satasify the new api * This fixes the remaing tests. * Update changelog * refactor the Noop meter and instruments into one package. * Renamed pkg.Instruments to pkg.InstrumentProvider Co-authored-by: Aaron Clawson <[email protected]>
1 parent b66c902 commit 18f4cb8

102 files changed

Lines changed: 1722 additions & 4405 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/dependabot.yml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -227,15 +227,6 @@ updates:
227227
schedule:
228228
day: sunday
229229
interval: weekly
230-
- package-ecosystem: gomod
231-
directory: /internal/metric
232-
labels:
233-
- dependencies
234-
- go
235-
- "Skip Changelog"
236-
schedule:
237-
day: sunday
238-
interval: weekly
239230
- package-ecosystem: gomod
240231
directory: /internal/tools
241232
labels:

CHANGELOG.md

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

99
## [Unreleased]
1010

11+
### ⚠️ Notice ⚠️
12+
13+
This update is a breaking change of the unstable Metrics API. Code instrumented with the `go.opentelemetry.io/otel/metric` <= v0.27.0 will need to be modified.
14+
1115
### Added
1216

1317
- Added support to configure the span limits with environment variables.
@@ -24,6 +28,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
2428

2529
- For tracestate's members, prepend the new element and remove the oldest one, which is over capacity (#2592)
2630
- Add event and link drop counts to the exported data from the `oltptrace` exporter. (#2601)
31+
- The metrics API has been significantly changed. (#2587)
2732
- Unify path cleaning functionally in the `otlpmetric` and `otlptrace` config. (#2639)
2833
- Change the debug message from the `sdk/trace.BatchSpanProcessor` to reflect the count is cumulative. (#2640)
2934

bridge/opencensus/aggregation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import (
2121

2222
"go.opencensus.io/metric/metricdata"
2323

24-
"go.opentelemetry.io/otel/metric/number"
2524
"go.opentelemetry.io/otel/sdk/metric/export/aggregation"
25+
"go.opentelemetry.io/otel/sdk/metric/number"
2626
)
2727

2828
var (

bridge/opencensus/exporter.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ import (
2727

2828
"go.opentelemetry.io/otel"
2929
"go.opentelemetry.io/otel/attribute"
30-
"go.opentelemetry.io/otel/metric"
31-
"go.opentelemetry.io/otel/metric/number"
32-
"go.opentelemetry.io/otel/metric/sdkapi"
30+
"go.opentelemetry.io/otel/metric/instrument"
3331
"go.opentelemetry.io/otel/metric/unit"
3432
"go.opentelemetry.io/otel/sdk/instrumentation"
3533
"go.opentelemetry.io/otel/sdk/metric/export"
3634
"go.opentelemetry.io/otel/sdk/metric/export/aggregation"
35+
"go.opentelemetry.io/otel/sdk/metric/number"
36+
"go.opentelemetry.io/otel/sdk/metric/sdkapi"
3737
"go.opentelemetry.io/otel/sdk/resource"
3838
)
3939

@@ -170,17 +170,17 @@ func convertDescriptor(ocDescriptor metricdata.Descriptor) (sdkapi.Descriptor, e
170170
// Includes TypeGaugeDistribution, TypeCumulativeDistribution, TypeSummary
171171
return sdkapi.Descriptor{}, fmt.Errorf("%w; descriptor type: %v", errConversion, ocDescriptor.Type)
172172
}
173-
opts := []metric.InstrumentOption{
174-
metric.WithDescription(ocDescriptor.Description),
173+
opts := []instrument.Option{
174+
instrument.WithDescription(ocDescriptor.Description),
175175
}
176176
switch ocDescriptor.Unit {
177177
case metricdata.UnitDimensionless:
178-
opts = append(opts, metric.WithUnit(unit.Dimensionless))
178+
opts = append(opts, instrument.WithUnit(unit.Dimensionless))
179179
case metricdata.UnitBytes:
180-
opts = append(opts, metric.WithUnit(unit.Bytes))
180+
opts = append(opts, instrument.WithUnit(unit.Bytes))
181181
case metricdata.UnitMilliseconds:
182-
opts = append(opts, metric.WithUnit(unit.Milliseconds))
182+
opts = append(opts, instrument.WithUnit(unit.Milliseconds))
183183
}
184-
cfg := metric.NewInstrumentConfig(opts...)
184+
cfg := instrument.NewConfig(opts...)
185185
return sdkapi.NewDescriptor(ocDescriptor.Name, ikind, nkind, cfg.Description(), cfg.Unit()), nil
186186
}

bridge/opencensus/exporter_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ import (
2626

2727
"go.opentelemetry.io/otel"
2828
"go.opentelemetry.io/otel/attribute"
29-
"go.opentelemetry.io/otel/metric"
30-
"go.opentelemetry.io/otel/metric/metrictest"
31-
"go.opentelemetry.io/otel/metric/number"
32-
"go.opentelemetry.io/otel/metric/sdkapi"
29+
"go.opentelemetry.io/otel/metric/instrument"
3330
"go.opentelemetry.io/otel/metric/unit"
3431
"go.opentelemetry.io/otel/sdk/instrumentation"
3532
"go.opentelemetry.io/otel/sdk/metric/controller/controllertest"
3633
"go.opentelemetry.io/otel/sdk/metric/export"
3734
"go.opentelemetry.io/otel/sdk/metric/export/aggregation"
35+
"go.opentelemetry.io/otel/sdk/metric/metrictest"
36+
"go.opentelemetry.io/otel/sdk/metric/number"
37+
"go.opentelemetry.io/otel/sdk/metric/sdkapi"
3838
"go.opentelemetry.io/otel/sdk/resource"
3939
)
4040

@@ -400,8 +400,8 @@ func TestConvertDescriptor(t *testing.T) {
400400
"foo",
401401
sdkapi.GaugeObserverInstrumentKind,
402402
number.Int64Kind,
403-
metric.WithDescription("bar"),
404-
metric.WithUnit(unit.Bytes),
403+
instrument.WithDescription("bar"),
404+
instrument.WithUnit(unit.Bytes),
405405
),
406406
},
407407
{
@@ -416,8 +416,8 @@ func TestConvertDescriptor(t *testing.T) {
416416
"foo",
417417
sdkapi.GaugeObserverInstrumentKind,
418418
number.Float64Kind,
419-
metric.WithDescription("bar"),
420-
metric.WithUnit(unit.Milliseconds),
419+
instrument.WithDescription("bar"),
420+
instrument.WithUnit(unit.Milliseconds),
421421
),
422422
},
423423
{
@@ -432,8 +432,8 @@ func TestConvertDescriptor(t *testing.T) {
432432
"foo",
433433
sdkapi.CounterObserverInstrumentKind,
434434
number.Int64Kind,
435-
metric.WithDescription("bar"),
436-
metric.WithUnit(unit.Dimensionless),
435+
instrument.WithDescription("bar"),
436+
instrument.WithUnit(unit.Dimensionless),
437437
),
438438
},
439439
{
@@ -448,8 +448,8 @@ func TestConvertDescriptor(t *testing.T) {
448448
"foo",
449449
sdkapi.CounterObserverInstrumentKind,
450450
number.Float64Kind,
451-
metric.WithDescription("bar"),
452-
metric.WithUnit(unit.Dimensionless),
451+
instrument.WithDescription("bar"),
452+
instrument.WithUnit(unit.Dimensionless),
453453
),
454454
},
455455
{

example/prometheus/main.go

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"go.opentelemetry.io/otel/attribute"
2626
"go.opentelemetry.io/otel/exporters/prometheus"
2727
"go.opentelemetry.io/otel/metric"
28-
"go.opentelemetry.io/otel/metric/global"
28+
"go.opentelemetry.io/otel/metric/instrument"
2929
"go.opentelemetry.io/otel/sdk/metric/aggregator/histogram"
3030
controller "go.opentelemetry.io/otel/sdk/metric/controller/basic"
3131
"go.opentelemetry.io/otel/sdk/metric/export/aggregation"
@@ -35,6 +35,9 @@ import (
3535

3636
var (
3737
lemonsKey = attribute.Key("ex.com/lemons")
38+
39+
// TODO Bring back Global package
40+
meterProvider metric.MeterProvider
3841
)
3942

4043
func initMeter() {
@@ -54,7 +57,9 @@ func initMeter() {
5457
if err != nil {
5558
log.Panicf("failed to initialize prometheus exporter %v", err)
5659
}
57-
global.SetMeterProvider(exporter.MeterProvider())
60+
// TODO Bring back Global package
61+
// global.SetMeterProvider(exporter.MeterProvider())
62+
meterProvider = exporter.MeterProvider()
5863

5964
http.HandleFunc("/", exporter.ServeHTTP)
6065
go func() {
@@ -67,23 +72,33 @@ func initMeter() {
6772
func main() {
6873
initMeter()
6974

70-
meter := global.Meter("ex.com/basic")
75+
// TODO Bring back Global package
76+
// meter := global.Meter("ex.com/basic")
77+
meter := meterProvider.Meter("ex.com/basic")
7178
observerLock := new(sync.RWMutex)
7279
observerValueToReport := new(float64)
7380
observerLabelsToReport := new([]attribute.KeyValue)
74-
cb := func(_ context.Context, result metric.Float64ObserverResult) {
81+
82+
gaugeObserver, err := meter.AsyncFloat64().Gauge("ex.com.one")
83+
if err != nil {
84+
log.Panicf("failed to initialize instrument: %v", err)
85+
}
86+
_ = meter.RegisterCallback([]instrument.Asynchronous{gaugeObserver}, func(ctx context.Context) {
7587
(*observerLock).RLock()
7688
value := *observerValueToReport
7789
labels := *observerLabelsToReport
7890
(*observerLock).RUnlock()
79-
result.Observe(value, labels...)
80-
}
81-
_ = metric.Must(meter).NewFloat64GaugeObserver("ex.com.one", cb,
82-
metric.WithDescription("A GaugeObserver set to 1.0"),
83-
)
91+
gaugeObserver.Observe(ctx, value, labels...)
92+
})
8493

85-
histogram := metric.Must(meter).NewFloat64Histogram("ex.com.two")
86-
counter := metric.Must(meter).NewFloat64Counter("ex.com.three")
94+
histogram, err := meter.SyncFloat64().Histogram("ex.com.two")
95+
if err != nil {
96+
log.Panicf("failed to initialize instrument: %v", err)
97+
}
98+
counter, err := meter.SyncFloat64().Counter("ex.com.three")
99+
if err != nil {
100+
log.Panicf("failed to initialize instrument: %v", err)
101+
}
87102

88103
commonLabels := []attribute.KeyValue{lemonsKey.Int(10), attribute.String("A", "1"), attribute.String("B", "2"), attribute.String("C", "3")}
89104
notSoCommonLabels := []attribute.KeyValue{lemonsKey.Int(13)}
@@ -94,38 +109,27 @@ func main() {
94109
*observerValueToReport = 1.0
95110
*observerLabelsToReport = commonLabels
96111
(*observerLock).Unlock()
97-
meter.RecordBatch(
98-
ctx,
99-
commonLabels,
100-
histogram.Measurement(2.0),
101-
counter.Measurement(12.0),
102-
)
112+
113+
histogram.Record(ctx, 2.0, commonLabels...)
114+
counter.Add(ctx, 12.0, commonLabels...)
103115

104116
time.Sleep(5 * time.Second)
105117

106118
(*observerLock).Lock()
107119
*observerValueToReport = 1.0
108120
*observerLabelsToReport = notSoCommonLabels
109121
(*observerLock).Unlock()
110-
meter.RecordBatch(
111-
ctx,
112-
notSoCommonLabels,
113-
histogram.Measurement(2.0),
114-
counter.Measurement(22.0),
115-
)
122+
histogram.Record(ctx, 2.0, notSoCommonLabels...)
123+
counter.Add(ctx, 22.0, notSoCommonLabels...)
116124

117125
time.Sleep(5 * time.Second)
118126

119127
(*observerLock).Lock()
120128
*observerValueToReport = 13.0
121129
*observerLabelsToReport = commonLabels
122130
(*observerLock).Unlock()
123-
meter.RecordBatch(
124-
ctx,
125-
commonLabels,
126-
histogram.Measurement(12.0),
127-
counter.Measurement(13.0),
128-
)
131+
histogram.Record(ctx, 12.0, commonLabels...)
132+
counter.Add(ctx, 13.0, commonLabels...)
129133

130134
fmt.Println("Example finished updating, please visit :2222")
131135

exporters/otlp/otlpmetric/exporter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ import (
2020
"sync"
2121

2222
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/internal/metrictransform"
23-
"go.opentelemetry.io/otel/metric/sdkapi"
2423
"go.opentelemetry.io/otel/sdk/metric/export"
2524
"go.opentelemetry.io/otel/sdk/metric/export/aggregation"
25+
"go.opentelemetry.io/otel/sdk/metric/sdkapi"
2626
"go.opentelemetry.io/otel/sdk/resource"
2727
)
2828

exporters/otlp/otlpmetric/exporter_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@ import (
2929
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric"
3030
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/internal/metrictransform"
3131
"go.opentelemetry.io/otel/metric"
32-
"go.opentelemetry.io/otel/metric/metrictest"
33-
"go.opentelemetry.io/otel/metric/number"
34-
"go.opentelemetry.io/otel/metric/sdkapi"
3532
"go.opentelemetry.io/otel/sdk/instrumentation"
3633
"go.opentelemetry.io/otel/sdk/metric/aggregator"
3734
"go.opentelemetry.io/otel/sdk/metric/aggregator/histogram"
3835
"go.opentelemetry.io/otel/sdk/metric/aggregator/sum"
3936
"go.opentelemetry.io/otel/sdk/metric/export"
4037
"go.opentelemetry.io/otel/sdk/metric/export/aggregation"
38+
"go.opentelemetry.io/otel/sdk/metric/metrictest"
39+
"go.opentelemetry.io/otel/sdk/metric/number"
4140
"go.opentelemetry.io/otel/sdk/metric/processor/processortest"
41+
"go.opentelemetry.io/otel/sdk/metric/sdkapi"
4242
"go.opentelemetry.io/otel/sdk/resource"
4343
commonpb "go.opentelemetry.io/proto/otlp/common/v1"
4444
metricpb "go.opentelemetry.io/proto/otlp/metrics/v1"

exporters/otlp/otlpmetric/internal/metrictransform/metric.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ import (
2424
"sync"
2525
"time"
2626

27-
"go.opentelemetry.io/otel/metric/number"
2827
"go.opentelemetry.io/otel/sdk/instrumentation"
2928
"go.opentelemetry.io/otel/sdk/metric/export"
3029
"go.opentelemetry.io/otel/sdk/metric/export/aggregation"
30+
"go.opentelemetry.io/otel/sdk/metric/number"
3131
"go.opentelemetry.io/otel/sdk/resource"
3232
commonpb "go.opentelemetry.io/proto/otlp/common/v1"
3333
metricpb "go.opentelemetry.io/proto/otlp/metrics/v1"

exporters/otlp/otlpmetric/internal/metrictransform/metric_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ import (
2525
"github.com/stretchr/testify/require"
2626

2727
"go.opentelemetry.io/otel/attribute"
28-
"go.opentelemetry.io/otel/metric/metrictest"
29-
"go.opentelemetry.io/otel/metric/number"
30-
"go.opentelemetry.io/otel/metric/sdkapi"
3128
"go.opentelemetry.io/otel/sdk/metric/aggregator"
3229
"go.opentelemetry.io/otel/sdk/metric/aggregator/lastvalue"
3330
"go.opentelemetry.io/otel/sdk/metric/aggregator/sum"
3431
"go.opentelemetry.io/otel/sdk/metric/export"
3532
"go.opentelemetry.io/otel/sdk/metric/export/aggregation"
33+
"go.opentelemetry.io/otel/sdk/metric/metrictest"
34+
"go.opentelemetry.io/otel/sdk/metric/number"
35+
"go.opentelemetry.io/otel/sdk/metric/sdkapi"
3636
commonpb "go.opentelemetry.io/proto/otlp/common/v1"
3737
metricpb "go.opentelemetry.io/proto/otlp/metrics/v1"
3838
)

0 commit comments

Comments
 (0)