Skip to content

Commit

Permalink
[exporter/elasticsearch] Add summary support for metrics (#34560)
Browse files Browse the repository at this point in the history
Add summary support for metrics

Added exporter test

Fixes #34561

---------

Co-authored-by: Sean Marciniak <30928402+MovieStoreGuy@users.noreply.github.com>
  • Loading branch information
carsonip and MovieStoreGuy authored Aug 12, 2024
1 parent 9fe842f commit dd0aed1
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .chloggen/elasticsearchexporter_summary-support.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: elasticsearchexporter

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add summary support for metrics

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [34560]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
10 changes: 10 additions & 0 deletions exporter/elasticsearchexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,16 @@ func (e *elasticsearchExporter) pushMetricsData(
continue
}
}
case pmetric.MetricTypeSummary:
dps := metric.Summary().DataPoints()
for l := 0; l < dps.Len(); l++ {
dp := dps.At(l)
val := summaryToValue(dp)
if err := upsertDataPoint(dp, val); err != nil {
errs = append(errs, err)
continue
}
}
}
}
}
Expand Down
44 changes: 44 additions & 0 deletions exporter/elasticsearchexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,50 @@ func TestExporterMetrics(t *testing.T) {

assertItemsEqual(t, expected, rec.Items(), false)
})

t.Run("publish summary", func(t *testing.T) {
rec := newBulkRecorder()
server := newESTestServer(t, func(docs []itemRequest) ([]itemResponse, error) {
rec.Record(docs)
return itemsAllOK(docs)
})

exporter := newTestMetricsExporter(t, server.URL, func(cfg *Config) {
cfg.Mapping.Mode = "ecs"
})

metrics := pmetric.NewMetrics()
resourceMetrics := metrics.ResourceMetrics().AppendEmpty()
scopeA := resourceMetrics.ScopeMetrics().AppendEmpty()
metricSlice := scopeA.Metrics()
fooMetric := metricSlice.AppendEmpty()
fooMetric.SetName("metric.foo")
fooDps := fooMetric.SetEmptySummary().DataPoints()
fooDp := fooDps.AppendEmpty()
fooDp.SetSum(1.5)
fooDp.SetCount(1)
fooOtherDp := fooDps.AppendEmpty()
fooOtherDp.SetTimestamp(pcommon.NewTimestampFromTime(time.Unix(3600, 0)))
fooOtherDp.SetSum(2)
fooOtherDp.SetCount(3)

mustSendMetrics(t, exporter, metrics)

rec.WaitItems(2)

expected := []itemRequest{
{
Action: []byte(`{"create":{"_index":"metrics-generic-default"}}`),
Document: []byte(`{"@timestamp":"1970-01-01T00:00:00.000000000Z","data_stream":{"dataset":"generic","namespace":"default","type":"metrics"},"metric":{"foo":{"sum":1.5,"value_count":1}}}`),
},
{
Action: []byte(`{"create":{"_index":"metrics-generic-default"}}`),
Document: []byte(`{"@timestamp":"1970-01-01T01:00:00.000000000Z","data_stream":{"dataset":"generic","namespace":"default","type":"metrics"},"metric":{"foo":{"sum":2,"value_count":3}}}`),
},
}

assertItemsEqual(t, expected, rec.Items(), false)
})
}

func TestExporterTraces(t *testing.T) {
Expand Down
10 changes: 10 additions & 0 deletions exporter/elasticsearchexporter/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,16 @@ func (m *encodeModel) upsertMetricDataPointValue(documents map[uint32]objmodel.D
return nil
}

func summaryToValue(dp pmetric.SummaryDataPoint) pcommon.Value {
// TODO: Add support for quantiles
// https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/34561
vm := pcommon.NewValueMap()
m := vm.Map()
m.PutDouble("sum", dp.Sum())
m.PutInt("value_count", int64(dp.Count()))
return vm
}

func histogramToValue(dp pmetric.HistogramDataPoint) (pcommon.Value, error) {
// Histogram conversion function is from
// https://github.com/elastic/apm-data/blob/3b28495c3cbdc0902983134276eb114231730249/input/otlp/metrics.go#L277
Expand Down

0 comments on commit dd0aed1

Please sign in to comment.