Skip to content

Commit

Permalink
fix bug where an empty exemplar was added to counters
Browse files Browse the repository at this point in the history
  • Loading branch information
dashpole committed Apr 15, 2024
1 parent 2faced4 commit ae18928
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Fixed

- Update all dependencies to address [GO-2024-2687]. (#5359)
- Fix bug where an empty exemplar was added to counters in `go.opentelemetry.io/contrib/bridges/prometheus`. (#TODO)

### Removed

Expand Down
8 changes: 5 additions & 3 deletions bridges/prometheus/producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ func convertCounter(metrics []*dto.Metric, now time.Time) metricdata.Sum[float64
StartTime: processStartTime,
Time: now,
Value: m.GetCounter().GetValue(),
Exemplars: []metricdata.Exemplar[float64]{convertExemplar(m.GetCounter().GetExemplar())},
}
if ex := m.GetCounter().GetExemplar(); ex != nil {
dp.Exemplars = []metricdata.Exemplar[float64]{convertExemplar(ex)}
}
createdTs := m.GetCounter().GetCreatedTimestamp()
if createdTs.IsValid() {
Expand Down Expand Up @@ -290,8 +292,8 @@ func convertBuckets(buckets []*dto.Bucket) ([]float64, []uint64, []metricdata.Ex
bounds[i] = bucket.GetUpperBound()
}
bucketCounts[i] = bucket.GetCumulativeCount()
if bucket.GetExemplar() != nil {
exemplars = append(exemplars, convertExemplar(bucket.GetExemplar()))
if ex := bucket.GetExemplar(); ex != nil {
exemplars = append(exemplars, convertExemplar(ex))
}
}
return bounds, bucketCounts, exemplars
Expand Down
76 changes: 75 additions & 1 deletion bridges/prometheus/producer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,41 @@ func TestProduce(t *testing.T) {
},
{
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.Add(245.3)
},
expected: []metricdata.ScopeMetrics{{
Scope: instrumentation.Scope{
Name: scopeName,
},
Metrics: []metricdata.Metrics{
{
Name: "test_counter_metric",
Description: "A counter metric for testing",
Data: metricdata.Sum[float64]{
Temporality: metricdata.CumulativeTemporality,
IsMonotonic: true,
DataPoints: []metricdata.DataPoint[float64]{
{
Attributes: attribute.NewSet(attribute.String("foo", "bar")),
Value: 245.3,
},
},
},
},
},
}},
},
{
name: "counter with exemplar",
testFn: func(reg *prometheus.Registry) {
metric := prometheus.NewCounter(prometheus.CounterOpts{
Name: "test_counter_metric",
Expand Down Expand Up @@ -158,6 +193,45 @@ func TestProduce(t *testing.T) {
},
{
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.Observe(578.3)
},
expected: []metricdata.ScopeMetrics{{
Scope: instrumentation.Scope{
Name: scopeName,
},
Metrics: []metricdata.Metrics{
{
Name: "test_histogram_metric",
Description: "A histogram metric for testing",
Data: metricdata.Histogram[float64]{
Temporality: metricdata.CumulativeTemporality,
DataPoints: []metricdata.HistogramDataPoint[float64]{
{
Count: 1,
Sum: 578.3,
// TODO: This appears to be missing the last bucket
Bounds: []float64{0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5},
BucketCounts: []uint64{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
Attributes: attribute.NewSet(attribute.String("foo", "bar")),
Exemplars: []metricdata.Exemplar[float64]{},
},
},
},
},
},
}},
},
{
name: "histogram with exemplar",
testFn: func(reg *prometheus.Registry) {
metric := prometheus.NewHistogram(prometheus.HistogramOpts{
Name: "test_histogram_metric",
Expand Down Expand Up @@ -189,7 +263,7 @@ func TestProduce(t *testing.T) {
{
Count: 1,
Sum: 578.3,
Bounds: []float64{0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10},
Bounds: prometheus.DefBuckets,
BucketCounts: []uint64{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
Attributes: attribute.NewSet(attribute.String("foo", "bar")),
Exemplars: []metricdata.Exemplar[float64]{
Expand Down

0 comments on commit ae18928

Please sign in to comment.