Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions instrumentation/opentelemetry/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package opentelemetry // import "github.com/hypertrace/goagent/instrumentation/o

import (
"net/http"
"sync"

"github.com/hypertrace/goagent/sdk"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
Expand All @@ -23,6 +24,7 @@ const (
type HttpOperationMetricsHandler struct {
operationNameGetter func(*http.Request) string
counters map[string]instrument.Int64Counter
countersMutex sync.RWMutex
}

var _ sdk.HttpOperationMetricsHandler = (*HttpOperationMetricsHandler)(nil)
Expand All @@ -44,6 +46,8 @@ func (mh *HttpOperationMetricsHandler) CreateRequestCount() {
otel.Handle(err)
}

mh.countersMutex.Lock()
defer mh.countersMutex.Unlock()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we think AddToRequestCount is racing with CreateRequestCount? But if I look at the code it looks like CreateRequestCount would have had to happen first as part init of WrapHandler?

Also we don't really need a map here right? Because we have a static set of metrics and can precreate counters?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This made me think about how this is designed and I agree with you. I need to maybe get rid of the map and switch to static counters. I had a map because I thought there would be more counters we would add but we do not need a map if we can reference the counters directly.

mh.counters[RequestCount] = requestCountCounter
}

Expand All @@ -53,5 +57,8 @@ func (mh *HttpOperationMetricsHandler) AddToRequestCount(n int64, r *http.Reques
labeler, _ := otelhttp.LabelerFromContext(ctx)
operationName := mh.operationNameGetter(r)
attributes := append(labeler.Get(), semconv.HTTPServerMetricAttributesFromHTTPRequest(operationName, r)...)

mh.countersMutex.RLock()
defer mh.countersMutex.RUnlock()
mh.counters[RequestCount].Add(ctx, n, attributes...)
}