@@ -6,15 +6,13 @@ package elasticsearchexporter // import "github.com/open-telemetry/opentelemetry
66import (
77 "bytes"
88 "encoding/binary"
9- "encoding/json"
109 "errors"
1110 "fmt"
1211 "hash"
1312 "hash/fnv"
1413 "math"
1514 "slices"
1615 "strings"
17- "time"
1816
1917 jsoniter "github.com/json-iterator/go"
2018 "go.opentelemetry.io/collector/pdata/pcommon"
@@ -26,7 +24,6 @@ import (
2624 "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/elasticsearchexporter/internal/exphistogram"
2725 "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/elasticsearchexporter/internal/mapping"
2826 "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/elasticsearchexporter/internal/objmodel"
29- "github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/traceutil"
3027)
3128
3229// resourceAttrsConversionMap contains conversions for resource-level attributes
@@ -106,12 +103,6 @@ type dataPoint interface {
106103 HasMappingHint (mappingHint ) bool
107104}
108105
109- const (
110- traceIDField = "traceID"
111- spanIDField = "spanID"
112- attributeField = "attribute"
113- )
114-
115106func (m * encodeModel ) encodeLog (resource pcommon.Resource , resourceSchemaURL string , record plog.LogRecord , scopeLogs plog.ScopeLogs ) ([]byte , error ) {
116107 var document objmodel.Document
117108 switch m .mode {
@@ -122,7 +113,7 @@ func (m *encodeModel) encodeLog(resource pcommon.Resource, resourceSchemaURL str
122113 case mapping .ModeBodyMap :
123114 return m .encodeLogBodyMapMode (record )
124115 default :
125- document = mapping.DefaultEncoder {Mode : m .mode }.EncodeLog (resource , record , scopeLogs )
116+ document = mapping.DefaultEncoder {Mode : m .mode }.EncodeLog (resource , scopeLogs , record )
126117 }
127118 // For OTel mode, prefix conflicts are not a problem as otel-data has subobjects: false
128119 document .Dedup (m .mode != mapping .ModeOTel )
@@ -644,7 +635,7 @@ func (m *encodeModel) encodeSpan(resourceSpans ptrace.ResourceSpans, scopeSpans
644635 case mapping .ModeOTel :
645636 document = m .encodeSpanOTelMode (resourceSpans .Resource (), resourceSpans .SchemaUrl (), span , scopeSpans .Scope (), scopeSpans .SchemaUrl ())
646637 default :
647- document = m . encodeSpanDefaultMode ( resourceSpans . Resource (), span , scopeSpans . Scope () )
638+ document = mapping. DefaultEncoder { Mode : m . mode }. EncodeSpan ( resourceSpans , scopeSpans , span )
648639 }
649640 // For OTel mode, prefix conflicts are not a problem as otel-data has subobjects: false
650641 document .Dedup (m .mode != mapping .ModeOTel )
@@ -694,26 +685,6 @@ func (m *encodeModel) encodeSpanOTelMode(resource pcommon.Resource, resourceSche
694685 return document
695686}
696687
697- func (m * encodeModel ) encodeSpanDefaultMode (resource pcommon.Resource , span ptrace.Span , scope pcommon.InstrumentationScope ) objmodel.Document {
698- var document objmodel.Document
699- 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.
700- document .AddTimestamp ("EndTimestamp" , span .EndTimestamp ())
701- document .AddTraceID ("TraceId" , span .TraceID ())
702- document .AddSpanID ("SpanId" , span .SpanID ())
703- document .AddSpanID ("ParentSpanId" , span .ParentSpanID ())
704- document .AddString ("Name" , span .Name ())
705- document .AddString ("Kind" , traceutil .SpanKindStr (span .Kind ()))
706- document .AddInt ("TraceStatus" , int64 (span .Status ().Code ()))
707- document .AddString ("TraceStatusDescription" , span .Status ().Message ())
708- document .AddString ("Link" , spanLinksToString (span .Links ()))
709- m .encodeAttributes (& document , span .Attributes ())
710- document .AddAttributes ("Resource" , resource .Attributes ())
711- m .encodeEvents (& document , span .Events ())
712- document .AddInt ("Duration" , durationAsMicroseconds (span .StartTimestamp ().AsTime (), span .EndTimestamp ().AsTime ())) // unit is microseconds
713- document .AddAttributes ("Scope" , scopeToAttributes (scope ))
714- return document
715- }
716-
717688func (m * encodeModel ) encodeSpanEvent (resource pcommon.Resource , resourceSchemaURL string , span ptrace.Span , spanEvent ptrace.SpanEvent , scope pcommon.InstrumentationScope , scopeSchemaURL string ) * objmodel.Document {
718689 if m .mode != mapping .ModeOTel {
719690 // Currently span events are stored separately only in OTel mapping mode.
@@ -736,52 +707,6 @@ func (m *encodeModel) encodeSpanEvent(resource pcommon.Resource, resourceSchemaU
736707 return & document
737708}
738709
739- func (m * encodeModel ) encodeAttributes (document * objmodel.Document , attributes pcommon.Map ) {
740- key := "Attributes"
741- if m .mode == mapping .ModeRaw {
742- key = ""
743- }
744- document .AddAttributes (key , attributes )
745- }
746-
747- func (m * encodeModel ) encodeEvents (document * objmodel.Document , events ptrace.SpanEventSlice ) {
748- key := "Events"
749- if m .mode == mapping .ModeRaw {
750- key = ""
751- }
752- document .AddEvents (key , events )
753- }
754-
755- func spanLinksToString (spanLinkSlice ptrace.SpanLinkSlice ) string {
756- linkArray := make ([]map [string ]any , 0 , spanLinkSlice .Len ())
757- for i := 0 ; i < spanLinkSlice .Len (); i ++ {
758- spanLink := spanLinkSlice .At (i )
759- link := map [string ]any {}
760- link [spanIDField ] = traceutil .SpanIDToHexOrEmptyString (spanLink .SpanID ())
761- link [traceIDField ] = traceutil .TraceIDToHexOrEmptyString (spanLink .TraceID ())
762- link [attributeField ] = spanLink .Attributes ().AsRaw ()
763- linkArray = append (linkArray , link )
764- }
765- linkArrayBytes , _ := json .Marshal (& linkArray )
766- return string (linkArrayBytes )
767- }
768-
769- // durationAsMicroseconds calculate span duration through end - start nanoseconds and converts time.Time to microseconds,
770- // which is the format the Duration field is stored in the Span.
771- func durationAsMicroseconds (start , end time.Time ) int64 {
772- return (end .UnixNano () - start .UnixNano ()) / 1000
773- }
774-
775- func scopeToAttributes (scope pcommon.InstrumentationScope ) pcommon.Map {
776- attrs := pcommon .NewMap ()
777- attrs .PutStr ("name" , scope .Name ())
778- attrs .PutStr ("version" , scope .Version ())
779- for k , v := range scope .Attributes ().AsRaw () {
780- attrs .PutStr (k , v .(string ))
781- }
782- return attrs
783- }
784-
785710func encodeAttributesECSMode (document * objmodel.Document , attrs pcommon.Map , conversionMap map [string ]string , preserveMap map [string ]bool ) {
786711 if len (conversionMap ) == 0 {
787712 // No conversions to be done; add all attributes at top level of
0 commit comments