Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Agent Management] Support External Labels from API #5670

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions pkg/config/agent_management_remote_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/grafana/agent/pkg/metrics/instance"
"github.com/prometheus/prometheus/model/labels"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -179,4 +180,77 @@ integration_configs:
require.Equal(t, true, c.Integrations.ConfigV1.ReplaceInstanceLabel)
require.Equal(t, 5*time.Second, c.Integrations.ConfigV1.IntegrationRestartBackoff)
})

t.Run("no external labels provided", func(t *testing.T) {
rc := RemoteConfig{
BaseConfig: BaseConfigContent(baseConfig),
Snippets: allSnippets,
}
c, err := rc.BuildAgentConfig()
require.NoError(t, err)
require.Equal(t, 1, len(c.Logs.Configs))
require.Empty(t, c.Metrics.Global.Prometheus.ExternalLabels)
})

t.Run("no external labels provided in remote config", func(t *testing.T) {
baseConfig := `
server:
log_level: debug
metrics:
global:
external_labels:
foo: bar`
rc := RemoteConfig{
BaseConfig: BaseConfigContent(baseConfig),
Snippets: allSnippets,
}
c, err := rc.BuildAgentConfig()
require.NoError(t, err)
require.Equal(t, 1, len(c.Logs.Configs))
require.Equal(t, 1, len(c.Metrics.Global.Prometheus.ExternalLabels))
require.Contains(t, c.Metrics.Global.Prometheus.ExternalLabels, labels.Label{Name: "foo", Value: "bar"})
})

t.Run("external labels provided", func(t *testing.T) {
rc := RemoteConfig{
BaseConfig: BaseConfigContent(baseConfig),
Snippets: allSnippets,
AgentMetadata: AgentMetadata{
ExternalLabels: map[string]string{
"foo": "bar",
},
},
}
c, err := rc.BuildAgentConfig()
require.NoError(t, err)
require.Equal(t, 1, len(c.Logs.Configs))
require.Equal(t, 1, len(c.Metrics.Configs))
require.Contains(t, c.Metrics.Global.Prometheus.ExternalLabels, labels.Label{Name: "foo", Value: "bar"})
})

t.Run("external labels don't override base config", func(t *testing.T) {
baseConfig := `
server:
log_level: debug
metrics:
global:
external_labels:
foo: bar
`
rc := RemoteConfig{
BaseConfig: BaseConfigContent(baseConfig),
Snippets: allSnippets,
AgentMetadata: AgentMetadata{
ExternalLabels: map[string]string{
"foo": "baz",
},
},
}
c, err := rc.BuildAgentConfig()
require.NoError(t, err)
require.Equal(t, 1, len(c.Logs.Configs))
require.Equal(t, 1, len(c.Metrics.Configs))
require.Contains(t, c.Metrics.Global.Prometheus.ExternalLabels, labels.Label{Name: "foo", Value: "bar"})
require.NotContains(t, c.Metrics.Global.Prometheus.ExternalLabels, labels.Label{Name: "foo", Value: "baz"})
})
}
26 changes: 24 additions & 2 deletions pkg/config/agentmanagement_remote_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import (
"github.com/grafana/agent/pkg/metrics/instance"
"github.com/grafana/loki/clients/pkg/promtail/scrapeconfig"
pc "github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/model/labels"
"gopkg.in/yaml.v2"
)

type (
RemoteConfig struct {
BaseConfig BaseConfigContent `json:"base_config" yaml:"base_config"`
Snippets []Snippet `json:"snippets" yaml:"snippets"`
BaseConfig BaseConfigContent `json:"base_config" yaml:"base_config"`
Snippets []Snippet `json:"snippets" yaml:"snippets"`
AgentMetadata AgentMetadata `json:"agent_metadata,omitempty" yaml:"agent_metadata,omitempty"`
}

// BaseConfigContent is the content of a base config
Expand All @@ -24,6 +26,10 @@ type (
Config string `json:"config" yaml:"config"`
}

AgentMetadata struct {
ExternalLabels map[string]string `json:"external_labels,omitempty" yaml:"external_labels,omitempty"`
}

// SnippetContent defines the internal structure of a snippet configuration.
SnippetContent struct {
// MetricsScrapeConfigs is a YAML containing list of metrics scrape configs.
Expand Down Expand Up @@ -63,6 +69,7 @@ func (rc *RemoteConfig) BuildAgentConfig() (*Config, error) {
if err != nil {
return nil, err
}
appendExternalLabels(&c, rc.AgentMetadata.ExternalLabels)
return &c, nil
}

Expand Down Expand Up @@ -116,3 +123,18 @@ func appendSnippets(c *Config, snippets []Snippet) error {
c.Integrations.ConfigV1.Integrations = append(c.Integrations.ConfigV1.Integrations, integrationConfigs.Integrations...)
return nil
}

func appendExternalLabels(c *Config, externalLabels map[string]string) {
// Avoid doing anything if there are no external labels
if len(externalLabels) == 0 {
return
}
// Start off with the existing external labels, which will only be added to (not replaced)
newExternalLabels := c.Metrics.Global.Prometheus.ExternalLabels.Map()
for k, v := range externalLabels {
if _, ok := newExternalLabels[k]; !ok {
newExternalLabels[k] = v
}
}
c.Metrics.Global.Prometheus.ExternalLabels = labels.FromMap(newExternalLabels)
}