generated from ipfs/ipfs-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 87
/
file_exporter.go
45 lines (39 loc) · 1.13 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
package tracing
import (
"context"
"fmt"
"os"
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
"go.opentelemetry.io/otel/sdk/trace"
)
// fileExporter wraps a file-writing exporter and closes the file when the exporter is shutdown.
type fileExporter struct {
file *os.File
writerExporter *stdouttrace.Exporter
}
func newFileExporter(file string) (*fileExporter, error) {
f, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0o666)
if err != nil {
return nil, fmt.Errorf("opening '%s' for OpenTelemetry file exporter: %w", file, err)
}
stdoutExporter, err := stdouttrace.New(stdouttrace.WithWriter(f))
if err != nil {
return nil, err
}
return &fileExporter{
writerExporter: stdoutExporter,
file: f,
}, nil
}
func (e *fileExporter) ExportSpans(ctx context.Context, spans []trace.ReadOnlySpan) error {
return e.writerExporter.ExportSpans(ctx, spans)
}
func (e *fileExporter) Shutdown(ctx context.Context) error {
if err := e.writerExporter.Shutdown(ctx); err != nil {
return err
}
if err := e.file.Close(); err != nil {
return fmt.Errorf("closing trace file: %w", err)
}
return nil
}