@@ -27,32 +27,149 @@ import (
27
27
attr "github.com/elastic/opentelemetry-collector-components/receiver/elasticapmintakereceiver/internal"
28
28
)
29
29
30
+ var compressionStrategyText = map [modelpb.CompressionStrategy ]string {
31
+ modelpb .CompressionStrategy_COMPRESSION_STRATEGY_EXACT_MATCH : "exact_match" ,
32
+ modelpb .CompressionStrategy_COMPRESSION_STRATEGY_SAME_KIND : "same_kind" ,
33
+ }
34
+
30
35
// SetElasticSpecificFieldsForSpan sets fields on spans that are not defined by OTel.
31
36
// Unlike fields from IntakeV2ToDerivedFields.go, these fields are not used by the UI
32
37
// and store information about a specific span type
33
38
func SetElasticSpecificFieldsForSpan (event * modelpb.APMEvent , attributesMap pcommon.Map ) {
34
- if event .Span .Db != nil {
35
- attributesMap .PutStr (attr .SpanDBLink , event .Span .Db .Link )
36
- // SemConv db.response.returned_rows is similar, but not the same
37
- attributesMap .PutInt (attr .SpanDBRowsAffected , int64 (* event .Span .Db .RowsAffected ))
38
- attributesMap .PutStr (attr .SpanDBUserName , event .Span .Db .UserName )
39
+ if event .Http != nil {
40
+ if event .Http .Request != nil {
41
+ if event .Http .Request .Body != nil {
42
+ attributesMap .PutStr (attr .HTTPRequestBody , event .Http .Request .Body .GetStringValue ())
43
+ }
44
+ if event .Http .Request .Id != "" {
45
+ attributesMap .PutStr (attr .HTTPRequestID , event .Http .Request .Id )
46
+ }
47
+ if event .Http .Request .Referrer != "" {
48
+ attributesMap .PutStr (attr .HTTPRequestReferrer , event .Http .Request .Referrer )
49
+ }
50
+ }
51
+
52
+ if event .Http .Response != nil {
53
+ if event .Http .Response .DecodedBodySize != nil {
54
+ attributesMap .PutInt (attr .HTTPResponseDecodedBodySize , int64 (* event .Http .Response .DecodedBodySize ))
55
+ }
56
+ if event .Http .Response .EncodedBodySize != nil {
57
+ attributesMap .PutInt (attr .HTTPResponseEncodedBodySize , int64 (* event .Http .Response .EncodedBodySize ))
58
+ }
59
+ if event .Http .Response .TransferSize != nil {
60
+ attributesMap .PutInt (attr .HTTPResponseTransferSize , int64 (* event .Http .Response .TransferSize ))
61
+ }
62
+ }
39
63
}
40
64
41
- if event .Http .Request != nil {
42
- attributesMap .PutStr (attr .HTTPRequestBody , event .Http .Request .Body .GetStringValue ())
43
- attributesMap .PutStr (attr .HTTPRequestID , event .Http .Request .Id )
44
- attributesMap .PutStr (attr .HTTPRequestReferrer , event .Http .Request .Referrer )
65
+ if event .Span == nil {
66
+ return
45
67
}
46
68
47
- if event .Http .Response != nil {
48
- // SemConv http.response.body.size may match one of these.
49
- attributesMap .PutInt (attr .HTTPResponseDecodedBodySize , int64 (* event .Http .Response .DecodedBodySize ))
50
- attributesMap .PutInt (attr .HTTPResponseEncodedBodySize , int64 (* event .Http .Response .EncodedBodySize ))
51
- attributesMap .PutInt (attr .HTTPResponseTransferSize , int64 (* event .Http .Response .TransferSize ))
69
+ if event .Span .Db != nil {
70
+ if event .Span .Db .Link != "" {
71
+ attributesMap .PutStr (attr .SpanDBLink , event .Span .Db .Link )
72
+ }
73
+ if event .Span .Db .RowsAffected != nil {
74
+ // SemConv db.response.returned_rows is similar, but not the same
75
+ attributesMap .PutInt (attr .SpanDBRowsAffected , int64 (* event .Span .Db .RowsAffected ))
76
+ }
77
+ if event .Span .Db .UserName != "" {
78
+ attributesMap .PutStr (attr .SpanDBUserName , event .Span .Db .UserName )
79
+ }
52
80
}
53
81
54
82
if event .Span .Message != nil {
55
- attributesMap .PutStr (attr .SpanMessageBody , event .Span .Message .Body )
83
+ if event .Span .Message .Body != "" {
84
+ attributesMap .PutStr (attr .SpanMessageBody , event .Span .Message .Body )
85
+ }
86
+ if event .Span .Message .AgeMillis != nil {
87
+ attributesMap .PutInt (attr .SpanMessageAgeMs , int64 (* event .Span .Message .AgeMillis ))
88
+ }
89
+ for _ , header := range event .Span .Message .Headers {
90
+ headerKey := attr .SpanMessageHeadersPrefix + header .Key
91
+ headerValues := attributesMap .PutEmptySlice (headerKey )
92
+ headerValues .EnsureCapacity (len (header .Value ))
93
+ for _ , v := range header .Value {
94
+ headerValues .AppendEmpty ().SetStr (v )
95
+ }
96
+ }
97
+ }
98
+
99
+ if event .Span .Composite != nil {
100
+ compressionStrategy , ok := compressionStrategyText [event .Span .Composite .CompressionStrategy ]
101
+ if ok {
102
+ attributesMap .PutStr (attr .SpanCompositeCompressionStrategy , compressionStrategy )
103
+ }
104
+ attributesMap .PutInt (attr .SpanCompositeCount , int64 (event .Span .Composite .Count ))
105
+ attributesMap .PutInt (attr .SpanCompositeSum , int64 (event .Span .Composite .Sum ))
106
+ }
107
+
108
+ attributesMap .PutDouble (attr .SpanRepresentativeCount , event .Span .RepresentativeCount )
109
+
110
+ setStackTraceList (attributesMap , event .Span .Stacktrace )
111
+ }
112
+
113
+ // setStackTraceList maps stacktrace frames to attributes map.
114
+ // The stacktrace will be a list of objects (maps), each map representing a frame.
115
+ func setStackTraceList (attributesMap pcommon.Map , stacktrace []* modelpb.StacktraceFrame ) {
116
+ if len (stacktrace ) == 0 {
117
+ return
118
+ }
119
+
120
+ stacktraceSlice := attributesMap .PutEmptySlice (attr .SpanStacktrace )
121
+ stacktraceSlice .EnsureCapacity (len (stacktrace ))
122
+ for _ , frame := range stacktrace {
123
+ frameMap := stacktraceSlice .AppendEmpty ().SetEmptyMap ()
124
+
125
+ if len (frame .Vars ) > 0 {
126
+ varsMap := frameMap .PutEmptyMap (attr .SpanStacktraceFrameVars )
127
+ for _ , varKV := range frame .Vars {
128
+ varsMap .PutStr (varKV .Key , varKV .Value .GetStringValue ())
129
+ }
130
+ }
131
+
132
+ if frame .Lineno != nil {
133
+ frameMap .PutInt (attr .SpanStacktraceFrameLineNumber , int64 (* frame .Lineno ))
134
+ }
135
+ if frame .Colno != nil {
136
+ frameMap .PutInt (attr .SpanStacktraceFrameLineColumn , int64 (* frame .Colno ))
137
+ }
138
+ if frame .Filename != "" {
139
+ frameMap .PutStr (attr .SpanStacktraceFrameFilename , frame .Filename )
140
+ }
141
+ if frame .Classname != "" {
142
+ frameMap .PutStr (attr .SpanStacktraceFrameClassname , frame .Classname )
143
+ }
144
+ if frame .ContextLine != "" {
145
+ frameMap .PutStr (attr .SpanStacktraceFrameLineContext , frame .ContextLine )
146
+ }
147
+ if frame .Module != "" {
148
+ frameMap .PutStr (attr .SpanStacktraceFrameModule , frame .Module )
149
+ }
150
+ if frame .Function != "" {
151
+ frameMap .PutStr (attr .SpanStacktraceFrameFunction , frame .Function )
152
+ }
153
+ if frame .AbsPath != "" {
154
+ frameMap .PutStr (attr .SpanStacktraceFrameAbsPath , frame .AbsPath )
155
+ }
156
+
157
+ if len (frame .PreContext ) > 0 {
158
+ preSlice := frameMap .PutEmptySlice (attr .SpanStacktraceFrameContextPre )
159
+ preSlice .EnsureCapacity (len (frame .PreContext ))
160
+ for _ , pre := range frame .PreContext {
161
+ preSlice .AppendEmpty ().SetStr (pre )
162
+ }
163
+ }
164
+ if len (frame .PostContext ) > 0 {
165
+ postSlice := frameMap .PutEmptySlice (attr .SpanStacktraceFrameContextPost )
166
+ postSlice .EnsureCapacity (len (frame .PostContext ))
167
+ for _ , post := range frame .PostContext {
168
+ postSlice .AppendEmpty ().SetStr (post )
169
+ }
170
+ }
171
+
172
+ frameMap .PutBool (attr .SpanStacktraceFrameLibraryFrame , frame .LibraryFrame )
56
173
}
57
174
}
58
175
0 commit comments