Skip to content

Commit

Permalink
DatadogAgentProfiles valid metric (#1245)
Browse files Browse the repository at this point in the history
* Add dap valid metric

* Use OR for predicates
  • Loading branch information
khewonc authored Jul 11, 2024
1 parent 516b1e5 commit c72fc48
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 8 deletions.
8 changes: 4 additions & 4 deletions controllers/datadogagent/controller_reconcile_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ func (r *Reconciler) reconcileInstanceV2(ctx context.Context, logger logr.Logger
// If introspection is disabled, reconcile the agent once using the empty provider `LegacyProvider`
providerList := map[string]struct{}{kubernetes.LegacyProvider: {}}
profiles := []datadoghqv1alpha1.DatadogAgentProfile{{}}
metrics.IntrospectionEnabled.Set(metrics.DisabledValue)
metrics.DAPEnabled.Set(metrics.DisabledValue)
metrics.IntrospectionEnabled.Set(metrics.FalseValue)
metrics.DAPEnabled.Set(metrics.FalseValue)

if r.options.DatadogAgentProfileEnabled || r.options.IntrospectionEnabled {
// Get a node list for profiles and introspection
Expand All @@ -171,11 +171,11 @@ func (r *Reconciler) reconcileInstanceV2(ctx context.Context, logger logr.Logger

if r.options.IntrospectionEnabled {
providerList = kubernetes.GetProviderListFromNodeList(nodeList, logger)
metrics.IntrospectionEnabled.Set(metrics.EnabledValue)
metrics.IntrospectionEnabled.Set(metrics.TrueValue)
}

if r.options.DatadogAgentProfileEnabled {
metrics.DAPEnabled.Set(metrics.EnabledValue)
metrics.DAPEnabled.Set(metrics.TrueValue)
var profilesByNode map[string]types.NamespacedName
profiles, profilesByNode, e = r.profilesToApply(ctx, logger, nodeList, now)
if err != nil {
Expand Down
10 changes: 8 additions & 2 deletions controllers/datadogagent_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
datadoghqv2alpha1 "github.com/DataDog/datadog-operator/apis/datadoghq/v2alpha1"
"github.com/DataDog/datadog-operator/controllers/datadogagent"
"github.com/DataDog/datadog-operator/controllers/datadogagent/object"
"github.com/DataDog/datadog-operator/controllers/metrics"
"github.com/DataDog/datadog-operator/pkg/controller/utils/datadog"
"github.com/DataDog/datadog-operator/pkg/kubernetes"
edsdatadoghqv1alpha1 "github.com/DataDog/extendeddaemonset/api/v1alpha1"
Expand Down Expand Up @@ -200,8 +201,13 @@ func (r *DatadogAgentReconciler) SetupWithManager(mgr ctrl.Manager) error {
builder.Watches(
&datadoghqv1alpha1.DatadogAgentProfile{},
handler.EnqueueRequestsFromMapFunc(r.enqueueRequestsForAllDDAs()),
ctrlbuilder.WithPredicates(predicate.GenerationChangedPredicate{}),
)
ctrlbuilder.WithPredicates(predicate.Or(predicate.GenerationChangedPredicate{}, predicate.Funcs{
DeleteFunc: func(e event.DeleteEvent) bool {
metrics.CleanupMetricsByProfile(e.Object)
return true
},
}),
))
}

// Watch nodes and reconcile all DatadogAgents for node creation, node deletion, and node label change events
Expand Down
6 changes: 4 additions & 2 deletions controllers/metrics/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const (
datadogAgentSubsystem = "datadogagent"
datadogAgentProfileSubsystem = "datadogagentprofile"

EnabledValue = 1.0
DisabledValue = 0.0
TrueValue = 1.0
FalseValue = 0.0

datadogAgentProfileLabelKey = "datadogagentprofile"
)
19 changes: 19 additions & 0 deletions controllers/metrics/datadogagentprofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package metrics

import (
"github.com/prometheus/client_golang/prometheus"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/metrics"
)

Expand All @@ -19,9 +20,27 @@ var (
Help: "1 if DatadogAgentProfiles are enabled. 0 if DatadogAgentProfiles are disabled",
},
)

// datadogagentprofile valid
DAPValid = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Subsystem: datadogAgentProfileSubsystem,
Name: "valid",
Help: "1 if the DatadogAgentProfile is valid. 0 if the DatadogAgentProfile is invalid",
},
[]string{
datadogAgentProfileLabelKey,
},
)
)

func init() {
// Register custom metrics with the global prometheus registry
metrics.Registry.MustRegister(DAPEnabled)
metrics.Registry.MustRegister(DAPValid)
}

// CleanupMetricsByProfile deletes profile-specific prometheus metrics given a profile
func CleanupMetricsByProfile(obj client.Object) {
DAPValid.Delete(prometheus.Labels{datadogAgentProfileLabelKey: obj.GetName()})
}
5 changes: 5 additions & 0 deletions pkg/agentprofile/agent_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import (
"github.com/DataDog/datadog-operator/apis/datadoghq/common/v1"
datadoghqv1alpha1 "github.com/DataDog/datadog-operator/apis/datadoghq/v1alpha1"
"github.com/DataDog/datadog-operator/apis/datadoghq/v2alpha1"
"github.com/DataDog/datadog-operator/controllers/metrics"
"github.com/DataDog/datadog-operator/pkg/controller/utils/comparison"

"github.com/go-logr/logr"
"github.com/prometheus/client_golang/prometheus"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
Expand Down Expand Up @@ -55,6 +57,7 @@ func ProfileToApply(logger logr.Logger, profile *datadoghqv1alpha1.DatadogAgentP

if err := datadoghqv1alpha1.ValidateDatadogAgentProfileSpec(&profile.Spec); err != nil {
logger.Error(err, "profile spec is invalid, skipping", "datadogagentprofile", profile.Name, "datadogagentprofile_namespace", profile.Namespace)
metrics.DAPValid.With(prometheus.Labels{"datadogagentprofile": profile.Name}).Set(metrics.FalseValue)
profileStatus.Conditions = SetDatadogAgentProfileCondition(profileStatus.Conditions, NewDatadogAgentProfileCondition(ValidConditionType, metav1.ConditionFalse, now, InvalidConditionReason, err.Error()))
profileStatus.Valid = metav1.ConditionFalse
UpdateProfileStatus(profile, profileStatus, now)
Expand All @@ -65,11 +68,13 @@ func ProfileToApply(logger logr.Logger, profile *datadoghqv1alpha1.DatadogAgentP
matchesNode, err := profileMatchesNode(profile, node.Labels)
if err != nil {
logger.Error(err, "profile selector is invalid, skipping", "datadogagentprofile", profile.Name, "datadogagentprofile_namespace", profile.Namespace)
metrics.DAPValid.With(prometheus.Labels{"datadogagentprofile": profile.Name}).Set(metrics.FalseValue)
profileStatus.Conditions = SetDatadogAgentProfileCondition(profileStatus.Conditions, NewDatadogAgentProfileCondition(ValidConditionType, metav1.ConditionFalse, now, InvalidConditionReason, err.Error()))
profileStatus.Valid = metav1.ConditionFalse
UpdateProfileStatus(profile, profileStatus, now)
return profileAppliedByNode, err
}
metrics.DAPValid.With(prometheus.Labels{"datadogagentprofile": profile.Name}).Set(metrics.TrueValue)
profileStatus.Valid = metav1.ConditionTrue
profileStatus.Conditions = SetDatadogAgentProfileCondition(profileStatus.Conditions, NewDatadogAgentProfileCondition(ValidConditionType, metav1.ConditionTrue, now, ValidConditionReason, "Valid manifest"))

Expand Down

0 comments on commit c72fc48

Please sign in to comment.