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

Prometheus bridge: Add tests to verify that start timestamps are working correctly #4704

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion bridges/prometheus/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
//
// Limitations:
// - Summary metrics are dropped by the bridge.
// - Start times for histograms and counters are set to the process start time.
// - Prometheus histograms are translated to OpenTelemetry fixed-bucket
// histograms, rather than exponential histograms.
//
Expand Down
86 changes: 86 additions & 0 deletions bridges/prometheus/producer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package prometheus // import "go.opentelemetry.io/contrib/bridges/prometheus"
import (
"context"
"testing"
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -251,3 +252,88 @@ func TestProduce(t *testing.T) {
})
}
}

func TestProduceForStartTime(t *testing.T) {
testCases := []struct {
name string
testFn func(*prometheus.Registry)
startTimeFn func(metricdata.Aggregation) []time.Time
}{
{
name: "counter",
testFn: func(reg *prometheus.Registry) {
metric := prometheus.NewCounter(prometheus.CounterOpts{
Name: "test_counter_metric",
Help: "A counter metric for testing",
ConstLabels: prometheus.Labels(map[string]string{
"foo": "bar",
}),
})
reg.MustRegister(metric)
metric.(prometheus.ExemplarAdder).AddWithExemplar(
245.3, prometheus.Labels{
"trace_id": traceIDStr,
"span_id": spanIDStr,
"other_attribute": "abcd",
},
)
},
startTimeFn: func(aggr metricdata.Aggregation) []time.Time {
dps := aggr.(metricdata.Sum[float64]).DataPoints
sts := make([]time.Time, len(dps))
for i, dp := range dps {
sts[i] = dp.StartTime
}
return sts
},
},
{
name: "histogram",
testFn: func(reg *prometheus.Registry) {
metric := prometheus.NewHistogram(prometheus.HistogramOpts{
Name: "test_histogram_metric",
Help: "A histogram metric for testing",
ConstLabels: prometheus.Labels(map[string]string{
"foo": "bar",
}),
})
reg.MustRegister(metric)
metric.(prometheus.ExemplarObserver).ObserveWithExemplar(
578.3, prometheus.Labels{
"trace_id": traceIDStr,
"span_id": spanIDStr,
"other_attribute": "efgh",
},
)
},
startTimeFn: func(aggr metricdata.Aggregation) []time.Time {
dps := aggr.(metricdata.Histogram[float64]).DataPoints
sts := make([]time.Time, len(dps))
for i, dp := range dps {
sts[i] = dp.StartTime
}
return sts
},
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
reg := prometheus.NewRegistry()
tt.testFn(reg)
p := NewMetricProducer(WithGatherer(reg))
output, err := p.Produce(context.Background())
assert.NoError(t, err)
assert.NotEmpty(t, output)
for _, sms := range output {
dashpole marked this conversation as resolved.
Show resolved Hide resolved
assert.NotEmpty(t, sms.Metrics)
for _, ms := range sms.Metrics {
sts := tt.startTimeFn(ms.Data)
assert.NotEmpty(t, sts)
for _, st := range sts {
assert.True(t, st.After(processStartTime))
}
}
}
})
}
}