Skip to content

Commit

Permalink
Provide useful cluster names for non k8s environments on managed prom… (
Browse files Browse the repository at this point in the history
#776)

* Provide useful cluster names for non k8s environments on managed prometheus exporter.

* Fixes from review.
  • Loading branch information
jsuereth committed Nov 30, 2023
1 parent a227a23 commit 18da23f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
28 changes: 24 additions & 4 deletions exporter/collector/googlemanagedprometheus/monitoredresource.go
Expand Up @@ -61,16 +61,31 @@ func (c Config) MapToPrometheusTarget(res pcommon.Resource) *monitoredrespb.Moni
Type: "prometheus_target",
Labels: map[string]string{
locationLabel: getStringOrEmpty(attrs, promTargetKeys[locationLabel]...),
clusterLabel: getStringOrEmpty(attrs, promTargetKeys[clusterLabel]...),
clusterLabel: getStringOrDefaultClusterName(attrs, promTargetKeys[clusterLabel]...),
namespaceLabel: getStringOrEmpty(attrs, promTargetKeys[namespaceLabel]...),
jobLabel: job,
instanceLabel: getStringOrEmpty(attrs, promTargetKeys[instanceLabel]...),
},
}
}

// getStringOrEmpty returns the value of the first key found, or the empty string.
func getStringOrEmpty(attributes pcommon.Map, keys ...string) string {
// According to Cloud Monitoring docs, there are special values for
// cluser in some runtimes.
// See: https://cloud.google.com/stackdriver/docs/managed-prometheus/setup-opsagent
func getStringOrDefaultClusterName(attrs pcommon.Map, keys ...string) string {
defaultClusterName := ""
cloudPlatform := getStringOrEmpty(attrs, semconv.AttributeCloudPlatform)
switch cloudPlatform {
case semconv.AttributeCloudPlatformGCPComputeEngine:
defaultClusterName = "__gce__"
case semconv.AttributeCloudPlatformGCPCloudRun:
defaultClusterName = "__run__"
}
return getStringOrDefault(attrs, defaultClusterName, promTargetKeys[clusterLabel]...)
}

// getStringOrEmpty returns the value of the first key found, or the orElse string.
func getStringOrDefault(attributes pcommon.Map, orElse string, keys ...string) string {
for _, k := range keys {
// skip the attribute if it starts with unknown_service, since the SDK
// sets this by default. It is used as a fallback below if no other
Expand All @@ -85,7 +100,12 @@ func getStringOrEmpty(attributes pcommon.Map, keys ...string) string {
return val.Str()
}
}
return ""
return orElse
}

// getStringOrEmpty returns the value of the first key found, or the empty string.
func getStringOrEmpty(attributes pcommon.Map, keys ...string) string {
return getStringOrDefault(attributes, "", keys...)
}

func contains(list []string, element string) bool {
Expand Down
Expand Up @@ -165,6 +165,45 @@ func TestMapToPrometheusTarget(t *testing.T) {
},
},
},
{
desc: "Attributes from cloud run with environment label",
resourceLabels: map[string]string{
"cloud.platform": "gcp_cloud_run",
"cloud.region": "us-central1",
"service.name": "unknown_service:go",
"faas.name": "my-cloud-run-service",
"faas.instance": "1234759430923053489543203",
},
expected: &monitoredrespb.MonitoredResource{
Type: "prometheus_target",
Labels: map[string]string{
"location": "us-central1",
"cluster": "__run__",
"namespace": "",
"job": "my-cloud-run-service",
"instance": "1234759430923053489543203",
},
},
},
{
desc: "Attributes from GCE with environment label",
resourceLabels: map[string]string{
"cloud.platform": "gcp_compute_engine",
"cloud.region": "us-central1",
"service.name": "service-name",
"service.instance.id": "1234759430923053489543203",
},
expected: &monitoredrespb.MonitoredResource{
Type: "prometheus_target",
Labels: map[string]string{
"location": "us-central1",
"cluster": "__gce__",
"namespace": "",
"job": "service-name",
"instance": "1234759430923053489543203",
},
},
},
} {
t.Run(tc.desc, func(t *testing.T) {
r := pcommon.NewResource()
Expand Down

0 comments on commit 18da23f

Please sign in to comment.