forked from r0mdau/fluentforwardexporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexporter.go
201 lines (173 loc) · 5.38 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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package fluentforwardexporter // import "github.com/r0mdau/fluentforwardexporter"
import (
"context"
"fmt"
"strings"
"sync"
fclient "github.com/IBM/fluent-forward-go/fluent/client"
"github.com/IBM/fluent-forward-go/fluent/protocol"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
)
type fluentforwardExporter struct {
config *Config
settings component.TelemetrySettings
client *fclient.Client
wg sync.WaitGroup
}
func newExporter(config *Config, settings component.TelemetrySettings) *fluentforwardExporter {
settings.Logger.Info("Creating the Fluent Forward exporter")
return &fluentforwardExporter{
config: config,
settings: settings,
}
}
func (f *fluentforwardExporter) start(ctx context.Context, host component.Host) error {
connOptions := fclient.ConnectionOptions{
RequireAck: f.config.RequireAck,
}
tlsConfig, err := f.config.ClientConfig.LoadTLSConfig(ctx)
if err != nil {
return err
}
connFactory := &fclient.ConnFactory{
Address: f.config.Endpoint.TCPAddr,
Timeout: f.config.ConnectionTimeout,
TLSConfig: tlsConfig,
}
connOptions.Factory = connFactory
if f.config.SharedKey != "" {
connOptions.AuthInfo = fclient.AuthInfo{
SharedKey: []byte(f.config.SharedKey),
}
}
client := fclient.New(connOptions)
f.client = client
f.connectForward()
return nil
}
func (f *fluentforwardExporter) stop(context.Context) (err error) {
f.wg.Wait()
return f.client.Disconnect()
}
// connectForward connects to the Fluent Forward endpoint and keep running otel even if the connection is failing
func (f *fluentforwardExporter) connectForward() {
if err := f.client.Connect(); err != nil {
f.settings.Logger.Error(fmt.Sprintf("Failed to connect to the endpoint %s", f.config.Endpoint.TCPAddr))
return
}
f.settings.Logger.Info(fmt.Sprintf("Successfull connection to the endpoint %s", f.config.Endpoint.TCPAddr))
if f.config.SharedKey != "" {
if err := f.client.Handshake(); err != nil {
f.settings.Logger.Error(fmt.Sprintf("Failed shared key handshake with the endpoint %s", f.config.Endpoint.TCPAddr))
return
}
f.settings.Logger.Info("Successfull shared key handshake with the endpoint")
}
}
func (f *fluentforwardExporter) pushLogData(ctx context.Context, ld plog.Logs) error {
// move for loops into a translator
entries := []protocol.EntryExt{}
rls := ld.ResourceLogs()
for i := 0; i < rls.Len(); i++ {
ills := rls.At(i).ScopeLogs()
for j := 0; j < ills.Len(); j++ {
logs := ills.At(j).LogRecords()
for k := 0; k < logs.Len(); k++ {
log := logs.At(k)
entry := protocol.EntryExt{
Timestamp: protocol.EventTimeNow(),
Record: f.convertLogToMap(log, rls.At(i)),
}
entries = append(entries, entry)
}
}
}
if f.config.CompressGzip {
return f.sendCompressed(entries)
}
return f.sendForward(entries)
}
func (f *fluentforwardExporter) convertLogToMap(lr plog.LogRecord, res plog.ResourceLogs) map[string]interface{} {
// move function into a translator
m := make(map[string]interface{})
m["severity"] = lr.SeverityText()
m["message"] = lr.Body().AsString()
for key, val := range f.config.DefaultLabelsEnabled {
if val {
attribute, found := lr.Attributes().Get(key)
if found {
m[key] = attribute.AsString()
}
}
}
if f.config.KubernetesMetadata != nil {
key := f.config.KubernetesMetadata.Key
if f.config.KubernetesMetadata.Key == "" {
key = "kubernetes"
}
var namespace, container, pod, node string
var labels map[string]string
res.Resource().Attributes().Range(func(k string, v pcommon.Value) bool {
if k == "k8s.namespace.name" {
namespace = v.AsString()
return true
}
if k == "k8s.container.name" {
container = v.AsString()
}
if k == "k8s.pod.name" {
pod = v.AsString()
}
if k == "k8s.node.name" {
node = v.AsString()
}
if f.config.KubernetesMetadata.IncludePodLabels && strings.HasPrefix(k, "k8s.pod.labels.") {
if labels == nil {
labels = make(map[string]string)
}
labelKey := strings.TrimPrefix(k, "k8s.pod.labels.")
labels[labelKey] = v.AsString()
}
return true
})
k8sMetadata := map[string]interface{}{
"namespace_name": namespace,
"container_name": container,
"pod_name": pod,
"host": node,
}
if f.config.KubernetesMetadata.IncludePodLabels {
k8sMetadata["labels"] = labels
}
m[key] = k8sMetadata
}
f.settings.Logger.Debug(fmt.Sprintf("message %+v", m))
return m
}
type sendFunc func(string, protocol.EntryList) error
func (f *fluentforwardExporter) send(sendMethod sendFunc, entries []protocol.EntryExt) error {
err := sendMethod(f.config.Tag, entries)
// sometimes the connection is lost, we try to reconnect and send the data again
if err != nil {
if errr := f.client.Disconnect(); errr != nil {
return errr
}
f.settings.Logger.Warn(fmt.Sprintf("Failed to send data to the endpoint %s, trying to reconnect", f.config.Endpoint.TCPAddr))
f.connectForward()
err = sendMethod(f.config.Tag, entries)
if err != nil {
return err
}
}
return nil
}
func (f *fluentforwardExporter) sendCompressed(entries []protocol.EntryExt) error {
return f.send(f.client.SendCompressed, entries)
}
func (f *fluentforwardExporter) sendForward(entries []protocol.EntryExt) error {
return f.send(f.client.SendForward, entries)
}