Skip to content

Commit

Permalink
Using sync counters
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Ludwig committed Dec 5, 2022
1 parent de29970 commit 34730d6
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 13 deletions.
4 changes: 2 additions & 2 deletions handler/middleware/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
5 changes: 3 additions & 2 deletions handler/transport/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
)
Expand All @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions handler/transport/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions logging/hooks/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,15 +314,15 @@ 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,
instrument.WithDescription(string(unit.Dimensionless)),
)

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.
Expand Down

0 comments on commit 34730d6

Please sign in to comment.