forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stdout.go
70 lines (58 loc) · 1.72 KB
/
stdout.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package stdout // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/output/stdout"
import (
"context"
"encoding/json"
"io"
"os"
"sync"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/entry"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper"
)
// Stdout is a global handle to standard output
var Stdout io.Writer = os.Stdout
func init() {
operator.Register("stdout", func() operator.Builder { return NewConfig("") })
}
// NewConfig creates a new stdout config with default values
func NewConfig(operatorID string) *Config {
return &Config{
OutputConfig: helper.NewOutputConfig(operatorID, "stdout"),
}
}
// Config is the configuration of the Stdout operator
type Config struct {
helper.OutputConfig `mapstructure:",squash"`
}
// Build will build a stdout operator.
func (c Config) Build(logger *zap.SugaredLogger) (operator.Operator, error) {
outputOperator, err := c.OutputConfig.Build(logger)
if err != nil {
return nil, err
}
return &Output{
OutputOperator: outputOperator,
encoder: json.NewEncoder(Stdout),
}, nil
}
// Output is an operator that logs entries using stdout.
type Output struct {
helper.OutputOperator
encoder *json.Encoder
mux sync.Mutex
}
// Process will log entries received.
func (o *Output) Process(_ context.Context, entry *entry.Entry) error {
o.mux.Lock()
err := o.encoder.Encode(entry)
if err != nil {
o.mux.Unlock()
o.Errorf("Failed to process entry: %s", err)
return err
}
o.mux.Unlock()
return nil
}