-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
otelmetric_option.go
108 lines (95 loc) · 3.16 KB
/
otelmetric_option.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
// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package otelmetric
import (
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/resource"
)
// newProviderConfigByOptions returns a config configured with options.
func newProviderConfigByOptions(options []Option) providerConfig {
conf := providerConfig{}
for _, o := range options {
conf = o.apply(conf)
}
return conf
}
// Option applies a configuration option value to a MeterProvider.
type Option interface {
apply(providerConfig) providerConfig
}
// optionFunc applies a set of options to a config.
type optionFunc func(providerConfig) providerConfig
// apply returns a config with option(s) applied.
func (o optionFunc) apply(conf providerConfig) providerConfig {
return o(conf)
}
// providerConfig is the configuration for Provider.
type providerConfig struct {
viewOption metric.Option
readerOption metric.Option
resourceOption metric.Option
enabledBuiltInMetrics bool
}
// IsBuiltInMetricsEnabled returns whether the builtin metrics is enabled.
func (cfg providerConfig) IsBuiltInMetricsEnabled() bool {
return cfg.enabledBuiltInMetrics
}
// MetricOptions converts and returns the providerConfig as metrics options.
func (cfg providerConfig) MetricOptions() []metric.Option {
var metricOptions = make([]metric.Option, 0)
if cfg.viewOption != nil {
metricOptions = append(metricOptions, cfg.viewOption)
}
if cfg.readerOption != nil {
metricOptions = append(metricOptions, cfg.readerOption)
}
if cfg.resourceOption != nil {
metricOptions = append(metricOptions, cfg.resourceOption)
}
return metricOptions
}
// WithBuiltInMetrics enables builtin metrics.
func WithBuiltInMetrics() Option {
return optionFunc(func(cfg providerConfig) providerConfig {
cfg.enabledBuiltInMetrics = true
return cfg
})
}
// WithResource associates a Resource with a MeterProvider. This Resource
// represents the entity producing telemetry and is associated with all Meters
// the MeterProvider will create.
func WithResource(res *resource.Resource) Option {
return optionFunc(func(cfg providerConfig) providerConfig {
cfg.resourceOption = metric.WithResource(res)
return cfg
})
}
// WithReader associates Reader r with a MeterProvider.
//
// By default, if this option is not used, the MeterProvider will perform no
// operations; no data will be exported without a Reader.
func WithReader(reader metric.Reader) Option {
return optionFunc(func(cfg providerConfig) providerConfig {
if reader == nil {
return cfg
}
cfg.readerOption = metric.WithReader(reader)
return cfg
})
}
// WithView associates views a MeterProvider.
//
// Views are appended to existing ones in a MeterProvider if this option is
// used multiple times.
//
// By default, if this option is not used, the MeterProvider will use the
// default view.
func WithView(views ...metric.View) Option {
return optionFunc(func(cfg providerConfig) providerConfig {
cfg.viewOption = metric.WithView(views...)
return cfg
})
}