forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
exporter.go
149 lines (132 loc) · 3.96 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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package syslogexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/syslogexporter"
import (
"context"
"crypto/tls"
"errors"
"fmt"
"strings"
"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/exporter"
"go.opentelemetry.io/collector/exporter/exporterhelper"
"go.opentelemetry.io/collector/pdata/plog"
"go.uber.org/zap"
)
type syslogexporter struct {
config *Config
logger *zap.Logger
tlsConfig *tls.Config
formatter formatter
}
func initExporter(cfg *Config, createSettings exporter.Settings) (*syslogexporter, error) {
var loadedTLSConfig *tls.Config
if cfg.Network == string(confignet.TransportTypeTCP) {
var err error
loadedTLSConfig, err = cfg.TLSSetting.LoadTLSConfig(context.Background())
if err != nil {
return nil, err
}
}
s := &syslogexporter{
config: cfg,
logger: createSettings.Logger,
tlsConfig: loadedTLSConfig,
formatter: createFormatter(cfg.Protocol, cfg.EnableOctetCounting),
}
s.logger.Info("Syslog Exporter configured",
zap.String("endpoint", cfg.Endpoint),
zap.String("protocol", cfg.Protocol),
zap.String("network", cfg.Network),
zap.Int("port", cfg.Port),
)
return s, nil
}
func newLogsExporter(
ctx context.Context,
params exporter.Settings,
cfg *Config,
) (exporter.Logs, error) {
s, err := initExporter(cfg, params)
if err != nil {
return nil, fmt.Errorf("failed to initialize the logs exporter: %w", err)
}
return exporterhelper.NewLogs(
ctx,
params,
cfg,
s.pushLogsData,
exporterhelper.WithTimeout(cfg.TimeoutSettings),
exporterhelper.WithRetry(cfg.BackOffConfig),
exporterhelper.WithQueue(cfg.QueueSettings),
)
}
func (se *syslogexporter) pushLogsData(ctx context.Context, logs plog.Logs) error {
batchMessages := se.config.Network == string(confignet.TransportTypeTCP)
var err error
if batchMessages {
err = se.exportBatch(ctx, logs)
} else {
err = se.exportNonBatch(ctx, logs)
}
return err
}
func (se *syslogexporter) exportBatch(ctx context.Context, logs plog.Logs) error {
var payload strings.Builder
for i := 0; i < logs.ResourceLogs().Len(); i++ {
resourceLogs := logs.ResourceLogs().At(i)
for j := 0; j < resourceLogs.ScopeLogs().Len(); j++ {
scopeLogs := resourceLogs.ScopeLogs().At(j)
for k := 0; k < scopeLogs.LogRecords().Len(); k++ {
logRecord := scopeLogs.LogRecords().At(k)
formatted := se.formatter.format(logRecord)
payload.WriteString(formatted)
}
}
}
if payload.Len() > 0 {
sender, err := connect(ctx, se.logger, se.config, se.tlsConfig)
if err != nil {
return consumererror.NewLogs(err, logs)
}
defer sender.close()
err = sender.Write(ctx, payload.String())
if err != nil {
return consumererror.NewLogs(err, logs)
}
}
return nil
}
func (se *syslogexporter) exportNonBatch(ctx context.Context, logs plog.Logs) error {
sender, err := connect(ctx, se.logger, se.config, se.tlsConfig)
if err != nil {
return consumererror.NewLogs(err, logs)
}
defer sender.close()
errs := []error{}
droppedLogs := plog.NewLogs()
for i := 0; i < logs.ResourceLogs().Len(); i++ {
resourceLogs := logs.ResourceLogs().At(i)
droppedResourceLogs := droppedLogs.ResourceLogs().AppendEmpty()
for j := 0; j < resourceLogs.ScopeLogs().Len(); j++ {
scopeLogs := resourceLogs.ScopeLogs().At(j)
droppedScopeLogs := droppedResourceLogs.ScopeLogs().AppendEmpty()
for k := 0; k < scopeLogs.LogRecords().Len(); k++ {
logRecord := scopeLogs.LogRecords().At(k)
formatted := se.formatter.format(logRecord)
err = sender.Write(ctx, formatted)
if err != nil {
errs = append(errs, err)
droppedLogRecord := droppedScopeLogs.LogRecords().AppendEmpty()
logRecord.CopyTo(droppedLogRecord)
}
}
}
}
if len(errs) > 0 {
errs = deduplicateErrors(errs)
return consumererror.NewLogs(errors.Join(errs...), droppedLogs)
}
return nil
}