-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathexporter.go
255 lines (227 loc) · 7.68 KB
/
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
// Copyright 2020, OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package awscloudwatchlogsexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awscloudwatchlogsexporter"
import (
"context"
"encoding/json"
"errors"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
"github.com/google/uuid"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/exporter/exporterhelper"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/awsutil"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/cwlogs"
)
type exporter struct {
Config *Config
logger *zap.Logger
retryCount int
collectorID string
svcStructuredLog *cwlogs.Client
pusher cwlogs.Pusher
}
func newCwLogsPusher(expConfig *Config, params component.ExporterCreateSettings) (component.LogsExporter, error) {
if expConfig == nil {
return nil, errors.New("awscloudwatchlogs exporter config is nil")
}
expConfig.logger = params.Logger
// create AWS session
awsConfig, session, err := awsutil.GetAWSConfigSession(params.Logger, &awsutil.Conn{}, &expConfig.AWSSessionSettings)
if err != nil {
return nil, err
}
// create CWLogs client with aws session config
svcStructuredLog := cwlogs.NewClient(params.Logger, awsConfig, params.BuildInfo, expConfig.LogGroupName, session)
collectorIdentifier, err := uuid.NewRandom()
if err != nil {
return nil, err
}
pusher := cwlogs.NewPusher(aws.String(expConfig.LogGroupName), aws.String(expConfig.LogStreamName), *awsConfig.MaxRetries, *svcStructuredLog, params.Logger)
logsExporter := &exporter{
svcStructuredLog: svcStructuredLog,
Config: expConfig,
logger: params.Logger,
retryCount: *awsConfig.MaxRetries,
collectorID: collectorIdentifier.String(),
pusher: pusher,
}
return logsExporter, nil
}
func newCwLogsExporter(config config.Exporter, params component.ExporterCreateSettings) (component.LogsExporter, error) {
expConfig := config.(*Config)
logsExporter, err := newCwLogsPusher(expConfig, params)
if err != nil {
return nil, err
}
return exporterhelper.NewLogsExporterWithContext(
context.TODO(),
params,
config,
logsExporter.ConsumeLogs,
exporterhelper.WithQueue(expConfig.enforcedQueueSettings()),
exporterhelper.WithRetry(expConfig.RetrySettings),
)
}
func (e *exporter) ConsumeLogs(ctx context.Context, ld plog.Logs) error {
cwLogsPusher := e.pusher
logEvents, _ := logsToCWLogs(e.logger, ld)
if len(logEvents) == 0 {
return nil
}
for _, logEvent := range logEvents {
logEvent := &cwlogs.Event{
InputLogEvent: logEvent,
GeneratedTime: time.Now(),
}
e.logger.Debug("Adding log event", zap.Any("event", logEvent))
err := cwLogsPusher.AddLogEntry(logEvent)
if err != nil {
e.logger.Error("Failed ", zap.Int("num_of_events", len(logEvents)))
}
}
e.logger.Debug("Log events are successfully put")
flushErr := cwLogsPusher.ForceFlush()
if flushErr != nil {
e.logger.Error("Error force flushing logs. Skipping to next logPusher.", zap.Error(flushErr))
return flushErr
}
return nil
}
func (e *exporter) Capabilities() consumer.Capabilities {
return consumer.Capabilities{MutatesData: false}
}
func (e *exporter) Shutdown(ctx context.Context) error {
if e.pusher != nil {
e.pusher.ForceFlush()
}
return nil
}
func (e *exporter) Start(ctx context.Context, host component.Host) error {
return nil
}
func logsToCWLogs(logger *zap.Logger, ld plog.Logs) ([]*cloudwatchlogs.InputLogEvent, int) {
n := ld.ResourceLogs().Len()
if n == 0 {
return []*cloudwatchlogs.InputLogEvent{}, 0
}
var dropped int
out := make([]*cloudwatchlogs.InputLogEvent, 0) // TODO(jbd): set a better capacity
rls := ld.ResourceLogs()
for i := 0; i < rls.Len(); i++ {
rl := rls.At(i)
resourceAttrs := attrsValue(rl.Resource().Attributes())
sls := rl.ScopeLogs()
for j := 0; j < sls.Len(); j++ {
sl := sls.At(j)
logs := sl.LogRecords()
for k := 0; k < logs.Len(); k++ {
log := logs.At(k)
event, err := logToCWLog(resourceAttrs, log)
if err != nil {
logger.Debug("Failed to convert to CloudWatch Log", zap.Error(err))
dropped++
} else {
out = append(out, event)
}
}
}
}
return out, dropped
}
type cwLogBody struct {
Body interface{} `json:"body,omitempty"`
SeverityNumber int32 `json:"severity_number,omitempty"`
SeverityText string `json:"severity_text,omitempty"`
DroppedAttributesCount uint32 `json:"dropped_attributes_count,omitempty"`
Flags uint32 `json:"flags,omitempty"`
TraceID string `json:"trace_id,omitempty"`
SpanID string `json:"span_id,omitempty"`
Attributes map[string]interface{} `json:"attributes,omitempty"`
Resource map[string]interface{} `json:"resource,omitempty"`
}
func logToCWLog(resourceAttrs map[string]interface{}, log plog.LogRecord) (*cloudwatchlogs.InputLogEvent, error) {
// TODO(jbd): Benchmark and improve the allocations.
// Evaluate go.elastic.co/fastjson as a replacement for encoding/json.
body := cwLogBody{
Body: attrValue(log.Body()),
SeverityNumber: int32(log.SeverityNumber()),
SeverityText: log.SeverityText(),
DroppedAttributesCount: log.DroppedAttributesCount(),
Flags: log.Flags(),
}
if traceID := log.TraceID(); !traceID.IsEmpty() {
body.TraceID = traceID.HexString()
}
if spanID := log.SpanID(); !spanID.IsEmpty() {
body.SpanID = spanID.HexString()
}
body.Attributes = attrsValue(log.Attributes())
body.Resource = resourceAttrs
bodyJSON, err := json.Marshal(body)
if err != nil {
return nil, err
}
return &cloudwatchlogs.InputLogEvent{
Timestamp: aws.Int64(int64(log.Timestamp()) / int64(time.Millisecond)), // in milliseconds
Message: aws.String(string(bodyJSON)),
}, nil
}
func attrsValue(attrs pcommon.Map) map[string]interface{} {
if attrs.Len() == 0 {
return nil
}
out := make(map[string]interface{}, attrs.Len())
attrs.Range(func(k string, v pcommon.Value) bool {
out[k] = attrValue(v)
return true
})
return out
}
func attrValue(value pcommon.Value) interface{} {
switch value.Type() {
case pcommon.ValueTypeInt:
return value.IntVal()
case pcommon.ValueTypeBool:
return value.BoolVal()
case pcommon.ValueTypeDouble:
return value.DoubleVal()
case pcommon.ValueTypeString:
return value.StringVal()
case pcommon.ValueTypeMap:
values := map[string]interface{}{}
value.MapVal().Range(func(k string, v pcommon.Value) bool {
values[k] = attrValue(v)
return true
})
return values
case pcommon.ValueTypeSlice:
arrayVal := value.SliceVal()
values := make([]interface{}, arrayVal.Len())
for i := 0; i < arrayVal.Len(); i++ {
values[i] = attrValue(arrayVal.At(i))
}
return values
case pcommon.ValueTypeEmpty:
return nil
default:
return nil
}
}