Skip to content
Open
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
4 changes: 4 additions & 0 deletions api/v1alpha1/managedmetric_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ type ManagedMetricSpec struct {
// Defines which managed resources to observe
// +optional
Target *GroupVersionKind `json:"target,omitempty"`
// Defines dimensions of the metric. All specified fields must be nested strings. Nested slices are not supported.
// If not specified, only status.conditions of the CR will be used as dimension.
// +optional
Dimensions map[string]string `json:"dimensions,omitempty"`
// Define labels of your object to adapt filters of the query
// +optional
LabelSelector string `json:"labelSelector,omitempty"`
Expand Down
7 changes: 7 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

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

Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ spec:
description: Sets the description that will be used to identify the
metric in Dynatrace(or other providers)
type: string
dimensions:
additionalProperties:
type: string
description: |-
Defines dimensions of the metric. All specified fields must be nested strings. Nested slices are not supported.
If not specified, only status.conditions of the CR will be used as dimension.
type: object
fieldSelector:
description: Define fields of your object to adapt filters of the
query
Expand Down
47 changes: 35 additions & 12 deletions internal/orchestrator/managedhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,48 @@ func (h *ManagedHandler) sendStatusBasedMetricValue(ctx context.Context) (string
// Create a new data point for each resource
dataPoint := clientoptl.NewDataPoint()

// Add GVK dimensions from resource
gv, err := schema.ParseGroupVersion(cr.MangedResource.APIVersion)
if err != nil {
return "", err
// Preserve old logic so that if custom dimensions are not set, we use status.conditions
// as default dimensions
if h.metric.Spec.Dimensions == nil {
gv, err := schema.ParseGroupVersion(cr.MangedResource.APIVersion)
if err != nil {
return "", err
}

dataPoint.AddDimension(KIND, cr.MangedResource.Kind)
dataPoint.AddDimension(GROUP, gv.Group)
dataPoint.AddDimension(VERSION, gv.Version)

for typ, state := range cr.Status {
t := strings.ToLower(typ)
if t == "ready" || t == "synced" {
dataPoint.AddDimension(t, strconv.FormatBool(state))
}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

nit: redundant empty line

} else {
objMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&cr.MangedResource)
if err != nil {
return "", err
}

u := &unstructured.Unstructured{Object: objMap}

for key, expr := range h.metric.Spec.Dimensions {
s, _, err := nestedPrimitiveValue(*u, expr)
if err != nil {
fmt.Printf("WARN: Could not parse expression '%s' for dimension field '%s'. Error: %v\n", key, expr, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

please use the log.FromContext(ctx) logger Error() instead

continue
}
dataPoint.AddDimension(key, s)
}
}
dataPoint.AddDimension(KIND, cr.MangedResource.Kind)
dataPoint.AddDimension(GROUP, gv.Group)
dataPoint.AddDimension(VERSION, gv.Version)

// Add cluster dimension if available
if h.clusterName != nil {
dataPoint.AddDimension(CLUSTER, *h.clusterName)
}

// Add status conditions as dimensions
for typ, state := range cr.Status {
dataPoint.AddDimension(strings.ToLower(typ), strconv.FormatBool(state))
}

// Set the value to 1 for each resource
dataPoint.SetValue(1)

Expand Down