1515package stackdriver // import "contrib.go.opencensus.io/exporter/stackdriver"
1616
1717import (
18- "contrib.go.opencensus.io/resource/resourcekeys"
18+ "fmt"
19+
1920 "go.opencensus.io/resource"
21+ "go.opencensus.io/resource/resourcekeys"
2022 monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres"
2123)
2224
@@ -39,71 +41,69 @@ const (
3941)
4042
4143// Mappings for the well-known OpenCensus resources to applicable Stackdriver resources.
42- var resourceMappings = []resourceMap {
43- {
44- srcType : resourcekeys .K8STypeContainer ,
45- dstType : "k8s_container" ,
46- labels : map [string ]string {
47- "project_id" : stackdriverProjectID ,
48- "location" : stackdriverLocation ,
49- "cluster_name" : resourcekeys .K8SKeyClusterName ,
50- "namespace_name" : resourcekeys .K8SKeyNamespaceName ,
51- "pod_name" : resourcekeys .K8SKeyPodName ,
52- "container_name" : resourcekeys .K8SKeyContainerName ,
53- },
54- },
55- {
56- srcType : resourcekeys .GCPTypeGCEInstance ,
57- dstType : "gce_instance" ,
58- labels : map [string ]string {
59- "project_id" : resourcekeys .GCPKeyGCEProjectID ,
60- "instance_id" : resourcekeys .GCPKeyGCEInstanceID ,
61- "zone" : resourcekeys .GCPKeyGCEZone ,
62- },
63- },
64- {
65- srcType : resourcekeys .AWSTypeEC2Instance ,
66- dstType : "aws_ec2_instance" ,
67- labels : map [string ]string {
68- "project_id" : stackdriverProjectID ,
69- "instance_id" : resourcekeys .AWSKeyEC2InstanceID ,
70- "region" : resourcekeys .AWSKeyEC2Region ,
71- "aws_account" : resourcekeys .AWSKeyEC2AccountID ,
72- },
73- },
74- // Fallback to generic task resource.
75- {
76- srcType : "" ,
77- dstType : "generic_task" ,
78- labels : map [string ]string {
79- "project_id" : stackdriverProjectID ,
80- "location" : stackdriverLocation ,
81- "namespace" : stackdriverGenericTaskNamespace ,
82- "job" : stackdriverGenericTaskJob ,
83- "task_id" : stackdriverGenericTaskID ,
84- },
85- },
44+ var k8sResourceMap = map [string ]string {
45+ "project_id" : stackdriverProjectID ,
46+ "location" : resourcekeys .CloudKeyZone ,
47+ "cluster_name" : resourcekeys .K8SKeyClusterName ,
48+ "namespace_name" : resourcekeys .K8SKeyNamespaceName ,
49+ "pod_name" : resourcekeys .K8SKeyPodName ,
50+ "container_name" : resourcekeys .ContainerKeyName ,
8651}
8752
88- func defaultMapResource (res * resource.Resource ) * monitoredrespb.MonitoredResource {
89- Outer:
90- for _ , rm := range resourceMappings {
91- if res .Type != rm .srcType {
92- continue
53+ var gcpResourceMap = map [string ]string {
54+ "project_id" : stackdriverProjectID ,
55+ "instance_id" : resourcekeys .HostKeyID ,
56+ "zone" : resourcekeys .CloudKeyZone ,
57+ }
58+
59+ var awsResourceMap = map [string ]string {
60+ "project_id" : stackdriverProjectID ,
61+ "instance_id" : resourcekeys .HostKeyID ,
62+ "region" : resourcekeys .CloudKeyRegion ,
63+ "aws_account" : resourcekeys .CloudKeyAccountID ,
64+ }
65+
66+ // Generic task resource.
67+ var genericResourceMap = map [string ]string {
68+ "project_id" : stackdriverProjectID ,
69+ "location" : stackdriverLocation ,
70+ "namespace" : stackdriverGenericTaskNamespace ,
71+ "job" : stackdriverGenericTaskJob ,
72+ "task_id" : stackdriverGenericTaskID ,
73+ }
74+
75+ func transformResource (match , input map [string ]string ) (map [string ]string , bool ) {
76+ output := make (map [string ]string , len (input ))
77+ for dst , src := range match {
78+ if v , ok := input [src ]; ok {
79+ output [dst ] = v
9380 }
94- result := & monitoredrespb.MonitoredResource {
95- Type : rm .dstType ,
96- Labels : make (map [string ]string , len (rm .labels )),
81+ }
82+ return output , true
83+ }
84+
85+ func defaultMapResource (res * resource.Resource ) * monitoredrespb.MonitoredResource {
86+ match := genericResourceMap
87+ result := & monitoredrespb.MonitoredResource {
88+ Type : "generic_task" ,
89+ }
90+ if res .Type == resourcekeys .K8SType {
91+ result .Type = "k8s_container"
92+ match = k8sResourceMap
93+ } else if v , ok := res .Labels [resourcekeys .CloudKeyProvider ]; ok {
94+ if v == resourcekeys .CloudProviderGCP {
95+ result .Type = "gce_instance"
96+ match = gcpResourceMap
97+ } else if v == resourcekeys .CloudProviderAWS {
98+ result .Type = "aws_ec2_instance"
99+ match = awsResourceMap
97100 }
98- for dst , src := range rm .labels {
99- if v , ok := res .Labels [src ]; ok {
100- result .Labels [dst ] = v
101- } else {
102- // A required label wasn't filled at all. Try subsequent mappings.
103- continue Outer
104- }
101+ }
102+ result .Labels , _ = transformResource (match , res .Labels )
103+ if result .Type == "aws_ec2_instance" {
104+ if v , ok := result .Labels ["region" ]; ok {
105+ result .Labels ["region" ] = fmt .Sprintf ("aws:%s" , v )
105106 }
106- return result
107107 }
108- return nil
108+ return result
109109}
0 commit comments