-
Notifications
You must be signed in to change notification settings - Fork 13
/
options.go
179 lines (143 loc) · 5 KB
/
options.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package otel
import (
"encoding/base64"
"errors"
"net/url"
"time"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp"
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
"google.golang.org/grpc/credentials"
"github.com/heroku/x/tlsconfig"
)
var (
// ErrExporterNil is returned if an exporter is required, but is not passed in.
ErrExporterNil = errors.New("exporter cannot be nil")
// ErrAggregatorNil is returned if an aggregator is required, but is not passed in.
ErrAggregatorNil = errors.New("aggregator cannot be nil")
// ErrEndpointNil is returned if an endpoint is required, but is not passed in.
ErrEndpointNil = errors.New("endpoint cannot be nil")
)
var (
DefaultAggregationSelector = WithExponentialHistograms
DefaultEndpointExporter = WithHTTPExporter
)
const (
// DefaultAgentEndpoint is a default exporter endpoint that points to a local otel collector.
DefaultAgentEndpoint = "http://0.0.0.0:55680"
)
// Option is used for optional arguments when initializing Provider.
type Option func(*config) error
func WithPrefix(prefix string) Option {
return func(cfg *config) error {
cfg.prefix = prefix
return nil
}
}
// WithCollectPeriod initial
func WithCollectPeriod(collectPeriod time.Duration) Option {
return func(c *config) error {
c.collectPeriod = collectPeriod
return nil
}
}
func CumulativeTemporalitySelector(_ metric.InstrumentKind) metricdata.Temporality {
return metricdata.CumulativeTemporality
}
func DeltaTemporalitySelector(_ metric.InstrumentKind) metricdata.Temporality {
return metricdata.DeltaTemporality
}
func WithCumulativeTemporality() Option {
return WithTemporalitySelector(CumulativeTemporalitySelector)
}
func WithDeltaTemporality() Option {
return WithTemporalitySelector(DeltaTemporalitySelector)
}
func WithTemporalitySelector(temporality metric.TemporalitySelector) Option {
return func(c *config) error {
c.temporalitySelector = temporality
return nil
}
}
func WithExponentialAggregation() Option {
return WithAggregationSelector(ExponentialAggregationSelector)
}
func WithExplicitAggregation() Option {
return WithAggregationSelector(ExplicitAggregationSelector)
}
// Deprecated: WithExponentialHistograms is deprecated use WithExponentialAggregation instead.
func WithExponentialHistograms() Option {
return WithExponentialAggregation()
}
// Deprecated: WithExplicitHistograms is deprecated use WithExplicitAggregation instead.
func WithExplicitHistograms() Option {
return WithExplicitAggregation()
}
func WithAggregationSelector(selector metric.AggregationSelector) Option {
return func(c *config) error {
c.aggregationSelector = selector
return nil
}
}
func WithHTTPExporter(options ...otlpmetrichttp.Option) Option {
return WithHTTPEndpointExporter(DefaultAgentEndpoint, options...)
}
func WithHTTPEndpointExporter(endpoint string, options ...otlpmetrichttp.Option) Option {
return WithExporterFunc(func(cfg *config) (metric.Exporter, error) {
u, err := url.Parse(endpoint)
if err != nil {
return nil, err
}
defaults := []otlpmetrichttp.Option{
otlpmetrichttp.WithEndpoint(u.Host),
otlpmetrichttp.WithAggregationSelector(cfg.aggregationSelector),
otlpmetrichttp.WithTemporalitySelector(cfg.temporalitySelector),
}
if u.Scheme == "https" {
defaults = append(defaults, otlpmetrichttp.WithTLSClientConfig(tlsconfig.New()))
} else {
defaults = append(defaults, otlpmetrichttp.WithInsecure())
}
if u.User.String() != "" {
authHeader := make(map[string]string)
authHeader["Authorization"] = "Basic " + base64.StdEncoding.EncodeToString([]byte(u.User.String()))
defaults = append(defaults, otlpmetrichttp.WithHeaders(authHeader))
}
// finally append any passed in options
options = append(defaults, options...)
return otlpmetrichttp.New(cfg.ctx, options...)
})
}
func WithGRPCExporter(endpoint string, options ...otlpmetricgrpc.Option) Option {
return WithExporterFunc(func(cfg *config) (metric.Exporter, error) {
u, err := url.Parse(endpoint)
if err != nil {
return nil, err
}
defaults := []otlpmetricgrpc.Option{
otlpmetricgrpc.WithEndpoint(u.Host),
otlpmetricgrpc.WithAggregationSelector(cfg.aggregationSelector),
otlpmetricgrpc.WithTemporalitySelector(cfg.temporalitySelector),
}
if u.Scheme == "https" {
// use system root ca for TLS
defaults = append(defaults, otlpmetricgrpc.WithTLSCredentials(credentials.NewTLS(tlsconfig.New())))
} else {
defaults = append(defaults, otlpmetricgrpc.WithInsecure())
}
if u.User.String() != "" {
authHeader := make(map[string]string)
authHeader["Authorization"] = "Basic " + base64.StdEncoding.EncodeToString([]byte(u.User.String()))
defaults = append(defaults, otlpmetricgrpc.WithHeaders(authHeader))
}
options = append(defaults, options...)
return otlpmetricgrpc.New(cfg.ctx, options...)
})
}
func WithExporterFunc(fn exporterFactory) Option {
return func(c *config) error {
c.exporterFactory = fn
return nil
}
}