Path: blob/main/pkg/integrations/v2/metricsutils/versionshim.go
5363 views
package metricsutils12import (3"context"4"errors"5"fmt"6"net/http"78"github.com/go-kit/log"9"github.com/prometheus/common/model"1011v1 "github.com/grafana/agent/pkg/integrations"12v2 "github.com/grafana/agent/pkg/integrations/v2"13"github.com/grafana/agent/pkg/integrations/v2/common"14"github.com/grafana/agent/pkg/util"15)1617// NewNamedShim returns a v2.UpgradeFunc which will upgrade a v1.Config to a18// v2.Config with a new name.19func NewNamedShim(newName string) v2.UpgradeFunc {20return func(before v1.Config, common common.MetricsConfig) v2.UpgradedConfig {21return &configShim{22orig: before,23common: common,24nameOverride: newName,25}26}27}2829// Shim upgrades a v1.Config to a v2.Config. The resulting config is NOT30// registered. Shim matches the v2.UpgradeFunc type.31func Shim(before v1.Config, common common.MetricsConfig) (after v2.UpgradedConfig) {32return &configShim{orig: before, common: common}33}3435type configShim struct {36orig v1.Config37common common.MetricsConfig38nameOverride string39}4041var (42_ v2.Config = (*configShim)(nil)43_ v2.UpgradedConfig = (*configShim)(nil)44_ v2.ComparableConfig = (*configShim)(nil)45)4647func (s *configShim) LegacyConfig() (v1.Config, common.MetricsConfig) { return s.orig, s.common }4849func (s *configShim) Name() string {50if s.nameOverride != "" {51return s.nameOverride52}53return s.orig.Name()54}5556func (s *configShim) ApplyDefaults(g v2.Globals) error {57s.common.ApplyDefaults(g.SubsystemOpts.Metrics.Autoscrape)58if id, err := s.Identifier(g); err == nil {59s.common.InstanceKey = &id60}61return nil62}6364func (s *configShim) ConfigEquals(c v2.Config) bool {65o, ok := c.(*configShim)66if !ok {67return false68}69return util.CompareYAML(s.orig, o.orig) && util.CompareYAML(s.common, o.common)70}7172func (s *configShim) Identifier(g v2.Globals) (string, error) {73if s.common.InstanceKey != nil {74return *s.common.InstanceKey, nil75}76return s.orig.InstanceKey(g.AgentIdentifier)77}7879func (s *configShim) NewIntegration(l log.Logger, g v2.Globals) (v2.Integration, error) {80v1Integration, err := s.orig.NewIntegration(l)81if err != nil {82return nil, err83}8485id, err := s.Identifier(g)86if err != nil {87return nil, err88}8990// Generate our handler. Original integrations didn't accept a prefix, and91// just assumed that they would be wired to /metrics somewhere.92handler, err := v1Integration.MetricsHandler()93if err != nil {94return nil, fmt.Errorf("generating http handler: %w", err)95} else if handler == nil {96handler = http.NotFoundHandler()97}9899// Generate targets. Original integrations used a static set of targets,100// so this mapping can always be generated just once.101//102// Targets are generated from the result of ScrapeConfigs(), which returns a103// tuple of job name and relative metrics path.104//105// Job names were prefixed at the subsystem level with integrations/, so we106// will retain that behavior here.107v1ScrapeConfigs := v1Integration.ScrapeConfigs()108targets := make([]handlerTarget, 0, len(v1ScrapeConfigs))109for _, sc := range v1ScrapeConfigs {110targets = append(targets, handlerTarget{111MetricsPath: sc.MetricsPath,112Labels: model.LabelSet{113model.JobLabel: model.LabelValue("integrations/" + sc.JobName),114},115})116}117118// Convert the run function. Original integrations sometimes returned119// ctx.Err() on exit. This isn't recommended anymore, but we need to hide the120// error if it happens, since the error was previously ignored.121runFunc := func(ctx context.Context) error {122err := v1Integration.Run(ctx)123switch {124case err == nil:125return nil126case errors.Is(err, context.Canceled) && ctx.Err() != nil:127// Hide error that no longer happens in newer integrations.128return nil129default:130return err131}132}133134// Aggregate our converted settings into a v2 integration.135return &metricsHandlerIntegration{136integrationName: s.Name(),137instanceID: id,138139common: s.common,140globals: g,141handler: handler,142targets: targets,143144runFunc: runFunc,145}, nil146}147148149