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

[exporter/elasticsearch] handle ecs mode mapping #31553

Merged
merged 20 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .chloggen/feat_ecs-format.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
j-kap-t marked this conversation as resolved.
Show resolved Hide resolved

# 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: Initial pass in implementing the `ecs` mapping mode
andrzej-stencel marked this conversation as resolved.
Show resolved Hide resolved

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [29742]
j-kap-t marked this conversation as resolved.
Show resolved Hide resolved

# (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: []
4 changes: 2 additions & 2 deletions exporter/elasticsearchexporter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ This exporter supports sending OpenTelemetry logs to [Elasticsearch](https://www
[index](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices.html)
or [datastream](https://www.elastic.co/guide/en/elasticsearch/reference/current/data-streams.html)
name to publish events to. The default value is `logs-generic-default`
- `logs_dynamic_index` (optional):
- `logs_dynamic_index` (optional):
takes resource or log record attribute named `elasticsearch.index.prefix` and `elasticsearch.index.suffix`
resulting dynamically prefixed / suffixed indexing based on `logs_index`. (priority: resource attribute > log record attribute)
- `enabled`(default=false): Enable/Disable dynamic index for log records
Expand Down Expand Up @@ -63,7 +63,7 @@ This exporter supports sending OpenTelemetry logs to [Elasticsearch](https://www
- `max_interval` (default=1m): Max waiting time if a HTTP request failed.
- `mapping`: Events are encoded to JSON. The `mapping` allows users to
configure additional mapping rules.
- `mode` (default=ecs): The fields naming mode. valid modes are:
- `mode` (default=none): The fields naming mode. valid modes are:
- `none`: Use original fields and event structure from the OTLP event.
- `ecs`: Try to map fields defined in the
[OpenTelemetry Semantic Conventions](https://github.com/open-telemetry/semantic-conventions)
Expand Down
15 changes: 12 additions & 3 deletions exporter/elasticsearchexporter/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,21 @@ type attrGetter interface {
Attributes() pcommon.Map
}

// retrieve attribute out of resource and record (span or log, if not found in resource)
func getFromBothResourceAndAttribute(name string, resource attrGetter, record attrGetter) string {
// retrieve attribute out of resource, scope, and record (span or log, if not found in resource)
func getFromAttributes(name string, resource, scope, record attrGetter) string {
var str string
val, exist := resource.Attributes().Get(name)
if !exist {
val, exist = record.Attributes().Get(name)
val, exist = scope.Attributes().Get(name)
if !exist {
val, exist = record.Attributes().Get(name)
if exist {
str = val.AsString()
}
}
if exist {
str = val.AsString()
}
}
if exist {
str = val.AsString()
Expand Down
6 changes: 3 additions & 3 deletions exporter/elasticsearchexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestLoad_DeprecatedIndexConfigOption(t *testing.T) {
MaxInterval: 1 * time.Minute,
},
Mapping: MappingsSettings{
Mode: "ecs",
Mode: "none",
Dedup: true,
Dedot: true,
},
Expand Down Expand Up @@ -138,7 +138,7 @@ func TestLoadConfig(t *testing.T) {
MaxInterval: 1 * time.Minute,
},
Mapping: MappingsSettings{
Mode: "ecs",
Mode: "none",
Dedup: true,
Dedot: true,
},
Expand Down Expand Up @@ -188,7 +188,7 @@ func TestLoadConfig(t *testing.T) {
MaxInterval: 1 * time.Minute,
},
Mapping: MappingsSettings{
Mode: "ecs",
Mode: "none",
Dedup: true,
Dedot: true,
},
Expand Down
2 changes: 1 addition & 1 deletion exporter/elasticsearchexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func createDefaultConfig() component.Config {
MaxInterval: 1 * time.Minute,
},
Mapping: MappingsSettings{
Mode: "ecs",
Mode: "none",
Dedup: true,
Dedot: true,
},
Expand Down
9 changes: 5 additions & 4 deletions exporter/elasticsearchexporter/logs_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ func (e *elasticsearchLogsExporter) pushLogsData(ctx context.Context, ld plog.Lo
resource := rl.Resource()
ills := rl.ScopeLogs()
for j := 0; j < ills.Len(); j++ {
scope := ills.At(j).Scope()
logs := ills.At(j).LogRecords()
ill := ills.At(j)
scope := ill.Scope()
logs := ill.LogRecords()
for k := 0; k < logs.Len(); k++ {
if err := e.pushLogRecord(ctx, resource, logs.At(k), scope); err != nil {
if cerr := ctx.Err(); cerr != nil {
Expand All @@ -110,8 +111,8 @@ func (e *elasticsearchLogsExporter) pushLogsData(ctx context.Context, ld plog.Lo
func (e *elasticsearchLogsExporter) pushLogRecord(ctx context.Context, resource pcommon.Resource, record plog.LogRecord, scope pcommon.InstrumentationScope) error {
fIndex := e.index
if e.dynamicIndex {
prefix := getFromBothResourceAndAttribute(indexPrefix, resource, record)
suffix := getFromBothResourceAndAttribute(indexSuffix, resource, record)
prefix := getFromAttributes(indexPrefix, resource, scope, record)
suffix := getFromAttributes(indexSuffix, resource, scope, record)

fIndex = fmt.Sprintf("%s%s%s", prefix, fIndex, suffix)
}
Expand Down
54 changes: 45 additions & 9 deletions exporter/elasticsearchexporter/logs_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func TestExporter_New(t *testing.T) {
cfg.Mapping.Dedot = false
cfg.Mapping.Dedup = true
}),
want: successWithInternalModel(&encodeModel{dedot: false, dedup: true, mode: MappingECS}),
want: successWithInternalModel(&encodeModel{dedot: false, dedup: true, mode: MappingNone}),
},
}

Expand Down Expand Up @@ -153,6 +153,7 @@ func TestExporter_PushEvent(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("skipping test on Windows, see https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/10178")
}

t.Run("publish with success", func(t *testing.T) {
rec := newBulkRecorder()
server := newESTestServer(t, func(docs []itemRequest) ([]itemResponse, error) {
Expand All @@ -167,6 +168,40 @@ func TestExporter_PushEvent(t *testing.T) {
rec.WaitItems(2)
})

t.Run("publish with ecs encoding", func(t *testing.T) {
rec := newBulkRecorder()
server := newESTestServer(t, func(docs []itemRequest) ([]itemResponse, error) {
rec.Record(docs)

expected := `{"@timestamp":"1970-01-01T00:00:00.000000000Z","application":"myapp","attrKey1":"abc","attrKey2":"def","error":{"stack_trace":"no no no no"},"message":"hello world","service":{"name":"myservice"}}`
actual := string(docs[0].Document)
assert.Equal(t, expected, actual)

return itemsAllOK(docs)
})

testConfig := withTestExporterConfig(func(cfg *Config) {
cfg.Mapping.Mode = "ecs"
})(server.URL)
exporter := newTestExporter(t, server.URL, func(cfg *Config) { *cfg = *testConfig })
mustSendLogsWithAttributes(t, exporter,
// resource attrs
j-kap-t marked this conversation as resolved.
Show resolved Hide resolved
map[string]string{
"application": "myapp",
"service.name": "myservice",
},
// record attrs
j-kap-t marked this conversation as resolved.
Show resolved Hide resolved
map[string]string{
"attrKey1": "abc",
"attrKey2": "def",
"exception.stacktrace": "no no no no",
},
// record body
"hello world",
)
rec.WaitItems(1)
})

t.Run("publish with dynamic index", func(t *testing.T) {

rec := newBulkRecorder()
Expand Down Expand Up @@ -206,6 +241,7 @@ func TestExporter_PushEvent(t *testing.T) {
map[string]string{
indexPrefix: prefix,
},
"hello world",
)

rec.WaitItems(1)
Expand Down Expand Up @@ -237,7 +273,7 @@ func TestExporter_PushEvent(t *testing.T) {
defaultCfg = *cfg
})

mustSendLogsWithAttributes(t, exporter, nil, nil)
mustSendLogsWithAttributes(t, exporter, nil, nil, "")

rec.WaitItems(1)
})
Expand Down Expand Up @@ -281,6 +317,7 @@ func TestExporter_PushEvent(t *testing.T) {
map[string]string{
indexPrefix: prefix,
},
"",
)
rec.WaitItems(1)
})
Expand Down Expand Up @@ -476,14 +513,13 @@ func mustSend(t *testing.T, exporter *elasticsearchLogsExporter, contents string
}

// send trace with span & resource attributes
func mustSendLogsWithAttributes(t *testing.T, exporter *elasticsearchLogsExporter, attrMp map[string]string, resMp map[string]string) {
func mustSendLogsWithAttributes(t *testing.T, exporter *elasticsearchLogsExporter, attrMp map[string]string, resMp map[string]string, body string) {
logs := newLogsWithAttributeAndResourceMap(attrMp, resMp)
resLogs := logs.ResourceLogs().At(0)
logRecords := resLogs.ScopeLogs().At(0).LogRecords().At(0)

scopeLogs := resLogs.ScopeLogs().AppendEmpty()
scope := scopeLogs.Scope()
resSpans := logs.ResourceLogs().At(0)
scopeLog := resSpans.ScopeLogs().At(0)
logRecords := scopeLog.LogRecords().At(0)
logRecords.Body().SetStr(body)

err := exporter.pushLogRecord(context.TODO(), resLogs.Resource(), logRecords, scope)
err := exporter.pushLogRecord(context.TODO(), resSpans.Resource(), logRecords, scopeLog.Scope())
require.NoError(t, err)
}
69 changes: 59 additions & 10 deletions exporter/elasticsearchexporter/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,65 @@ const (

func (m *encodeModel) encodeLog(resource pcommon.Resource, record plog.LogRecord, scope pcommon.InstrumentationScope) ([]byte, error) {
var document objmodel.Document
document.AddTimestamp("@timestamp", record.Timestamp()) // We use @timestamp in order to ensure that we can index if the default data stream logs template is used.
document.AddTraceID("TraceId", record.TraceID())
document.AddSpanID("SpanId", record.SpanID())
document.AddInt("TraceFlags", int64(record.Flags()))
document.AddString("SeverityText", record.SeverityText())
document.AddInt("SeverityNumber", int64(record.SeverityNumber()))
document.AddAttribute("Body", record.Body())
m.encodeAttributes(&document, record.Attributes())
document.AddAttributes("Resource", resource.Attributes())
document.AddAttributes("Scope", scopeToAttributes(scope))

switch m.mode {
case MappingECS:
document.AddTimestamp("@timestamp", record.Timestamp()) // We use @timestamp in order to ensure that we can index if the default data stream logs template is used.
j-kap-t marked this conversation as resolved.
Show resolved Hide resolved
document.AddTraceID("trace.id", record.TraceID())
document.AddSpanID("span.id", record.SpanID())

if f := record.Flags(); f != 0 {
document.AddInt("trace.flags", int64(record.Flags()))
j-kap-t marked this conversation as resolved.
Show resolved Hide resolved
}

if n := record.SeverityNumber(); n != plog.SeverityNumberUnspecified {
document.AddInt("log.syslog.severity.code", int64(record.SeverityNumber()))
j-kap-t marked this conversation as resolved.
Show resolved Hide resolved
document.AddString("log.syslog.severity.name", record.SeverityText())
j-kap-t marked this conversation as resolved.
Show resolved Hide resolved
}

document.AddAttribute("message", record.Body())
j-kap-t marked this conversation as resolved.
Show resolved Hide resolved

fieldMapper := func(k string) string {
switch k {
case "exception.type":
return "error.type"
case "exception.message":
return "error.message"
case "exception.stacktrace":
return "error.stack_trace"
default:
return k
}
}

resource.Attributes().Range(func(k string, v pcommon.Value) bool {
k = fieldMapper(k)
document.AddAttribute(k, v)
return true
})
scope.Attributes().Range(func(k string, v pcommon.Value) bool {
k = fieldMapper(k)
document.AddAttribute(k, v)
return true
})
record.Attributes().Range(func(k string, v pcommon.Value) bool {
k = fieldMapper(k)
document.AddAttribute(k, v)
return true
})
default:
document.AddTimestamp("@timestamp", record.Timestamp()) // We use @timestamp in order to ensure that we can index if the default data stream logs template is used.
document.AddTraceID("TraceId", record.TraceID())
document.AddSpanID("SpanId", record.SpanID())
document.AddInt("TraceFlags", int64(record.Flags()))
document.AddString("SeverityText", record.SeverityText())
document.AddInt("SeverityNumber", int64(record.SeverityNumber()))
document.AddAttribute("Body", record.Body())
m.encodeAttributes(&document, record.Attributes())
document.AddAttributes("Attributes", record.Attributes())
j-kap-t marked this conversation as resolved.
Show resolved Hide resolved
document.AddAttributes("Resource", resource.Attributes())
document.AddAttributes("Scope", scopeToAttributes(scope))
}

if m.dedup {
document.Dedup()
Expand Down
12 changes: 7 additions & 5 deletions exporter/elasticsearchexporter/trace_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,12 @@ func (e *elasticsearchTracesExporter) pushTraceData(
resource := il.Resource()
scopeSpans := il.ScopeSpans()
for j := 0; j < scopeSpans.Len(); j++ {
scope := scopeSpans.At(j).Scope()
spans := scopeSpans.At(j).Spans()
scopeSpan := scopeSpans.At(j)
scope := scopeSpan.Scope()
spans := scopeSpan.Spans()
for k := 0; k < spans.Len(); k++ {
if err := e.pushTraceRecord(ctx, resource, spans.At(k), scope); err != nil {
span := spans.At(k)
if err := e.pushTraceRecord(ctx, resource, span, scope); err != nil {
if cerr := ctx.Err(); cerr != nil {
return cerr
}
Expand All @@ -102,8 +104,8 @@ func (e *elasticsearchTracesExporter) pushTraceData(
func (e *elasticsearchTracesExporter) pushTraceRecord(ctx context.Context, resource pcommon.Resource, span ptrace.Span, scope pcommon.InstrumentationScope) error {
fIndex := e.index
if e.dynamicIndex {
prefix := getFromBothResourceAndAttribute(indexPrefix, resource, span)
suffix := getFromBothResourceAndAttribute(indexSuffix, resource, span)
prefix := getFromAttributes(indexPrefix, resource, scope, span)
suffix := getFromAttributes(indexSuffix, resource, scope, span)

fIndex = fmt.Sprintf("%s%s%s", prefix, fIndex, suffix)
}
Expand Down
2 changes: 1 addition & 1 deletion exporter/elasticsearchexporter/traces_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func TestTracesExporter_New(t *testing.T) {
cfg.Mapping.Dedot = false
cfg.Mapping.Dedup = true
}),
want: successWithInternalModel(&encodeModel{dedot: false, dedup: true, mode: MappingECS}),
want: successWithInternalModel(&encodeModel{dedot: false, dedup: true, mode: MappingNone}),
},
}

Expand Down
Loading