Skip to content

Commit

Permalink
[feature/prometheusscrape] Allow to configure the version of the Open…
Browse files Browse the repository at this point in the history
…metrics check (#645)

* [v2alpha1/types] Add version to the Prometheus scrape feature

* [v2alpha1/default] Set default OpenMetrics version to 2

* [feature/prometheusscrape] Handle Openmetrics version
  • Loading branch information
davidor authored and khewonc committed Nov 22, 2022
1 parent fd19f4c commit 1a5d89f
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 1 deletion.
1 change: 1 addition & 0 deletions apis/datadoghq/common/envvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ const (
DDPrometheusScrapeChecks = "DD_PROMETHEUS_SCRAPE_CHECKS"
DDPrometheusScrapeEnabled = "DD_PROMETHEUS_SCRAPE_ENABLED"
DDPrometheusScrapeServiceEndpoints = "DD_PROMETHEUS_SCRAPE_SERVICE_ENDPOINTS"
DDPrometheusScrapeVersion = "DD_PROMETHEUS_SCRAPE_VERSION"
DDRuntimeSecurityConfigEnabled = "DD_RUNTIME_SECURITY_CONFIG_ENABLED"
DDRuntimeSecurityConfigPoliciesDir = "DD_RUNTIME_SECURITY_CONFIG_POLICIES_DIR"
DDRuntimeSecurityConfigRemoteTaggerEnabled = "DD_RUNTIME_SECURITY_CONFIG_REMOTE_TAGGER"
Expand Down
4 changes: 3 additions & 1 deletion apis/datadoghq/v2alpha1/datadogagent_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ const (

// defaultPrometheusScrapeEnabled bool = false
defaultPrometheusScrapeEnableServiceEndpoints bool = false
defaultPrometheusScrapeVersion int = 2

defaultKubeletAgentCAPath = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"
defaultKubeletAgentCAPathHostPathSet = "/var/run/host-kubelet-ca.crt"
Expand Down Expand Up @@ -275,7 +276,8 @@ func defaultFeaturesConfig(ddaSpec *DatadogAgentSpec) {
}

// PrometheusScrape Feature
if ddaSpec.Features.PrometheusScrape != nil && *ddaSpec.Features.PrometheusScrape.Enabled {
if ddaSpec.Features.PrometheusScrape != nil && ddaSpec.Features.PrometheusScrape.Enabled != nil && *ddaSpec.Features.PrometheusScrape.Enabled {
apiutils.DefaultBooleanIfUnset(&ddaSpec.Features.PrometheusScrape.EnableServiceEndpoints, defaultPrometheusScrapeEnableServiceEndpoints)
apiutils.DefaultIntIfUnset(&ddaSpec.Features.PrometheusScrape.Version, defaultPrometheusScrapeVersion)
}
}
5 changes: 5 additions & 0 deletions apis/datadoghq/v2alpha1/datadogagent_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,11 @@ type PrometheusScrapeFeatureConfig struct {
// AdditionalConfigs allows adding advanced Prometheus check configurations with custom discovery rules.
// +optional
AdditionalConfigs *string `json:"additionalConfigs,omitempty"`

// Version specifies the version of the OpenMetrics check.
// Default: 2
// +optional
Version *int `json:"version,omitempty"`
}

// Generic support structs
Expand Down
5 changes: 5 additions & 0 deletions apis/datadoghq/v2alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions apis/datadoghq/v2alpha1/zz_generated.openapi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions apis/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ func NewInt64Pointer(i int64) *int64 {
return &i
}

// NewIntPointer returns pointer to an int value
func NewIntPointer(i int) *int {
return &i
}

// NewStringPointer returns pointer on a new string value instance
func NewStringPointer(s string) *string {
return &s
Expand Down Expand Up @@ -64,6 +69,13 @@ func DefaultInt32IfUnset(valPtr **int32, d int32) {
}
}

// DefaultIntIfUnset sets value val of an int if unset
func DefaultIntIfUnset(ptr **int, val int) {
if *ptr == nil {
*ptr = &val
}
}

// DefaultStringIfUnset sets default value d of a string if unset
func DefaultStringIfUnset(valPtr **string, d string) {
if *valPtr == nil {
Expand Down
4 changes: 4 additions & 0 deletions config/crd/bases/v1/datadoghq.com_datadogagents.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13418,6 +13418,10 @@ spec:
description: 'Enable autodiscovery of pods and services exposing
Prometheus metrics. Default: false'
type: boolean
version:
description: 'Version specifies the version of the OpenMetrics
check. Default: 2'
type: integer
type: object
tcpQueueLength:
description: TCPQueueLength configuration.
Expand Down
4 changes: 4 additions & 0 deletions config/crd/bases/v1beta1/datadoghq.com_datadogagents.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26362,6 +26362,10 @@ spec:
description: 'Enable autodiscovery of pods and services exposing
Prometheus metrics. Default: false'
type: boolean
version:
description: 'Version specifies the version of the OpenMetrics
check. Default: 2'
type: integer
type: object
tcpQueueLength:
description: TCPQueueLength configuration.
Expand Down
16 changes: 16 additions & 0 deletions controllers/datadogagent/feature/prometheusscrape/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func buildPrometheusScrapeFeature(options *feature.Options) feature.Feature {
type prometheusScrapeFeature struct {
enableServiceEndpoints bool
additionalConfigs string
openmetricsVersion int
}

// ID returns the ID of the Feature
Expand All @@ -55,6 +56,9 @@ func (f *prometheusScrapeFeature) Configure(dda *v2alpha1.DatadogAgent) (reqComp
if prometheusScrape.AdditionalConfigs != nil {
f.additionalConfigs = *prometheusScrape.AdditionalConfigs
}
if prometheusScrape.Version != nil {
f.openmetricsVersion = *prometheusScrape.Version
}
reqComp = feature.RequiredComponents{
Agent: feature.RequiredComponent{
IsRequired: apiutils.NewBoolPointer(true),
Expand Down Expand Up @@ -125,6 +129,12 @@ func (f *prometheusScrapeFeature) ManageClusterAgent(managers feature.PodTemplat
Value: apiutils.YAMLToJSONString(f.additionalConfigs),
})
}
if f.openmetricsVersion != 0 {
managers.EnvVar().AddEnvVarToContainer(apicommonv1.ClusterAgentContainerName, &corev1.EnvVar{
Name: apicommon.DDPrometheusScrapeVersion,
Value: strconv.Itoa(f.openmetricsVersion),
})
}

return nil
}
Expand All @@ -146,6 +156,12 @@ func (f *prometheusScrapeFeature) ManageNodeAgent(managers feature.PodTemplateMa
Value: apiutils.YAMLToJSONString(f.additionalConfigs),
})
}
if f.openmetricsVersion != 0 {
managers.EnvVar().AddEnvVarToContainer(apicommonv1.CoreAgentContainerName, &corev1.EnvVar{
Name: apicommon.DDPrometheusScrapeVersion,
Value: strconv.Itoa(f.openmetricsVersion),
})
}

return nil
}
Expand Down
50 changes: 50 additions & 0 deletions controllers/datadogagent/feature/prometheusscrape/feature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ func Test_prometheusScrapeFeature_Configure(t *testing.T) {
ddav2PrometheusScrapeAdditionalConfigs.Spec.Features.PrometheusScrape.AdditionalConfigs = apiutils.NewStringPointer(yamlConfigs)
}

ddav2PrometheusScrapeWithVersion := ddav2PrometheusScrapeEnabled.DeepCopy()
ddav2PrometheusScrapeWithVersion.Spec.Features.PrometheusScrape.Version = apiutils.NewIntPointer(1)

tests := test.FeatureTestSuite{
///////////////////////////
// v1alpha1.DatadogAgent //
Expand Down Expand Up @@ -356,6 +359,53 @@ func Test_prometheusScrapeFeature_Configure(t *testing.T) {
},
),
},
{
Name: "v2alpha1 version specified",
DDAv2: ddav2PrometheusScrapeWithVersion,
WantConfigure: true,
Agent: test.NewDefaultComponentTest().WithWantFunc(
func(t testing.TB, mgrInterface feature.PodTemplateManagers) {
mgr := mgrInterface.(*fake.PodTemplateManagers)
wantEnvVars := []*corev1.EnvVar{
{
Name: apicommon.DDPrometheusScrapeEnabled,
Value: "true",
},
{
Name: apicommon.DDPrometheusScrapeServiceEndpoints,
Value: "false",
},
{
Name: apicommon.DDPrometheusScrapeVersion,
Value: "1",
},
}
coreAgentEnvVars := mgr.EnvVarMgr.EnvVarsByC[apicommonv1.CoreAgentContainerName]
assert.True(t, apiutils.IsEqualStruct(coreAgentEnvVars, wantEnvVars), "Core Agent envvars \ndiff = %s", cmp.Diff(coreAgentEnvVars, wantEnvVars))
},
),
ClusterAgent: test.NewDefaultComponentTest().WithWantFunc(
func(t testing.TB, mgrInterface feature.PodTemplateManagers) {
mgr := mgrInterface.(*fake.PodTemplateManagers)
dcaEnvVars := mgr.EnvVarMgr.EnvVarsByC[apicommonv1.ClusterAgentContainerName]
want := []*corev1.EnvVar{
{
Name: apicommon.DDPrometheusScrapeEnabled,
Value: "true",
},
{
Name: apicommon.DDPrometheusScrapeServiceEndpoints,
Value: "false",
},
{
Name: apicommon.DDPrometheusScrapeVersion,
Value: "1",
},
}
assert.True(t, apiutils.IsEqualStruct(dcaEnvVars, want), "DCA envvars \ndiff = %s", cmp.Diff(dcaEnvVars, want))
},
),
},
}

tests.Run(t, buildPrometheusScrapeFeature)
Expand Down
1 change: 1 addition & 0 deletions docs/configuration.v2alpha1.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ spec:
| features.prometheusScrape.additionalConfigs | AdditionalConfigs allows adding advanced Prometheus check configurations with custom discovery rules. |
| features.prometheusScrape.enableServiceEndpoints | EnableServiceEndpoints enables generating dedicated checks for service endpoints. Default: false |
| features.prometheusScrape.enabled | Enable autodiscovery of pods and services exposing Prometheus metrics. Default: false |
| features.prometheusScrape.version | Version specifies the version of the OpenMetrics check. Default: 2 |
| features.tcpQueueLength.enabled | Enables the TCP queue length eBPF-based check. Default: false |
| features.usm.enabled | Enabled enables Universal Service Monitoring. Default: false |
| global.clusterAgentToken | ClusterAgentToken is the token for communication between the NodeAgent and ClusterAgent. |
Expand Down

0 comments on commit 1a5d89f

Please sign in to comment.