Repository: census-ecosystem/opencensus-go-exporter-stackdriver
Pull request #253 (2020-02-20 00:22:35)

Allow exporting metrics with resource gae_instance.

codecov-io: # [Codecov](https://codecov.io/gh/census-ecosystem/opencensus-go-exporter-stackdriver/pull/253?src=pr&el=h1) Report
> Merging [#253](https://codecov.io/gh/census-ecosystem/opencensus-go-exporter-stackdriver/pull/253?src=pr&el=desc) into [master](https://codecov.io/gh/census-ecosystem/opencensus-go-exporter-stackdriver/commit/ddbc64abe5fffb1dd27cf39339f0ac45e7f2ea46?src=pr&el=desc) will **increase** coverage by `0.05%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/census-ecosystem/opencensus-go-exporter-stackdriver/pull/253/graphs/tree.svg?width=650&token=os6w8WyKNy&height=150&src=pr)](https://codecov.io/gh/census-ecosystem/opencensus-go-exporter-stackdriver/pull/253?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #253      +/-   ##
==========================================
+ Coverage   72.11%   72.16%   +0.05%     
==========================================
  Files          14       14              
  Lines        1646     1649       +3     
==========================================
+ Hits         1187     1190       +3     
  Misses        383      383              
  Partials       76       76
```


| [Impacted Files](https://codecov.io/gh/census-ecosystem/opencensus-go-exporter-stackdriver/pull/253?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [resource.go](https://codecov.io/gh/census-ecosystem/opencensus-go-exporter-stackdriver/pull/253/diff?src=pr&el=tree#diff-cmVzb3VyY2UuZ28=) | `100% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/census-ecosystem/opencensus-go-exporter-stackdriver/pull/253?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/census-ecosystem/opencensus-go-exporter-stackdriver/pull/253?src=pr&el=footer). Last update [ddbc64a...c9aebc2](https://codecov.io/gh/census-ecosystem/opencensus-go-exporter-stackdriver/pull/253?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

rghetia: @imccarten1 can you please also resolve the conflicts (due to #252)? 
imccarten1: I've resolved the conflicts and added gae_instance tests to the testdata in and out files.

Patch:
diff --git a/resource.go b/resource.go
index 82173c0..431bea6 100644
--- a/resource.go
+++ b/resource.go
@@ -40,6 +40,12 @@ const (
 	knativeRevisionName      = "revision_name"
 	knativeConfigurationName = "configuration_name"
 	knativeNamespaceName     = "namespace_name"
+
+	appEngineInstanceType = "gae_instance"
+
+	appEngineService  = "appengine.service.id"
+	appEngineVersion  = "appengine.version.id"
+	appEngineInstance = "appengine.instance.id"
 )
 
 var (
@@ -100,6 +106,14 @@ var awsResourceMap = map[string]string{
 	"aws_account": resourcekeys.CloudKeyAccountID,
 }
 
+var appEngineInstanceMap = map[string]string{
+	"project_id":  stackdriverProjectID,
+	"location":    resourcekeys.CloudKeyRegion,
+	"module_id":   appEngineService,
+	"version_id":  appEngineVersion,
+	"instance_id": appEngineInstance,
+}
+
 // Generic task resource.
 var genericResourceMap = map[string]string{
 	"project_id": stackdriverProjectID,
@@ -186,6 +200,9 @@ func defaultMapResource(res *resource.Resource) *monitoredrespb.MonitoredResourc
 	case res.Type == resourcekeys.HostType && res.Labels[resourcekeys.K8SKeyClusterName] != "":
 		result.Type = "k8s_node"
 		match = k8sNodeMap
+	case res.Type == appEngineInstanceType:
+		result.Type = appEngineInstanceType
+		match = appEngineInstanceMap
 	case res.Labels[resourcekeys.CloudKeyProvider] == resourcekeys.CloudProviderGCP:
 		result.Type = "gce_instance"
 		match = gcpResourceMap


Test patch:
diff --git a/resource_test.go b/resource_test.go
index fa3bd4d..01f66f4 100644
--- a/resource_test.go
+++ b/resource_test.go
@@ -238,6 +238,29 @@ func TestDefaultMapResource(t *testing.T) {
 				},
 			},
 		},
+		// Convert to App Engine Instance.
+		{
+			input: &resource.Resource{
+				Type: appEngineInstanceType,
+				Labels: map[string]string{
+					stackdriverProjectID:        "proj1",
+					resourcekeys.CloudKeyRegion: "region1",
+					appEngineService:            "default",
+					appEngineVersion:            "version1",
+					appEngineInstance:           "inst1",
+				},
+			},
+			want: &monitoredrespb.MonitoredResource{
+				Type: "gae_instance",
+				Labels: map[string]string{
+					"project_id":  "proj1",
+					"location":    "region1",
+					"module_id":   "default",
+					"version_id":  "version1",
+					"instance_id": "inst1",
+				},
+			},
+		},
 		// Convert to Global.
 		{
 			input: &resource.Resource{
diff --git a/testdata/Resources/in.txt b/testdata/Resources/in.txt
index deff5b5..22fb86d 100644
--- a/testdata/Resources/in.txt
+++ b/testdata/Resources/in.txt
@@ -121,3 +121,21 @@ labels: <
   key: "k8s.pod.name"
   value: "pod1"
 >
+---
+type: "gae_instance"
+labels: <
+  key: "appengine.service.id"
+  value: "default"
+>
+labels: <
+  key: "appengine.version.id"
+  value: "version1"
+>
+labels: <
+  key: "appengine.instance.id"
+  value: "inst1"
+>
+labels: <
+  key: "cloud.region"
+  value: "region1"
+>
diff --git a/testdata/Resources/out.txt b/testdata/Resources/out.txt
index 26cb400..109dd3e 100644
--- a/testdata/Resources/out.txt
+++ b/testdata/Resources/out.txt
@@ -77,4 +77,21 @@ labels: <
 >
 ---
 type: "global"
-
+---
+type: "gae_instance"
+labels: <
+  key: "module_id"
+  value: "default"
+>
+labels: <
+  key: "version_id"
+  value: "version1"
+>
+labels: <
+  key: "instance_id"
+  value: "inst1"
+>
+labels: <
+  key: "location"
+  value: "region1"
+>
diff --git a/testdata/ResourcesWithMissingFields/in.txt b/testdata/ResourcesWithMissingFields/in.txt
index b8340b6..3322f83 100644
--- a/testdata/ResourcesWithMissingFields/in.txt
+++ b/testdata/ResourcesWithMissingFields/in.txt
@@ -121,3 +121,22 @@ labels: <
   key: "k8s.pod.name"
   value: "pod1"
 >
+---
+type: "gae_instance"
+#labels: <
+#  key: "appengine.service.id"
+#  value: "default"
+#>
+labels: <
+  key: "appengine.version.id"
+  value: "version1"
+>
+labels: <
+  key: "appengine.instance.id"
+  value: "inst1"
+>
+labels: <
+  key: "cloud.region"
+  value: "region1"
+>
+
diff --git a/testdata/ResourcesWithMissingFields/out.txt b/testdata/ResourcesWithMissingFields/out.txt
index 5093a9f..f14d7fc 100644
--- a/testdata/ResourcesWithMissingFields/out.txt
+++ b/testdata/ResourcesWithMissingFields/out.txt
@@ -9,4 +9,5 @@ type: "global"
 type: "global"
 ---
 type: "global"
-
+---
+type: "global"