Skip to content

Commit

Permalink
only use unknown_service as a fallback for job label
Browse files Browse the repository at this point in the history
  • Loading branch information
dashpole committed Nov 1, 2023
1 parent 2fb61df commit 0c26e6c
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 39 deletions.
24 changes: 21 additions & 3 deletions exporter/collector/googlemanagedprometheus/monitoredresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package googlemanagedprometheus

import (
"strings"

"go.opentelemetry.io/collector/pdata/pcommon"
semconv "go.opentelemetry.io/collector/semconv/v1.18.0"
monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres"
Expand All @@ -32,6 +34,7 @@ const (
jobLabel = "job"
serviceNamespaceLabel = "service_namespace"
instanceLabel = "instance"
unknownServicePrefix = "unknown_service"
)

// promTargetKeys are attribute keys which are used in the prometheus_target monitored resource.
Expand All @@ -40,9 +43,9 @@ var promTargetKeys = map[string][]string{
locationLabel: {locationLabel, semconv.AttributeCloudAvailabilityZone, semconv.AttributeCloudRegion},
clusterLabel: {clusterLabel, semconv.AttributeK8SClusterName},
namespaceLabel: {namespaceLabel, semconv.AttributeK8SNamespaceName},
jobLabel: {jobLabel, semconv.AttributeFaaSName, semconv.AttributeServiceName},
jobLabel: {jobLabel, semconv.AttributeServiceName, semconv.AttributeFaaSName},
serviceNamespaceLabel: {semconv.AttributeServiceNamespace},
instanceLabel: {instanceLabel, semconv.AttributeFaaSInstance, semconv.AttributeServiceInstanceID},
instanceLabel: {instanceLabel, semconv.AttributeServiceInstanceID, semconv.AttributeFaaSInstance},
}

func (c Config) MapToPrometheusTarget(res pcommon.Resource) *monitoredrespb.MonitoredResource {
Expand All @@ -69,9 +72,24 @@ func (c Config) MapToPrometheusTarget(res pcommon.Resource) *monitoredrespb.Moni
// getStringOrEmpty returns the value of the first key found, or the empty string.
func getStringOrEmpty(attributes pcommon.Map, keys ...string) string {
for _, k := range keys {
if val, ok := attributes.Get(k); ok {
if val, ok := attributes.Get(k); ok && !strings.HasPrefix(val.Str(), unknownServicePrefix) {
return val.Str()
}
}
if contains(keys, string(semconv.AttributeServiceName)) {
// the service name started with unknown_service, and was ignored above
if val, ok := attributes.Get(semconv.AttributeServiceName); ok {
return val.Str()
}
}
return ""
}

func contains(list []string, element string) bool {
for _, item := range list {
if item == element {
return true
}
}
return false
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func TestMapToPrometheusTarget(t *testing.T) {
desc: "Attributes from cloud run",
resourceLabels: map[string]string{
"cloud.region": "us-central1",
"service.name": "service:unknown",
"service.name": "unknown_service:go",
"faas.name": "my-cloud-run-service",
"faas.instance": "1234759430923053489543203",
},
Expand Down
64 changes: 64 additions & 0 deletions exporter/collector/monitoredresource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,70 @@ func TestResourceMetricsToMonitoringMonitoredResource(t *testing.T) {
},
},
},
{
name: "Cloud Run Instance",
resourceLabels: map[string]string{
"cloud.provider": "gcp",
"cloud.platform": "gcp_cloud_run",
"cloud.region": "my-region",
"faas.instance": "myinstanceid",
"faas.name": "myfaasname",
"faas.version": "v1",
},
expectMr: &monitoredrespb.MonitoredResource{
Type: "generic_task",
Labels: map[string]string{
"job": "myfaasname",
"location": "my-region",
"namespace": "",
"task_id": "myinstanceid",
},
},
},
{
name: "Cloud Run Instance with default service",
resourceLabels: map[string]string{
"cloud.provider": "gcp",
"cloud.platform": "gcp_cloud_run",
"cloud.region": "my-region",
"service.name": "unknown_service:go",
"faas.instance": "myinstanceid",
"faas.name": "myfaasname",
"faas.version": "v1",
},
expectMr: &monitoredrespb.MonitoredResource{
Type: "generic_task",
Labels: map[string]string{
"job": "myfaasname",
"location": "my-region",
"namespace": "",
"task_id": "myinstanceid",
},
},
},
{
name: "Cloud Run Instance with custom service",
resourceLabels: map[string]string{
"cloud.provider": "gcp",
"cloud.platform": "gcp_cloud_run",
"cloud.region": "my-region",
"service.name": "customservice",
"service.namespace": "customnamespace",
"service.instance.id": "customserviceid",
"faas.instance": "myinstanceid",
"faas.name": "myfaasname",
"faas.version": "v1",
},
expectMr: &monitoredrespb.MonitoredResource{
Type: "generic_task",
Labels: map[string]string{
"job": "customservice",
"location": "my-region",
"namespace": "customnamespace",
"task_id": "customserviceid",
},
},
},
}

for _, test := range tests {
Expand Down
3 changes: 2 additions & 1 deletion exporter/metric/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ func TestResourceToMonitoredResourcepb(t *testing.T) {
},
},
{
desc: "Cloud Run From Detector",
desc: "Cloud Run From Detector with default service",
resource: resource.NewWithAttributes(
semconv.SchemaURL,
attribute.String("cloud.provider", "gcp"),
Expand All @@ -578,6 +578,7 @@ func TestResourceToMonitoredResourcepb(t *testing.T) {
attribute.String("faas.instance", "bar"),
attribute.String("faas.name", "x-service"),
attribute.String("faas.version", "v1"),
attribute.String("service.name", "unknown_service:go"),
),
expectedType: "generic_task",
expectedLabels: map[string]string{
Expand Down
82 changes: 48 additions & 34 deletions internal/resourcemapping/resourcemapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,40 @@ import (
const (
ProjectIDAttributeKey = "gcp.project.id"

awsAccount = "aws_account"
awsEc2Instance = "aws_ec2_instance"
clusterName = "cluster_name"
containerName = "container_name"
gceInstance = "gce_instance"
genericNode = "generic_node"
genericTask = "generic_task"
instanceID = "instance_id"
job = "job"
k8sCluster = "k8s_cluster"
k8sContainer = "k8s_container"
k8sNode = "k8s_node"
k8sPod = "k8s_pod"
location = "location"
namespace = "namespace"
namespaceName = "namespace_name"
nodeID = "node_id"
nodeName = "node_name"
podName = "pod_name"
region = "region"
taskID = "task_id"
zone = "zone"
gaeInstance = "gae_instance"
gaeApp = "gae_app"
gaeModuleID = "module_id"
gaeVersionID = "version_id"
cloudRunRevision = "cloud_run_revision"
cloudFunction = "cloud_function"
cloudFunctionName = "function_name"
serviceName = "service_name"
configurationName = "configuration_name"
revisionName = "revision_name"
bmsInstance = "baremetalsolution.googleapis.com/Instance"
awsAccount = "aws_account"
awsEc2Instance = "aws_ec2_instance"
clusterName = "cluster_name"
containerName = "container_name"
gceInstance = "gce_instance"
genericNode = "generic_node"
genericTask = "generic_task"
instanceID = "instance_id"
job = "job"
k8sCluster = "k8s_cluster"
k8sContainer = "k8s_container"
k8sNode = "k8s_node"
k8sPod = "k8s_pod"
location = "location"
namespace = "namespace"
namespaceName = "namespace_name"
nodeID = "node_id"
nodeName = "node_name"
podName = "pod_name"
region = "region"
taskID = "task_id"
zone = "zone"
gaeInstance = "gae_instance"
gaeApp = "gae_app"
gaeModuleID = "module_id"
gaeVersionID = "version_id"
cloudRunRevision = "cloud_run_revision"
cloudFunction = "cloud_function"
cloudFunctionName = "function_name"
serviceName = "service_name"
configurationName = "configuration_name"
revisionName = "revision_name"
bmsInstance = "baremetalsolution.googleapis.com/Instance"
unknownServicePrefix = "unknown_service"
)

var (
Expand Down Expand Up @@ -252,10 +253,14 @@ func createMonitoredResource(
// Coalesce the possible keys in order
for _, otelKey := range mappingConfig.otelKeys {
mrValue, ok = resourceAttrs.GetString(otelKey)
if mrValue != "" {
if mrValue != "" && !strings.HasPrefix(mrValue, unknownServicePrefix) {
break
}
}
if mrValue == "" && contains(mappingConfig.otelKeys, string(semconv.ServiceNameKey)) {
// the service name started with unknown_service, and was ignored above
mrValue, ok = resourceAttrs.GetString(string(semconv.ServiceNameKey))
}
if !ok || mrValue == "" {
mrValue = mappingConfig.fallbackLiteral
}
Expand All @@ -267,6 +272,15 @@ func createMonitoredResource(
}
}

func contains(list []string, element string) bool {
for _, item := range list {
if item == element {
return true
}
}
return false
}

func sanitizeUTF8(s string) string {
return strings.ToValidUTF8(s, "�")
}

0 comments on commit 0c26e6c

Please sign in to comment.