Skip to content

Commit

Permalink
Use new mapping.omit_attributes_prefix option while encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
ycombinator committed Jan 17, 2024
1 parent a6ff601 commit 233e552
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
6 changes: 5 additions & 1 deletion exporter/elasticsearchexporter/logs_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ func newLogsExporter(logger *zap.Logger, cfg *Config) (*elasticsearchLogsExporte
maxAttempts = cfg.Retry.MaxRequests
}

model := &encodeModel{dedup: cfg.Mapping.Dedup, dedot: cfg.Mapping.Dedot}
model := &encodeModel{
dedup: cfg.Mapping.Dedup,
dedot: cfg.Mapping.Dedot,
omitAttributesPrefix: cfg.Mapping.OmitAttributesPrefix,
}

indexStr := cfg.LogsIndex
if cfg.Index != "" {
Expand Down
18 changes: 14 additions & 4 deletions exporter/elasticsearchexporter/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ type mappingModel interface {
//
// See: https://github.com/open-telemetry/oteps/blob/master/text/logs/0097-log-data-model.md
type encodeModel struct {
dedup bool
dedot bool
dedup bool
dedot bool
omitAttributesPrefix bool
}

const (
Expand All @@ -47,7 +48,7 @@ func (m *encodeModel) encodeLog(resource pcommon.Resource, record plog.LogRecord
document.AddString("SeverityText", record.SeverityText())
document.AddInt("SeverityNumber", int64(record.SeverityNumber()))
document.AddAttribute("Body", record.Body())
document.AddAttributes("Attributes", record.Attributes())
m.encodeAttributes(&document, record.Attributes())
document.AddAttributes("Resource", resource.Attributes())
document.AddAttributes("Scope", scopeToAttributes(scope))

Expand All @@ -74,7 +75,7 @@ func (m *encodeModel) encodeSpan(resource pcommon.Resource, span ptrace.Span, sc
document.AddInt("TraceStatus", int64(span.Status().Code()))
document.AddString("TraceStatusDescription", span.Status().Message())
document.AddString("Link", spanLinksToString(span.Links()))
document.AddAttributes("Attributes", span.Attributes())
m.encodeAttributes(&document, span.Attributes())
document.AddAttributes("Resource", resource.Attributes())
document.AddEvents("Events", span.Events())
document.AddInt("Duration", durationAsMicroseconds(span.StartTimestamp().AsTime(), span.EndTimestamp().AsTime())) // unit is microseconds
Expand All @@ -91,6 +92,15 @@ func (m *encodeModel) encodeSpan(resource pcommon.Resource, span ptrace.Span, sc
return buf.Bytes(), err
}

func (m *encodeModel) encodeAttributes(document *objmodel.Document, attributes pcommon.Map) {
if m.omitAttributesPrefix {
rawDoc := objmodel.DocumentFromAttributes(attributes)
document.MergeFrom(rawDoc)
} else {
document.AddAttributes("Attributes", attributes)
}
}

func spanLinksToString(spanLinkSlice ptrace.SpanLinkSlice) string {
linkArray := make([]map[string]any, 0, spanLinkSlice.Len())
for i := 0; i < spanLinkSlice.Len(); i++ {
Expand Down
6 changes: 5 additions & 1 deletion exporter/elasticsearchexporter/trace_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ func newTracesExporter(logger *zap.Logger, cfg *Config) (*elasticsearchTracesExp
maxAttempts = cfg.Retry.MaxRequests
}

model := &encodeModel{dedup: cfg.Mapping.Dedup, dedot: cfg.Mapping.Dedot}
model := &encodeModel{
dedup: cfg.Mapping.Dedup,
dedot: cfg.Mapping.Dedot,
omitAttributesPrefix: cfg.Mapping.OmitAttributesPrefix,
}

return &elasticsearchTracesExporter{
logger: logger,
Expand Down

0 comments on commit 233e552

Please sign in to comment.