From aff0019802b410b33a2ec535478155c18663b586 Mon Sep 17 00:00:00 2001 From: Wondertan Date: Tue, 24 Oct 2023 12:50:34 +0200 Subject: [PATCH 1/7] feat(p2p): metrics for ExchangeServer --- p2p/server.go | 18 +++++++++ p2p/server_metrics.go | 87 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 p2p/server_metrics.go diff --git a/p2p/server.go b/p2p/server.go index a08c1ac6..c071cf5e 100644 --- a/p2p/server.go +++ b/p2p/server.go @@ -30,6 +30,7 @@ type ExchangeServer[H header.Header[H]] struct { host host.Host store header.Store[H] + metrics *serverMetrics ctx context.Context cancel context.CancelFunc @@ -52,10 +53,20 @@ func NewExchangeServer[H header.Header[H]]( return nil, err } + var metrics *serverMetrics + if params.metrics { + var err error + metrics, err = newServerMetrics() + if err != nil { + return nil, err + } + } + return &ExchangeServer[H]{ protocolID: protocolID(params.networkID), host: host, store: store, + metrics: metrics, Params: params, }, nil } @@ -158,6 +169,7 @@ func (serv *ExchangeServer[H]) requestHandler(stream network.Stream) { // handleRequestByHash returns the Header at the given hash // if it exists. func (serv *ExchangeServer[H]) handleRequestByHash(hash []byte) ([]H, error) { + startTime := time.Now() log.Debugw("server: handling header request", "hash", header.Hash(hash).String()) ctx, cancel := context.WithTimeout(serv.ctx, serv.Params.RangeRequestTimeout) defer cancel() @@ -178,6 +190,8 @@ func (serv *ExchangeServer[H]) handleRequestByHash(hash []byte) ([]H, error) { attribute.Int64("height", int64(h.Height()))), ) span.SetStatus(codes.Ok, "") + + serv.metrics.getServed(ctx, time.Since(startTime)) return []H{h}, nil } @@ -188,6 +202,7 @@ func (serv *ExchangeServer[H]) handleRequest(from, to uint64) ([]H, error) { return serv.handleHeadRequest() } + startTime := time.Now() ctx, span := tracer.Start(serv.ctx, "request-range", trace.WithAttributes( attribute.Int64("from", int64(from)), attribute.Int64("to", int64(to)))) @@ -243,11 +258,13 @@ func (serv *ExchangeServer[H]) handleRequest(from, to uint64) ([]H, error) { span.AddEvent("fetched-range-of-headers", trace.WithAttributes( attribute.Int("amount", len(headersByRange)))) span.SetStatus(codes.Ok, "") + serv.metrics.rangeServed(ctx, time.Since(startTime), len(headersByRange)) return headersByRange, nil } // handleHeadRequest returns the latest stored head. func (serv *ExchangeServer[H]) handleHeadRequest() ([]H, error) { + startTime := time.Now() log.Debug("server: handling head request") ctx, cancel := context.WithTimeout(serv.ctx, serv.Params.RangeRequestTimeout) defer cancel() @@ -266,5 +283,6 @@ func (serv *ExchangeServer[H]) handleHeadRequest() ([]H, error) { attribute.Int64("height", int64(head.Height()))), ) span.SetStatus(codes.Ok, "") + serv.metrics.headServed(ctx, time.Since(startTime)) return []H{head}, nil } diff --git a/p2p/server_metrics.go b/p2p/server_metrics.go new file mode 100644 index 00000000..6c1e5337 --- /dev/null +++ b/p2p/server_metrics.go @@ -0,0 +1,87 @@ +package p2p + +import ( + "context" + "time" + + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/metric" +) + +const headersServedKey = "num_headers_served" + +type serverMetrics struct { + headersServed metric.Int64Counter + headServeTimeInst metric.Int64Histogram + rangeServeTimeInst metric.Int64Histogram + getServeTimeInst metric.Int64Histogram +} + +func newServerMetrics() (m *serverMetrics, err error) { + m = new(serverMetrics) + m.headersServed, err = meter.Int64Counter( + "hdr_p2p_exch_srvr_headers_served", + metric.WithDescription("number of headers served"), + ) + if err != nil { + return nil, err + } + m.headServeTimeInst, err = meter.Int64Histogram( + "hdr_p2p_exch_srvr_head_serve_time_hist", + metric.WithDescription("exchange server head serve time in milliseconds"), + ) + if err != nil { + return nil, err + } + m.rangeServeTimeInst, err = meter.Int64Histogram( + "hdr_p2p_exch_srvr_range_serve_time_hist", + metric.WithDescription("exchange server range serve time in milliseconds"), + ) + if err != nil { + return nil, err + } + m.getServeTimeInst, err = meter.Int64Histogram( + "hdr_p2p_exch_srvr_get_serve_time_hist", + metric.WithDescription("exchange server get serve time in milliseconds"), + ) + if err != nil { + return nil, err + } + return m, nil +} + +func (m *serverMetrics) headServed(ctx context.Context, duration time.Duration) { + m.observe(ctx, func(ctx context.Context) { + m.headersServed.Add(ctx, 1) + m.headServeTimeInst.Record(ctx, duration.Milliseconds()) + }) +} + +func (m *serverMetrics) rangeServed(ctx context.Context, duration time.Duration, headersServed int) { + m.observe(ctx, func(ctx context.Context) { + m.headersServed.Add(ctx, int64(headersServed)) + m.rangeServeTimeInst.Record(ctx, + duration.Milliseconds(), + metric.WithAttributes(attribute.Int(headersServedKey, headersServed)), + ) + }) +} + +func (m *serverMetrics) getServed(ctx context.Context, duration time.Duration) { + m.observe(ctx, func(ctx context.Context) { + m.headersServed.Add(ctx, 1) + m.getServeTimeInst.Record(ctx, duration.Milliseconds()) + }) +} + +func (m *serverMetrics) observe(ctx context.Context, f func(context.Context)) { + if m == nil { + return + } + + if ctx.Err() != nil { + ctx = context.Background() + } + + f(ctx) +} \ No newline at end of file From 76621142121454096f40d3f793abbd9d7c19afb1 Mon Sep 17 00:00:00 2001 From: Wondertan Date: Tue, 24 Oct 2023 15:48:13 +0200 Subject: [PATCH 2/7] lint --- p2p/server.go | 6 +++--- p2p/server_metrics.go | 16 ++++++++-------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/p2p/server.go b/p2p/server.go index c071cf5e..d0294fc3 100644 --- a/p2p/server.go +++ b/p2p/server.go @@ -28,8 +28,8 @@ var ( type ExchangeServer[H header.Header[H]] struct { protocolID protocol.ID - host host.Host - store header.Store[H] + host host.Host + store header.Store[H] metrics *serverMetrics ctx context.Context @@ -66,7 +66,7 @@ func NewExchangeServer[H header.Header[H]]( protocolID: protocolID(params.networkID), host: host, store: store, - metrics: metrics, + metrics: metrics, Params: params, }, nil } diff --git a/p2p/server_metrics.go b/p2p/server_metrics.go index 6c1e5337..2f4cb09d 100644 --- a/p2p/server_metrics.go +++ b/p2p/server_metrics.go @@ -11,15 +11,15 @@ import ( const headersServedKey = "num_headers_served" type serverMetrics struct { - headersServed metric.Int64Counter - headServeTimeInst metric.Int64Histogram + headersServedInst metric.Int64Counter + headServeTimeInst metric.Int64Histogram rangeServeTimeInst metric.Int64Histogram - getServeTimeInst metric.Int64Histogram + getServeTimeInst metric.Int64Histogram } func newServerMetrics() (m *serverMetrics, err error) { m = new(serverMetrics) - m.headersServed, err = meter.Int64Counter( + m.headersServedInst, err = meter.Int64Counter( "hdr_p2p_exch_srvr_headers_served", metric.WithDescription("number of headers served"), ) @@ -52,14 +52,14 @@ func newServerMetrics() (m *serverMetrics, err error) { func (m *serverMetrics) headServed(ctx context.Context, duration time.Duration) { m.observe(ctx, func(ctx context.Context) { - m.headersServed.Add(ctx, 1) + m.headersServedInst.Add(ctx, 1) m.headServeTimeInst.Record(ctx, duration.Milliseconds()) }) } func (m *serverMetrics) rangeServed(ctx context.Context, duration time.Duration, headersServed int) { m.observe(ctx, func(ctx context.Context) { - m.headersServed.Add(ctx, int64(headersServed)) + m.headersServedInst.Add(ctx, int64(headersServed)) m.rangeServeTimeInst.Record(ctx, duration.Milliseconds(), metric.WithAttributes(attribute.Int(headersServedKey, headersServed)), @@ -69,7 +69,7 @@ func (m *serverMetrics) rangeServed(ctx context.Context, duration time.Duration, func (m *serverMetrics) getServed(ctx context.Context, duration time.Duration) { m.observe(ctx, func(ctx context.Context) { - m.headersServed.Add(ctx, 1) + m.headersServedInst.Add(ctx, 1) m.getServeTimeInst.Record(ctx, duration.Milliseconds()) }) } @@ -84,4 +84,4 @@ func (m *serverMetrics) observe(ctx context.Context, f func(context.Context)) { } f(ctx) -} \ No newline at end of file +} From 9fd9fea52deb355d4d261a0a79f0aa90244416f0 Mon Sep 17 00:00:00 2001 From: Wondertan Date: Tue, 24 Oct 2023 16:35:54 +0200 Subject: [PATCH 3/7] use float histogram and seconds --- p2p/server_metrics.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/p2p/server_metrics.go b/p2p/server_metrics.go index 2f4cb09d..af27c242 100644 --- a/p2p/server_metrics.go +++ b/p2p/server_metrics.go @@ -12,9 +12,9 @@ const headersServedKey = "num_headers_served" type serverMetrics struct { headersServedInst metric.Int64Counter - headServeTimeInst metric.Int64Histogram - rangeServeTimeInst metric.Int64Histogram - getServeTimeInst metric.Int64Histogram + headServeTimeInst metric.Float64Histogram + rangeServeTimeInst metric.Float64Histogram + getServeTimeInst metric.Float64Histogram } func newServerMetrics() (m *serverMetrics, err error) { @@ -26,23 +26,23 @@ func newServerMetrics() (m *serverMetrics, err error) { if err != nil { return nil, err } - m.headServeTimeInst, err = meter.Int64Histogram( + m.headServeTimeInst, err = meter.Float64Histogram( "hdr_p2p_exch_srvr_head_serve_time_hist", - metric.WithDescription("exchange server head serve time in milliseconds"), + metric.WithDescription("exchange server head serve time in seconds"), ) if err != nil { return nil, err } - m.rangeServeTimeInst, err = meter.Int64Histogram( + m.rangeServeTimeInst, err = meter.Float64Histogram( "hdr_p2p_exch_srvr_range_serve_time_hist", - metric.WithDescription("exchange server range serve time in milliseconds"), + metric.WithDescription("exchange server range serve time in seconds"), ) if err != nil { return nil, err } - m.getServeTimeInst, err = meter.Int64Histogram( + m.getServeTimeInst, err = meter.Float64Histogram( "hdr_p2p_exch_srvr_get_serve_time_hist", - metric.WithDescription("exchange server get serve time in milliseconds"), + metric.WithDescription("exchange server get serve time in seconds"), ) if err != nil { return nil, err @@ -53,7 +53,7 @@ func newServerMetrics() (m *serverMetrics, err error) { func (m *serverMetrics) headServed(ctx context.Context, duration time.Duration) { m.observe(ctx, func(ctx context.Context) { m.headersServedInst.Add(ctx, 1) - m.headServeTimeInst.Record(ctx, duration.Milliseconds()) + m.headServeTimeInst.Record(ctx, duration.Seconds()) }) } @@ -61,7 +61,7 @@ func (m *serverMetrics) rangeServed(ctx context.Context, duration time.Duration, m.observe(ctx, func(ctx context.Context) { m.headersServedInst.Add(ctx, int64(headersServed)) m.rangeServeTimeInst.Record(ctx, - duration.Milliseconds(), + duration.Seconds(), metric.WithAttributes(attribute.Int(headersServedKey, headersServed)), ) }) @@ -70,7 +70,7 @@ func (m *serverMetrics) rangeServed(ctx context.Context, duration time.Duration, func (m *serverMetrics) getServed(ctx context.Context, duration time.Duration) { m.observe(ctx, func(ctx context.Context) { m.headersServedInst.Add(ctx, 1) - m.getServeTimeInst.Record(ctx, duration.Milliseconds()) + m.getServeTimeInst.Record(ctx, duration.Seconds()) }) } From f615a1baa4d7a3e8c1a9d5804c290835579105c1 Mon Sep 17 00:00:00 2001 From: Wondertan Date: Wed, 25 Oct 2023 16:07:05 +0200 Subject: [PATCH 4/7] sync review fixes --- p2p/server_metrics.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/p2p/server_metrics.go b/p2p/server_metrics.go index af27c242..4cfbc884 100644 --- a/p2p/server_metrics.go +++ b/p2p/server_metrics.go @@ -20,7 +20,7 @@ type serverMetrics struct { func newServerMetrics() (m *serverMetrics, err error) { m = new(serverMetrics) m.headersServedInst, err = meter.Int64Counter( - "hdr_p2p_exch_srvr_headers_served", + "hdr_p2p_exch_srvr_headers_served_counter", metric.WithDescription("number of headers served"), ) if err != nil { @@ -62,7 +62,7 @@ func (m *serverMetrics) rangeServed(ctx context.Context, duration time.Duration, m.headersServedInst.Add(ctx, int64(headersServed)) m.rangeServeTimeInst.Record(ctx, duration.Seconds(), - metric.WithAttributes(attribute.Int(headersServedKey, headersServed)), + metric.WithAttributes(attribute.Int(headersServedKey, headersServed/100)), ) }) } From a7ceae96f911408c806e233aafe7e63f919d67f5 Mon Sep 17 00:00:00 2001 From: Wondertan Date: Wed, 25 Oct 2023 16:31:07 +0200 Subject: [PATCH 5/7] add comment --- p2p/server_metrics.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/p2p/server_metrics.go b/p2p/server_metrics.go index 4cfbc884..f299d62f 100644 --- a/p2p/server_metrics.go +++ b/p2p/server_metrics.go @@ -62,7 +62,7 @@ func (m *serverMetrics) rangeServed(ctx context.Context, duration time.Duration, m.headersServedInst.Add(ctx, int64(headersServed)) m.rangeServeTimeInst.Record(ctx, duration.Seconds(), - metric.WithAttributes(attribute.Int(headersServedKey, headersServed/100)), + metric.WithAttributes(attribute.Int(headersServedKey, headersServed/100)), // divide by 100 to reduce cardinality ) }) } From bb9cfaf54f9416fafc5b4bea5298f603f0ad8a71 Mon Sep 17 00:00:00 2001 From: Wondertan Date: Thu, 26 Oct 2023 13:39:26 +0200 Subject: [PATCH 6/7] track serve failures --- p2p/server.go | 6 ++++++ p2p/server_metrics.go | 37 ++++++++++++++++++++++++++++--------- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/p2p/server.go b/p2p/server.go index d0294fc3..8001484c 100644 --- a/p2p/server.go +++ b/p2p/server.go @@ -182,6 +182,7 @@ func (serv *ExchangeServer[H]) handleRequestByHash(hash []byte) ([]H, error) { if err != nil { log.Errorw("server: getting header by hash", "hash", header.Hash(hash).String(), "err", err) span.SetStatus(codes.Error, err.Error()) + serv.metrics.getServed(ctx, time.Since(startTime), err) return nil, err } @@ -211,6 +212,7 @@ func (serv *ExchangeServer[H]) handleRequest(from, to uint64) ([]H, error) { if to-from > header.MaxRangeRequestSize { log.Errorw("server: skip request for too many headers.", "amount", to-from) span.SetStatus(codes.Error, header.ErrHeadersLimitExceeded.Error()) + serv.metrics.rangeServed(ctx, time.Since(startTime), int(to-from), header.ErrHeadersLimitExceeded) return nil, header.ErrHeadersLimitExceeded } @@ -221,6 +223,7 @@ func (serv *ExchangeServer[H]) handleRequest(from, to uint64) ([]H, error) { if err != nil { span.SetStatus(codes.Error, err.Error()) log.Debugw("server: could not get current head", "err", err) + serv.metrics.rangeServed(ctx, time.Since(startTime), int(to-from), err) return nil, err } @@ -233,6 +236,7 @@ func (serv *ExchangeServer[H]) handleRequest(from, to uint64) ([]H, error) { "currentHead", head.Height(), ) + serv.metrics.rangeServed(ctx, time.Since(startTime), int(to-from), header.ErrNotFound) return nil, header.ErrNotFound } @@ -252,6 +256,7 @@ func (serv *ExchangeServer[H]) handleRequest(from, to uint64) ([]H, error) { return nil, header.ErrNotFound } log.Errorw("server: getting headers", "from", from, "to", to, "err", err) + serv.metrics.rangeServed(ctx, time.Since(startTime), int(to-from), err) return nil, err } @@ -275,6 +280,7 @@ func (serv *ExchangeServer[H]) handleHeadRequest() ([]H, error) { if err != nil { log.Errorw("server: getting head", "err", err) span.SetStatus(codes.Error, err.Error()) + serv.metrics.headServed(ctx, time.Since(startTime), err) return nil, err } diff --git a/p2p/server_metrics.go b/p2p/server_metrics.go index f299d62f..52a3c062 100644 --- a/p2p/server_metrics.go +++ b/p2p/server_metrics.go @@ -8,7 +8,10 @@ import ( "go.opentelemetry.io/otel/metric" ) -const headersServedKey = "num_headers_served" +const ( + headersServedKey = "num_headers_served" + failedRequestKey = "failed_request" +) type serverMetrics struct { headersServedInst metric.Int64Counter @@ -50,27 +53,43 @@ func newServerMetrics() (m *serverMetrics, err error) { return m, nil } -func (m *serverMetrics) headServed(ctx context.Context, duration time.Duration) { +func (m *serverMetrics) headServed(ctx context.Context, duration time.Duration, err ...error) { m.observe(ctx, func(ctx context.Context) { - m.headersServedInst.Add(ctx, 1) - m.headServeTimeInst.Record(ctx, duration.Seconds()) + m.headersServedInst.Add(ctx, + 1, + metric.WithAttributes(attribute.Bool(failedRequestKey, err != nil)), + ) + m.headServeTimeInst.Record(ctx, + duration.Seconds(), + metric.WithAttributes(attribute.Bool(failedRequestKey, err != nil)), + ) }) } -func (m *serverMetrics) rangeServed(ctx context.Context, duration time.Duration, headersServed int) { +func (m *serverMetrics) rangeServed(ctx context.Context, duration time.Duration, headersServed int, err ...error) { m.observe(ctx, func(ctx context.Context) { - m.headersServedInst.Add(ctx, int64(headersServed)) + m.headersServedInst.Add(ctx, + int64(headersServed), + metric.WithAttributes(attribute.Bool(failedRequestKey, err != nil)), + ) m.rangeServeTimeInst.Record(ctx, duration.Seconds(), metric.WithAttributes(attribute.Int(headersServedKey, headersServed/100)), // divide by 100 to reduce cardinality + metric.WithAttributes(attribute.Bool(failedRequestKey, err != nil)), ) }) } -func (m *serverMetrics) getServed(ctx context.Context, duration time.Duration) { +func (m *serverMetrics) getServed(ctx context.Context, duration time.Duration, err ...error) { m.observe(ctx, func(ctx context.Context) { - m.headersServedInst.Add(ctx, 1) - m.getServeTimeInst.Record(ctx, duration.Seconds()) + m.headersServedInst.Add(ctx, + 1, + metric.WithAttributes(attribute.Bool(failedRequestKey, err != nil)), + ) + m.getServeTimeInst.Record(ctx, + duration.Seconds(), + metric.WithAttributes(attribute.Bool(failedRequestKey, err != nil)), + ) }) } From a7a1f073889fcc6cf4045ae7815a5db9d714746d Mon Sep 17 00:00:00 2001 From: Wondertan Date: Thu, 26 Oct 2023 14:00:36 +0200 Subject: [PATCH 7/7] use bool instead of variadic --- p2p/server.go | 18 +++++++++--------- p2p/server_metrics.go | 18 +++++++++--------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/p2p/server.go b/p2p/server.go index 8001484c..327b9158 100644 --- a/p2p/server.go +++ b/p2p/server.go @@ -182,7 +182,7 @@ func (serv *ExchangeServer[H]) handleRequestByHash(hash []byte) ([]H, error) { if err != nil { log.Errorw("server: getting header by hash", "hash", header.Hash(hash).String(), "err", err) span.SetStatus(codes.Error, err.Error()) - serv.metrics.getServed(ctx, time.Since(startTime), err) + serv.metrics.getServed(ctx, time.Since(startTime), true) return nil, err } @@ -192,7 +192,7 @@ func (serv *ExchangeServer[H]) handleRequestByHash(hash []byte) ([]H, error) { ) span.SetStatus(codes.Ok, "") - serv.metrics.getServed(ctx, time.Since(startTime)) + serv.metrics.getServed(ctx, time.Since(startTime), false) return []H{h}, nil } @@ -212,7 +212,7 @@ func (serv *ExchangeServer[H]) handleRequest(from, to uint64) ([]H, error) { if to-from > header.MaxRangeRequestSize { log.Errorw("server: skip request for too many headers.", "amount", to-from) span.SetStatus(codes.Error, header.ErrHeadersLimitExceeded.Error()) - serv.metrics.rangeServed(ctx, time.Since(startTime), int(to-from), header.ErrHeadersLimitExceeded) + serv.metrics.rangeServed(ctx, time.Since(startTime), int(to-from), true) return nil, header.ErrHeadersLimitExceeded } @@ -223,7 +223,7 @@ func (serv *ExchangeServer[H]) handleRequest(from, to uint64) ([]H, error) { if err != nil { span.SetStatus(codes.Error, err.Error()) log.Debugw("server: could not get current head", "err", err) - serv.metrics.rangeServed(ctx, time.Since(startTime), int(to-from), err) + serv.metrics.rangeServed(ctx, time.Since(startTime), int(to-from), true) return nil, err } @@ -236,7 +236,7 @@ func (serv *ExchangeServer[H]) handleRequest(from, to uint64) ([]H, error) { "currentHead", head.Height(), ) - serv.metrics.rangeServed(ctx, time.Since(startTime), int(to-from), header.ErrNotFound) + serv.metrics.rangeServed(ctx, time.Since(startTime), int(to-from), true) return nil, header.ErrNotFound } @@ -256,14 +256,14 @@ func (serv *ExchangeServer[H]) handleRequest(from, to uint64) ([]H, error) { return nil, header.ErrNotFound } log.Errorw("server: getting headers", "from", from, "to", to, "err", err) - serv.metrics.rangeServed(ctx, time.Since(startTime), int(to-from), err) + serv.metrics.rangeServed(ctx, time.Since(startTime), int(to-from), true) return nil, err } span.AddEvent("fetched-range-of-headers", trace.WithAttributes( attribute.Int("amount", len(headersByRange)))) span.SetStatus(codes.Ok, "") - serv.metrics.rangeServed(ctx, time.Since(startTime), len(headersByRange)) + serv.metrics.rangeServed(ctx, time.Since(startTime), len(headersByRange), false) return headersByRange, nil } @@ -280,7 +280,7 @@ func (serv *ExchangeServer[H]) handleHeadRequest() ([]H, error) { if err != nil { log.Errorw("server: getting head", "err", err) span.SetStatus(codes.Error, err.Error()) - serv.metrics.headServed(ctx, time.Since(startTime), err) + serv.metrics.headServed(ctx, time.Since(startTime), true) return nil, err } @@ -289,6 +289,6 @@ func (serv *ExchangeServer[H]) handleHeadRequest() ([]H, error) { attribute.Int64("height", int64(head.Height()))), ) span.SetStatus(codes.Ok, "") - serv.metrics.headServed(ctx, time.Since(startTime)) + serv.metrics.headServed(ctx, time.Since(startTime), false) return []H{head}, nil } diff --git a/p2p/server_metrics.go b/p2p/server_metrics.go index 52a3c062..a4a9b1c9 100644 --- a/p2p/server_metrics.go +++ b/p2p/server_metrics.go @@ -53,42 +53,42 @@ func newServerMetrics() (m *serverMetrics, err error) { return m, nil } -func (m *serverMetrics) headServed(ctx context.Context, duration time.Duration, err ...error) { +func (m *serverMetrics) headServed(ctx context.Context, duration time.Duration, failed bool) { m.observe(ctx, func(ctx context.Context) { m.headersServedInst.Add(ctx, 1, - metric.WithAttributes(attribute.Bool(failedRequestKey, err != nil)), + metric.WithAttributes(attribute.Bool(failedRequestKey, failed)), ) m.headServeTimeInst.Record(ctx, duration.Seconds(), - metric.WithAttributes(attribute.Bool(failedRequestKey, err != nil)), + metric.WithAttributes(attribute.Bool(failedRequestKey, failed)), ) }) } -func (m *serverMetrics) rangeServed(ctx context.Context, duration time.Duration, headersServed int, err ...error) { +func (m *serverMetrics) rangeServed(ctx context.Context, duration time.Duration, headersServed int, failed bool) { m.observe(ctx, func(ctx context.Context) { m.headersServedInst.Add(ctx, int64(headersServed), - metric.WithAttributes(attribute.Bool(failedRequestKey, err != nil)), + metric.WithAttributes(attribute.Bool(failedRequestKey, failed)), ) m.rangeServeTimeInst.Record(ctx, duration.Seconds(), metric.WithAttributes(attribute.Int(headersServedKey, headersServed/100)), // divide by 100 to reduce cardinality - metric.WithAttributes(attribute.Bool(failedRequestKey, err != nil)), + metric.WithAttributes(attribute.Bool(failedRequestKey, failed)), ) }) } -func (m *serverMetrics) getServed(ctx context.Context, duration time.Duration, err ...error) { +func (m *serverMetrics) getServed(ctx context.Context, duration time.Duration, failed bool) { m.observe(ctx, func(ctx context.Context) { m.headersServedInst.Add(ctx, 1, - metric.WithAttributes(attribute.Bool(failedRequestKey, err != nil)), + metric.WithAttributes(attribute.Bool(failedRequestKey, failed)), ) m.getServeTimeInst.Record(ctx, duration.Seconds(), - metric.WithAttributes(attribute.Bool(failedRequestKey, err != nil)), + metric.WithAttributes(attribute.Bool(failedRequestKey, failed)), ) }) }