Repository: census-ecosystem/opencensus-go-exporter-stackdriver
Pull request #278 (2020-12-05 00:29:07)

Default mapping for knative_broker and knative_trigger.
Default mapping for knative_broker and knative_trigger.


Patch:
diff --git a/resource.go b/resource.go
index e32c4da..50331d2 100644
--- a/resource.go
+++ b/resource.go
@@ -41,6 +41,11 @@ const (
 	knativeConfigurationName = "configuration_name"
 	knativeNamespaceName     = "namespace_name"
 
+	knativeBrokerType  = "knative_broker"
+	knativeBrokerName  = "broker_name"
+	knativeTriggerType = "knative_trigger"
+	knativeTriggerName = "trigger_name"
+
 	appEngineInstanceType = "gae_instance"
 
 	appEngineService  = "appengine.service.id"
@@ -133,6 +138,23 @@ var knativeRevisionResourceMap = map[string]string{
 	knativeNamespaceName:     knativeNamespaceName,
 }
 
+var knativeBrokerResourceMap = map[string]string{
+	"project_id":         stackdriverProjectID,
+	"location":           resourcekeys.CloudKeyZone,
+	"cluster_name":       resourcekeys.K8SKeyClusterName,
+	knativeNamespaceName: knativeNamespaceName,
+	knativeBrokerName:    knativeBrokerName,
+}
+
+var knativeTriggerResourceMap = map[string]string{
+	"project_id":         stackdriverProjectID,
+	"location":           resourcekeys.CloudKeyZone,
+	"cluster_name":       resourcekeys.K8SKeyClusterName,
+	knativeNamespaceName: knativeNamespaceName,
+	knativeBrokerName:    knativeBrokerName,
+	knativeTriggerName:   knativeTriggerName,
+}
+
 // getAutodetectedLabels returns all the labels from the Monitored Resource detected
 // from the environment by calling monitoredresource.Autodetect. If a "zone" label is detected,
 // a "location" label is added with the same value to account for differences between
@@ -213,6 +235,12 @@ func DefaultMapResource(res *resource.Resource) *monitoredrespb.MonitoredResourc
 	case res.Type == knativeResType:
 		result.Type = res.Type
 		match = knativeRevisionResourceMap
+	case res.Type == knativeBrokerType:
+		result.Type = knativeBrokerType
+		match = knativeBrokerResourceMap
+	case res.Type == knativeTriggerType:
+		result.Type = knativeTriggerType
+		match = knativeTriggerResourceMap
 	}
 
 	var missing bool


Test patch:
diff --git a/resource_test.go b/resource_test.go
index 1a95a2e..87341a7 100644
--- a/resource_test.go
+++ b/resource_test.go
@@ -360,6 +360,108 @@ func TestDefaultMapResource(t *testing.T) {
 				},
 			},
 		},
+		// Mapping for knative_broker with autodetected GCP metadata labels.
+		{
+			input: &resource.Resource{
+				Type: "knative_broker",
+				Labels: map[string]string{
+					knativeNamespaceName: "namespace1",
+					knativeBrokerName:    "default",
+				},
+			},
+			autoRes: &monitoredresource.GKEContainer{
+				ProjectID:   "proj1",
+				Zone:        "zone1",
+				ClusterName: "cluster1",
+			},
+			want: &monitoredrespb.MonitoredResource{
+				Type: "knative_broker",
+				Labels: map[string]string{
+					"project_id":     "proj1",
+					"location":       "zone1",
+					"cluster_name":   "cluster1",
+					"namespace_name": "namespace1",
+					"broker_name":    "default",
+				},
+			},
+		},
+		// Mapping for knative_broker with explicit GCP metadata labels.
+		{
+			input: &resource.Resource{
+				Type: "knative_broker",
+				Labels: map[string]string{
+					stackdriverProjectID:           "proj1",
+					resourcekeys.CloudKeyZone:      "zone1",
+					resourcekeys.K8SKeyClusterName: "cluster1",
+					knativeNamespaceName:           "namespace1",
+					knativeBrokerName:              "default",
+				},
+			},
+			autoRes: &monitoredresource.GKEContainer{},
+			want: &monitoredrespb.MonitoredResource{
+				Type: "knative_broker",
+				Labels: map[string]string{
+					"project_id":     "proj1",
+					"location":       "zone1",
+					"cluster_name":   "cluster1",
+					"namespace_name": "namespace1",
+					"broker_name":    "default",
+				},
+			},
+		},
+		// Mapping for knative_trigger with autodetected GCP metadata labels.
+		{
+			input: &resource.Resource{
+				Type: "knative_trigger",
+				Labels: map[string]string{
+					knativeNamespaceName: "namespace1",
+					knativeBrokerName:    "default",
+					knativeTriggerName:   "trigger-storage",
+				},
+			},
+			autoRes: &monitoredresource.GKEContainer{
+				ProjectID:   "proj1",
+				Zone:        "zone1",
+				ClusterName: "cluster1",
+			},
+			want: &monitoredrespb.MonitoredResource{
+				Type: "knative_trigger",
+				Labels: map[string]string{
+					"project_id":     "proj1",
+					"location":       "zone1",
+					"cluster_name":   "cluster1",
+					"namespace_name": "namespace1",
+					"broker_name":    "default",
+					"trigger_name":   "trigger-storage",
+				},
+			},
+		},
+		// Mapping for knative_trigger with explicit GCP metadata labels.
+		{
+			input: &resource.Resource{
+				Type: "knative_trigger",
+				Labels: map[string]string{
+					stackdriverProjectID:           "proj1",
+					resourcekeys.CloudKeyZone:      "zone1",
+					resourcekeys.K8SKeyClusterName: "cluster1",
+					knativeNamespaceName:           "namespace1",
+					knativeBrokerName:              "default",
+					knativeTriggerName:             "trigger-storage",
+				},
+			},
+			autoRes: &monitoredresource.GKEContainer{},
+			want: &monitoredrespb.MonitoredResource{
+				Type: "knative_trigger",
+				Labels: map[string]string{
+					"project_id":     "proj1",
+					"location":       "zone1",
+					"cluster_name":   "cluster1",
+					"namespace_name": "namespace1",
+					"broker_name":    "default",
+					"trigger_name":   "trigger-storage",
+				},
+			},
+		},
 	}
 	for i, c := range cases {
 		t.Run(fmt.Sprintf("case-%d", i), func(t *testing.T) {