This repository was archived by the owner on Jul 28, 2026. It is now read-only.
Repository navigation
[dev.multiple-integrations] integrations/v2: allow shimming between v1 and v2 integrations. #1179
Merged
rfratto
merged 4 commits into
grafana-cold-storage:dev.multiple-integrations
from
rfratto:integrations-v2-shim
Dec 16, 2021
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
60005fc
integrations/v2: allow shimming between v1 and v2 integrations.
rfratto ca4a46b
fix lint
rfratto 2ed0101
pkg/integrations/v2: use "RegisterLegacy" instead of a generic mechanism
rfratto cb2e94b
fine, I won't add the deprecation notice if it will make the linter sad
rfratto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package install | ||
|
|
||
| import ( | ||
| v1 "github.com/grafana/agent/pkg/integrations" | ||
| v2 "github.com/grafana/agent/pkg/integrations/v2" | ||
| metricsutils "github.com/grafana/agent/pkg/integrations/v2/metricsutils" | ||
| ) | ||
|
|
||
| // Perform a migration of v1 integrations which do not yet have a v2 | ||
| // counterpart. These integrations will be registered as Singletons | ||
| // to maintain existing behavior. | ||
| // | ||
| // To migrate a v1 integration to a v2 migration with support for multiple | ||
| // instances, v1 integrations must manually migrate themselves by calling | ||
| // v2.RegisterDynamic directly. | ||
| func init() { | ||
| for _, v1Integration := range v1.RegisteredIntegrations() { | ||
| // Look to see if there's a v2 integration with the same name. | ||
| var found bool | ||
| for _, v2Integration := range v2.Registered() { | ||
| if v2Integration.Name() == v1Integration.Name() { | ||
| found = true | ||
| break | ||
| } | ||
| } | ||
| if !found { | ||
| v2.RegisterDynamic(v1Integration, v1Integration.Name(), v2.TypeSingleton, func(in interface{}) v2.WrappedConfig { | ||
| return metricsutils.CreateShim(in.(v1.Config)) | ||
| }) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| package metricsutils | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "net/http" | ||
|
|
||
| "github.com/go-kit/log" | ||
| "github.com/prometheus/common/model" | ||
|
|
||
| v1 "github.com/grafana/agent/pkg/integrations" | ||
| v2 "github.com/grafana/agent/pkg/integrations/v2" | ||
| ) | ||
|
|
||
| // CreateShim creates a shim between the v1.Config and v2.Config. The resulting | ||
| // config is NOT registered. | ||
| func CreateShim(before v1.Config) (after v2.WrappedConfig) { | ||
| return &configShim{Orig: before} | ||
| } | ||
|
|
||
| type configShim struct { | ||
| Orig v1.Config | ||
| } | ||
|
|
||
| var ( | ||
| _ v2.Config = (*configShim)(nil) | ||
| _ v2.WrappedConfig = (*configShim)(nil) | ||
| ) | ||
|
|
||
| // UnwrapConfig implements v2.WrappedConfig. | ||
| func (s *configShim) UnwrapConfig() interface{} { return s.Orig } | ||
|
|
||
| func (s *configShim) Name() string { return s.Orig.Name() } | ||
| func (s *configShim) Identifier(g v2.Globals) (string, error) { | ||
| return s.Orig.InstanceKey(g.AgentIdentifier) | ||
| } | ||
|
|
||
| func (s *configShim) NewIntegration(l log.Logger, g v2.Globals) (v2.Integration, error) { | ||
| v1Integration, err := s.Orig.NewIntegration(l) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| id, err := s.Identifier(g) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // Map from the original CommonConfig to the new settings. This is a 1:1 | ||
| // mapping, minus the loss of WALTruncateFrequency. | ||
| origCommon := s.Orig.CommonConfig() | ||
| newCommon := CommonConfig{ | ||
| Enabled: origCommon.Enabled, | ||
| InstanceKey: origCommon.InstanceKey, | ||
| ScrapeIntegration: origCommon.ScrapeIntegration, | ||
| ScrapeInterval: origCommon.ScrapeInterval, | ||
| ScrapeTimeout: origCommon.ScrapeTimeout, | ||
| RelabelConfigs: origCommon.RelabelConfigs, | ||
| MetricRelabelConfigs: origCommon.MetricRelabelConfigs, | ||
| } | ||
|
|
||
| // Generate our handler. Original integrations didn't accept a prefix, and | ||
| // just assumed that they would be wired to /metrics somewhere. | ||
| handler, err := v1Integration.MetricsHandler() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("generating http handler: %w", err) | ||
| } else if handler == nil { | ||
| handler = http.NotFoundHandler() | ||
| } | ||
|
|
||
| // Generate targets. Original integrations used a static set of targets, | ||
| // so this mapping can always be generated just once. | ||
| // | ||
| // Targets are generated from the result of ScrapeConfigs(), which returns a | ||
| // tuple of job name and relative metrics path. | ||
| // | ||
| // Job names were prefixed at the subsystem level with integrations/, so we | ||
| // will retain that behavior here. | ||
| v1ScrapeConfigs := v1Integration.ScrapeConfigs() | ||
| targets := make([]handlerTarget, 0, len(v1ScrapeConfigs)) | ||
| for _, sc := range v1ScrapeConfigs { | ||
| targets = append(targets, handlerTarget{ | ||
| MetricsPath: sc.MetricsPath, | ||
| Labels: model.LabelSet{ | ||
| model.JobLabel: model.LabelValue("integrations/" + sc.JobName), | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| // Convert he run function. Original integrations sometimes returned | ||
| // ctx.Err() on exit. This isn't recommended anymore, but we need to hide the | ||
| // error if it happens, since the error was previously ignored. | ||
| runFunc := func(ctx context.Context) error { | ||
| err := v1Integration.Run(ctx) | ||
| switch { | ||
| case err == nil: | ||
| return nil | ||
| case errors.Is(err, context.Canceled) && ctx.Err() != nil: | ||
| // Hide error that no longer happens in newer integrations. | ||
| return nil | ||
| default: | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| // Aggregate our converted settings into a v2 integration. | ||
| return &metricsHandlerIntegration{ | ||
| integrationName: s.Orig.Name(), | ||
| instanceID: id, | ||
|
|
||
| common: newCommon, | ||
| globals: g, | ||
| handler: handler, | ||
| targets: targets, | ||
|
|
||
| runFunc: runFunc, | ||
| }, nil | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to log that we were doing this, but by the time a logger is created it's already too late and integrations would've been unmarshaled.