-
Notifications
You must be signed in to change notification settings - Fork 0
/
logs_exporter.go
279 lines (238 loc) · 7.54 KB
/
logs_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
package kineticaotelexporter
import (
"context"
"encoding/hex"
"github.com/google/uuid"
"github.com/influxdata/influxdb-observability/common"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
"go.uber.org/multierr"
"go.uber.org/zap"
)
type kineticaLogsExporter struct {
logger *zap.Logger
writer *KiWriter
}
type kineticaLogRecord struct {
log *Log
logAttribute []LogAttribute
resourceAttribute []ResourceAttribute
scopeAttribute []ScopeAttribute
}
// newLogsExporter
//
// @param logger
// @param cfg
// @return *kineticaLogsExporter
// @return error
func newLogsExporter(logger *zap.Logger, cfg *Config) (*kineticaLogsExporter, error) {
if err := cfg.Validate(); err != nil {
return nil, err
}
writer := NewKiWriter(context.TODO(), *cfg, logger)
logsExp := &kineticaLogsExporter{
logger: logger,
writer: writer,
}
return logsExp, nil
}
// pushLogsData
//
// @receiver e
// @param ctx
// @param ld
// @return error
func (e *kineticaLogsExporter) pushLogsData(ctx context.Context, logData plog.Logs) error {
var errs []error
var logRecords []kineticaLogRecord
resourceLogs := logData.ResourceLogs()
for i := 0; i < resourceLogs.Len(); i++ {
rl := resourceLogs.At(i)
resource := rl.Resource()
scopeLogs := rl.ScopeLogs()
for j := 0; j < scopeLogs.Len(); j++ {
scopeLog := scopeLogs.At(j)
logs := scopeLogs.At(j).LogRecords()
for k := 0; k < logs.Len(); k++ {
if kiLogRecord, err := e.createLogRecord(ctx, resource, scopeLog.Scope(), logs.At(k)); err != nil {
if cerr := ctx.Err(); cerr != nil {
return cerr
}
errs = append(errs, err)
} else {
logRecords = append(logRecords, *kiLogRecord)
}
}
}
}
if err := e.writer.persistLogRecord(logRecords); err != nil {
errs = append(errs, err)
}
return multierr.Combine(errs...)
}
// createLogRecord //
//
// @receiver e
// @param ctx
// @param resource
// @param instrumentationLibrary
// @param logRecord
// @return *kineticaLogRecord
// @return error
func (e *kineticaLogsExporter) createLogRecord(ctx context.Context, resource pcommon.Resource, instrumentationLibrary pcommon.InstrumentationScope, logRecord plog.LogRecord) (*kineticaLogRecord, error) {
var errs []error
ts := logRecord.Timestamp().AsTime().Unix()
ots := logRecord.ObservedTimestamp().AsTime().Unix()
tags := make(map[string]string)
fields := make(map[string]interface{})
// TODO handle logRecord.Flags()
if traceID := logRecord.TraceID(); !traceID.IsEmpty() {
tags[AttributeTraceID] = hex.EncodeToString(traceID[:])
if spanID := logRecord.SpanID(); !spanID.IsEmpty() {
tags[AttributeSpanID] = hex.EncodeToString(spanID[:])
}
}
if severityNumber := logRecord.SeverityNumber(); severityNumber != plog.SeverityNumberUnspecified {
fields[AttributeSeverityNumber] = int64(severityNumber)
}
if severityText := logRecord.SeverityText(); severityText != "" {
fields[AttributeSeverityText] = severityText
}
if v, err := AttributeValueToKineticaFieldValue(logRecord.Body()); err != nil {
e.logger.Debug("invalid log record body", zap.String("Error", err.Error()))
fields[AttributeBody] = nil
} else {
fields[AttributeBody] = v
}
var logAttribute []LogAttribute
logAttributes := make(map[string]ValueTypePair)
droppedAttributesCount := uint64(logRecord.DroppedAttributesCount())
logRecord.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 {
logAttributes[k] = v
}
return true
})
if droppedAttributesCount > 0 {
fields[common.AttributeDroppedAttributesCount] = droppedAttributesCount
}
severityNumber, ok := (fields[AttributeSeverityNumber]).(int)
if !ok {
severityNumber = 0
e.logger.Warn("severity_number conversion failed, possibly not an integer; storing 0")
}
severityText, ok := (fields[AttributeSeverityText]).(string)
if !ok {
e.logger.Warn("severity_text conversion failed, possibly not a string; storing empty string")
severityText = ""
}
// create log - Body, dropped_attribute_count and flags not handled now
log := NewLog(uuid.New().String(), tags[AttributeTraceID], tags[AttributeSpanID], ts, ots, int8(severityNumber), severityText, "", 0)
// _, err := log.insertLog()
// errs = append(errs, err)
// Insert log attributes
for key := range logAttributes {
vtPair := logAttributes[key]
la := newLogAttributeValue(log.LogID, key, vtPair)
logAttribute = append(logAttribute, *la)
}
var resourceAttribute []ResourceAttribute
// Insert resource attributes
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(log.LogID, key, vtPair)
resourceAttribute = append(resourceAttribute, *ra)
}
// Insert scope attributes
var scopeAttribute []ScopeAttribute
scopeAttributes := make(map[string]ValueTypePair)
scopeName := instrumentationLibrary.Name()
scopeVersion := instrumentationLibrary.Version()
instrumentationLibrary.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(log.LogID, key, scopeName, scopeVersion, vtPair)
scopeAttribute = append(scopeAttribute, *sa)
}
kiLogRecord := new(kineticaLogRecord)
kiLogRecord.log = log
copy(kiLogRecord.logAttribute, logAttribute)
copy(kiLogRecord.resourceAttribute, resourceAttribute)
copy(kiLogRecord.scopeAttribute, scopeAttribute)
return kiLogRecord, multierr.Combine(errs...)
}
// newLogAttributeValue
//
// @param logID
// @param key
// @param vtPair
// @return *LogsAttribute
func newLogAttributeValue(logID string, key string, vtPair ValueTypePair) *LogAttribute {
var av *AttributeValue
var err error
av, err = getAttributeValue(vtPair)
if err != nil {
la := NewLogAttribute(logID, key, *av)
return la
}
return nil
}
// newResourceAttributeValue
//
// @param resourceID
// @param key
// @param vtPair
// @return *LogsResourceAttribute
func newResourceAttributeValue(resourceID string, key string, vtPair ValueTypePair) *ResourceAttribute {
var av *AttributeValue
var err error
av, err = getAttributeValue(vtPair)
if err != nil {
la := NewResourceAttribute(resourceID, key, *av)
return la
}
return nil
}
// newScopeAttributeValue newResourceAttributeValue
//
// @param key
// @param scopeName
// @param scopeVersion
// @param vtPair
// @return *LogsScopeAttribute
func newScopeAttributeValue(scopeID string, key string, scopeName string, scopeVersion string, vtPair ValueTypePair) *ScopeAttribute {
var av *AttributeValue
var err error
av, err = getAttributeValue(vtPair)
if err != nil {
sa := NewScopeAttribute(scopeID, key, scopeName, scopeVersion, *av)
return sa
}
return nil
}