Skip to content

Commit

Permalink
Remove Autodiscovery deps in config package
Browse files Browse the repository at this point in the history
  • Loading branch information
clamoriniere committed Nov 22, 2023
1 parent fceacae commit 03babc6
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 39 deletions.
10 changes: 10 additions & 0 deletions pkg/autodiscovery/common/types/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package types

import (
"encoding/json"
"fmt"
"regexp"
"strings"
Expand Down Expand Up @@ -145,6 +146,15 @@ type InclExcl struct {
Excl map[string]string `mapstructure:"exclude" yaml:"exclude,omitempty" json:"exclude,omitempty"`
}

// PrometheusScrapeChecksTransformer unmarshals a prometheus check.
func PrometheusScrapeChecksTransformer(in string) ([]*PrometheusCheck, error) {
var promChecks []*PrometheusCheck
if err := json.Unmarshal([]byte(in), &promChecks); err != nil {
return promChecks, fmt.Errorf(`"prometheus_scrape.checks" can not be parsed: %v`, err)
}
return promChecks, nil
}

// Init prepares the PrometheusCheck structure and defaults its values
// init must be called only once
func (pc *PrometheusCheck) Init(version int) error {
Expand Down
13 changes: 13 additions & 0 deletions pkg/autodiscovery/common/types/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,16 @@ func TestPrometheusCheck_IsIncluded(t *testing.T) {
})
}
}

