Skip to content

Commit

Permalink
remove id attribute from the final document
Browse files Browse the repository at this point in the history
  • Loading branch information
mauri870 committed Jan 9, 2025
1 parent 1080790 commit 9e0ad2f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 3 additions & 2 deletions exporter/elasticsearchexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,12 @@ func (e *elasticsearchExporter) pushLogRecord(
fIndex = formattedIndex
}

docID := e.extractDocumentIDAttribute(record.Attributes())
document, err := e.model.encodeLog(resource, resourceSchemaURL, record, scope, scopeSchemaURL)
if err != nil {
return fmt.Errorf("failed to encode log event: %w", err)
}

docID := e.getDocumentIDAttribute(record.Attributes())
return bulkIndexerSession.Add(ctx, fIndex, docID, bytes.NewReader(document), nil)
}

Expand Down Expand Up @@ -460,9 +460,10 @@ func (e *elasticsearchExporter) pushSpanEvent(
return bulkIndexerSession.Add(ctx, fIndex, "", bytes.NewReader(docBytes), nil)
}

func (e *elasticsearchExporter) getDocumentIDAttribute(m pcommon.Map) string {
func (e *elasticsearchExporter) extractDocumentIDAttribute(m pcommon.Map) string {
if e.config.LogsDynamicID.Enabled {
docID, ok := getFromAttributes(documentIDAttributeName, "", m)
m.Remove(documentIDAttributeName)
if docID != "" && ok {
return docID
}
Expand Down
4 changes: 4 additions & 0 deletions exporter/elasticsearchexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -787,10 +787,14 @@ func TestExporterLogs(t *testing.T) {
} else {
assert.Equal(t, tt.expectedDocID, actionJSONToID(t, docs[0].Action), "expected _id to be set")
}

// Ensure the document id attribute is removed from the final document.
assert.NotContains(t, docs[0].Document, documentIDAttributeName, "expected document id attribute to be removed")
return itemsAllOK(docs)
})

exporter := newTestLogsExporter(t, server.URL, func(cfg *Config) {
cfg.Mapping.Mode = "otel"
cfg.LogsDynamicID.Enabled = true
cfgFn(cfg)
})
Expand Down

0 comments on commit 9e0ad2f

Please sign in to comment.