-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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] Add OTel mapping mode for traces #34472
Changes from 10 commits
e7f41d2
07ac2e7
e8337c7
66ee18f
b2cec62
d19eebf
fd5da92
9d6d3db
7c6f811
b45df76
fb69873
3130cf1
54925f4
71cc306
f6416f1
9b8588e
deb70c4
f18b053
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 OTel mapping mode for traces, update OTel mapping mode for logs | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [34472] | ||
carsonip 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: [user] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,7 +65,7 @@ var resourceAttrsToPreserve = map[string]bool{ | |
|
||
type mappingModel interface { | ||
encodeLog(pcommon.Resource, string, plog.LogRecord, pcommon.InstrumentationScope, string) ([]byte, error) | ||
encodeSpan(pcommon.Resource, ptrace.Span, pcommon.InstrumentationScope) ([]byte, error) | ||
encodeSpan(pcommon.Resource, string, ptrace.Span, pcommon.InstrumentationScope, string) ([]byte, error) | ||
upsertMetricDataPointValue(map[uint32]objmodel.Document, pcommon.Resource, pcommon.InstrumentationScope, pmetric.Metric, dataPoint, pcommon.Value) error | ||
encodeDocument(objmodel.Document) ([]byte, error) | ||
} | ||
|
@@ -130,8 +130,6 @@ func (m *encodeModel) encodeLogDefaultMode(resource pcommon.Resource, record plo | |
return document | ||
} | ||
|
||
var datastreamKeys = []string{dataStreamType, dataStreamDataset, dataStreamNamespace} | ||
|
||
func (m *encodeModel) encodeLogOTelMode(resource pcommon.Resource, resourceSchemaURL string, record plog.LogRecord, scope pcommon.InstrumentationScope, scopeSchemaURL string) objmodel.Document { | ||
var document objmodel.Document | ||
|
||
|
@@ -145,76 +143,13 @@ func (m *encodeModel) encodeLogOTelMode(resource pcommon.Resource, resourceSchem | |
|
||
document.AddTraceID("trace_id", record.TraceID()) | ||
document.AddSpanID("span_id", record.SpanID()) | ||
document.AddInt("trace_flags", int64(record.Flags())) | ||
carsonip marked this conversation as resolved.
Show resolved
Hide resolved
|
||
document.AddString("severity_text", record.SeverityText()) | ||
document.AddInt("severity_number", int64(record.SeverityNumber())) | ||
document.AddInt("dropped_attributes_count", int64(record.DroppedAttributesCount())) | ||
|
||
// At this point the data_stream attributes are expected to be in the record attributes, | ||
// updated by the router. | ||
// Move them to the top of the document and remove them from the record | ||
attributeMap := record.Attributes() | ||
|
||
forEachDataStreamKey := func(fn func(key string)) { | ||
for _, key := range datastreamKeys { | ||
fn(key) | ||
} | ||
} | ||
|
||
forEachDataStreamKey(func(key string) { | ||
if value, exists := attributeMap.Get(key); exists { | ||
document.AddAttribute(key, value) | ||
attributeMap.Remove(key) | ||
} | ||
}) | ||
|
||
document.AddAttributes("attributes", attributeMap) | ||
|
||
// Resource | ||
resourceMapVal := pcommon.NewValueMap() | ||
resourceMap := resourceMapVal.Map() | ||
resourceMap.PutStr("schema_url", resourceSchemaURL) | ||
resourceMap.PutInt("dropped_attributes_count", int64(resource.DroppedAttributesCount())) | ||
resourceAttrMap := resourceMap.PutEmptyMap("attributes") | ||
|
||
resource.Attributes().CopyTo(resourceAttrMap) | ||
|
||
// Remove data_stream attributes from the resources attributes if present | ||
forEachDataStreamKey(func(key string) { | ||
resourceAttrMap.Remove(key) | ||
}) | ||
|
||
document.Add("resource", objmodel.ValueFromAttribute(resourceMapVal)) | ||
|
||
// Scope | ||
scopeMapVal := pcommon.NewValueMap() | ||
scopeMap := scopeMapVal.Map() | ||
if scope.Name() != "" { | ||
scopeMap.PutStr("name", scope.Name()) | ||
} | ||
if scope.Version() != "" { | ||
scopeMap.PutStr("version", scope.Version()) | ||
} | ||
if scopeSchemaURL != "" { | ||
scopeMap.PutStr("schema_url", scopeSchemaURL) | ||
} | ||
if scope.DroppedAttributesCount() > 0 { | ||
scopeMap.PutInt("dropped_attributes_count", int64(scope.DroppedAttributesCount())) | ||
} | ||
scopeAttributes := scope.Attributes() | ||
if scopeAttributes.Len() > 0 { | ||
scopeAttrMap := scopeMap.PutEmptyMap("attributes") | ||
scopeAttributes.CopyTo(scopeAttrMap) | ||
|
||
// Remove data_stream attributes from the scope attributes if present | ||
forEachDataStreamKey(func(key string) { | ||
scopeAttrMap.Remove(key) | ||
}) | ||
} | ||
|
||
if scopeMap.Len() > 0 { | ||
document.Add("scope", objmodel.ValueFromAttribute(scopeMapVal)) | ||
} | ||
m.encodeAttributesOTelMode(&document, record.Attributes()) | ||
m.encodeResourceOTelMode(&document, resource, resourceSchemaURL) | ||
m.encodeScopeOTelMode(&document, scope, scopeSchemaURL) | ||
|
||
// Body | ||
setOTelLogBody(&document, record.Body()) | ||
|
@@ -385,7 +320,131 @@ func numberToValue(dp pmetric.NumberDataPoint) (pcommon.Value, error) { | |
return pcommon.Value{}, errInvalidNumberDataPoint | ||
} | ||
|
||
func (m *encodeModel) encodeSpan(resource pcommon.Resource, span ptrace.Span, scope pcommon.InstrumentationScope) ([]byte, error) { | ||
func (m *encodeModel) encodeResourceOTelMode(document *objmodel.Document, resource pcommon.Resource, resourceSchemaURL string) { | ||
resourceMapVal := pcommon.NewValueMap() | ||
resourceMap := resourceMapVal.Map() | ||
if resourceSchemaURL != "" { | ||
andrzej-stencel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
resourceMap.PutStr("schema_url", resourceSchemaURL) | ||
} | ||
resourceMap.PutInt("dropped_attributes_count", int64(resource.DroppedAttributesCount())) | ||
resourceAttrMap := resourceMap.PutEmptyMap("attributes") | ||
resource.Attributes().CopyTo(resourceAttrMap) | ||
resourceAttrMap.RemoveIf(func(key string, _ pcommon.Value) bool { | ||
switch key { | ||
case dataStreamType, dataStreamDataset, dataStreamNamespace: | ||
return true | ||
} | ||
return false | ||
}) | ||
|
||
document.Add("resource", objmodel.ValueFromAttribute(resourceMapVal)) | ||
carsonip marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
func (m *encodeModel) encodeScopeOTelMode(document *objmodel.Document, scope pcommon.InstrumentationScope, scopeSchemaURL string) { | ||
scopeMapVal := pcommon.NewValueMap() | ||
scopeMap := scopeMapVal.Map() | ||
if scope.Name() != "" { | ||
scopeMap.PutStr("name", scope.Name()) | ||
} | ||
if scope.Version() != "" { | ||
scopeMap.PutStr("version", scope.Version()) | ||
} | ||
if scopeSchemaURL != "" { | ||
scopeMap.PutStr("schema_url", scopeSchemaURL) | ||
} | ||
if scope.DroppedAttributesCount() > 0 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is a difference of treatment compared to the resource level. Don't you want to set this always even if zero? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice catch. I think it was initially implemented in this manner in a previous PR for efficiency: #33290 . Also, But I'll get back to the team and see if there is any implication on data correctness. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated the PR to keep the handling consistent |
||
scopeMap.PutInt("dropped_attributes_count", int64(scope.DroppedAttributesCount())) | ||
} | ||
scopeAttributes := scope.Attributes() | ||
if scopeAttributes.Len() > 0 { | ||
scopeAttrMap := scopeMap.PutEmptyMap("attributes") | ||
scopeAttributes.CopyTo(scopeAttrMap) | ||
scopeAttrMap.RemoveIf(func(key string, _ pcommon.Value) bool { | ||
switch key { | ||
case dataStreamType, dataStreamDataset, dataStreamNamespace: | ||
return true | ||
} | ||
return false | ||
}) | ||
} | ||
if scopeMap.Len() > 0 { | ||
document.Add("scope", objmodel.ValueFromAttribute(scopeMapVal)) | ||
} | ||
} | ||
|
||
func (m *encodeModel) encodeAttributesOTelMode(document *objmodel.Document, attributeMap pcommon.Map) { | ||
attributeMap.RemoveIf(func(key string, val pcommon.Value) bool { | ||
switch key { | ||
case dataStreamType, dataStreamDataset, dataStreamNamespace: | ||
// At this point the data_stream attributes are expected to be in the record attributes, | ||
// updated by the router. | ||
// Move them to the top of the document and remove them from the record | ||
document.AddAttribute(key, val) | ||
return true | ||
} | ||
return false | ||
}) | ||
document.AddAttributes("attributes", attributeMap) | ||
} | ||
|
||
func (m *encodeModel) encodeSpan(resource pcommon.Resource, resourceSchemaURL string, span ptrace.Span, scope pcommon.InstrumentationScope, scopeSchemaURL string) ([]byte, error) { | ||
var document objmodel.Document | ||
switch m.mode { | ||
case MappingOTel: | ||
document = m.encodeSpanOTelMode(resource, resourceSchemaURL, span, scope, scopeSchemaURL) | ||
default: | ||
document = m.encodeSpanDefaultMode(resource, span, scope) | ||
} | ||
document.Dedup() | ||
var buf bytes.Buffer | ||
err := document.Serialize(&buf, m.dedot, m.mode == MappingOTel) | ||
return buf.Bytes(), err | ||
} | ||
|
||
func (m *encodeModel) encodeSpanOTelMode(resource pcommon.Resource, resourceSchemaURL string, span ptrace.Span, scope pcommon.InstrumentationScope, scopeSchemaURL string) objmodel.Document { | ||
var document objmodel.Document | ||
document.AddTimestamp("@timestamp", span.StartTimestamp()) | ||
document.AddTraceID("trace_id", span.TraceID()) | ||
document.AddSpanID("span_id", span.SpanID()) | ||
document.AddString("trace_state", span.TraceState().AsRaw()) | ||
document.AddSpanID("parent_span_id", span.ParentSpanID()) | ||
carsonip marked this conversation as resolved.
Show resolved
Hide resolved
|
||
document.AddString("name", span.Name()) | ||
document.AddString("kind", span.Kind().String()) | ||
document.AddInt("duration", int64(span.EndTimestamp()-span.StartTimestamp())) | ||
|
||
m.encodeAttributesOTelMode(&document, span.Attributes()) | ||
|
||
document.AddInt("dropped_attributes_count", int64(span.DroppedAttributesCount())) | ||
document.AddInt("dropped_events_count", int64(span.DroppedEventsCount())) | ||
|
||
links := pcommon.NewValueSlice() | ||
linkSlice := links.SetEmptySlice() | ||
spanLinks := span.Links() | ||
for i := 0; i < spanLinks.Len(); i++ { | ||
linkMap := linkSlice.AppendEmpty().SetEmptyMap() | ||
spanLink := spanLinks.At(i) | ||
linkMap.PutStr("trace_id", spanLink.TraceID().String()) | ||
linkMap.PutStr("span_id", spanLink.SpanID().String()) | ||
linkMap.PutStr("trace_state", spanLink.TraceState().AsRaw()) | ||
mAttr := linkMap.PutEmptyMap("attributes") | ||
spanLink.Attributes().CopyTo(mAttr) | ||
linkMap.PutInt("dropped_attributes_count", int64(spanLink.DroppedAttributesCount())) | ||
} | ||
document.AddAttribute("links", links) | ||
|
||
document.AddInt("dropped_links_count", int64(span.DroppedLinksCount())) | ||
document.AddString("status.message", span.Status().Message()) | ||
document.AddString("status.code", span.Status().Code().String()) | ||
|
||
m.encodeResourceOTelMode(&document, resource, resourceSchemaURL) | ||
m.encodeScopeOTelMode(&document, scope, scopeSchemaURL) | ||
|
||
// TODO: add span events to log data streams | ||
andrzej-stencel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return document | ||
} | ||
|
||
func (m *encodeModel) encodeSpanDefaultMode(resource pcommon.Resource, span ptrace.Span, scope pcommon.InstrumentationScope) objmodel.Document { | ||
var document objmodel.Document | ||
document.AddTimestamp("@timestamp", span.StartTimestamp()) // We use @timestamp in order to ensure that we can index if the default data stream logs template is used. | ||
document.AddTimestamp("EndTimestamp", span.EndTimestamp()) | ||
|
@@ -402,12 +461,7 @@ func (m *encodeModel) encodeSpan(resource pcommon.Resource, span ptrace.Span, sc | |
m.encodeEvents(&document, span.Events()) | ||
document.AddInt("Duration", durationAsMicroseconds(span.StartTimestamp().AsTime(), span.EndTimestamp().AsTime())) // unit is microseconds | ||
document.AddAttributes("Scope", scopeToAttributes(scope)) | ||
document.Dedup() | ||
|
||
var buf bytes.Buffer | ||
// OTel serialization is not supported for traces yet | ||
err := document.Serialize(&buf, m.dedot, false) | ||
return buf.Bytes(), err | ||
return document | ||
} | ||
|
||
func (m *encodeModel) encodeAttributes(document *objmodel.Document, attributes pcommon.Map) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same feedback as on the metrics PR, any chance you can add more to this note to explain what this change is about?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OTel mapping mode, aka OTel native mapping mode, is a new mapping mode that is actively under development, where it maps to a model that's very close to the actual OTel data model, unlike ECS mode where a lot of translations are done in elasticsearchexporter. See elasticsearchexporter docs for details. We don't have public docs about mapping that we can point to yet, as it is all under active development. It requires Elasticsearch native support to magically keep the existing UIs working.
There's an initial implementation for logs (done in #33290) as you may have noticed, and here comes the traces (this PR) and metrics (#34248) counterparts.