Debian Codegraph @

exporters/stdout/stdoutmetric/config.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package stdoutmetric // import "go.opentelemetry.io/otel/exporters/stdout/stdoutmetric"
5
6 import (
7 "encoding/json"
8 "io"
9 "os"
10
11 "go.opentelemetry.io/otel/sdk/metric"
12 )
13
14 // config contains options for the exporter.
15 type config struct {
16 prettyPrint bool
17 encoder *encoderHolder
18 temporalitySelector metric.TemporalitySelector
19 aggregationSelector metric.AggregationSelector
20 redactTimestamps bool
21 }
22
23 // newConfig creates a validated config configured with options.
24 func newConfig(options ...Option) config {
25 cfg := config{}
26 for _, opt := range options {
27 cfg = opt.apply(cfg)
28 }
29
30 if cfg.encoder == nil {
31 enc := json.NewEncoder(os.Stdout)
32 cfg.encoder = &encoderHolder{encoder: enc}
33 }
34
35 if cfg.prettyPrint {
36 if e, ok := cfg.encoder.encoder.(*json.Encoder); ok {
37 e.SetIndent("", "\t")
38 }
39 }
40
41 if cfg.temporalitySelector == nil {
42 cfg.temporalitySelector = metric.DefaultTemporalitySelector
43 }
44
45 if cfg.aggregationSelector == nil {
46 cfg.aggregationSelector = metric.DefaultAggregationSelector
47 }
48
49 return cfg
50 }
51
52 // Option sets exporter option values.
53 type Option interface {
54 apply(config) config
55 }
56
57 type optionFunc func(config) config
58
59 func (o optionFunc) apply(c config) config {
60 return o(c)
61 }
62
63 // WithEncoder sets the exporter to use encoder to encode all the metric
64 // data-types to an output.
65 func WithEncoder(encoder Encoder) Option {
66 return optionFunc(func(c config) config {
67 if encoder != nil {
68 c.encoder = &encoderHolder{encoder: encoder}
69 }
70 return c
71 })
72 }
73
74 // WithWriter sets the export stream destination.
75 // Using this option overrides any previously set encoder.
76 func WithWriter(w io.Writer) Option {
77 return WithEncoder(json.NewEncoder(w))
78 }
79
80 // WithPrettyPrint prettifies the emitted output.
81 // This option only works if the encoder is a *json.Encoder, as is the case
82 // when using `WithWriter`.
83 func WithPrettyPrint() Option {
84 return optionFunc(func(c config) config {
85 c.prettyPrint = true
86 return c
87 })
88 }
89
90 // WithTemporalitySelector sets the TemporalitySelector the exporter will use
91 // to determine the Temporality of an instrument based on its kind. If this
92 // option is not used, the exporter will use the DefaultTemporalitySelector
93 // from the go.opentelemetry.io/otel/sdk/metric package.
94 func WithTemporalitySelector(selector metric.TemporalitySelector) Option {
95 return temporalitySelectorOption{selector: selector}
96 }
97
98 type temporalitySelectorOption struct {
99 selector metric.TemporalitySelector
100 }
101
102 func (t temporalitySelectorOption) apply(c config) config {
103 c.temporalitySelector = t.selector
104 return c
105 }
106
107 // WithAggregationSelector sets the AggregationSelector the exporter will use
108 // to determine the aggregation to use for an instrument based on its kind. If
109 // this option is not used, the exporter will use the
110 // DefaultAggregationSelector from the go.opentelemetry.io/otel/sdk/metric
111 // package or the aggregation explicitly passed for a view matching an
112 // instrument.
113 func WithAggregationSelector(selector metric.AggregationSelector) Option {
114 return aggregationSelectorOption{selector: selector}
115 }
116
117 type aggregationSelectorOption struct {
118 selector metric.AggregationSelector
119 }
120
121 func (t aggregationSelectorOption) apply(c config) config {
122 c.aggregationSelector = t.selector
123 return c
124 }
125
126 // WithoutTimestamps sets all timestamps to zero in the output stream.
127 func WithoutTimestamps() Option {
128 return optionFunc(func(c config) config {
129 c.redactTimestamps = true
130 return c
131 })
132 }