-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
file_exporter.go
72 lines (62 loc) · 1.78 KB
/
file_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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package fileexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/fileexporter"
import (
"context"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/ptrace"
)
// fileExporter is the implementation of file exporter that writes telemetry data to a file
type fileExporter struct {
conf *Config
marshaller *marshaller
writer *fileWriter
}
func (e *fileExporter) consumeTraces(_ context.Context, td ptrace.Traces) error {
buf, err := e.marshaller.marshalTraces(td)
if err != nil {
return err
}
return e.writer.export(buf)
}
func (e *fileExporter) consumeMetrics(_ context.Context, md pmetric.Metrics) error {
buf, err := e.marshaller.marshalMetrics(md)
if err != nil {
return err
}
return e.writer.export(buf)
}
func (e *fileExporter) consumeLogs(_ context.Context, ld plog.Logs) error {
buf, err := e.marshaller.marshalLogs(ld)
if err != nil {
return err
}
return e.writer.export(buf)
}
// Start starts the flush timer if set.
func (e *fileExporter) Start(_ context.Context, host component.Host) error {
var err error
e.marshaller, err = newMarshaller(e.conf, host)
if err != nil {
return err
}
export := buildExportFunc(e.conf)
e.writer, err = newFileWriter(e.conf.Path, e.conf.Append, e.conf.Rotation, e.conf.FlushInterval, export)
if err != nil {
return err
}
e.writer.start()
return nil
}
// Shutdown stops the exporter and is invoked during shutdown.
// It stops the flush ticker if set.
func (e *fileExporter) Shutdown(context.Context) error {
if e.writer == nil {
return nil
}
w := e.writer
e.writer = nil
return w.shutdown()
}