Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/integrations/v2/metricsutils/versionshim.go
5363 views
1
package metricsutils
2
3
import (
4
"context"
5
"errors"
6
"fmt"
7
"net/http"
8
9
"github.com/go-kit/log"
10
"github.com/prometheus/common/model"
11
12
v1 "github.com/grafana/agent/pkg/integrations"
13
v2 "github.com/grafana/agent/pkg/integrations/v2"
14
"github.com/grafana/agent/pkg/integrations/v2/common"
15
"github.com/grafana/agent/pkg/util"
16
)
17
18
// NewNamedShim returns a v2.UpgradeFunc which will upgrade a v1.Config to a
19
// v2.Config with a new name.
20
func NewNamedShim(newName string) v2.UpgradeFunc {
21
return func(before v1.Config, common common.MetricsConfig) v2.UpgradedConfig {
22
return &configShim{
23
orig: before,
24
common: common,
25
nameOverride: newName,
26
}
27
}
28
}
29
30
// Shim upgrades a v1.Config to a v2.Config. The resulting config is NOT
31
// registered. Shim matches the v2.UpgradeFunc type.
32
func Shim(before v1.Config, common common.MetricsConfig) (after v2.UpgradedConfig) {
33
return &configShim{orig: before, common: common}
34
}
35
36
type configShim struct {
37
orig v1.Config
38
common common.MetricsConfig
39
nameOverride string
40
}
41
42
var (
43
_ v2.Config = (*configShim)(nil)
44
_ v2.UpgradedConfig = (*configShim)(nil)
45
_ v2.ComparableConfig = (*configShim)(nil)
46
)
47
48
func (s *configShim) LegacyConfig() (v1.Config, common.MetricsConfig) { return s.orig, s.common }
49
50
func (s *configShim) Name() string {
51
if s.nameOverride != "" {
52
return s.nameOverride
53
}
54
return s.orig.Name()
55
}
56
57
func (s *configShim) ApplyDefaults(g v2.Globals) error {
58
s.common.ApplyDefaults(g.SubsystemOpts.Metrics.Autoscrape)
59
if id, err := s.Identifier(g); err == nil {
60
s.common.InstanceKey = &id
61
}
62
return nil
63
}
64
65
func (s *configShim) ConfigEquals(c v2.Config) bool {
66
o, ok := c.(*configShim)
67
if !ok {
68
return false
69
}
70
return util.CompareYAML(s.orig, o.orig) && util.CompareYAML(s.common, o.common)
71
}
72
73
func (s *configShim) Identifier(g v2.Globals) (string, error) {
74
if s.common.InstanceKey != nil {
75
return *s.common.InstanceKey, nil
76
}
77
return s.orig.InstanceKey(g.AgentIdentifier)
78
}
79
80
func (s *configShim) NewIntegration(l log.Logger, g v2.Globals) (v2.Integration, error) {
81
v1Integration, err := s.orig.NewIntegration(l)
82
if err != nil {
83
return nil, err
84
}
85
86
id, err := s.Identifier(g)
87
if err != nil {
88
return nil, err
89
}
90
91
// Generate our handler. Original integrations didn't accept a prefix, and
92
// just assumed that they would be wired to /metrics somewhere.
93
handler, err := v1Integration.MetricsHandler()
94
if err != nil {
95
return nil, fmt.Errorf("generating http handler: %w", err)
96
} else if handler == nil {
97
handler = http.NotFoundHandler()
98
}
99
100
// Generate targets. Original integrations used a static set of targets,
101
// so this mapping can always be generated just once.
102
//
103
// Targets are generated from the result of ScrapeConfigs(), which returns a
104
// tuple of job name and relative metrics path.
105
//
106
// Job names were prefixed at the subsystem level with integrations/, so we
107
// will retain that behavior here.
108
v1ScrapeConfigs := v1Integration.ScrapeConfigs()
109
targets := make([]handlerTarget, 0, len(v1ScrapeConfigs))
110
for _, sc := range v1ScrapeConfigs {
111
targets = append(targets, handlerTarget{
112
MetricsPath: sc.MetricsPath,
113
Labels: model.LabelSet{
114
model.JobLabel: model.LabelValue("integrations/" + sc.JobName),
115
},
116
})
117
}
118
119
// Convert the run function. Original integrations sometimes returned
120
// ctx.Err() on exit. This isn't recommended anymore, but we need to hide the
121
// error if it happens, since the error was previously ignored.
122
runFunc := func(ctx context.Context) error {
123
err := v1Integration.Run(ctx)
124
switch {
125
case err == nil:
126
return nil
127
case errors.Is(err, context.Canceled) && ctx.Err() != nil:
128
// Hide error that no longer happens in newer integrations.
129
return nil
130
default:
131
return err
132
}
133
}
134
135
// Aggregate our converted settings into a v2 integration.
136
return &metricsHandlerIntegration{
137
integrationName: s.Name(),
138
instanceID: id,
139
140
common: s.common,
141
globals: g,
142
handler: handler,
143
targets: targets,
144
145
runFunc: runFunc,
146
}, nil
147
}
148
149