Skip to content

Commit

Permalink
Adding a feature to prometheusexporter to export exemplars along with…
Browse files Browse the repository at this point in the history
… histogram metrics
  • Loading branch information
arun-shopify committed May 18, 2022
1 parent 76d58ec commit 3fc352c
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 5 deletions.
27 changes: 27 additions & 0 deletions exporter/prometheusexporter/collector.go
Expand Up @@ -193,8 +193,10 @@ func (c *collector) convertDoubleHistogram(metric pmetric.Metric, resourceAttrs
desc, attributes := c.getMetricMetadata(metric, ip.Attributes(), resourceAttrs)

indicesMap := make(map[float64]int)

buckets := make([]float64, 0, len(ip.MBucketCounts()))
for index, bucket := range ip.MExplicitBounds() {

if _, added := indicesMap[bucket]; !added {
indicesMap[bucket] = index
buckets = append(buckets, bucket)
Expand All @@ -215,11 +217,36 @@ func (c *collector) convertDoubleHistogram(metric pmetric.Metric, resourceAttrs
points[bucket] = cumCount
}

arrLen := ip.Exemplars().Len()
exemplars := make([]prometheus.Exemplar, arrLen)
for i := 0; i < arrLen; i++ {
e := ip.Exemplars().At(i)

labels := make(prometheus.Labels, e.FilteredAttributes().Len())
e.FilteredAttributes().Range(func(k string, v pcommon.Value) bool {
labels[k] = v.AsString()
return true
})

exemplars[i] = prometheus.Exemplar{
Value: e.DoubleVal(),
Labels: labels,
Timestamp: e.Timestamp().AsTime(),
}
}

m, err := prometheus.NewConstHistogram(desc, ip.Count(), ip.Sum(), points, attributes...)
if err != nil {
return nil, err
}

if arrLen > 0 {
m, err = prometheus.NewMetricWithExemplars(m, exemplars...)
if err != nil {
return nil, err
}
}

if c.sendTimestamps {
return prometheus.NewMetricWithTimestamp(ip.Timestamp().AsTime(), m), nil
}
Expand Down
80 changes: 80 additions & 0 deletions exporter/prometheusexporter/collector_test.go
Expand Up @@ -96,6 +96,86 @@ func TestConvertInvalidMetric(t *testing.T) {
}
}

func TestConvertDoubleHistogramExemplar(t *testing.T) {
// initialize empty histogram
metric := pmetric.NewMetric()
metric.SetDataType(pmetric.MetricDataTypeHistogram)
metric.SetName("test_metric")
metric.SetDescription("this is test metric")
metric.SetUnit("T")

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

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

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"},
},
}

// 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))
}

pMap := pcommon.NewMap()

c := collector{
accumulator: &mockAccumulator{
metrics: []pmetric.Metric{metric},
resourceAttributes: pMap,
},
logger: zap.NewNop(),
}

pbMetric, _ := c.convertDoubleHistogram(metric, pMap)
m := io_prometheus_client.Metric{}
pbMetric.Write(&m)

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

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())
}

// errorCheckCore keeps track of logged errors
type errorCheckCore struct {
errorMessages []string
Expand Down
3 changes: 3 additions & 0 deletions exporter/prometheusexporter/config.go
Expand Up @@ -46,6 +46,9 @@ type Config struct {
// ResourceToTelemetrySettings defines configuration for converting resource attributes to metric labels.
ResourceToTelemetrySettings resourcetotelemetry.Settings `mapstructure:"resource_to_telemetry_conversion"`

// EnableOpenMetrics enables the use of the OpenMetrics encoding option for the prometheus exporter.
EnableOpenMetrics bool `mapstructure:"enable_open_metrics"`

// skipSanitizeLabel if enabled, labels that start with _ are not sanitized
skipSanitizeLabel bool
}
Expand Down
1 change: 1 addition & 0 deletions exporter/prometheusexporter/factory.go
Expand Up @@ -47,6 +47,7 @@ func createDefaultConfig() config.Exporter {
SendTimestamps: false,
MetricExpiration: time.Minute * 5,
skipSanitizeLabel: featuregate.GetRegistry().IsEnabled(dropSanitizationGate.ID),
EnableOpenMetrics: false,
}
}

Expand Down
2 changes: 1 addition & 1 deletion exporter/prometheusexporter/go.mod
Expand Up @@ -6,7 +6,7 @@ require (
github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.51.0
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/resourcetotelemetry v0.51.0
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusreceiver v0.51.0
github.com/prometheus/client_golang v1.12.2
github.com/prometheus/client_golang v1.12.2-0.20220318110013-3bc8f2c651ff
github.com/prometheus/client_model v0.2.0
github.com/prometheus/common v0.34.0
github.com/prometheus/prometheus v0.35.1-0.20220503184552-2381d7be5731
Expand Down
4 changes: 2 additions & 2 deletions exporter/prometheusexporter/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions exporter/prometheusexporter/prometheus.go
Expand Up @@ -56,8 +56,9 @@ func newPrometheusExporter(config *Config, set component.ExporterCreateSettings)
handler: promhttp.HandlerFor(
registry,
promhttp.HandlerOpts{
ErrorHandling: promhttp.ContinueOnError,
ErrorLog: newPromLogger(set.Logger),
ErrorHandling: promhttp.ContinueOnError,
ErrorLog: newPromLogger(set.Logger),
EnableOpenMetrics: config.EnableOpenMetrics,
},
),
}, nil
Expand Down

0 comments on commit 3fc352c

Please sign in to comment.