Skip to content

Commit

Permalink
Prometheus bridge: Add test for verify that start timestamps are work…
Browse files Browse the repository at this point in the history
…ing correctly
  • Loading branch information
hiroyaonoe committed Jan 13, 2024
1 parent 3cecdcf commit 849e455
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
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
83 changes: 83 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,85 @@ 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)
for _, sms := range output {
for _, ms := range sms.Metrics {
sts := tt.startTimeFn(ms.Data)
for _, st := range sts {
assert.True(t, st.After(processStartTime))
}
}
}
})
}
}

0 comments on commit 849e455

Please sign in to comment.