Skip to content

Commit

Permalink
fix: remove metric maps (#211)
Browse files Browse the repository at this point in the history
Switch to struct fields
  • Loading branch information
tim-mwangi authored Feb 24, 2023
1 parent 1afdcd7 commit a69e370
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 54 deletions.
3 changes: 0 additions & 3 deletions instrumentation/opencensus/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,5 @@ func NewHttpOperationMetricsHandler() sdk.HttpOperationMetricsHandler {
return &httpOperationMetricsHandler{}
}

func (mh *httpOperationMetricsHandler) CreateRequestCount() {
}

func (mh *httpOperationMetricsHandler) AddToRequestCount(n int64, r *http.Request) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ const (
DefaultScheduleDelay = 5000
DefaultExportTimeout = 30000
DefaultMaxExportBatchSize = 512
SpansReceivedCounter = "hypertrace.agent.bsp.spans_received"
SpansDroppedCounter = "hypertrace.agent.bsp.spans_dropped"
SpansUnSampledCounter = "hypertrace.agent.bsp.spans_unsampled"
spansReceivedCounterName = "hypertrace.agent.bsp.spans_received"
spansDroppedCounterName = "hypertrace.agent.bsp.spans_dropped"
spansUnsampledCounterName = "hypertrace.agent.bsp.spans_unsampled"
meterName = "go.opentelemetry.io/otel/sdk/trace"
)

// batchSpanProcessor is a SpanProcessor that batches asynchronously-received
Expand All @@ -57,7 +58,9 @@ type batchSpanProcessor struct {
stopOnce sync.Once
stopCh chan struct{}
// Some metrics in here.
counters map[string]instrument.Int64Counter
spansReceivedCounter instrument.Int64Counter
spansDroppedCounter instrument.Int64Counter
spansUnsampledCounter instrument.Int64Counter
}

var _ sdktrace.SpanProcessor = (*batchSpanProcessor)(nil)
Expand Down Expand Up @@ -90,39 +93,36 @@ func NewBatchSpanProcessor(exporter sdktrace.SpanExporter, options ...sdktrace.B

// Setup metrics
mp := metricglobal.MeterProvider()
meter := mp.Meter("go.opentelemetry.io/otel/sdk/trace",
metric.WithInstrumentationVersion(otel.Version()))
counters := make(map[string]instrument.Int64Counter)
meter := mp.Meter(meterName, metric.WithInstrumentationVersion(otel.Version()))

// Spans received by processor
spansReceivedCounter, err := meter.Int64Counter(SpansReceivedCounter)
spansReceivedCounter, err := meter.Int64Counter(spansReceivedCounterName)
if err != nil {
otel.Handle(err)
}
counters[SpansReceivedCounter] = spansReceivedCounter

// Spans Dropped by processor once the buffer is full.
spansDroppedCounter, err := meter.Int64Counter(SpansDroppedCounter)
spansDroppedCounter, err := meter.Int64Counter(spansDroppedCounterName)
if err != nil {
otel.Handle(err)
}
counters[SpansDroppedCounter] = spansDroppedCounter

// Spans that are not sampled.(Useful to know when sampling is enabled)
spansUnSampledCounter, err := meter.Int64Counter(SpansUnSampledCounter)
spansUnsampledCounter, err := meter.Int64Counter(spansUnsampledCounterName)
if err != nil {
otel.Handle(err)
}
counters[SpansUnSampledCounter] = spansUnSampledCounter

bsp := &batchSpanProcessor{
e: exporter,
o: o,
batch: make([]sdktrace.ReadOnlySpan, 0, o.MaxExportBatchSize),
timer: time.NewTimer(o.BatchTimeout),
queue: make(chan sdktrace.ReadOnlySpan, o.MaxQueueSize),
stopCh: make(chan struct{}),
counters: counters,
e: exporter,
o: o,
batch: make([]sdktrace.ReadOnlySpan, 0, o.MaxExportBatchSize),
timer: time.NewTimer(o.BatchTimeout),
queue: make(chan sdktrace.ReadOnlySpan, o.MaxQueueSize),
stopCh: make(chan struct{}),
spansReceivedCounter: spansReceivedCounter,
spansDroppedCounter: spansDroppedCounter,
spansUnsampledCounter: spansUnsampledCounter,
}

bsp.stopWait.Add(1)
Expand Down Expand Up @@ -400,12 +400,12 @@ func (bsp *batchSpanProcessor) enqueueBlockOnQueueFull(ctx context.Context, sd s
func (bsp *batchSpanProcessor) enqueueDrop(ctx context.Context, sd sdktrace.ReadOnlySpan) bool {
if !sd.SpanContext().IsSampled() {
// Count the span as unsampled
bsp.counters[SpansUnSampledCounter].Add(ctx, 1)
bsp.spansUnsampledCounter.Add(ctx, 1)
return false
}

// Count the span as received.
bsp.counters[SpansReceivedCounter].Add(ctx, 1)
bsp.spansReceivedCounter.Add(ctx, 1)

// This ensures the bsp.queue<- below does not panic as the
// processor shuts down.
Expand All @@ -423,7 +423,7 @@ func (bsp *batchSpanProcessor) enqueueDrop(ctx context.Context, sd sdktrace.Read
default:
atomic.AddUint32(&bsp.dropped, 1)
// Count the span as dropped.
bsp.counters[SpansDroppedCounter].Add(ctx, 1)
bsp.spansDroppedCounter.Add(ctx, 1)
}
return false
}
Expand Down
33 changes: 12 additions & 21 deletions instrumentation/opentelemetry/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ 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 @@ -15,40 +14,35 @@ import (

// Server HTTP metrics.
const (
meterName = "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
// Pseudo of go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp#RequestCount since a metric is not
// created for that one for some reason.(annotated with hypertrace to avoid a duplicate if otel go ever implement
// their own)
RequestCount = "hypertrace.http.server.request_count" // Incoming request count total
requestCountCounterName = "hypertrace.http.server.request_count" // Incoming request count total
)

type HttpOperationMetricsHandler struct {
operationNameGetter func(*http.Request) string
counters map[string]instrument.Int64Counter
countersMutex sync.RWMutex
requestCountCounter instrument.Int64Counter
}

var _ sdk.HttpOperationMetricsHandler = (*HttpOperationMetricsHandler)(nil)

func NewHttpOperationMetricsHandler(nameGetter func(*http.Request) string) sdk.HttpOperationMetricsHandler {
return &HttpOperationMetricsHandler{
operationNameGetter: nameGetter,
counters: make(map[string]instrument.Int64Counter, 1),
}
}

func (mh *HttpOperationMetricsHandler) CreateRequestCount() {
mp := global.MeterProvider()
meter := mp.Meter("go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp",
metric.WithInstrumentationVersion(otelhttp.SemVersion()))
meter := mp.Meter(meterName, metric.WithInstrumentationVersion(otelhttp.SemVersion()))

requestCountCounter, err := meter.Int64Counter(RequestCount)
// Set up net http metrics
// RequestCount Counter
requestCountCounter, err := meter.Int64Counter(requestCountCounterName)
if err != nil {
otel.Handle(err)
}

mh.countersMutex.Lock()
defer mh.countersMutex.Unlock()
mh.counters[RequestCount] = requestCountCounter
return &HttpOperationMetricsHandler{
operationNameGetter: nameGetter,
requestCountCounter: requestCountCounter,
}
}

func (mh *HttpOperationMetricsHandler) AddToRequestCount(n int64, r *http.Request) {
Expand All @@ -57,8 +51,5 @@ 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...)
mh.requestCountCounter.Add(ctx, n, attributes...)
}
3 changes: 0 additions & 3 deletions sdk/instrumentation/net/http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ func WrapHandler(delegate http.Handler, spanFromContext sdk.SpanFromContext, opt
f = options.Filter
}

// Create request count metric
mh.CreateRequestCount()

return &handler{delegate, defaultAttributes, spanFromContext, internalconfig.GetConfig().GetDataCapture(), f, mh}
}

Expand Down
3 changes: 0 additions & 3 deletions sdk/instrumentation/net/http/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ import (
type metricsHandler struct {
}

func (mh *metricsHandler) CreateRequestCount() {

}
func (mh *metricsHandler) AddToRequestCount(int64, *http.Request) {

}
Expand Down
1 change: 0 additions & 1 deletion sdk/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ import (
)

type HttpOperationMetricsHandler interface {
CreateRequestCount()
AddToRequestCount(int64, *http.Request)
}

0 comments on commit a69e370

Please sign in to comment.