diff --git a/go.mod b/go.mod index f9b24a9c..9ffb04a4 100644 --- a/go.mod +++ b/go.mod @@ -13,10 +13,10 @@ require ( github.com/ipfs/go-log/v2 v2.5.1 github.com/libp2p/go-libp2p v0.26.3 github.com/libp2p/go-libp2p-pubsub v0.9.3 - github.com/stretchr/testify v1.8.1 - go.opentelemetry.io/otel v1.11.2 - go.opentelemetry.io/otel/metric v0.34.0 - go.opentelemetry.io/otel/trace v1.11.2 + github.com/stretchr/testify v1.8.3 + go.opentelemetry.io/otel v1.16.0 + go.opentelemetry.io/otel/metric v1.16.0 + go.opentelemetry.io/otel/trace v1.16.0 golang.org/x/crypto v0.7.0 ) @@ -28,7 +28,7 @@ require ( github.com/davidlazar/go-crypto v0.0.0-20200604182044-b73af7476f6c // indirect github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect github.com/francoispqt/gojay v1.2.13 // 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/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect github.com/golang/mock v1.6.0 // indirect diff --git a/go.sum b/go.sum index 8997db25..28ee7641 100644 --- a/go.sum +++ b/go.sum @@ -195,8 +195,8 @@ github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs= 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/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= @@ -835,17 +835,14 @@ github.com/streadway/amqp v0.0.0-20190827072141-edfb9018d271/go.mod h1:AZpEONHx3 github.com/streadway/handy v0.0.0-20190108123426-d5acb3125c2a/go.mod h1:qNTQ5P5JnDBl6z3cMAg/SywNDC5ABu5ApDIw6lUbRmI= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/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.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 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.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= -github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +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/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ= github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA= github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= @@ -877,12 +874,12 @@ go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8= go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= -go.opentelemetry.io/otel v1.11.2 h1:YBZcQlsVekzFsFbjygXMOXSs6pialIZxcjfO/mBDmR0= -go.opentelemetry.io/otel v1.11.2/go.mod h1:7p4EUV+AqgdlNV9gL97IgUZiVR3yrFXYo53f9BM3tRI= -go.opentelemetry.io/otel/metric v0.34.0 h1:MCPoQxcg/26EuuJwpYN1mZTeCYAUGx8ABxfW07YkjP8= -go.opentelemetry.io/otel/metric v0.34.0/go.mod h1:ZFuI4yQGNCupurTXCwkeD/zHBt+C2bR7bw5JqUm/AP8= -go.opentelemetry.io/otel/trace v1.11.2 h1:Xf7hWSF2Glv0DE3MH7fBHvtpSBsjcBUe5MYAmZM/+y0= -go.opentelemetry.io/otel/trace v1.11.2/go.mod h1:4N+yC7QEz7TTsG9BSRLNAa63eg5E06ObSbKPmxQ/pKA= +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/metric v1.16.0 h1:RbrpwVG1Hfv85LgnZ7+txXioPDoh6EdbZHo26Q3hqOo= +go.opentelemetry.io/otel/metric v1.16.0/go.mod h1:QE47cpOmkwipPiefDwo2wDzwJrlfxxNYodqc4xnGCo4= +go.opentelemetry.io/otel/trace v1.16.0 h1:8JRpaObFoW0pxuVPapkgH8UhHQj+bJW8jJsCZEu5MQs= +go.opentelemetry.io/otel/trace v1.16.0/go.mod h1:Yt9vYq1SdNz3xdjZZK7wcXv1qv2pwLkqr2QVwea0ef0= go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI= go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= diff --git a/metrics.go b/metrics.go index 6e9dcf37..a1999c55 100644 --- a/metrics.go +++ b/metrics.go @@ -4,43 +4,36 @@ import ( "context" "time" + "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/metric/global" - "go.opentelemetry.io/otel/metric/instrument" - "go.opentelemetry.io/otel/metric/unit" + "go.opentelemetry.io/otel/metric" ) -var meter = global.MeterProvider().Meter("header") +var meter = otel.Meter("header") // WithMetrics enables Otel metrics to monitor head and total amount of synced headers. func WithMetrics[H Header](store Store[H]) error { - headC, _ := meter.AsyncInt64().Counter( + headC, _ := meter.Int64ObservableCounter( "head", - instrument.WithUnit(unit.Dimensionless), - instrument.WithDescription("Subjective head of the node"), + metric.WithDescription("Subjective head of the node"), ) - err := meter.RegisterCallback( - []instrument.Asynchronous{ - headC, - }, - func(ctx context.Context) { - // add timeout to limit the time it takes to get the head - // in case there is a deadlock - ctx, cancel := context.WithTimeout(ctx, time.Second) - defer cancel() + callback := func(ctx context.Context, observer metric.Observer) error { + // add timeout to limit the time it takes to get the head + // in case there is a deadlock + ctx, cancel := context.WithTimeout(ctx, time.Second) + defer cancel() - head, err := store.Head(ctx) - if err != nil { - headC.Observe(ctx, 0, attribute.String("err", err.Error())) - return - } - - headC.Observe( - ctx, - head.Height(), - ) - }, - ) + head, err := store.Head(ctx) + if err != nil { + observer.ObserveInt64(headC, 0, + metric.WithAttributes( + attribute.String("err", err.Error()))) + } else { + observer.ObserveInt64(headC, head.Height()) + } + return nil + } + _, err := meter.RegisterCallback(callback, headC) return err } diff --git a/p2p/metrics.go b/p2p/metrics.go index f6fe1d69..9ce59efb 100644 --- a/p2p/metrics.go +++ b/p2p/metrics.go @@ -3,38 +3,33 @@ package p2p import ( "context" + "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/metric/global" - "go.opentelemetry.io/otel/metric/instrument" - "go.opentelemetry.io/otel/metric/instrument/syncfloat64" + "go.opentelemetry.io/otel/metric" ) type metrics struct { - responseSize syncfloat64.Histogram - responseDuration syncfloat64.Histogram + responseSize metric.Float64Histogram + responseDuration metric.Float64Histogram } var ( - meter = global.MeterProvider().Meter("header/p2p") + meter = otel.Meter("header/p2p") ) func (ex *Exchange[H]) InitMetrics() error { - responseSize, err := meter. - SyncFloat64(). - Histogram( - "header_p2p_headers_response_size", - instrument.WithDescription("Size of get headers response in bytes"), - ) + responseSize, err := meter.Float64Histogram( + "header_p2p_headers_response_size", + metric.WithDescription("Size of get headers response in bytes"), + ) if err != nil { return err } - responseDuration, err := meter. - SyncFloat64(). - Histogram( - "header_p2p_headers_request_duration", - instrument.WithDescription("Duration of get headers request in seconds"), - ) + responseDuration, err := meter.Float64Histogram( + "header_p2p_headers_request_duration", + metric.WithDescription("Duration of get headers request in seconds"), + ) if err != nil { return err } @@ -50,14 +45,10 @@ func (m *metrics) observeResponse(ctx context.Context, size uint64, duration uin if m == nil { return } - m.responseSize.Record( - ctx, - float64(size), - attribute.Bool("failed", err != nil), + m.responseSize.Record(ctx, float64(size), + metric.WithAttributes(attribute.Bool("failed", err != nil)), ) - m.responseDuration.Record( - ctx, - float64(duration), - attribute.Bool("failed", err != nil), + m.responseDuration.Record(ctx, float64(duration), + metric.WithAttributes(attribute.Bool("failed", err != nil)), ) } diff --git a/sync/metrics.go b/sync/metrics.go index d76823b7..d90008b0 100644 --- a/sync/metrics.go +++ b/sync/metrics.go @@ -4,11 +4,11 @@ import ( "context" "sync/atomic" - "go.opentelemetry.io/otel/metric/global" - "go.opentelemetry.io/otel/metric/instrument" + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/metric" ) -var meter = global.MeterProvider().Meter("header/sync") +var meter = otel.Meter("header/sync") type metrics struct { totalSynced int64 @@ -17,24 +17,20 @@ type metrics struct { func (s *Syncer[H]) InitMetrics() error { s.metrics = &metrics{} - totalSynced, err := meter. - AsyncFloat64(). - Gauge( - "total_synced_headers", - instrument.WithDescription("total synced headers"), - ) + totalSynced, err := meter.Float64ObservableGauge( + "total_synced_headers", + metric.WithDescription("total synced headers"), + ) if err != nil { return err } - return meter.RegisterCallback( - []instrument.Asynchronous{ - totalSynced, - }, - func(ctx context.Context) { - totalSynced.Observe(ctx, float64(atomic.LoadInt64(&s.metrics.totalSynced))) - }, - ) + callback := func(ctx context.Context, observer metric.Observer) error { + observer.ObserveFloat64(totalSynced, float64(atomic.LoadInt64(&s.metrics.totalSynced))) + return nil + } + _, err = meter.RegisterCallback(callback, totalSynced) + return err } // recordTotalSynced records the total amount of synced headers.