-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
factory.go
128 lines (111 loc) · 3.38 KB
/
factory.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
//go:generate mdatagen metadata.yaml
package elasticsearchexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/elasticsearchexporter"
import (
"context"
"fmt"
"runtime"
"time"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/exporter"
"go.opentelemetry.io/collector/exporter/exporterhelper"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/elasticsearchexporter/internal/metadata"
)
const (
// The value of "type" key in configuration.
defaultLogsIndex = "logs-generic-default"
defaultTracesIndex = "traces-generic-default"
userAgentHeaderKey = "User-Agent"
)
// NewFactory creates a factory for Elastic exporter.
func NewFactory() exporter.Factory {
return exporter.NewFactory(
metadata.Type,
createDefaultConfig,
exporter.WithLogs(createLogsExporter, metadata.LogsStability),
exporter.WithTraces(createTracesExporter, metadata.TracesStability),
)
}
func createDefaultConfig() component.Config {
qs := exporterhelper.NewDefaultQueueSettings()
qs.Enabled = false
return &Config{
QueueSettings: qs,
ClientConfig: ClientConfig{
Timeout: 90 * time.Second,
},
Index: "",
LogsIndex: defaultLogsIndex,
TracesIndex: defaultTracesIndex,
Retry: RetrySettings{
Enabled: true,
MaxRequests: 3,
InitialInterval: 100 * time.Millisecond,
MaxInterval: 1 * time.Minute,
},
Mapping: MappingsSettings{
Mode: "ecs",
Dedup: true,
Dedot: true,
},
LogstashFormat: LogstashFormatSettings{
Enabled: false,
PrefixSeparator: "-",
DateFormat: "%Y.%m.%d",
},
}
}
// createLogsExporter creates a new exporter for logs.
//
// Logs are directly indexed into Elasticsearch.
func createLogsExporter(
ctx context.Context,
set exporter.CreateSettings,
cfg component.Config,
) (exporter.Logs, error) {
cf := cfg.(*Config)
if cf.Index != "" {
set.Logger.Warn("index option are deprecated and replaced with logs_index and traces_index.")
}
setDefaultUserAgentHeader(cf, set.BuildInfo)
logsExporter, err := newLogsExporter(set.Logger, cf)
if err != nil {
return nil, fmt.Errorf("cannot configure Elasticsearch logsExporter: %w", err)
}
return exporterhelper.NewLogsExporter(
ctx,
set,
cfg,
logsExporter.pushLogsData,
exporterhelper.WithShutdown(logsExporter.Shutdown),
exporterhelper.WithQueue(cf.QueueSettings),
)
}
func createTracesExporter(ctx context.Context,
set exporter.CreateSettings,
cfg component.Config) (exporter.Traces, error) {
cf := cfg.(*Config)
setDefaultUserAgentHeader(cf, set.BuildInfo)
tracesExporter, err := newTracesExporter(set.Logger, cf)
if err != nil {
return nil, fmt.Errorf("cannot configure Elasticsearch tracesExporter: %w", err)
}
return exporterhelper.NewTracesExporter(
ctx,
set,
cfg,
tracesExporter.pushTraceData,
exporterhelper.WithShutdown(tracesExporter.Shutdown),
exporterhelper.WithQueue(cf.QueueSettings))
}
// set default User-Agent header with BuildInfo if User-Agent is empty
func setDefaultUserAgentHeader(cf *Config, info component.BuildInfo) {
if _, found := cf.Headers[userAgentHeaderKey]; found {
return
}
if cf.Headers == nil {
cf.Headers = make(map[string]string)
}
cf.Headers[userAgentHeaderKey] = fmt.Sprintf("%s/%s (%s/%s)", info.Description, info.Version, runtime.GOOS, runtime.GOARCH)
}