Skip to content

add system metadata labels #416

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

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
28 changes: 28 additions & 0 deletions collectors/monitoring_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package collectors

import (
"encoding/json"
"errors"
"fmt"
"log/slog"
Expand Down Expand Up @@ -452,6 +453,33 @@ func (c *MonitoringCollector) reportTimeSeriesMetrics(
}
}

// Add system metadata labels
// @see https://cloud.google.com/monitoring/api/ref_v3/rest/v3/TimeSeries
if timeSeries.Metadata != nil && timeSeries.Metadata.SystemLabels != nil {
// Marshal the SystemLabels struct to JSON
jsonBytes, err := json.Marshal(timeSeries.Metadata.SystemLabels)
if err != nil {
c.logger.Error("error marshaling SystemLabels to JSON", "err", err)
continue
}

// Unmarshal the JSON into a map[string]interface{}
var systemLabelsMap map[string]interface{}
err = json.Unmarshal(jsonBytes, &systemLabelsMap)
if err != nil {
c.logger.Error("error unmarshaling JSON to map", "err", err)
continue
}

for key, value := range systemLabelsMap {
valueStr := fmt.Sprintf("%v", value)
if !c.keyExists(labelKeys, key) {
labelKeys = append(labelKeys, key)
labelValues = append(labelValues, valueStr)
}
}
}

if c.monitoringDropDelegatedProjects {
dropDelegatedProject := false

Expand Down