Debian Codegraph @

instrumentation/runtime/options.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package runtime // import "go.opentelemetry.io/contrib/instrumentation/runtime"
5
6 import (
7 "time"
8
9 "go.opentelemetry.io/otel"
10 "go.opentelemetry.io/otel/metric"
11 )
12
13 // config contains optional settings for reporting runtime metrics.
14 type config struct {
15 // MinimumReadMemStatsInterval sets the minimum interval
16 // between calls to runtime.ReadMemStats(). Negative values
17 // are ignored.
18 MinimumReadMemStatsInterval time.Duration
19
20 // MeterProvider sets the metric.MeterProvider. If nil, the global
21 // Provider will be used.
22 MeterProvider metric.MeterProvider
23 }
24
25 // Option supports configuring optional settings for runtime metrics.
26 type Option interface {
27 apply(*config)
28 }
29
30 // ProducerOption supports configuring optional settings for runtime metrics using a
31 // metric producer in addition to standard instrumentation.
32 type ProducerOption interface {
33 Option
34 applyProducer(*config)
35 }
36
37 // DefaultMinimumReadMemStatsInterval is the default minimum interval
38 // between calls to runtime.ReadMemStats(). Use the
39 // WithMinimumReadMemStatsInterval() option to modify this setting in
40 // Start().
41 const DefaultMinimumReadMemStatsInterval time.Duration = 15 * time.Second
42
43 // WithMinimumReadMemStatsInterval sets a minimum interval between calls to
44 // runtime.ReadMemStats(), which is a relatively expensive call to make
45 // frequently. This setting is ignored when `d` is negative.
46 func WithMinimumReadMemStatsInterval(d time.Duration) Option {
47 return minimumReadMemStatsIntervalOption(d)
48 }
49
50 type minimumReadMemStatsIntervalOption time.Duration
51
52 func (o minimumReadMemStatsIntervalOption) apply(c *config) {
53 if o >= 0 {
54 c.MinimumReadMemStatsInterval = time.Duration(o)
55 }
56 }
57
58 func (o minimumReadMemStatsIntervalOption) applyProducer(c *config) { o.apply(c) }
59
60 // WithMeterProvider sets the Metric implementation to use for
61 // reporting. If this option is not used, the global metric.MeterProvider
62 // will be used. `provider` must be non-nil.
63 func WithMeterProvider(provider metric.MeterProvider) Option {
64 return metricProviderOption{provider}
65 }
66
67 type metricProviderOption struct{ metric.MeterProvider }
68
69 func (o metricProviderOption) apply(c *config) {
70 if o.MeterProvider != nil {
71 c.MeterProvider = o.MeterProvider
72 }
73 }
74
75 // newConfig computes a config from the supplied Options.
76 func newConfig(opts ...Option) config {
77 c := config{
78 MeterProvider: otel.GetMeterProvider(),
79 }
80 for _, opt := range opts {
81 opt.apply(&c)
82 }
83 if c.MinimumReadMemStatsInterval <= 0 {
84 c.MinimumReadMemStatsInterval = DefaultMinimumReadMemStatsInterval
85 }
86 return c
87 }
88
89 // newConfig computes a config from the supplied ProducerOptions.
90 func newProducerConfig(opts ...ProducerOption) config {
91 c := config{}
92 for _, opt := range opts {
93 opt.applyProducer(&c)
94 }
95 if c.MinimumReadMemStatsInterval <= 0 {
96 c.MinimumReadMemStatsInterval = DefaultMinimumReadMemStatsInterval
97 }
98 return c
99 }