Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] OTEL / Prom metrics benchmark #5676

Draft
wants to merge 23 commits into
base: main
Choose a base branch
from
Draft
Changes from 22 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
108 changes: 108 additions & 0 deletions internal/metrics/benchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Copyright (c) 2024 The Jaeger Authors.
// SPDX-License-Identifier: Apache-2.0

package benchmark_test

import (
"context"
"testing"

promsdk "github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/bridge/opencensus"
"go.opentelemetry.io/otel/metric"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"

promExporter "go.opentelemetry.io/otel/exporters/prometheus"
)

func BenchmarkPrometheusCounter(b *testing.B) {
reg := promsdk.NewRegistry()
opts := promsdk.CounterOpts{
Name: "test_counter",
Help: "help",
}
cv := promsdk.NewCounterVec(opts, []string{"tag1"})
reg.MustRegister(cv)
counter := cv.WithLabelValues("value1")

b.ResetTimer()
for i := 0; i < b.N; i++ {
counter.Add(1)
}
}

func BenchmarkOTELCounter(b *testing.B) {
counter := otelCounter(b)
ctx := context.Background()

b.ResetTimer()
for i := 0; i < b.N; i++ {
counter.Add(ctx, 1)
}
}

func BenchmarkCencusCounter(b *testing.B) {
counter := censusCounter(b)
ctx := context.Background()

b.ResetTimer()
for i := 0; i < b.N; i++ {
counter.Add(ctx, 1)
}
}

func BenchmarkOTELCounterWithLabel(b *testing.B) {
counter := otelCounter(b)
ctx := context.Background()
attrSet := attribute.NewSet(attribute.String("tag1", "value1"))
attrOpt := metric.WithAttributeSet(attrSet)

Comment on lines +82 to +84
Copy link

Choose a reason for hiding this comment

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

Curious how you feel about these two lines, and why you've excluded them from the benchmark? Ergonomically speaking, would you cache the attrOpt value after computing it? If so, you'd be better off with bound instruments. If not, you should measure it. I ask because the introduction of functional options adds a bunch of allocations, so unless you compute and re-use the []Option slice, you've either got an ergonomics problem or a performance problem. We stopped using the OTel-Go API as a result of this and installed a more-efficient functional-option-free bypass. lightstep/otel-launcher-go#446

Copy link
Member Author

Choose a reason for hiding this comment

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

Ergonomically speaking, would you cache the attrOpt value after computing it?

@jmacd yes, that's exactly what Jaeger is doing, we always used "bound" instruments.

type otelCounter struct {
counter metric.Int64Counter
fixedCtx context.Context
option metric.AddOption
}
func (c *otelCounter) Inc(value int64) {
c.counter.Add(c.fixedCtx, value, c.option)
}

But this does not help to completely avoid allocations. It's not the passing of vararg options that's causing the allocations, it's something deeper in the implementation.

b.ResetTimer()
for i := 0; i < b.N; i++ {
counter.Add(ctx, 1, attrOpt)
}
}
func BenchmarkCencusCounterWithLabel(b *testing.B) {
counter := censusCounter(b)
ctx := context.Background()
attrSet := attribute.NewSet(attribute.String("tag1", "value1"))
attrOpt := metric.WithAttributeSet(attrSet)

b.ResetTimer()
for i := 0; i < b.N; i++ {
counter.Add(ctx, 1, attrOpt)
}
}

func otelCounter(b *testing.B) metric.Int64Counter {
registry := promsdk.NewRegistry()
exporter, err := promExporter.New(promExporter.WithRegisterer(registry))
require.NoError(b, err)
meterProvider := sdkmetric.NewMeterProvider(
sdkmetric.WithReader(exporter),
)

meter := meterProvider.Meter("test")
counter, err := meter.Int64Counter("test_counter")
require.NoError(b, err)
return counter
}

func censusCounter(b *testing.B) metric.Int64Counter {
registry := promsdk.NewRegistry()
exporter, err := promExporter.New(
promExporter.WithRegisterer(registry),
promExporter.WithProducer(opencensus.NewMetricProducer()),
)
require.NoError(b, err)
meterProvider := sdkmetric.NewMeterProvider(
sdkmetric.WithReader(exporter),
)

meter := meterProvider.Meter("test")
counter, err := meter.Int64Counter("test_counter")
require.NoError(b, err)
return counter
}
Loading