Repository: census-ecosystem/opencensus-go-exporter-stackdriver
Pull request #255 (2020-03-30 23:00:11)

Make defaultMapResource function public to allow reusing it in custom functions
In case there is a need to handle extra resource types, it's useful to be able to keep the original behavior, e.g.

```go
func myMapResource(res *resource.Resource) *monitoredrespb.MonitoredResource {
  if res.Type == "my-resource-type" {
    // custom handling
    ...
    return result
  }

  // otherwise return default result
  return stackdriver.DefaultMapResource(res)
}
```


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

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

```diff
@@           Coverage Diff           @@
##           master     #255   +/-   ##
=======================================
  Coverage   72.02%   72.02%           
=======================================
  Files          17       17           
  Lines        1691     1691           
=======================================
  Hits         1218     1218           
  Misses        397      397           
  Partials       76       76           
```


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

------

[Continue to review full report at Codecov](https://codecov.io/gh/census-ecosystem/opencensus-go-exporter-stackdriver/pull/255?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/255?src=pr&el=footer). Last update [1d104f7...59690dd](https://codecov.io/gh/census-ecosystem/opencensus-go-exporter-stackdriver/pull/255?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

nilebox: /cc @rghetia 
nilebox: The build succeeds in Travis CI (https://travis-ci.org/github/census-ecosystem/opencensus-go-exporter-stackdriver/builds/669049722), but GitHub doesn't receive the status for some reason.
rghetia: > The build succeeds in Travis CI (https://travis-ci.org/github/census-ecosystem/opencensus-go-exporter-stackdriver/builds/669049722), but GitHub doesn't receive the status for some reason.

Restarted the build.

Patch:
diff --git a/resource.go b/resource.go
index 431bea6..e32c4da 100644
--- a/resource.go
+++ b/resource.go
@@ -181,7 +181,8 @@ func transformResource(match, input map[string]string) (map[string]string, bool)
 	return output, false
 }
 
-func defaultMapResource(res *resource.Resource) *monitoredrespb.MonitoredResource {
+// DefaultMapResource implements default resource mapping for well-known resource types
+func DefaultMapResource(res *resource.Resource) *monitoredrespb.MonitoredResource {
 	match := genericResourceMap
 	result := &monitoredrespb.MonitoredResource{
 		Type: "global",
diff --git a/stackdriver.go b/stackdriver.go
index 3d0d289..7524b44 100644
--- a/stackdriver.go
+++ b/stackdriver.go
@@ -183,7 +183,7 @@ type Options struct {
 
 	// MapResource converts a OpenCensus resource to a Stackdriver monitored resource.
 	//
-	// If this field is unset, defaultMapResource will be used which encodes a set of default
+	// If this field is unset, DefaultMapResource will be used which encodes a set of default
 	// conversions from auto-detected resources to well-known Stackdriver monitored resources.
 	MapResource func(*resource.Resource) *monitoredrespb.MonitoredResource
 
@@ -333,7 +333,7 @@ func NewExporter(o Options) (*Exporter, error) {
 		o.Resource = convertMonitoredResourceToPB(o.MonitoredResource)
 	}
 	if o.MapResource == nil {
-		o.MapResource = defaultMapResource
+		o.MapResource = DefaultMapResource
 	}
 	if o.ResourceDetector != nil {
 		// For backwards-compatibility we still respect the deprecated resource field.


Test patch:
diff --git a/equivalence_test.go b/equivalence_test.go
index 172eefc..a2ffadf 100644
--- a/equivalence_test.go
+++ b/equivalence_test.go
@@ -87,7 +87,7 @@ func TestStatsAndMetricsEquivalence(t *testing.T) {
 	// Now perform some exporting.
 	for i, metric := range metrics {
 		se := &statsExporter{
-			o: Options{ProjectID: "equivalence", MapResource: defaultMapResource},
+			o: Options{ProjectID: "equivalence", MapResource: DefaultMapResource},
 		}
 
 		ctx := context.Background()
@@ -148,7 +148,7 @@ func TestEquivalenceStatsVsMetricsUploads(t *testing.T) {
 		// so that batching is performed deterministically and flushing is
 		// fully controlled by us.
 		BundleDelayThreshold: 2 * time.Hour,
-		MapResource:          defaultMapResource,
+		MapResource:          DefaultMapResource,
 	}
 	se, err := newStatsExporter(exporterOptions)
 	if err != nil {
diff --git a/metrics_proto_test.go b/metrics_proto_test.go
index f69a9f4..aa99609 100644
--- a/metrics_proto_test.go
+++ b/metrics_proto_test.go
@@ -54,7 +54,7 @@ func TestExportTimeSeriesWithDifferentLabels(t *testing.T) {
 
 		// Set empty labels to avoid the opencensus-task
 		DefaultMonitoringLabels: &Labels{},
-		MapResource:             defaultMapResource,
+		MapResource:             DefaultMapResource,
 	}
 	se, err := newStatsExporter(exporterOptions)
 	if err != nil {
@@ -260,7 +260,7 @@ func TestProtoMetricToCreateTimeSeriesRequest(t *testing.T) {
 				},
 			},
 			statsExporter: &statsExporter{
-				o: Options{ProjectID: "foo", MapResource: defaultMapResource},
+				o: Options{ProjectID: "foo", MapResource: DefaultMapResource},
 			},
 			want: []*monitoringpb.CreateTimeSeriesRequest{
 				{
@@ -331,7 +331,7 @@ func TestProtoMetricToCreateTimeSeriesRequest(t *testing.T) {
 				},
 			},
 			statsExporter: &statsExporter{
-				o: Options{ProjectID: "foo", MapResource: defaultMapResource},
+				o: Options{ProjectID: "foo", MapResource: DefaultMapResource},
 			},
 			want: []*monitoringpb.CreateTimeSeriesRequest{
 				{
@@ -446,7 +446,7 @@ func TestProtoMetricWithDifferentResource(t *testing.T) {
 				},
 			},
 			statsExporter: &statsExporter{
-				o: Options{ProjectID: "foo", MapResource: defaultMapResource},
+				o: Options{ProjectID: "foo", MapResource: DefaultMapResource},
 			},
 			want: []*monitoringpb.CreateTimeSeriesRequest{
 				{
@@ -518,7 +518,7 @@ func TestProtoMetricWithDifferentResource(t *testing.T) {
 				},
 			},
 			statsExporter: &statsExporter{
-				o: Options{ProjectID: "foo", MapResource: defaultMapResource},
+				o: Options{ProjectID: "foo", MapResource: DefaultMapResource},
 			},
 			want: []*monitoringpb.CreateTimeSeriesRequest{
 				{
diff --git a/resource_test.go b/resource_test.go
index 01f66f4..1a95a2e 100644
--- a/resource_test.go
+++ b/resource_test.go
@@ -373,7 +373,7 @@ func TestDefaultMapResource(t *testing.T) {
 				autodetectFunc = func() gcp.Interface { return c.autoRes }
 			}
 
-			got := defaultMapResource(c.input)
+			got := DefaultMapResource(c.input)
 			if diff := cmp.Diff(got, c.want); diff != "" {
 				t.Errorf("Values differ -got +want: %s", diff)
 			}