-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathconfig.go
119 lines (98 loc) · 3.28 KB
/
config.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
package state
import (
"github.com/krakend/krakend-otel/config"
luraconfig "github.com/luraproject/lura/v2/config"
)
type Config interface {
OTEL() OTEL
// GlobalOpts gets the configuration at the service level.
GlobalOpts() *config.GlobalOpts
// Gets the OTEL instance for a given endpoint
EndpointOTEL(cfg *luraconfig.EndpointConfig) OTEL
// EndpointPipeOpts retrieve "proxy" level configuration for a given
// endpoint.
EndpointPipeOpts(cfg *luraconfig.EndpointConfig) *config.PipeOpts
// EndpointBackendOpts should return a config for all the child
// backend of this endpoint.
//
// Deprecated: the interface should only need to fetch the BackendOpts
// from a luraconfig.Backend when configuring at the Backend level:
// the BackendOpts function must be used instead.
EndpointBackendOpts(cfg *luraconfig.Backend) *config.BackendOpts
BackendOTEL(cfg *luraconfig.Backend) OTEL
BackendOpts(cfg *luraconfig.Backend) *config.BackendOpts
// SkipEndpoint tells if an endpoint should not be instrumented
SkipEndpoint(endpoint string) bool
}
var _ Config = (*StateConfig)(nil)
type StateConfig struct {
cfgData config.ConfigData
}
func (*StateConfig) OTEL() OTEL {
return GlobalState()
}
func (s *StateConfig) GlobalOpts() *config.GlobalOpts {
return s.cfgData.Layers.Global
}
func (*StateConfig) EndpointOTEL(_ *luraconfig.EndpointConfig) OTEL {
return GlobalState()
}
// EndpointPipeOpts checks if there is an override for pipe ("proxy")
// options at the endpoint levels a fully replaces (it DOES NOT MERGE
// attributes) the existing config from the service level configuration.
// If none of those configs are found, it falls back to the defaults.
func (s *StateConfig) EndpointPipeOpts(cfg *luraconfig.EndpointConfig) *config.PipeOpts {
var opts *config.PipeOpts
if s != nil && s.cfgData.Layers != nil {
opts = s.cfgData.Layers.Pipe
}
cfgLayer, err := config.LuraLayerExtraCfg(cfg.ExtraConfig)
if err == nil && cfgLayer != nil && cfgLayer.Pipe != nil {
opts = cfgLayer.Pipe
}
if opts == nil {
return new(config.PipeOpts)
}
return opts
}
// EndpointBackendOpts is a bad interface function, as is should receive
// as a param a luraconfig.Endpoint .. but also makes no sense to have it
// because we only need the backend configuration at
func (s *StateConfig) EndpointBackendOpts(cfg *luraconfig.Backend) *config.BackendOpts {
return s.mergedBackendOpts(cfg)
}
func (*StateConfig) BackendOTEL(_ *luraconfig.Backend) OTEL {
return GlobalState()
}
func (s *StateConfig) BackendOpts(cfg *luraconfig.Backend) *config.BackendOpts {
return s.mergedBackendOpts(cfg)
}
func (s *StateConfig) mergedBackendOpts(cfg *luraconfig.Backend) *config.BackendOpts {
var opts *config.BackendOpts
if s != nil && s.cfgData.Layers != nil {
opts = s.cfgData.Layers.Backend
}
cfgLayer, err := config.LuraLayerExtraCfg(cfg.ExtraConfig)
if err == nil && cfgLayer != nil && cfgLayer.Backend != nil {
opts = cfgLayer.Backend
}
if opts == nil {
return new(config.BackendOpts)
}
return opts
}
func (s *StateConfig) SkipEndpoint(endpoint string) bool {
for _, toSkip := range s.cfgData.SkipPaths {
if toSkip == endpoint {
return true
}
}
return false
}
func NewConfig(cfgData *config.ConfigData) *StateConfig {
s := &StateConfig{
cfgData: *cfgData,
}
s.cfgData.UnsetFieldsToDefaults()
return s
}