Skip to content
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

Provide useful cluster names for non k8s environments on managed prom… #776

Merged
merged 2 commits into from Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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