From 34730d62eab624048e79cbaa66baea816e0f6d81 Mon Sep 17 00:00:00 2001 From: Marcel Ludwig Date: Mon, 5 Dec 2022 12:12:35 +0100 Subject: [PATCH] Using sync counters --- handler/middleware/trace.go | 4 ++-- handler/transport/backend.go | 5 +++-- handler/transport/connection.go | 8 ++++---- logging/hooks/error.go | 6 +++--- server/http.go | 4 ++-- 5 files changed, 14 insertions(+), 13 deletions(-) diff --git a/handler/middleware/trace.go b/handler/middleware/trace.go index 16b5a8294..9596a7c63 100644 --- a/handler/middleware/trace.go +++ b/handler/middleware/trace.go @@ -64,11 +64,11 @@ func (th *TraceHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { meter := provider.Meter("couper/server") - counter, _ := meter.AsyncInt64(). + counter, _ := meter.SyncInt64(). Counter(instrumentation.ClientRequest, instrument.WithDescription(string(unit.Dimensionless))) duration, _ := meter.SyncFloat64(). Histogram(instrumentation.ClientRequestDuration, instrument.WithDescription(string(unit.Dimensionless))) - counter.Observe(req.Context(), 1) + counter.Add(req.Context(), 1) duration.Record(req.Context(), end.Seconds()) } diff --git a/handler/transport/backend.go b/handler/transport/backend.go index 746bc4d07..316a3fe1e 100644 --- a/handler/transport/backend.go +++ b/handler/transport/backend.go @@ -291,7 +291,7 @@ func (b *Backend) innerRoundTrip(req *http.Request, tc *Config, deadlineErr <-ch } meter := provider.Meter(instrumentation.BackendInstrumentationName) - counter, _ := meter.AsyncInt64().Counter( + counter, _ := meter.SyncInt64().Counter( instrumentation.BackendRequest, instrument.WithDescription(string(unit.Dimensionless)), ) @@ -317,7 +317,8 @@ func (b *Backend) innerRoundTrip(req *http.Request, tc *Config, deadlineErr <-ch if beresp != nil { attrs = append(attrs, statusKey.Int(beresp.StatusCode)) } - defer counter.Observe(req.Context(), 1, attrs...) + + defer counter.Add(req.Context(), 1, attrs...) defer duration.Record(req.Context(), endSeconds, attrs...) if err != nil { diff --git a/handler/transport/connection.go b/handler/transport/connection.go index 31bd2583f..638d33b02 100644 --- a/handler/transport/connection.go +++ b/handler/transport/connection.go @@ -10,8 +10,8 @@ import ( "github.com/sirupsen/logrus" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/metric/instrument" - "go.opentelemetry.io/otel/metric/instrument/asyncint64" "go.opentelemetry.io/otel/metric/instrument/syncfloat64" + "go.opentelemetry.io/otel/metric/instrument/syncint64" "go.opentelemetry.io/otel/metric/unit" "github.com/avenga/couper/config/request" @@ -69,7 +69,7 @@ func NewOriginConn(ctx context.Context, conn net.Conn, conf *Config, entry *logr counter, gauge := newMeterCounter() - counter.Observe(ctx, 1, o.labels...) + counter.Add(ctx, 1, o.labels...) gauge.Add(ctx, 1, o.labels...) return o @@ -119,10 +119,10 @@ func (o *OriginConn) Close() error { return o.Conn.Close() } -func newMeterCounter() (asyncint64.Counter, syncfloat64.UpDownCounter) { +func newMeterCounter() (syncint64.Counter, syncfloat64.UpDownCounter) { meter := provider.Meter("couper/connection") - counter, _ := meter.AsyncInt64(). + counter, _ := meter.SyncInt64(). Counter(instrumentation.BackendConnectionsTotal, instrument.WithDescription(string(unit.Dimensionless))) gauge, _ := meter.SyncFloat64().UpDownCounter( instrumentation.BackendConnections, diff --git a/logging/hooks/error.go b/logging/hooks/error.go index 0e1f3b660..b0484204d 100644 --- a/logging/hooks/error.go +++ b/logging/hooks/error.go @@ -44,13 +44,13 @@ func (l *Error) Fire(entry *logrus.Entry) error { meter := provider.Meter("couper/errors") - counter, _ := meter.AsyncInt64(). + counter, _ := meter.SyncInt64(). Counter( - instrumentation.Prefix+"client_request_error_types_total", + instrumentation.Prefix+"client_request_error_types", instrument.WithDescription(string(unit.Dimensionless)), ) - counter.Observe(entry.Context, 1, attribute.String("error", kind)) + counter.Add(entry.Context, 1, attribute.String("error", kind)) entry.Message = errors.AppendMsg(entry.Message, gerr.LogError()) return nil diff --git a/server/http.go b/server/http.go index d9b3ee41e..52da0bf01 100644 --- a/server/http.go +++ b/server/http.go @@ -314,7 +314,7 @@ func (s *HTTPServer) cleanHostAppendPort(host string) string { func (s *HTTPServer) onConnState(_ net.Conn, state http.ConnState) { meter := provider.Meter("couper/server") - counter, _ := meter.AsyncInt64(). + counter, _ := meter.SyncInt64(). Counter(instrumentation.ClientConnectionsTotal, instrument.WithDescription(string(unit.Dimensionless))) gauge, _ := meter.SyncFloat64().UpDownCounter( instrumentation.ClientConnections, @@ -322,7 +322,7 @@ func (s *HTTPServer) onConnState(_ net.Conn, state http.ConnState) { ) if state == http.StateNew { - counter.Observe(context.Background(), 1) + counter.Add(context.Background(), 1) gauge.Add(context.Background(), 1) // we have no callback for closing a hijacked one, so count them down too. // TODO: if required we COULD override given conn ptr value with own obj.