Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Commit

Permalink
Fix dropping all labels (#943)
Browse files Browse the repository at this point in the history
* simplify filtering and logging

* oh I'm dumb
  • Loading branch information
icco authored Sep 1, 2020
1 parent e9943d8 commit c75b1e7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
40 changes: 21 additions & 19 deletions pkg/observability/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,6 @@ import (

var (
_ monitoredresource.Interface = (*stackdriverMonitoredResource)(nil)

// The labels each resource type requires.
requiredLabels = map[string]map[string]bool{
// https://cloud.google.com/monitoring/api/resources#tag_generic_task
"generic_task": {"project_id": true, "location": true, "namespace": true, "job": true, "task_id": true},
// https://cloud.google.com/monitoring/api/resources#tag_gke_container
"gke_container": {"project_id": true, "cluster_name": true, "namespace_id": true, "instance_id": true, "pod_id": true, "container_name": true, "zone": true},
}
)

type stackdriverMonitoredResource struct {
Expand Down Expand Up @@ -107,23 +99,33 @@ func NewStackdriverMonitoredResource(ctx context.Context, c *StackdriverConfig)

labels["namespace"] = c.Namespace

if _, ok := requiredLabels[resource]; !ok {
logger.Warnw("unknown resource type", "resource", resource, "labels", labels)
} else {
// Delete unused labels to not flood stackdriver.
for k := range labels {
if _, ok := requiredLabels[k]; !ok {
delete(labels, k)
}
}
}
filteredLabels := removeUnusedLabels(resource, labels)

logger.Debugw("resource type defined", "resource", resource, "labels", labels, "filteredLabels", filteredLabels)
return &stackdriverMonitoredResource{
resource: resource,
labels: labels,
labels: filteredLabels,
}
}

func (s *stackdriverMonitoredResource) MonitoredResource() (string, map[string]string) {
return s.resource, s.labels
}

// removeUnusedLabels deletes unused labels to not flood stackdriver.
func removeUnusedLabels(resource string, in map[string]string) map[string]string {
// The labels each resource type requires.
requiredLabels := map[string]map[string]bool{
// https://cloud.google.com/monitoring/api/resources#tag_generic_task
"generic_task": {"project_id": true, "location": true, "namespace": true, "job": true, "task_id": true},
}

ret := map[string]string{}
for k, v := range in {
if requiredLabels[resource][k] {
ret[k] = v
}
}

return ret
}
2 changes: 1 addition & 1 deletion pkg/observability/stackdriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func NewStackdriver(ctx context.Context, config *StackdriverConfig) (Exporter, e
ReportingInterval: time.Minute, // stackdriver export interval minimum
MonitoredResource: monitoredResource,
OnError: func(err error) {
logger.Errorw("failed to export metric", "error", err)
logger.Errorw("failed to export metric", "error", err, "resource", monitoredResource)
},
})

Expand Down

0 comments on commit c75b1e7

Please sign in to comment.