Skip to content

Commit

Permalink
Make conversion of histogram and summary compatible with prometheus (#46
Browse files Browse the repository at this point in the history
)

Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu authored May 26, 2022
1 parent 1bd2247 commit 12a0a29
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
10 changes: 6 additions & 4 deletions protocol/otlp/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
var (
// Some standard dimension keys.
// upper bound dimension key for histogram buckets.
upperBoundDimensionKey = "upper_bound"
upperBoundDimensionKey = "le"

// infinity bound dimension value is used on all histograms.
infinityBoundSFxDimValue = float64ToDimValue(math.Inf(1))
Expand Down Expand Up @@ -219,7 +219,7 @@ func convertHistogram(histDPs []*metricsv1.HistogramDataPoint, typ datapoint.Met
i++

sumPt := &out[i]
sumPt.Name = name
sumPt.Name = name + "_sum"
sumPt.Type = typ
sum := histDP.GetSum()
sumPt.DP.Attributes = stringAttrs
Expand All @@ -236,6 +236,7 @@ func convertHistogram(histDPs []*metricsv1.HistogramDataPoint, typ datapoint.Met
continue
}

var le int64
for j, c := range counts {
bound := infinityBoundSFxDimValue
if j < len(bounds) {
Expand All @@ -254,7 +255,8 @@ func convertHistogram(histDPs []*metricsv1.HistogramDataPoint, typ datapoint.Met
},
})
dp.DP.TimeUnixNano = histDP.GetTimeUnixNano()
dp.DP.Value = &metricsv1.NumberDataPoint_AsInt{AsInt: int64(c)}
le += int64(c)
dp.DP.Value = &metricsv1.NumberDataPoint_AsInt{AsInt: le}
i++
}
}
Expand Down Expand Up @@ -289,7 +291,7 @@ func convertSummaryDataPoints(
i++

sumPt := &out[i]
sumPt.Name = name
sumPt.Name = name + "_sum"
sumPt.Type = datapoint.Counter
sumPt.DP.Attributes = stringAttrs
sumPt.DP.TimeUnixNano = inDp.GetTimeUnixNano()
Expand Down
23 changes: 12 additions & 11 deletions protocol/otlp/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,11 +528,11 @@ func Test_FromMetrics(t *testing.T) {
expectedFromHistogram("double_delta_histo", labelMap, doubleHistDP, true),
[]*datapoint.Datapoint{
int64SFxDataPoint("double_histo_bad_counts_count", datapoint.Counter, labelMap, int64(doubleHistDP.Count)),
doubleSFxDataPoint("double_histo_bad_counts", datapoint.Counter, labelMap, *doubleHistDP.Sum),
doubleSFxDataPoint("double_histo_bad_counts_sum", datapoint.Counter, labelMap, *doubleHistDP.Sum),
},
[]*datapoint.Datapoint{
int64SFxDataPoint("int_histo_bad_counts_count", datapoint.Counter, labelMap, int64(intHistDP.Count)),
doubleSFxDataPoint("int_histo_bad_counts", datapoint.Counter, labelMap, *intHistDP.Sum),
doubleSFxDataPoint("int_histo_bad_counts_sum", datapoint.Counter, labelMap, *intHistDP.Sum),
},
),
},
Expand Down Expand Up @@ -776,27 +776,29 @@ func expectedFromHistogram(

dps = append(dps,
int64SFxDataPoint(metricName+"_count", typ, dims, int64(histDP.GetCount())),
doubleSFxDataPoint(metricName, typ, dims, histDP.GetSum()))
doubleSFxDataPoint(metricName+"_sum", typ, dims, histDP.GetSum()))

explicitBounds := histDP.GetExplicitBounds()
if explicitBounds == nil {
return dps
}
var le int64
for i := 0; i < len(explicitBounds); i++ {
dimsCopy := cloneStringMap(dims)
dimsCopy[upperBoundDimensionKey] = float64ToDimValue(explicitBounds[i])
dps = append(dps, int64SFxDataPoint(metricName+"_bucket", typ, dimsCopy, int64(buckets[i])))
le += int64(buckets[i])
dps = append(dps, int64SFxDataPoint(metricName+"_bucket", typ, dimsCopy, le))
}
dimsCopy := cloneStringMap(dims)
dimsCopy[upperBoundDimensionKey] = float64ToDimValue(math.Inf(1))
dps = append(dps, int64SFxDataPoint(metricName+"_bucket", typ, dimsCopy, int64(buckets[len(buckets)-1])))
le += int64(buckets[len(buckets)-1])
dps = append(dps, int64SFxDataPoint(metricName+"_bucket", typ, dimsCopy, le))
return dps
}

func expectedFromSummary(name string, labelMap map[string]string, count int64, sumVal float64) []*datapoint.Datapoint {
countName := name + "_count"
countPt := int64SFxDataPoint(countName, datapoint.Counter, labelMap, count)
sumPt := doubleSFxDataPoint(name, datapoint.Counter, labelMap, sumVal)
countPt := int64SFxDataPoint(name+"_count", datapoint.Counter, labelMap, count)
sumPt := doubleSFxDataPoint(name+"_sum", datapoint.Counter, labelMap, sumVal)
out := []*datapoint.Datapoint{countPt, sumPt}
quantileDimVals := []string{"0.25", "0.5", "0.75", "1"}
for i := 0; i < 4; i++ {
Expand All @@ -813,9 +815,8 @@ func expectedFromSummary(name string, labelMap map[string]string, count int64, s
}

func expectedFromEmptySummary(name string, labelMap map[string]string, count int64, sumVal float64) []*datapoint.Datapoint {
countName := name + "_count"
countPt := int64SFxDataPoint(countName, datapoint.Counter, labelMap, count)
sumPt := doubleSFxDataPoint(name, datapoint.Counter, labelMap, sumVal)
countPt := int64SFxDataPoint(name+"_count", datapoint.Counter, labelMap, count)
sumPt := doubleSFxDataPoint(name+"_sum", datapoint.Counter, labelMap, sumVal)
return []*datapoint.Datapoint{countPt, sumPt}
}

Expand Down

0 comments on commit 12a0a29

Please sign in to comment.