Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a feature to prometheusexporter to export exemplars along with… #9945

Merged
merged 6 commits into from Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -84,6 +84,8 @@
- `jmxreceiver`: Add latest releases of jmx metrics gatherer & wildfly jar to supported jars hash list (#11134)
- `rabbitmqreceiver`: Add integration test for rabbitmq receiver (#10865)
- `transformprocessor`: Allow using trace_state with key-value struct (#11029)
- `prometheusexporter` : Added a feature to prometheusexporter to export exemplars along with histogram metrics.
- `cmd/tracegen`: support add additional resource attributes. (#11145)

### 🧰 Bug fixes 🧰

Expand Down
2 changes: 2 additions & 0 deletions cmd/configschema/go.mod
Expand Up @@ -893,3 +893,5 @@ exclude github.com/StackExchange/wmi v1.2.0

// see https://github.com/distribution/distribution/issues/3590
exclude github.com/docker/distribution v2.8.0+incompatible

replace github.com/prometheus/client_golang => github.com/prometheus/client_golang v1.12.2-0.20220318110013-3bc8f2c651ff
39 changes: 2 additions & 37 deletions cmd/configschema/go.sum

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

3 changes: 3 additions & 0 deletions exporter/prometheusexporter/README.md
Expand Up @@ -23,6 +23,8 @@ The following settings can be optionally configured:
- `metric_expiration` (default = `5m`): defines how long metrics are exposed without updates
- `resource_to_telemetry_conversion`
- `enabled` (default = false): If `enabled` is `true`, all the resource attributes will be converted to metric labels by default.
- `enable_open_metrics`
- `enabled` (default = false): If `enabled` is `true`, metrics will be exported using the OpenMetrics format. Exemplars are only exported in the OpenMetrics format.

Example:

Expand All @@ -36,6 +38,7 @@ exporters:
"another label": spaced value
send_timestamps: true
metric_expiration: 180m
enable_open_metrics: true
resource_to_telemetry_conversion:
enabled: true
```
Expand Down
25 changes: 25 additions & 0 deletions exporter/prometheusexporter/collector.go
Expand Up @@ -209,11 +209,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
83 changes: 83 additions & 0 deletions exporter/prometheusexporter/collector_test.go
Expand Up @@ -96,6 +96,89 @@ 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.SetMExplicitBounds(bounds)
bc := []uint64{2, 35, 70}
hd.SetMBucketCounts(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{}
err := pbMetric.Write(&m)
if err != nil {
return
}

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 @@ -44,6 +44,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"`
mx-psi marked this conversation as resolved.
Show resolved Hide resolved
}

var _ config.Exporter = (*Config)(nil)
Expand Down
9 changes: 5 additions & 4 deletions exporter/prometheusexporter/factory.go
Expand Up @@ -41,10 +41,11 @@ func NewFactory() component.ExporterFactory {

func createDefaultConfig() config.Exporter {
return &Config{
ExporterSettings: config.NewExporterSettings(config.NewComponentID(typeStr)),
ConstLabels: map[string]string{},
SendTimestamps: false,
MetricExpiration: time.Minute * 5,
ExporterSettings: config.NewExporterSettings(config.NewComponentID(typeStr)),
ConstLabels: map[string]string{},
SendTimestamps: false,
MetricExpiration: time.Minute * 5,
EnableOpenMetrics: false,
}
}

Expand Down
2 changes: 2 additions & 0 deletions exporter/prometheusexporter/go.mod
Expand Up @@ -160,3 +160,5 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/resourceto
replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusreceiver => ../../receiver/prometheusreceiver

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/prometheus => ../../pkg/translator/prometheus

replace github.com/prometheus/client_golang => github.com/prometheus/client_golang v1.12.2-0.20220318110013-3bc8f2c651ff