forked from open-telemetry/opentelemetry-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exporter.go
72 lines (58 loc) · 1.5 KB
/
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 stdoutlog // import "go.opentelemetry.io/otel/exporters/stdout/stdoutlog"
import (
"context"
"encoding/json"
"sync/atomic"
"go.opentelemetry.io/otel/sdk/log"
)
var _ log.Exporter = &Exporter{}
// Exporter writes JSON-encoded log records to an [io.Writer] ([os.Stdout] by default).
// Exporter must be created with [New].
type Exporter struct {
encoder atomic.Pointer[json.Encoder]
timestamps bool
}
// New creates an [Exporter].
func New(options ...Option) (*Exporter, error) {
cfg := newConfig(options)
enc := json.NewEncoder(cfg.Writer)
if cfg.PrettyPrint {
enc.SetIndent("", "\t")
}
e := Exporter{
timestamps: cfg.Timestamps,
}
e.encoder.Store(enc)
return &e, nil
}
// Export exports log records to writer.
func (e *Exporter) Export(ctx context.Context, records []log.Record) error {
enc := e.encoder.Load()
if enc == nil {
return nil
}
for _, record := range records {
// Honor context cancellation.
if err := ctx.Err(); err != nil {
return err
}
// Encode record, one by one.
recordJSON := e.newRecordJSON(record)
if err := enc.Encode(recordJSON); err != nil {
return err
}
}
return nil
}
// Shutdown shuts down the Exporter.
// Calls to Export will perform no operation after this is called.
func (e *Exporter) Shutdown(context.Context) error {
e.encoder.Store(nil)
return nil
}
// ForceFlush performs no action.
func (e *Exporter) ForceFlush(context.Context) error {
return nil
}