This repository was archived by the owner on Jul 28, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 513
[dev.multiple-integrations] Hide integrations/v2 behind a feature flag #1185
Merged
rfratto
merged 11 commits into
grafana-cold-storage:dev.multiple-integrations
from
rfratto:v2-feature-flag
Dec 17, 2021
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
11d4936
feature flag wip
rfratto aee3b56
dynamically switch between integrations v1 and v2
rfratto 8037357
pkg/integrations/versionselector to file in pkg/config
rfratto 9681995
pkg/config: fix defaults for Integrations
rfratto 0326eef
pkg/config: use more generic way to unmarshal differently based on flag
rfratto 74ec74f
add missing godoc comment
rfratto d686ce7
more comments
rfratto 86ed5aa
switch to deferred unmarshaling
rfratto 17daf66
remove unused Config field
rfratto a7b1873
simplify completeUnmarshal
rfratto 96de6e1
do not perform lazy deferred unmarshaling
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
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,148 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "reflect" | ||
|
|
||
| "github.com/go-kit/log" | ||
| "github.com/gorilla/mux" | ||
| v1 "github.com/grafana/agent/pkg/integrations" | ||
| v2 "github.com/grafana/agent/pkg/integrations/v2" | ||
| "github.com/grafana/agent/pkg/metrics" | ||
| "github.com/grafana/agent/pkg/util" | ||
| "github.com/prometheus/statsd_exporter/pkg/level" | ||
| "github.com/weaveworks/common/server" | ||
| "gopkg.in/yaml.v2" | ||
| ) | ||
|
|
||
| type integrationsVersion int | ||
|
|
||
| const ( | ||
| integrationsVersion1 integrationsVersion = iota | ||
| integrationsVersion2 | ||
| ) | ||
|
|
||
| // DefaultVersionedIntegrations is the default config for integrations. | ||
| var DefaultVersionedIntegrations = VersionedIntegrations{ | ||
| version: integrationsVersion1, | ||
| configV1: &v1.DefaultManagerConfig, | ||
| } | ||
|
|
||
| // VersionedIntegrations abstracts the subsystem configs for integrations v1 | ||
| // and v2. VersionedIntegrations can only be unmarshaled as part of Load. | ||
| type VersionedIntegrations struct { | ||
| version integrationsVersion | ||
| raw util.RawYAML | ||
|
|
||
| configV1 *v1.ManagerConfig | ||
| configV2 *v2.SubsystemOptions | ||
| } | ||
|
|
||
| var ( | ||
| _ yaml.Unmarshaler = (*VersionedIntegrations)(nil) | ||
| _ yaml.Marshaler = (*VersionedIntegrations)(nil) | ||
| ) | ||
|
|
||
| // UnmarshalYAML implements yaml.Unmarshaler. Full unmarshaling is deferred until | ||
| // setVersion is invoked. | ||
| func (c *VersionedIntegrations) UnmarshalYAML(unmarshal func(interface{}) error) error { | ||
| c.configV1 = nil | ||
| c.configV2 = nil | ||
| return unmarshal(&c.raw) | ||
| } | ||
|
|
||
| // MarshalYAML implements yaml.Marshaler. | ||
| func (c VersionedIntegrations) MarshalYAML() (interface{}, error) { | ||
| switch { | ||
| case c.configV1 != nil: | ||
| return c.configV1, nil | ||
| case c.configV2 != nil: | ||
| return c.configV2, nil | ||
| default: | ||
| return c.raw, nil | ||
| } | ||
| } | ||
|
|
||
| // IsZero implements yaml.IsZeroer. | ||
| func (c VersionedIntegrations) IsZero() bool { | ||
| switch { | ||
| case c.configV1 != nil: | ||
| return reflect.ValueOf(*c.configV1).IsZero() | ||
| case c.configV2 != nil: | ||
| return reflect.ValueOf(*c.configV2).IsZero() | ||
| default: | ||
| return len(c.raw) == 0 | ||
| } | ||
| } | ||
|
|
||
| // ApplyDefaults applies defaults to the subsystem based on globals. | ||
| func (c *VersionedIntegrations) ApplyDefaults(scfg *server.Config, mcfg *metrics.Config) error { | ||
| if c.version != integrationsVersion2 { | ||
| return c.configV1.ApplyDefaults(scfg, mcfg) | ||
| } | ||
| return c.configV2.ApplyDefaults(mcfg) | ||
| } | ||
|
|
||
| // setVersion completes the deferred unmarshal and unmarshals the raw YAML into | ||
| // the subsystem config for version v. | ||
| func (c *VersionedIntegrations) setVersion(v integrationsVersion) error { | ||
| c.version = v | ||
|
|
||
| switch c.version { | ||
| case integrationsVersion1: | ||
| cfg := v1.DefaultManagerConfig | ||
| c.configV1 = &cfg | ||
| return yaml.UnmarshalStrict(c.raw, c.configV1) | ||
| case integrationsVersion2: | ||
| cfg := v2.DefaultSubsystemOptions | ||
| c.configV2 = &cfg | ||
| return yaml.UnmarshalStrict(c.raw, c.configV2) | ||
| default: | ||
| panic(fmt.Sprintf("unknown integrations version %d", c.version)) | ||
| } | ||
| } | ||
|
|
||
| // IntegrationsGlobals is a global struct shared across integrations. | ||
| type IntegrationsGlobals = v2.Globals | ||
|
|
||
| // Integrations is an abstraction over both the v1 and v2 systems. | ||
| type Integrations interface { | ||
| ApplyConfig(*VersionedIntegrations, IntegrationsGlobals) error | ||
| WireAPI(*mux.Router) | ||
| Stop() | ||
| } | ||
|
|
||
| // NewIntegrations creates a new subsystem. globals should be provided regardless | ||
| // of useV2. globals.SubsystemOptions will be automatically set if cfg.Version | ||
| // is set to IntegrationsVersion2. | ||
| func NewIntegrations(logger log.Logger, cfg *VersionedIntegrations, globals IntegrationsGlobals) (Integrations, error) { | ||
| if cfg.version != integrationsVersion2 { | ||
| instance, err := v1.NewManager(*cfg.configV1, logger, globals.Metrics.InstanceManager(), globals.Metrics.Validate) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return &v1Integrations{Manager: instance}, nil | ||
| } | ||
|
|
||
| level.Warn(logger).Log("msg", "integrations-next is enabled. integrations-next is subject to change") | ||
|
|
||
| globals.SubsystemOpts = *cfg.configV2 | ||
| instance, err := v2.NewSubsystem(logger, globals) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return &v2Integrations{Subsystem: instance}, nil | ||
| } | ||
|
|
||
| type v1Integrations struct{ *v1.Manager } | ||
|
|
||
| func (s *v1Integrations) ApplyConfig(cfg *VersionedIntegrations, globals IntegrationsGlobals) error { | ||
| return s.Manager.ApplyConfig(*cfg.configV1) | ||
| } | ||
|
|
||
| type v2Integrations struct{ *v2.Subsystem } | ||
|
|
||
| func (s *v2Integrations) ApplyConfig(cfg *VersionedIntegrations, globals IntegrationsGlobals) error { | ||
| globals.SubsystemOpts = *cfg.configV2 | ||
| return s.Subsystem.ApplyConfig(globals) | ||
| } |
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
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.
My editor was complaining about this; we had the package imported twice with a different alias.