Skip to content

Commit

Permalink
avoid allocations
Browse files Browse the repository at this point in the history
tested via
```
$ go test -benchmem -benchtime=5s -bench=Benchmark ./internal/metrics/
```

before:
```
BenchmarkPrometheusCounter-10           856818336                6.875 ns/op           0 B/op          0 allocs/op
BenchmarkOTELCounter-10                 146044255               40.92 ns/op           32 B/op          2 allocs/op
```

after:
``
BenchmarkPrometheusCounter-10           855046669                6.924 ns/op           0 B/op          0 allocs/op
BenchmarkOTELCounter-10                 293330721               21.05 ns/op           16 B/op          1 allocs/op
```

Signed-off-by: Yuri Shkuro <[email protected]>
  • Loading branch information
yurishkuro committed Jun 22, 2024
1 parent f508b8a commit 4a99291
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
11 changes: 8 additions & 3 deletions internal/metrics/otelmetrics/factory.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package otelmetrics

import (
"github.com/jaegertracing/jaeger/pkg/metrics"
"context"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"

"github.com/jaegertracing/jaeger/pkg/metrics"
)

type otelFactory struct{}
Expand All @@ -26,8 +30,9 @@ func (f *otelFactory) Counter(opts metrics.Options) metrics.Counter {
attributeSet := attribute.NewSet(attributes...)

return &otelCounter{
counter: counter,
attributeSet: attributeSet,
counter: counter,
fixedCtx: context.Background(),
option: metric.WithAttributeSet(attributeSet),
}
}

Expand Down
8 changes: 4 additions & 4 deletions internal/metrics/otelmetrics/otelCounter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package otelmetrics
import (
"context"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
)

type otelCounter struct {
counter metric.Int64Counter
attributeSet attribute.Set
counter metric.Int64Counter
fixedCtx context.Context
option metric.AddOption
}

func (c *otelCounter) Inc(value int64) {
c.counter.Add(context.Background(), value, metric.WithAttributeSet(c.attributeSet))
c.counter.Add(c.fixedCtx, value, c.option)
}

0 comments on commit 4a99291

Please sign in to comment.