Skip to content
This repository was archived by the owner on Jul 28, 2026. It is now read-only.

integrations-next: allow all integrations to be unmarshaled as an array or object - #1463

Merged
rfratto merged 1 commit into
grafana-cold-storage:mainfrom
rfratto:simpler-integrations-next-unmarshaling
Mar 3, 2022
Merged

rfratto merged 1 commit into
grafana-cold-storage:mainfrom
rfratto:simpler-integrations-next-unmarshaling

Conversation

@rfratto

@rfratto rfratto commented Mar 3, 2022

Copy link
Copy Markdown
Contributor

This PR relaxes the restrictions on how v2 integrations are unmarshaled so that any integration, regardless of type, can be unmarshaled as an JSON object or an array.

Introducing this change makes it much easier for the operator to generate configs for integrations, as we no longer have to switch between the _configs and non-_configs key based on the integration being generated.

With this change, configurations like these are now valid:

integrations:
  node_exporter_configs:
  - {}

Such a configuration would not be recommended to users, but as there is only one defined instance of node_exporter, it still satisfies the singleton requirements. It is unlikely that we would document this is possible.

Instead, the following configuration would still be documented and recommended for singleton integrations:

integrations:
  node_exporter: {}

Re-marshaling

Regardless of how the integrations config is unmarshaled, it will be re-marshaled as the non-array type. This means this config:

integrations:
  node_exporter_configs:
  - {}

Will display as the following at the /-/config endpoint:

integrations:
  node_exporter: {}

… <name>_configs

This relaxes the restrictions on how integrations are unmarshaled so
that any integration, regardless of type, can be unmarshaled as an JSON
object or an array.

This makes configurations like this technically valid:

integrations:
  node_exporter_configs:
  - {}

Such a configuration would not be recommended to users, but as there is
only one defined instance of node_exporter, it still satisfies the
singleton requirements. It is unlikely that we would document this is
possible.

Introducing this change makes it *much* easier for the operator to
generate configs for integrations, as we no longer have to switch
between the _configs and non-_configs key based on the integration being
generated.
@rfratto
rfratto requested a review from captncraig March 3, 2022 16:26
@rfratto
rfratto requested review from gaantunes and rgeyer as code owners March 3, 2022 16:26
Comment on lines +102 to 124
// Ensure that no singleton integration is defined twice
var (
duplicatedSingletons []string
singletonSet = make(map[string]struct{})
)
for _, cfg := range cfg {
if t, ok := RegisteredType(cfg.Name()); ok && t == TypeSingleton {
// Add to the map but if we find it already exists then create an error
if _, ok := singletonCheck[cfg.Name()]; ok {
returnError = multierror.Append(returnError, fmt.Errorf("found multiple instances of singleton integration %s", cfg.Name()))
} else {
singletonCheck[cfg.Name()] = struct{}{}
}
t, _ := RegisteredType(cfg.Name())
if t != TypeSingleton {
continue
}

if _, exists := singletonSet[cfg.Name()]; exists {
duplicatedSingletons = append(duplicatedSingletons, cfg.Name())
continue
}
singletonSet[cfg.Name()] = struct{}{}
}
if returnError != nil {
return returnError
if len(duplicatedSingletons) == 1 {
return fmt.Errorf("integration %q may only be defined once", duplicatedSingletons[0])
} else if len(duplicatedSingletons) > 1 {
list := strings.Join(duplicatedSingletons, ", ")
return fmt.Errorf("the following integrations may only be defined once each: %s", list)
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just doing a little bit of tidying here so there's less indentation

Comment on lines +89 to +90
// TypeInvalid is an invalid type.
TypeInvalid Type = iota

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Introducing TypeInvalid as the zero value removes the need for examining ok from RegisteredType

@rfratto
rfratto requested review from mattdurham and removed request for captncraig March 3, 2022 16:58

@mattdurham mattdurham left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good and nice simplification!

@rfratto
rfratto merged commit 449db00 into grafana-cold-storage:main Mar 3, 2022
@rfratto
rfratto deleted the simpler-integrations-next-unmarshaling branch March 3, 2022 20:13
@github-actions github-actions Bot added the frozen-due-to-age Locked due to a period of inactivity. Please open new issues or PRs if more discussion is needed. label Apr 3, 2024
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Apr 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

frozen-due-to-age Locked due to a period of inactivity. Please open new issues or PRs if more discussion is needed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants