-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrace_exporter.go
336 lines (287 loc) · 9.2 KB
/
trace_exporter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package kineticaotelexporter
import (
"context"
"encoding/hex"
"encoding/json"
"errors"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"
"go.uber.org/multierr"
"go.uber.org/zap"
)
// kineticaTracesExporter
type kineticaTracesExporter struct {
logger *zap.Logger
writer *KiWriter
}
type kineticaTraceRecord struct {
span *Span
spanAttribute []SpanAttribute
resourceAttribute []ResourceAttribute
scopeAttribute []ScopeAttribute
eventAttribute []EventAttribute
linkAttribute []LinkAttribute
}
// newTracesExporter
//
// @param logger
// @param cfg
// @return *kineticaTracesExporter
// @return error
func newTracesExporter(logger *zap.Logger, cfg *Config) (*kineticaTracesExporter, error) {
if err := cfg.Validate(); err != nil {
return nil, err
}
writer := NewKiWriter(context.TODO(), *cfg, logger)
tracesExp := &kineticaTracesExporter{
logger: logger,
writer: writer,
}
return tracesExp, nil
}
// pushTraceData
//
// @receiver e
// @param ctx
// @param td
// @return error
func (e *kineticaTracesExporter) pushTraceData(ctx context.Context, td ptrace.Traces) error {
var errs []error
var traceRecords []kineticaTraceRecord
resourceSpans := td.ResourceSpans()
for i := 0; i < resourceSpans.Len(); i++ {
resourceSpan := resourceSpans.At(i)
resource := resourceSpan.Resource()
scopeSpans := resourceSpan.ScopeSpans()
for j := 0; j < scopeSpans.Len(); j++ {
spans := scopeSpans.At(j).Spans()
scope := scopeSpans.At(j).Scope()
for k := 0; k < spans.Len(); k++ {
if kiTraceRecord, err := e.createTraceRecord(ctx, resource, scope, spans.At(k)); err != nil {
if cerr := ctx.Err(); cerr != nil {
return cerr
}
errs = append(errs, err)
} else {
traceRecords = append(traceRecords, *kiTraceRecord)
}
}
}
}
if err := e.writer.persistTraceRecord(traceRecords); err != nil {
errs = append(errs, err)
}
return multierr.Combine(errs...)
}
// createTraceRecord //
//
// @receiver e
// @param ctx
// @param resource
// @param scope
// @param span
// @return error
func (e *kineticaTracesExporter) createTraceRecord(ctx context.Context, resource pcommon.Resource, scope pcommon.InstrumentationScope, spanRecord ptrace.Span) (*kineticaTraceRecord, error) {
var errs []error
tags := make(map[string]string)
fields := make(map[string]interface{})
traceID := spanRecord.TraceID()
if traceID.IsEmpty() {
return nil, errors.New("span has no trace ID")
}
spanID := spanRecord.SpanID()
if spanID.IsEmpty() {
return nil, errors.New("span has no span ID")
}
tags[AttributeTraceID] = hex.EncodeToString(traceID[:])
tags[AttributeSpanID] = hex.EncodeToString(spanID[:])
if traceState := spanRecord.TraceState().AsRaw(); traceState != "" {
fields[AttributeTraceState] = traceState
}
if parentSpanID := spanRecord.ParentSpanID(); !parentSpanID.IsEmpty() {
fields[AttributeParentSpanID] = hex.EncodeToString(parentSpanID[:])
}
if name := spanRecord.Name(); name != "" {
fields[AttributeName] = name
}
if kind := spanRecord.Kind(); kind != ptrace.SpanKindUnspecified {
fields[AttributeSpanKind] = kind.String()
}
ts := spanRecord.StartTimestamp().AsTime().UnixNano()
if ts == 0 {
return nil, errors.New("span has no timestamp")
}
endTime := spanRecord.EndTimestamp().AsTime().UnixNano()
if endTime == 0 {
fields[AttributeEndTimeUnixNano] = endTime
fields[AttributeDurationNano] = endTime - ts
}
droppedAttributesCount := uint64(spanRecord.DroppedAttributesCount())
if spanRecord.Attributes().Len() > 0 {
marshalledAttributes, err := json.Marshal(spanRecord.Attributes().AsRaw())
if err != nil {
e.logger.Debug("failed to marshal attributes to JSON %s", zap.String("", err.Error()))
droppedAttributesCount += uint64(spanRecord.Attributes().Len())
} else {
fields[AttributeAttributes] = string(marshalledAttributes)
}
}
if droppedAttributesCount > 0 {
fields[AttributeDroppedAttributesCount] = droppedAttributesCount
}
droppedEventsCount := spanRecord.DroppedEventsCount()
kiTraceRecord := new(kineticaTraceRecord)
span := NewSpan(tags[AttributeTraceID], tags[AttributeSpanID], fields[AttributeParentSpanID].(string), fields[AttributeTraceState].(string), fields[AttributeName].(string), fields[AttributeSpanKind].(int8), ts, endTime, int(droppedAttributesCount), int(droppedEventsCount), 0, "", 0)
kiTraceRecord.span = span
var spanAttribute []SpanAttribute
spanAttributes := make(map[string]ValueTypePair)
spanRecord.Attributes().Range(func(k string, v pcommon.Value) bool {
if k == "" {
droppedAttributesCount++
e.logger.Debug("log record attribute key is empty")
} else if v, err := AttributeValueToKineticaFieldValue(v); err != nil {
droppedAttributesCount++
e.logger.Debug("invalid log record attribute value", zap.String("Error", err.Error()))
} else {
spanAttributes[k] = v
}
return true
})
for key := range spanAttributes {
vtPair := spanAttributes[key]
sa := newSpanAttributeValue(span.ID, key, vtPair)
spanAttribute = append(spanAttribute, *sa)
}
copy(kiTraceRecord.spanAttribute, spanAttribute)
var resourceAttribute []ResourceAttribute
resourceAttributes := make(map[string]ValueTypePair)
resource.Attributes().Range(func(k string, v pcommon.Value) bool {
if k == "" {
e.logger.Debug("resource attribute key is empty")
} else if v, err := AttributeValueToKineticaFieldValue(v); err != nil {
e.logger.Debug("Invalid resource attribute value", zap.String("Error", err.Error()))
} else {
resourceAttributes[k] = v
}
return true
})
for key := range resourceAttributes {
vtPair := resourceAttributes[key]
ra := newResourceAttributeValue(span.SpanID, key, vtPair)
resourceAttribute = append(resourceAttribute, *ra)
}
copy(kiTraceRecord.resourceAttribute, resourceAttribute)
// Insert scope attributes
var scopeAttribute []ScopeAttribute
scopeAttributes := make(map[string]ValueTypePair)
scopeName := scope.Name()
scopeVersion := scope.Version()
scope.Attributes().Range(func(k string, v pcommon.Value) bool {
if k == "" {
e.logger.Debug("scope attribute key is empty")
} else if v, err := AttributeValueToKineticaFieldValue(v); err != nil {
e.logger.Debug("invalid scope attribute value", zap.String("Error", err.Error()))
} else {
scopeAttributes[k] = v
}
return true
})
for key := range scopeAttributes {
vtPair := scopeAttributes[key]
sa := newScopeAttributeValue(span.SpanID, key, scopeName, scopeVersion, vtPair)
scopeAttribute = append(scopeAttribute, *sa)
}
copy(kiTraceRecord.scopeAttribute, scopeAttribute)
//Insert event attributes
var eventAttribute []EventAttribute
eventAttributes := make(map[string]ValueTypePair)
spanEvents := spanRecord.Events()
for i := 0; i < spanEvents.Len(); i++ {
event := spanEvents.At(i)
event.Attributes().Range(func(k string, v pcommon.Value) bool {
if k == "" {
e.logger.Debug("Event attribute key is empty")
} else if v, err := AttributeValueToKineticaFieldValue(v); err != nil {
e.logger.Debug("invalid event attribute value", zap.String("Error", err.Error()))
} else {
eventAttributes[k] = v
ea := newEventAttributeValue(span.SpanID, event.Name(), k, v)
eventAttribute = append(eventAttribute, *ea)
}
return true
})
}
copy(kiTraceRecord.eventAttribute, eventAttribute)
//Insert link attributes
var linkAttribute []LinkAttribute
linkAttributes := make(map[string]ValueTypePair)
spanLinks := spanRecord.Links()
for i := 0; i < spanLinks.Len(); i++ {
link := spanLinks.At(i)
link.Attributes().Range(func(k string, v pcommon.Value) bool {
if k == "" {
e.logger.Debug("Event attribute key is empty")
} else if v, err := AttributeValueToKineticaFieldValue(v); err != nil {
e.logger.Debug("invalid event attribute value", zap.String("Error", err.Error()))
} else {
linkAttributes[k] = v
la := newLinkAttributeValue(span.SpanID, span.TraceID, span.SpanID, k, v)
linkAttribute = append(linkAttribute, *la)
}
return true
})
}
copy(kiTraceRecord.linkAttribute, linkAttribute)
return kiTraceRecord, multierr.Combine(errs...)
}
// newLinkAttributeValue
//
// @param linkID
// @param traceID
// @param spanID
// @param key
// @param vtPair
// @return *LinkAttribute
func newLinkAttributeValue(linkID string, traceID, spanID, key string, vtPair ValueTypePair) *LinkAttribute {
var av *AttributeValue
var err error
av, err = getAttributeValue(vtPair)
if err != nil {
la := NewLinkAttribute(linkID, traceID, spanID, key, *av)
return la
}
return nil
}
// newEventAttributeValue
//
// @param eventID
// @param eventName
// @param key
// @param vtPair
// @return *EventAttribute
func newEventAttributeValue(eventID string, eventName string, key string, vtPair ValueTypePair) *EventAttribute {
var av *AttributeValue
var err error
av, err = getAttributeValue(vtPair)
if err != nil {
sa := NewEventAttribute(eventID, eventName, key, *av)
return sa
}
return nil
}
// newSpanAttributeValue
//
// @param spanID
// @param key
// @param vtPair
// @return *SpanAttribute
func newSpanAttributeValue(spanID string, key string, vtPair ValueTypePair) *SpanAttribute {
var av *AttributeValue
var err error
av, err = getAttributeValue(vtPair)
if err != nil {
sa := NewSpanAttribute(spanID, key, *av)
return sa
}
return nil
}