func TestPrometheusScrapeChecksTransformer(t *testing.T) {
input := `[{"configurations":[{"timeout":5,"send_distribution_buckets":true}],"autodiscovery":{"kubernetes_container_names":["my-app"],"kubernetes_annotations":{"include":{"custom_label":"true"}}}}]`
expected := []*PrometheusCheck{
{
Instances: []*OpenmetricsInstance{{Timeout: 5, DistributionBuckets: true}},
AD: &ADConfig{KubeContainerNames: []string{"my-app"}, KubeAnnotations: &InclExcl{Incl: map[string]string{"custom_label": "true"}}},
},
}

value, _ := PrometheusScrapeChecksTransformer(input)
assert.EqualValues(t, value, expected)
}
15 changes: 10 additions & 5 deletions pkg/autodiscovery/listeners/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,16 @@ func (f *containerFilters) IsExcluded(filter containers.FilterType, annotations
// getPrometheusIncludeAnnotations returns the Prometheus AD include annotations based on the Prometheus config
func getPrometheusIncludeAnnotations() types.PrometheusAnnotations {
annotations := types.PrometheusAnnotations{}
checks := []*types.PrometheusCheck{}
err := config.Datadog.UnmarshalKey("prometheus_scrape.checks", &checks)
if err != nil {
log.Warnf("Couldn't get configurations from 'prometheus_scrape.checks': %v", err)
return annotations
tmpConfigString := config.Datadog.GetString("prometheus_scrape.checks")

var checks []*types.PrometheusCheck
if len(tmpConfigString) > 0 {
var err error
checks, err = types.PrometheusScrapeChecksTransformer(tmpConfigString)
if err != nil {
log.Warnf("Couldn't get configurations from 'prometheus_scrape.checks': %v", err)
return annotations
}
}

if len(checks) == 0 {
Expand Down
11 changes: 6 additions & 5 deletions pkg/autodiscovery/listeners/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package listeners

import (
"encoding/json"
"testing"

"github.com/DataDog/datadog-agent/pkg/autodiscovery/common/types"
Expand Down Expand Up @@ -196,12 +197,12 @@ func TestGetPrometheusIncludeAnnotations(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
mockConfig := config.Mock(t)

originalChecks := []*types.PrometheusCheck{}
err := mockConfig.UnmarshalKey("prometheus_scrape.checks", &originalChecks)
assert.NoError(t, err)
val := mockConfig.GetString("prometheus_scrape.checks")
assert.Equal(t, val, "")

mockConfig.SetWithoutSource("prometheus_scrape.checks", tt.config)
defer mockConfig.SetWithoutSource("prometheus_scrape.checks", originalChecks)
confBytes, _ := json.Marshal(tt.config)
mockConfig.SetWithoutSource("prometheus_scrape.checks", string(confBytes))
defer mockConfig.SetWithoutSource("prometheus_scrape.checks", "")

assert.EqualValues(t, tt.want, getPrometheusIncludeAnnotations())
})
Expand Down
1 change: 0 additions & 1 deletion pkg/autodiscovery/listeners/kube_services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,6 @@ func TestShouldIgnore(t *testing.T) {
promInclAnnot: getPrometheusIncludeAnnotations(),
targetAllServices: tt.targetAll,
}

assert.Equal(t, tt.want, l.shouldIgnore(tt.ksvc))
})
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/autodiscovery/providers/prometheus_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import (
// getPrometheusConfigs reads and initializes the openmetrics checks from the configuration
// It defines a default openmetrics instances with default AD if the checks configuration is empty
func getPrometheusConfigs() ([]*types.PrometheusCheck, error) {
checks := []*types.PrometheusCheck{}
err := config.Datadog.UnmarshalKey("prometheus_scrape.checks", &checks)
checks, err := types.PrometheusScrapeChecksTransformer(config.Datadog.GetString("prometheus_scrape.checks"))
if err != nil {
return []*types.PrometheusCheck{}, err
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/autodiscovery/providers/prometheus_common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package providers

import (
"encoding/json"
"regexp"
"testing"

Expand Down Expand Up @@ -196,7 +197,8 @@ func TestGetPrometheusConfigs(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config.Datadog.SetWithoutSource("prometheus_scrape.checks", tt.config)
confBytes, _ := json.Marshal(tt.config)
config.Datadog.SetWithoutSource("prometheus_scrape.checks", string(confBytes))
checks, err := getPrometheusConfigs()
if (err != nil) != tt.wantErr {
t.Errorf("getPrometheusConfigs() error = %v, wantErr %v", err, tt.wantErr)
Expand Down
13 changes: 1 addition & 12 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

"gopkg.in/yaml.v2"

"github.com/DataDog/datadog-agent/pkg/autodiscovery/common/types"
"github.com/DataDog/datadog-agent/pkg/collector/check/defaults"
pkgconfigenv "github.com/DataDog/datadog-agent/pkg/config/env"
pkgconfigmodel "github.com/DataDog/datadog-agent/pkg/config/model"
Expand Down Expand Up @@ -127,15 +126,6 @@ var (
// List of integrations allowed to be configured by RC by default
var defaultAllowedRCIntegrations = []string{}

// PrometheusScrapeChecksTransformer unmarshals a prometheus check.
func PrometheusScrapeChecksTransformer(in string) interface{} {
var promChecks []*types.PrometheusCheck
if err := json.Unmarshal([]byte(in), &promChecks); err != nil {
log.Warnf(`"prometheus_scrape.checks" can not be parsed: %v`, err)
}
return promChecks
}

// ConfigurationProviders helps unmarshalling `config_providers` config param
type ConfigurationProviders struct {
Name string `mapstructure:"name"`
Expand Down Expand Up @@ -670,8 +660,7 @@ func InitConfig(config Config) {
config.BindEnvAndSetDefault("prometheus_scrape.enabled", false) // Enables the prometheus config provider
config.BindEnvAndSetDefault("prometheus_scrape.service_endpoints", false) // Enables Service Endpoints checks in the prometheus config provider
config.BindEnv("prometheus_scrape.checks") // Defines any extra prometheus/openmetrics check configurations to be handled by the prometheus config provider
config.SetEnvKeyTransformer("prometheus_scrape.checks", PrometheusScrapeChecksTransformer)
config.BindEnvAndSetDefault("prometheus_scrape.version", 1) // Version of the openmetrics check to be scheduled by the Prometheus auto-discovery
config.BindEnvAndSetDefault("prometheus_scrape.version", 1) // Version of the openmetrics check to be scheduled by the Prometheus auto-discovery

// Network Devices Monitoring
bindEnvAndSetLogsConfigKeys(config, "network_devices.metadata.")
Expand Down
13 changes: 0 additions & 13 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/DataDog/datadog-agent/pkg/autodiscovery/common/types"
"github.com/DataDog/datadog-agent/pkg/config/model"
)

Expand Down Expand Up @@ -645,18 +644,6 @@ network_devices:
assert.Equal(t, "dev", config.GetString("network_devices.namespace"))
}

func TestPrometheusScrapeChecksTransformer(t *testing.T) {
input := `[{"configurations":[{"timeout":5,"send_distribution_buckets":true}],"autodiscovery":{"kubernetes_container_names":["my-app"],"kubernetes_annotations":{"include":{"custom_label":"true"}}}}]`
expected := []*types.PrometheusCheck{
{
Instances: []*types.OpenmetricsInstance{{Timeout: 5, DistributionBuckets: true}},
AD: &types.ADConfig{KubeContainerNames: []string{"my-app"}, KubeAnnotations: &types.InclExcl{Incl: map[string]string{"custom_label": "true"}}},
},
}

assert.EqualValues(t, PrometheusScrapeChecksTransformer(input), expected)
}

func TestUsePodmanLogsAndDockerPathOverride(t *testing.T) {
// If use_podman_logs is true and docker_path_override is set, the config should return an error
datadogYaml := `
Expand Down

0 comments on commit 03babc6

Please sign in to comment.