Skip to content

Commit

Permalink
updated prometheusexporter to use TraceID, SpanID
Browse files Browse the repository at this point in the history
  • Loading branch information
arun-shopify committed Aug 23, 2022
1 parent 2c35644 commit 925b749
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 61 deletions.
16 changes: 10 additions & 6 deletions exporter/prometheusexporter/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,18 +219,22 @@ func (c *collector) convertDoubleHistogram(metric pmetric.Metric, resourceAttrs

arrLen := ip.Exemplars().Len()
exemplars := make([]prometheus.Exemplar, arrLen)

for i := 0; i < arrLen; i++ {
e := ip.Exemplars().At(i)
exemplarLabels := make(prometheus.Labels, 0)

labels := make(prometheus.Labels, e.FilteredAttributes().Len())
e.FilteredAttributes().Range(func(k string, v pcommon.Value) bool {
labels[k] = v.AsString()
return true
})
if !e.TraceID().IsEmpty() {
exemplarLabels["trace_id"] = e.TraceID().HexString()
}

if !e.SpanID().IsEmpty() {
exemplarLabels["span_id"] = e.SpanID().HexString()
}

exemplars[i] = prometheus.Exemplar{
Value: e.DoubleVal(),
Labels: labels,
Labels: exemplarLabels,
Timestamp: e.Timestamp().AsTime(),
}
}
Expand Down
82 changes: 27 additions & 55 deletions exporter/prometheusexporter/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package prometheusexporter

import (
"encoding/hex"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -106,50 +107,35 @@ func TestConvertDoubleHistogramExemplar(t *testing.T) {
metric.SetUnit("T")

// initialize empty datapoint
hd := metric.Histogram().DataPoints().AppendEmpty()
histogramDataPoint := metric.Histogram().DataPoints().AppendEmpty()

bounds := pcommon.NewImmutableFloat64Slice([]float64{5, 25, 90})
hd.SetExplicitBounds(bounds)
histogramDataPoint.SetExplicitBounds(bounds)
bc := pcommon.NewImmutableUInt64Slice([]uint64{2, 35, 70})
hd.SetBucketCounts(bc)
histogramDataPoint.SetBucketCounts(bc)

// add test exemplar values to the metric
promExporterExemplars := histogramDataPoint.Exemplars().AppendEmpty()
var tBytes [16]byte
testTraceID, _ := hex.DecodeString("641d68e314a58152cc2581e7663435d1")
copy(tBytes[:], testTraceID)
traceID := pcommon.NewTraceID(tBytes)
promExporterExemplars.SetTraceID(traceID)

var sBytes [8]byte
testSpanID, _ := hex.DecodeString("7436d6ac76178623")
copy(sBytes[:], testSpanID)
spanID := pcommon.NewSpanID(sBytes)
promExporterExemplars.SetSpanID(spanID)

exemplarTs, _ := time.Parse("unix", "Mon Jan _2 15:04:05 MST 2006")
exemplars := []prometheus.Exemplar{
{
Timestamp: exemplarTs,
Value: 3,
Labels: prometheus.Labels{"test_label_0": "label_value_0"},
},
{
Timestamp: exemplarTs,
Value: 50,
Labels: prometheus.Labels{"test_label_1": "label_value_1"},
},
{
Timestamp: exemplarTs,
Value: 78,
Labels: prometheus.Labels{"test_label_2": "label_value_2"},
},
{
Timestamp: exemplarTs,
Value: 100,
Labels: prometheus.Labels{"test_label_3": "label_value_3"},
},
}

// add each exemplar value to the metric
for _, e := range exemplars {
pde := hd.Exemplars().AppendEmpty()
pde.SetDoubleVal(e.Value)
for k, v := range e.Labels {
pde.FilteredAttributes().InsertString(k, v)
}
pde.SetTimestamp(pcommon.NewTimestampFromTime(e.Timestamp))
}
promExporterExemplars.SetTimestamp(pcommon.NewTimestampFromTime(exemplarTs))
promExporterExemplars.SetDoubleVal(3.0)

pMap := pcommon.NewMap()

c := collector{

accumulator: &mockAccumulator{
metrics: []pmetric.Metric{metric},
resourceAttributes: pMap,
Expand All @@ -166,29 +152,15 @@ func TestConvertDoubleHistogramExemplar(t *testing.T) {

buckets := m.GetHistogram().GetBucket()

require.Equal(t, 4, len(buckets))
require.Equal(t, 3, len(buckets))

require.Equal(t, 3.0, buckets[0].GetExemplar().GetValue())
require.Equal(t, int32(128654848), buckets[0].GetExemplar().GetTimestamp().GetNanos())
require.Equal(t, 1, len(buckets[0].GetExemplar().GetLabel()))
require.Equal(t, "test_label_0", buckets[0].GetExemplar().GetLabel()[0].GetName())
require.Equal(t, "label_value_0", buckets[0].GetExemplar().GetLabel()[0].GetValue())

require.Equal(t, 0.0, buckets[1].GetExemplar().GetValue())
require.Equal(t, int32(0), buckets[1].GetExemplar().GetTimestamp().GetNanos())
require.Equal(t, 0, len(buckets[1].GetExemplar().GetLabel()))

require.Equal(t, 78.0, buckets[2].GetExemplar().GetValue())
require.Equal(t, int32(128654848), buckets[2].GetExemplar().GetTimestamp().GetNanos())
require.Equal(t, 1, len(buckets[2].GetExemplar().GetLabel()))
require.Equal(t, "test_label_2", buckets[2].GetExemplar().GetLabel()[0].GetName())
require.Equal(t, "label_value_2", buckets[2].GetExemplar().GetLabel()[0].GetValue())

require.Equal(t, 100.0, buckets[3].GetExemplar().GetValue())
require.Equal(t, int32(128654848), buckets[3].GetExemplar().GetTimestamp().GetNanos())
require.Equal(t, 1, len(buckets[3].GetExemplar().GetLabel()))
require.Equal(t, "test_label_3", buckets[3].GetExemplar().GetLabel()[0].GetName())
require.Equal(t, "label_value_3", buckets[3].GetExemplar().GetLabel()[0].GetValue())
require.Equal(t, 2, len(buckets[0].GetExemplar().GetLabel()))
require.Equal(t, "trace_id", buckets[0].GetExemplar().GetLabel()[0].GetName())
require.Equal(t, "641d68e314a58152cc2581e7663435d1", buckets[0].GetExemplar().GetLabel()[0].GetValue())
require.Equal(t, "span_id", buckets[0].GetExemplar().GetLabel()[1].GetName())
require.Equal(t, "7436d6ac76178623", buckets[0].GetExemplar().GetLabel()[1].GetValue())
}

// errorCheckCore keeps track of logged errors
Expand Down

0 comments on commit 925b749

Please sign in to comment.