From b9a439002508c7945076d755b095f06d1aabc711 Mon Sep 17 00:00:00 2001 From: Acho Arnold Date: Sat, 29 Jul 2023 13:50:40 +0300 Subject: [PATCH] Update open telemetry to the latest version --- config.go | 4 +-- example_test.go | 10 +++---- go.mod | 26 +++++++++++------- go.sum | 57 ++++++++++++++++++++++----------------- option_test.go | 5 ++-- round_tripper.go | 63 +++++++++++++++++++++---------------------- round_tripper_test.go | 4 +-- 7 files changed, 91 insertions(+), 78 deletions(-) diff --git a/config.go b/config.go index 6a0324a..18e9434 100644 --- a/config.go +++ b/config.go @@ -1,11 +1,11 @@ package otelroundtripper import ( + "go.opentelemetry.io/otel" "net/http" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/metric" - "go.opentelemetry.io/otel/metric/global" ) type config struct { @@ -19,6 +19,6 @@ func defaultConfig() *config { return &config{ name: "http.client", parent: http.DefaultTransport, - meter: global.MeterProvider().Meter("http.client"), + meter: otel.GetMeterProvider().Meter("http.client"), } } diff --git a/example_test.go b/example_test.go index 56a04cc..e59f442 100644 --- a/example_test.go +++ b/example_test.go @@ -3,6 +3,7 @@ package otelroundtripper import ( "context" "encoding/json" + "go.opentelemetry.io/otel" "log" "math/rand" "net/http" @@ -11,7 +12,6 @@ import ( "time" "go.opentelemetry.io/otel/exporters/stdout/stdoutmetric" - "go.opentelemetry.io/otel/metric/global" "go.opentelemetry.io/otel/sdk/metric" semconv "go.opentelemetry.io/otel/semconv/v1.18.0" ) @@ -30,7 +30,7 @@ func InstallExportPipeline(ctx context.Context) func() { metric.WithReader(metric.NewPeriodicReader(exporter)), ) - global.SetMeterProvider(sdk) + otel.SetMeterProvider(sdk) return func() { if err := sdk.Shutdown(ctx); err != nil { @@ -48,17 +48,17 @@ func Example() { client := http.Client{ Transport: New( - WithMeter(global.MeterProvider().Meter("otel-round-tripper")), + WithMeter(otel.GetMeterProvider().Meter("otel-round-tripper")), WithAttributes( semconv.ServiceNameKey.String("otel-round-tripper"), ), ), } - rand.Seed(time.Now().UnixNano()) + random := rand.New(rand.NewSource(time.Now().UnixNano())) for i := 0; i < 10; i++ { // Add a random sleep duration so that we will see the metrics in the console - url := "https://httpstat.us/200?sleep=" + strconv.Itoa(rand.Intn(1000)+1000) //nolint:gosec + url := "https://httpstat.us/200?sleep=" + strconv.Itoa(random.Intn(1000)+1000) //nolint:gosec log.Printf("GET: %s", url) response, err := client.Get(url) diff --git a/go.mod b/go.mod index b95eecb..f6e2054 100644 --- a/go.mod +++ b/go.mod @@ -1,22 +1,28 @@ module github.com/NdoleStudio/go-otelroundtripper -go 1.19 +go 1.20 require ( - github.com/stretchr/testify v1.8.2 - go.opentelemetry.io/otel v1.14.0 - go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.37.0 - go.opentelemetry.io/otel/metric v0.37.0 - go.opentelemetry.io/otel/sdk/metric v0.37.0 + github.com/stretchr/testify v1.8.3 + go.opentelemetry.io/otel v1.16.0 + go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.39.0 + go.opentelemetry.io/otel/metric v1.16.0 + go.opentelemetry.io/otel/sdk/metric v0.39.0 ) require ( github.com/davecgh/go-spew v1.1.1 // indirect - github.com/go-logr/logr v1.2.3 // indirect + github.com/go-logr/logr v1.2.4 // indirect github.com/go-logr/stdr v1.2.2 // indirect + github.com/kyoh86/richgo v0.3.12 // indirect + github.com/kyoh86/xdg v1.2.0 // indirect + github.com/mattn/go-isatty v0.0.19 // indirect + github.com/morikuni/aec v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - go.opentelemetry.io/otel/sdk v1.14.0 // indirect - go.opentelemetry.io/otel/trace v1.14.0 // indirect - golang.org/x/sys v0.5.0 // indirect + github.com/wacul/ptr v1.0.0 // indirect + go.opentelemetry.io/otel/sdk v1.16.0 // indirect + go.opentelemetry.io/otel/trace v1.16.0 // indirect + golang.org/x/sys v0.10.0 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 24624c3..f0ef39d 100644 --- a/go.sum +++ b/go.sum @@ -1,37 +1,46 @@ -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= -github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0= -github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ= +github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/kyoh86/richgo v0.3.12 h1:W66IRaaC1BWoCPIyI94t/PChWBQ38QFDRsF1nWu4904= +github.com/kyoh86/richgo v0.3.12/go.mod h1:op8jQyhnxSZQelHDyupswwJGerEDB6zkKfzCnuqAwx4= +github.com/kyoh86/xdg v1.2.0 h1:CERuT/ShdTDj+A2UaX3hQ3mOV369+Sj+wyn2nIRIIkI= +github.com/kyoh86/xdg v1.2.0/go.mod h1:/mg8zwu1+qe76oTFUBnyS7rJzk7LLC0VGEzJyJ19DHs= +github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A= +github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= -github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -go.opentelemetry.io/otel v1.14.0 h1:/79Huy8wbf5DnIPhemGB+zEPVwnN6fuQybr/SRXa6hM= -go.opentelemetry.io/otel v1.14.0/go.mod h1:o4buv+dJzx8rohcUeRmWUZhqupFvzWis188WlggnNeU= -go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.37.0 h1:S1Y8Wkl44weO903rqc1mCV4Gqbb7Vd+R+qU1yceN7XQ= -go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.37.0/go.mod h1:6xZwq1h4G4NxtU8PhjJnWSSVMaJ+yaNbjeSXfCYow+M= -go.opentelemetry.io/otel/metric v0.37.0 h1:pHDQuLQOZwYD+Km0eb657A25NaRzy0a+eLyKfDXedEs= -go.opentelemetry.io/otel/metric v0.37.0/go.mod h1:DmdaHfGt54iV6UKxsV9slj2bBRJcKC1B1uvDLIioc1s= -go.opentelemetry.io/otel/sdk v1.14.0 h1:PDCppFRDq8A1jL9v6KMI6dYesaq+DFcDZvjsoGvxGzY= -go.opentelemetry.io/otel/sdk v1.14.0/go.mod h1:bwIC5TjrNG6QDCHNWvW4HLHtUQ4I+VQDsnjhvyZCALM= -go.opentelemetry.io/otel/sdk/metric v0.37.0 h1:haYBBtZZxiI3ROwSmkZnI+d0+AVzBWeviuYQDeBWosU= -go.opentelemetry.io/otel/sdk/metric v0.37.0/go.mod h1:mO2WV1AZKKwhwHTV3AKOoIEb9LbUaENZDuGUQd+j4A0= -go.opentelemetry.io/otel/trace v1.14.0 h1:wp2Mmvj41tDsyAJXiWDWpfNsOiIyd38fy85pyKcFq/M= -go.opentelemetry.io/otel/trace v1.14.0/go.mod h1:8avnQLK+CG77yNLUae4ea2JDQ6iT+gozhnZjy/rw9G8= -golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU= -golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY= +github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/wacul/ptr v1.0.0 h1:FIKu08Wx0YUIf9MNsfF62OCmBSmz5A1Tk65zWhOIL/I= +github.com/wacul/ptr v1.0.0/go.mod h1:BD0gjsZrCwtoR+yWDB9v2hQ8STlq9tT84qKfa+3txOc= +go.opentelemetry.io/otel v1.16.0 h1:Z7GVAX/UkAXPKsy94IU+i6thsQS4nb7LviLpnaNeW8s= +go.opentelemetry.io/otel v1.16.0/go.mod h1:vl0h9NUa1D5s1nv3A5vZOYWn8av4K8Ml6JDeHrT/bx4= +go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.39.0 h1:fl2WmyenEf6LYYlfHAtCUEDyGcpwJNqD4dHGO7PVm4w= +go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.39.0/go.mod h1:csyQxQ0UHHKVA8KApS7eUO/klMO5sd/av5CNZNU4O6w= +go.opentelemetry.io/otel/metric v1.16.0 h1:RbrpwVG1Hfv85LgnZ7+txXioPDoh6EdbZHo26Q3hqOo= +go.opentelemetry.io/otel/metric v1.16.0/go.mod h1:QE47cpOmkwipPiefDwo2wDzwJrlfxxNYodqc4xnGCo4= +go.opentelemetry.io/otel/sdk v1.16.0 h1:Z1Ok1YsijYL0CSJpHt4cS3wDDh7p572grzNrBMiMWgE= +go.opentelemetry.io/otel/sdk v1.16.0/go.mod h1:tMsIuKXuuIWPBAOrH+eHtvhTL+SntFtXF9QD68aP6p4= +go.opentelemetry.io/otel/sdk/metric v0.39.0 h1:Kun8i1eYf48kHH83RucG93ffz0zGV1sh46FAScOTuDI= +go.opentelemetry.io/otel/sdk/metric v0.39.0/go.mod h1:piDIRgjcK7u0HCL5pCA4e74qpK/jk3NiUoAHATVAmiI= +go.opentelemetry.io/otel/trace v1.16.0 h1:8JRpaObFoW0pxuVPapkgH8UhHQj+bJW8jJsCZEu5MQs= +go.opentelemetry.io/otel/trace v1.16.0/go.mod h1:Yt9vYq1SdNz3xdjZZK7wcXv1qv2pwLkqr2QVwea0ef0= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU= +golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA= +golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/option_test.go b/option_test.go index 8e9847d..369a52e 100644 --- a/option_test.go +++ b/option_test.go @@ -1,11 +1,10 @@ package otelroundtripper import ( + "go.opentelemetry.io/otel/metric/noop" "net/http" "testing" - "go.opentelemetry.io/otel/metric" - "go.opentelemetry.io/otel/attribute" semconv "go.opentelemetry.io/otel/semconv/v1.18.0" @@ -82,7 +81,7 @@ func TestWithMeter(t *testing.T) { // Arrange cfg := defaultConfig() - meter := metric.NewNoopMeter() + meter := noop.NewMeterProvider().Meter("http.client") // Act WithMeter(meter).apply(cfg) diff --git a/round_tripper.go b/round_tripper.go index be6ff3b..6171039 100644 --- a/round_tripper.go +++ b/round_tripper.go @@ -3,28 +3,27 @@ package otelroundtripper import ( "context" "errors" + "go.opentelemetry.io/otel/attribute" + api "go.opentelemetry.io/otel/metric" + semconv "go.opentelemetry.io/otel/semconv/v1.18.0" "net" "net/http" "strings" "time" - - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/metric/instrument" - semconv "go.opentelemetry.io/otel/semconv/v1.18.0" ) type otelHTTPMetrics struct { - attemptsCounter instrument.Int64Counter - noRequestCounter instrument.Int64Counter - errorsCounter instrument.Int64Counter - successesCounter instrument.Int64Counter - failureCounter instrument.Int64Counter - redirectCounter instrument.Int64Counter - timeoutsCounter instrument.Int64Counter - canceledCounter instrument.Int64Counter - deadlineExceededCounter instrument.Int64Counter - totalDurationCounter instrument.Int64Histogram - inFlightCounter instrument.Int64UpDownCounter + attemptsCounter api.Int64Counter + noRequestCounter api.Int64Counter + errorsCounter api.Int64Counter + successesCounter api.Int64Counter + failureCounter api.Int64Counter + redirectCounter api.Int64Counter + timeoutsCounter api.Int64Counter + canceledCounter api.Int64Counter + deadlineExceededCounter api.Int64Counter + totalDurationCounter api.Int64Histogram + inFlightCounter api.Int64UpDownCounter } // otelRoundTripper is the http.RoundTripper which emits open telemetry metrics @@ -61,21 +60,21 @@ func New(options ...Option) http.RoundTripper { } } -func mustCounter(counter instrument.Int64Counter, err error) instrument.Int64Counter { +func mustCounter(counter api.Int64Counter, err error) api.Int64Counter { if err != nil { panic(err) } return counter } -func mustUpDownCounter(counter instrument.Int64UpDownCounter, err error) instrument.Int64UpDownCounter { +func mustUpDownCounter(counter api.Int64UpDownCounter, err error) api.Int64UpDownCounter { if err != nil { panic(err) } return counter } -func mustHistogram(histogram instrument.Int64Histogram, err error) instrument.Int64Histogram { +func mustHistogram(histogram api.Int64Histogram, err error) api.Int64Histogram { if err != nil { panic(err) } @@ -133,24 +132,24 @@ func (roundTripper *otelRoundTripper) failureHook( ctx context.Context, attributes []attribute.KeyValue, ) { - roundTripper.metrics.inFlightCounter.Add(ctx, -1, attributes...) - roundTripper.metrics.failureCounter.Add(ctx, 1, attributes...) + roundTripper.metrics.inFlightCounter.Add(ctx, -1, api.WithAttributes(attributes...)) + roundTripper.metrics.failureCounter.Add(ctx, 1, api.WithAttributes(attributes...)) } func (roundTripper *otelRoundTripper) redirectHook( ctx context.Context, attributes []attribute.KeyValue, ) { - roundTripper.metrics.inFlightCounter.Add(ctx, -1, attributes...) - roundTripper.metrics.redirectCounter.Add(ctx, 1, attributes...) + roundTripper.metrics.inFlightCounter.Add(ctx, -1, api.WithAttributes(attributes...)) + roundTripper.metrics.redirectCounter.Add(ctx, 1, api.WithAttributes(attributes...)) } func (roundTripper *otelRoundTripper) successHook( ctx context.Context, attributes []attribute.KeyValue, ) { - roundTripper.metrics.inFlightCounter.Add(ctx, -1, attributes...) - roundTripper.metrics.successesCounter.Add(ctx, 1, attributes...) + roundTripper.metrics.inFlightCounter.Add(ctx, -1, api.WithAttributes(attributes...)) + roundTripper.metrics.successesCounter.Add(ctx, 1, api.WithAttributes(attributes...)) } func (roundTripper *otelRoundTripper) beforeHook( @@ -158,10 +157,10 @@ func (roundTripper *otelRoundTripper) beforeHook( attributes []attribute.KeyValue, request *http.Request, ) { - roundTripper.metrics.inFlightCounter.Add(ctx, 1, attributes...) - roundTripper.metrics.attemptsCounter.Add(ctx, 1, attributes...) + roundTripper.metrics.inFlightCounter.Add(ctx, 1, api.WithAttributes(attributes...)) + roundTripper.metrics.attemptsCounter.Add(ctx, 1, api.WithAttributes(attributes...)) if request == nil { - roundTripper.metrics.noRequestCounter.Add(ctx, 1, attributes...) + roundTripper.metrics.noRequestCounter.Add(ctx, 1, api.WithAttributes(attributes...)) } } @@ -170,7 +169,7 @@ func (roundTripper *otelRoundTripper) afterHook( duration int64, attributes []attribute.KeyValue, ) { - roundTripper.metrics.totalDurationCounter.Record(ctx, duration, attributes...) + roundTripper.metrics.totalDurationCounter.Record(ctx, duration, api.WithAttributes(attributes...)) } func (roundTripper *otelRoundTripper) responseAttributes( @@ -194,16 +193,16 @@ func (roundTripper *otelRoundTripper) requestAttributes(request *http.Request) [ } func (roundTripper *otelRoundTripper) errorHook(ctx context.Context, err error, attributes []attribute.KeyValue) { - roundTripper.metrics.inFlightCounter.Add(ctx, -1, attributes...) - roundTripper.metrics.errorsCounter.Add(ctx, 1, attributes...) + roundTripper.metrics.inFlightCounter.Add(ctx, -1, api.WithAttributes(attributes...)) + roundTripper.metrics.errorsCounter.Add(ctx, 1, api.WithAttributes(attributes...)) var timeoutErr net.Error if errors.As(err, &timeoutErr) && timeoutErr.Timeout() { - roundTripper.metrics.timeoutsCounter.Add(ctx, 1, attributes...) + roundTripper.metrics.timeoutsCounter.Add(ctx, 1, api.WithAttributes(attributes...)) } if strings.HasSuffix(err.Error(), context.Canceled.Error()) { - roundTripper.metrics.canceledCounter.Add(ctx, 1, attributes...) + roundTripper.metrics.canceledCounter.Add(ctx, 1, api.WithAttributes(attributes...)) } } diff --git a/round_tripper_test.go b/round_tripper_test.go index e262a84..573f545 100644 --- a/round_tripper_test.go +++ b/round_tripper_test.go @@ -3,6 +3,7 @@ package otelroundtripper import ( "context" "errors" + "go.opentelemetry.io/otel/metric/noop" "io" "net" "net/http" @@ -11,7 +12,6 @@ import ( "testing" "time" - "go.opentelemetry.io/otel/metric" semconv "go.opentelemetry.io/otel/semconv/v1.18.0" "github.com/stretchr/testify/assert" @@ -30,7 +30,7 @@ func TestNew(t *testing.T) { roundTripper := New( WithName("name"), WithParent(http.DefaultTransport), - WithMeter(metric.NewNoopMeter()), + WithMeter(noop.NewMeterProvider().Meter("http.client")), WithAttributes([]attribute.KeyValue{semconv.ServiceNameKey.String("service")}...), ) assert.NotNil(t, roundTripper)