-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathfactory.go
More file actions
206 lines (183 loc) · 5.85 KB
/
factory.go
File metadata and controls
206 lines (183 loc) · 5.85 KB
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package fileexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/fileexporter"
import (
"context"
"io"
"os"
"time"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/exporter"
"go.opentelemetry.io/collector/exporter/exporterhelper"
"go.opentelemetry.io/collector/exporter/exporterhelper/xexporterhelper"
"go.opentelemetry.io/collector/exporter/xexporter"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/pprofile"
"go.opentelemetry.io/collector/pdata/ptrace"
"go.uber.org/zap"
"gopkg.in/natefinch/lumberjack.v2"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/fileexporter/internal/metadata"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/sharedcomponent"
)
const (
// the number of old log files to retain
defaultMaxBackups = 100
// the format of encoded telemetry data
formatTypeJSON = "json"
formatTypeProto = "proto"
// the type of compression codec
compressionZSTD = "zstd"
defaultMaxOpenFiles = 100
defaultResourceAttribute = "fileexporter.path_segment"
)
type FileExporter interface {
component.Component
consumeTraces(_ context.Context, td ptrace.Traces) error
consumeMetrics(_ context.Context, md pmetric.Metrics) error
consumeLogs(_ context.Context, ld plog.Logs) error
consumeProfiles(_ context.Context, pd pprofile.Profiles) error
}
// NewFactory creates a factory for OTLP exporter.
func NewFactory() exporter.Factory {
return xexporter.NewFactory(
metadata.Type,
createDefaultConfig,
xexporter.WithTraces(createTracesExporter, metadata.TracesStability),
xexporter.WithMetrics(createMetricsExporter, metadata.MetricsStability),
xexporter.WithLogs(createLogsExporter, metadata.LogsStability),
xexporter.WithProfiles(createProfilesExporter, metadata.ProfilesStability))
}
func createDefaultConfig() component.Config {
return &Config{
FormatType: formatTypeJSON,
Rotation: &Rotation{MaxBackups: defaultMaxBackups},
GroupBy: &GroupBy{
ResourceAttribute: defaultResourceAttribute,
MaxOpenFiles: defaultMaxOpenFiles,
},
}
}
func createTracesExporter(
ctx context.Context,
set exporter.Settings,
cfg component.Config,
) (exporter.Traces, error) {
fe := getOrCreateFileExporter(cfg, set.Logger)
return exporterhelper.NewTraces(
ctx,
set,
cfg,
fe.consumeTraces,
exporterhelper.WithStart(fe.Start),
exporterhelper.WithShutdown(fe.Shutdown),
exporterhelper.WithCapabilities(consumer.Capabilities{MutatesData: false}),
)
}
func createMetricsExporter(
ctx context.Context,
set exporter.Settings,
cfg component.Config,
) (exporter.Metrics, error) {
fe := getOrCreateFileExporter(cfg, set.Logger)
return exporterhelper.NewMetrics(
ctx,
set,
cfg,
fe.consumeMetrics,
exporterhelper.WithStart(fe.Start),
exporterhelper.WithShutdown(fe.Shutdown),
exporterhelper.WithCapabilities(consumer.Capabilities{MutatesData: false}),
)
}
func createLogsExporter(
ctx context.Context,
set exporter.Settings,
cfg component.Config,
) (exporter.Logs, error) {
fe := getOrCreateFileExporter(cfg, set.Logger)
return exporterhelper.NewLogs(
ctx,
set,
cfg,
fe.consumeLogs,
exporterhelper.WithStart(fe.Start),
exporterhelper.WithShutdown(fe.Shutdown),
exporterhelper.WithCapabilities(consumer.Capabilities{MutatesData: false}),
)
}
func createProfilesExporter(
ctx context.Context,
set exporter.Settings,
cfg component.Config,
) (xexporter.Profiles, error) {
fe := getOrCreateFileExporter(cfg, set.Logger)
return xexporterhelper.NewProfiles(
ctx,
set,
cfg,
fe.consumeProfiles,
exporterhelper.WithStart(fe.Start),
exporterhelper.WithShutdown(fe.Shutdown),
exporterhelper.WithCapabilities(consumer.Capabilities{MutatesData: false}),
)
}
// getOrCreateFileExporter creates a FileExporter and caches it for a particular configuration,
// or returns the already cached one. Caching is required because the factory is asked trace and
// metric receivers separately when it gets CreateTraces() and CreateMetrics()
// but they must not create separate objects, they must use one Exporter object per configuration.
func getOrCreateFileExporter(cfg component.Config, logger *zap.Logger) FileExporter {
conf := cfg.(*Config)
fe := exporters.GetOrAdd(cfg, func() component.Component {
return newFileExporter(conf, logger)
})
c := fe.Unwrap()
return c.(FileExporter)
}
func newFileExporter(conf *Config, logger *zap.Logger) FileExporter {
if conf.GroupBy == nil || !conf.GroupBy.Enabled {
return &fileExporter{
conf: conf,
}
}
return &groupingFileExporter{
conf: conf,
logger: logger,
}
}
func newFileWriter(path string, shouldAppend bool, rotation *Rotation, flushInterval time.Duration, export exportFunc) (*fileWriter, error) {
var wc io.WriteCloser
if rotation == nil {
fileFlags := os.O_RDWR | os.O_CREATE
if shouldAppend {
fileFlags |= os.O_APPEND
} else {
fileFlags |= os.O_TRUNC
}
f, err := os.OpenFile(path, fileFlags, 0o644)
if err != nil {
return nil, err
}
wc = newBufferedWriteCloser(f)
} else {
wc = &lumberjack.Logger{
Filename: path,
MaxSize: rotation.MaxMegabytes,
MaxAge: rotation.MaxDays,
MaxBackups: rotation.MaxBackups,
LocalTime: rotation.LocalTime,
}
}
return &fileWriter{
path: path,
file: wc,
exporter: export,
flushInterval: flushInterval,
}, nil
}
// This is the map of already created File exporters for particular configurations.
// We maintain this map because the Factory is asked trace and metric receivers separately
// when it gets CreateTraces() and CreateMetrics() but they must not
// create separate objects, they must use one Exporter object per configuration.
var exporters = sharedcomponent.NewSharedComponents()