forked from SumoLogic/sumologic-otel-collector
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
269 lines (232 loc) · 9.99 KB
/
config.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
// 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 sumologicexporter
import (
"errors"
"fmt"
"net/url"
"time"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configauth"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/exporter/exporterhelper"
)
// Config defines configuration for Sumo Logic exporter.
type Config struct {
confighttp.HTTPClientSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct.
exporterhelper.QueueSettings `mapstructure:"sending_queue"`
exporterhelper.RetrySettings `mapstructure:"retry_on_failure"`
// Compression encoding format, either empty string, gzip or deflate (default gzip)
// Empty string means no compression
CompressEncoding CompressEncodingType `mapstructure:"compress_encoding"`
// Max HTTP request body size in bytes before compression (if applied).
// By default 1MB is recommended.
MaxRequestBodySize int `mapstructure:"max_request_body_size"`
// Logs related configuration
// Format to post logs into Sumo. (default json)
// * text - Logs will appear in Sumo Logic in text format.
// * json - Logs will appear in Sumo Logic in json format.
// * otlp - Logs will be send in otlp format and will appear in Sumo Logic in text format.
LogFormat LogFormatType `mapstructure:"log_format"`
// Metrics related configuration
// The format of metrics you will be sending, either otlp or prometheus (Default is otlp)
MetricFormat MetricFormatType `mapstructure:"metric_format"`
// Traces related configuration
// The format of traces you will be sending, currently only otlp format is supported
TraceFormat TraceFormatType `mapstructure:"trace_format"`
// DEPRECATED: The below attributes only exist so we can print a nicer error
// message about not supporting them anymore.
TranslateAttributes bool `mapstructure:"translate_attributes"`
TranslateTelegrafMetrics bool `mapstructure:"translate_telegraf_attributes"`
MetadataAttributes []string `mapstructure:"metadata_attributes"`
SourceCategory string `mapstructure:"source_category"`
SourceName string `mapstructure:"source_name"`
SourceHost string `mapstructure:"source_host"`
// Attribute used by routingprocessor which should be dropped during data ingestion
// This is workaround for the following issue:
// https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/7407
DropRoutingAttribute string `mapstructure:"routing_atttribute_to_drop"`
// Sumo specific options
// Name of the client
Client string `mapstructure:"client"`
// ClearTimestamp defines if timestamp for logs should be set to 0.
// It indicates that backend will extract timestamp from logs.
// This option affects OTLP format only.
// By default this is true.
ClearLogsTimestamp bool `mapstructure:"clear_logs_timestamp"`
JSONLogs `mapstructure:"json_logs"`
}
type JSONLogs struct {
// LogKey defines which key will be used to attach the log body at.
// This option affects JSON log format only.
// By default this is "log".
LogKey string `mapstructure:"log_key"`
// AddTimestamp defines whether to include a timestamp field when sending
// JSON logs, which would contain UNIX epoch timestamp in milliseconds.
// This option affects JSON log format only.
// By default this is true.
AddTimestamp bool `mapstructure:"add_timestamp"`
// When add_timestamp is set to true then this key defines what is the name
// of the timestamp key.
// By default this is "timestamp".
TimestampKey string `mapstructure:"timestamp_key"`
// When flatten_body is set to true and log is a map,
// log's body is going to be flattened and `log_key` won't be used
// By default this is false.
FlattenBody bool `mapstructure:"flatten_body"`
}
// CreateDefaultHTTPClientSettings returns default http client settings
func CreateDefaultHTTPClientSettings() confighttp.HTTPClientSettings {
return confighttp.HTTPClientSettings{
Timeout: defaultTimeout,
Auth: &configauth.Authentication{
AuthenticatorID: component.NewID("sumologic"),
},
}
}
func (cfg *Config) Validate() error {
if len(cfg.MetadataAttributes) > 0 {
return fmt.Errorf(`metadata_attributes is not supported anymore.
Please consult the changelog at https://github.com/SumoLogic/sumologic-otel-collector/releases/tag/v0.49.0-sumo-0`,
)
}
if cfg.TranslateTelegrafMetrics {
return fmt.Errorf(`translate_telegraf_attributes is not supported anymore.
Please consult the changelog at https://github.com/SumoLogic/sumologic-otel-collector/releases/tag/v0.59.0-sumo-0`,
)
}
if cfg.TranslateAttributes {
return fmt.Errorf(`translate_attributes is not supported anymore.
Please consult the changelog at https://github.com/SumoLogic/sumologic-otel-collector/releases/tag/v0.59.0-sumo-0`,
)
}
if cfg.SourceCategory != "" || cfg.SourceHost != "" || cfg.SourceName != "" {
return fmt.Errorf(`setting source headers is not supported anymore.
Please consult the changelog at https://github.com/SumoLogic/sumologic-otel-collector/releases/tag/v0.60.0-sumo-0`,
)
}
switch cfg.LogFormat {
case OTLPLogFormat:
case JSONFormat:
case TextFormat:
default:
return fmt.Errorf("unexpected log format: %s", cfg.LogFormat)
}
switch cfg.MetricFormat {
case OTLPMetricFormat:
case PrometheusFormat:
case RemovedGraphiteFormat:
return fmt.Errorf("support for the graphite metric format was removed, please use prometheus or otlp instead")
case RemovedCarbon2Format:
return fmt.Errorf("support for the carbon2 metric format was removed, please use prometheus or otlp instead")
default:
return fmt.Errorf("unexpected metric format: %s", cfg.MetricFormat)
}
switch cfg.TraceFormat {
case OTLPTraceFormat:
default:
return fmt.Errorf("unexpected trace format: %s", cfg.TraceFormat)
}
if err := cfg.CompressEncoding.Validate(); err != nil {
return err
}
if len(cfg.HTTPClientSettings.Endpoint) == 0 && cfg.HTTPClientSettings.Auth == nil {
return errors.New("no endpoint and no auth extension specified")
}
if _, err := url.Parse(cfg.HTTPClientSettings.Endpoint); err != nil {
return fmt.Errorf("failed parsing endpoint URL: %s; err: %w",
cfg.HTTPClientSettings.Endpoint, err,
)
}
if err := cfg.QueueSettings.Validate(); err != nil {
return fmt.Errorf("queue settings has invalid configuration: %w", err)
}
return nil
}
// LogFormatType represents log_format
type LogFormatType string
// MetricFormatType represents metric_format
type MetricFormatType string
// TraceFormatType represents trace_format
type TraceFormatType string
// PipelineType represents type of the pipeline
type PipelineType string
// CompressEncodingType represents type of the pipeline
type CompressEncodingType string
func (cet CompressEncodingType) Validate() error {
switch cet {
case GZIPCompression:
case NoCompression:
case DeflateCompression:
default:
return fmt.Errorf("invalid compression encoding type: %v", cet)
}
return nil
}
const (
// TextFormat represents log_format: text
TextFormat LogFormatType = "text"
// JSONFormat represents log_format: json
JSONFormat LogFormatType = "json"
// OTLPLogFormat represents log_format: otlp
OTLPLogFormat LogFormatType = "otlp"
// RemovedGraphiteFormat represents the no longer supported graphite metric format
RemovedGraphiteFormat MetricFormatType = "graphite"
// RemovedCarbon2Format represents the no longer supported carbon2 metric format
RemovedCarbon2Format MetricFormatType = "carbon2"
// PrometheusFormat represents metric_format: prometheus
PrometheusFormat MetricFormatType = "prometheus"
// OTLPMetricFormat represents metric_format: otlp
OTLPMetricFormat MetricFormatType = "otlp"
// OTLPTraceFormat represents trace_format: otlp
OTLPTraceFormat TraceFormatType = "otlp"
// GZIPCompression represents compress_encoding: gzip
GZIPCompression CompressEncodingType = "gzip"
// DeflateCompression represents compress_encoding: deflate
DeflateCompression CompressEncodingType = "deflate"
// NoCompression represents disabled compression
NoCompression CompressEncodingType = ""
// MetricsPipeline represents metrics pipeline
MetricsPipeline PipelineType = "metrics"
// LogsPipeline represents metrics pipeline
LogsPipeline PipelineType = "logs"
// TracesPipeline represents traces pipeline
TracesPipeline PipelineType = "traces"
// defaultTimeout
defaultTimeout time.Duration = 5 * time.Second
// DefaultCompress defines default Compress
DefaultCompress bool = true
// DefaultCompressEncoding defines default CompressEncoding
DefaultCompressEncoding CompressEncodingType = "gzip"
// DefaultMaxRequestBodySize defines default MaxRequestBodySize in bytes
DefaultMaxRequestBodySize int = 1 * 1024 * 1024
// DefaultLogFormat defines default LogFormat
DefaultLogFormat LogFormatType = OTLPLogFormat
// DefaultMetricFormat defines default MetricFormat
DefaultMetricFormat MetricFormatType = OTLPMetricFormat
// DefaultClient defines default Client
DefaultClient string = "otelcol"
// DefaultClearTimestamp defines default ClearLogsTimestamp value
DefaultClearLogsTimestamp bool = true
// DefaultLogKey defines default LogKey value
DefaultLogKey string = "log"
// DefaultAddTimestamp defines default AddTimestamp value
DefaultAddTimestamp bool = true
// DefaultTimestampKey defines default TimestampKey value
DefaultTimestampKey string = "timestamp"
// DefaultFlattenBody defines default FlattenBody value
DefaultFlattenBody bool = false
// DefaultDropRoutingAttribute defines default DropRoutingAttribute
DefaultDropRoutingAttribute string = ""
)