option.go   A
last analyzed

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 25
dl 0
loc 53
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A otelroundtripper.WithName 0 4 3
A otelroundtripper.WithMeter 0 3 2
A otelroundtripper.optionFunc.apply 0 2 1
A otelroundtripper.WithParent 0 4 3
A otelroundtripper.WithAttributes 0 3 2
1
package otelroundtripper
2
3
import (
4
	"net/http"
5
	"strings"
6
7
	"go.opentelemetry.io/otel/attribute"
8
	"go.opentelemetry.io/otel/metric"
9
)
10
11
// Option applies a configuration to the given config
12
type Option interface {
13
	apply(cfg *config)
14
}
15
16
type optionFunc func(cfg *config)
17
18
func (fn optionFunc) apply(cfg *config) {
19
	fn(cfg)
20
}
21
22
// WithParent sets the underlying http.RoundTripper which is wrapped by this round tripper.
23
// If the provided http.RoundTripper is nil, http.DefaultTransport will be used as the base http.RoundTripper
24
func WithParent(parent http.RoundTripper) Option {
25
	return optionFunc(func(cfg *config) {
26
		if parent != nil {
27
			cfg.parent = parent
28
		}
29
	})
30
}
31
32
// WithName sets the prefix for the metrics emitted by this round tripper.
33
// by default, the "http.client" name is used.
34
func WithName(name string) Option {
35
	return optionFunc(func(cfg *config) {
36
		if strings.TrimSpace(name) != "" {
37
			cfg.name = strings.TrimSpace(name)
38
		}
39
	})
40
}
41
42
// WithMeter sets the underlying  metric.Meter that is used to create metric instruments
43
// By default the no-op meter is used.
44
func WithMeter(meter metric.Meter) Option {
45
	return optionFunc(func(cfg *config) {
46
		cfg.meter = meter
47
	})
48
}
49
50
// WithAttributes sets a list of attribute.KeyValue labels for all metrics associated with this round tripper
51
func WithAttributes(attributes ...attribute.KeyValue) Option {
52
	return optionFunc(func(cfg *config) {
53
		cfg.attributes = attributes
54
	})
55
}
56