Repository: GoogleCloudPlatform/opentelemetry-operations-go
Pull request #564 (2023-01-10 16:31:38)

Include service attributes in logs exporter by default
This copies the flow from metrics to include service resource attributes as LogEntry labels

codecov[bot]: # [Codecov](https://codecov.io/gh/GoogleCloudPlatform/opentelemetry-operations-go/pull/564?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=GoogleCloudPlatform) Report
> Merging [#564](https://codecov.io/gh/GoogleCloudPlatform/opentelemetry-operations-go/pull/564?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=GoogleCloudPlatform) (c29f620) into [main](https://codecov.io/gh/GoogleCloudPlatform/opentelemetry-operations-go/commit/cb447ae4e6547a832914ca622502e3b448c86c01?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=GoogleCloudPlatform) (cb447ae) will **increase** coverage by `0.12%`.
> The diff coverage is `75.00%`.

```diff
@@            Coverage Diff             @@
##             main     #564      +/-   ##
==========================================
+ Coverage   64.00%   64.12%   +0.12%     
==========================================
  Files          37       37              
  Lines        4175     4178       +3     
==========================================
+ Hits         2672     2679       +7     
+ Misses       1390     1386       -4     
  Partials      113      113              
```


| [Impacted Files](https://codecov.io/gh/GoogleCloudPlatform/opentelemetry-operations-go/pull/564?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=GoogleCloudPlatform) | Coverage Δ | |
|---|---|---|
| [exporter/collector/logs.go](https://codecov.io/gh/GoogleCloudPlatform/opentelemetry-operations-go/pull/564?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=GoogleCloudPlatform#diff-ZXhwb3J0ZXIvY29sbGVjdG9yL2xvZ3MuZ28=) | `42.05% <0.00%> (ø)` | |
| [exporter/collector/config.go](https://codecov.io/gh/GoogleCloudPlatform/opentelemetry-operations-go/pull/564?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=GoogleCloudPlatform#diff-ZXhwb3J0ZXIvY29sbGVjdG9yL2NvbmZpZy5nbw==) | `47.16% <100.00%> (+1.53%)` | :arrow_up: |
| [...lector/internal/datapointstorage/datapointcache.go](https://codecov.io/gh/GoogleCloudPlatform/opentelemetry-operations-go/pull/564?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=GoogleCloudPlatform#diff-ZXhwb3J0ZXIvY29sbGVjdG9yL2ludGVybmFsL2RhdGFwb2ludHN0b3JhZ2UvZGF0YXBvaW50Y2FjaGUuZ28=) | `83.33% <0.00%> (+3.03%)` | :arrow_up: |

:mega: We’re building smart automated test selection to slash your CI/CD build times. [Learn more](https://about.codecov.io/iterative-testing/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=GoogleCloudPlatform)

damemi: (cc @jsuereth)
dashpole: Is this still a WIP?
damemi: > Is this still a WIP?

no, sorry. going with the labels for now and opened #565 to determine if that's our long-term solution or not

Patch:
diff --git a/exporter/collector/config.go b/exporter/collector/config.go
index d438927..fbda5ed 100644
--- a/exporter/collector/config.go
+++ b/exporter/collector/config.go
@@ -159,6 +159,10 @@ type LogConfig struct {
 	// Defaults to empty, which won't include any additional resource labels.
 	ResourceFilters []ResourceFilter `mapstructure:"resource_filters"`
 	ClientConfig    ClientConfig     `mapstructure:",squash"`
+	// ServiceResourceLabels, if true, causes the exporter to copy OTel's
+	// service.name, service.namespace, and service.instance.id resource attributes into the Cloud Logging LogEntry labels.
+	// Disabling this option does not prevent resource_filters from adding those labels. Default is true.
+	ServiceResourceLabels bool `mapstructure:"service_resource_labels"`
 }
 
 // Known metric domains. Note: This is now configurable for advanced usages.
@@ -168,6 +172,9 @@ var domains = []string{"googleapis.com", "kubernetes.io", "istio.io", "knative.d
 func DefaultConfig() Config {
 	return Config{
 		UserAgent: "opentelemetry-collector-contrib {{version}}",
+		LogConfig: LogConfig{
+			ServiceResourceLabels: true,
+		},
 		MetricConfig: MetricConfig{
 			KnownDomains:                     domains,
 			Prefix:                           "workload.googleapis.com",
diff --git a/exporter/collector/logs.go b/exporter/collector/logs.go
index 323677f..6897ee1 100644
--- a/exporter/collector/logs.go
+++ b/exporter/collector/logs.go
@@ -212,7 +212,7 @@ func (l logMapper) createEntries(ld plog.Logs) ([]*logpb.LogEntry, error) {
 	for i := 0; i < ld.ResourceLogs().Len(); i++ {
 		rl := ld.ResourceLogs().At(i)
 		mr := defaultResourceToMonitoredResource(rl.Resource())
-		extraResourceLabels := resourceToLabels(rl.Resource(), false, l.cfg.LogConfig.ResourceFilters, l.obs.log)
+		extraResourceLabels := resourceToLabels(rl.Resource(), l.cfg.LogConfig.ServiceResourceLabels, l.cfg.LogConfig.ResourceFilters, l.obs.log)
 		projectID := l.cfg.ProjectID
 		// override project ID with gcp.project.id, if present
 		if projectFromResource, found := rl.Resource().Attributes().Get(resourcemapping.ProjectIDAttributeKey); found {


Test patch:
diff --git a/exporter/collector/integrationtest/config/config_test.go b/exporter/collector/integrationtest/config/config_test.go
index ac8a213..52e4916 100644
--- a/exporter/collector/integrationtest/config/config_test.go
+++ b/exporter/collector/integrationtest/config/config_test.go
@@ -82,7 +82,8 @@ func TestLoadConfig(t *testing.T) {
 					ClientConfig: collector.ClientConfig{
 						GRPCPoolSize: 1,
 					},
-					DefaultLogName: "foo-log",
+					DefaultLogName:        "foo-log",
+					ServiceResourceLabels: true,
 				},
 			},
 		})
diff --git a/exporter/collector/integrationtest/testdata/fixtures/logs/logs_apache_access_resource_attributes.json b/exporter/collector/integrationtest/testdata/fixtures/logs/logs_apache_access_resource_attributes.json
index ada3e16..64b6217 100644
--- a/exporter/collector/integrationtest/testdata/fixtures/logs/logs_apache_access_resource_attributes.json
+++ b/exporter/collector/integrationtest/testdata/fixtures/logs/logs_apache_access_resource_attributes.json
@@ -14,6 +14,24 @@
             "value": {
               "stringValue": "baz"
             }
+          },
+          {
+            "key": "service.name",
+            "value": {
+              "stringValue": "my_service_name"
+            }
+          },
+          {
+            "key": "service.namespace",
+            "value": {
+              "stringValue": "my_service_namespace"
+            }
+          },
+          {
+            "key": "service.instance.id",
+            "value": {
+              "stringValue": "my_service_instance_id"
+            }
           }
         ]
       },
diff --git a/exporter/collector/integrationtest/testdata/fixtures/logs/logs_apache_access_resource_attributes_expected.json b/exporter/collector/integrationtest/testdata/fixtures/logs/logs_apache_access_resource_attributes_expected.json
index 78cf820..c980c0d 100644
--- a/exporter/collector/integrationtest/testdata/fixtures/logs/logs_apache_access_resource_attributes_expected.json
+++ b/exporter/collector/integrationtest/testdata/fixtures/logs/logs_apache_access_resource_attributes_expected.json
@@ -23,7 +23,10 @@
           },
           "labels": {
             "custom_foobar": "baz",
-            "log.file.name": "test.log"
+            "log.file.name": "test.log",
+            "service_instance_id": "my_service_instance_id",
+            "service_name": "my_service_name",
+            "service_namespace": "my_service_namespace"
           }
         },
         {
@@ -47,7 +50,10 @@
           },
           "labels": {
             "custom_foobar": "baz",
-            "log.file.name": "test.log"
+            "log.file.name": "test.log",
+            "service_instance_id": "my_service_instance_id",
+            "service_name": "my_service_name",
+            "service_namespace": "my_service_namespace"
           }
         },
         {
@@ -71,7 +77,10 @@
           },
           "labels": {
             "custom_foobar": "baz",
-            "log.file.name": "test.log"
+            "log.file.name": "test.log",
+            "service_instance_id": "my_service_instance_id",
+            "service_name": "my_service_name",
+            "service_namespace": "my_service_namespace"
           }
         },
         {
@@ -95,7 +104,10 @@
           },
           "labels": {
             "custom_foobar": "baz",
-            "log.file.name": "test.log"
+            "log.file.name": "test.log",
+            "service_instance_id": "my_service_instance_id",
+            "service_name": "my_service_name",
+            "service_namespace": "my_service_namespace"
           }
         },
         {
@@ -119,7 +131,10 @@
           },
           "labels": {
             "custom_foobar": "baz",
-            "log.file.name": "test.log"
+            "log.file.name": "test.log",
+            "service_instance_id": "my_service_instance_id",
+            "service_name": "my_service_name",
+            "service_namespace": "my_service_namespace"
           }
         },
         {
@@ -143,7 +158,10 @@
           },
           "labels": {
             "custom_foobar": "baz",
-            "log.file.name": "test.log"
+            "log.file.name": "test.log",
+            "service_instance_id": "my_service_instance_id",
+            "service_name": "my_service_name",
+            "service_namespace": "my_service_namespace"
           }
         },
         {
@@ -167,7 +185,10 @@
           },
           "labels": {
             "custom_foobar": "baz",
-            "log.file.name": "test.log"
+            "log.file.name": "test.log",
+            "service_instance_id": "my_service_instance_id",
+            "service_name": "my_service_name",
+            "service_namespace": "my_service_namespace"
           }
         },
         {
@@ -191,7 +212,10 @@
           },
           "labels": {
             "custom_foobar": "baz",
-            "log.file.name": "test.log"
+            "log.file.name": "test.log",
+            "service_instance_id": "my_service_instance_id",
+            "service_name": "my_service_name",
+            "service_namespace": "my_service_namespace"
           }
         },
         {
@@ -215,7 +239,10 @@
           },
           "labels": {
             "custom_foobar": "baz",
-            "log.file.name": "test.log"
+            "log.file.name": "test.log",
+            "service_instance_id": "my_service_instance_id",
+            "service_name": "my_service_name",
+            "service_namespace": "my_service_namespace"
           }
         },
         {
@@ -239,7 +266,10 @@
           },
           "labels": {
             "custom_foobar": "baz",
-            "log.file.name": "test.log"
+            "log.file.name": "test.log",
+            "service_instance_id": "my_service_instance_id",
+            "service_name": "my_service_name",
+            "service_namespace": "my_service_namespace"
           }
         },
         {
@@ -263,7 +293,10 @@
           },
           "labels": {
             "custom_foobar": "baz",
-            "log.file.name": "test.log"
+            "log.file.name": "test.log",
+            "service_instance_id": "my_service_instance_id",
+            "service_name": "my_service_name",
+            "service_namespace": "my_service_namespace"
           }
         },
         {
@@ -287,7 +320,10 @@
           },
           "labels": {
             "custom_foobar": "baz",
-            "log.file.name": "test.log"
+            "log.file.name": "test.log",
+            "service_instance_id": "my_service_instance_id",
+            "service_name": "my_service_name",
+            "service_namespace": "my_service_namespace"
           }
         },
         {
@@ -311,7 +347,10 @@
           },
           "labels": {
             "custom_foobar": "baz",
-            "log.file.name": "test.log"
+            "log.file.name": "test.log",
+            "service_instance_id": "my_service_instance_id",
+            "service_name": "my_service_name",
+            "service_namespace": "my_service_namespace"
           }
         },
         {
@@ -335,7 +374,10 @@
           },
           "labels": {
             "custom_foobar": "baz",
-            "log.file.name": "test.log"
+            "log.file.name": "test.log",
+            "service_instance_id": "my_service_instance_id",
+            "service_name": "my_service_name",
+            "service_namespace": "my_service_namespace"
           }
         },
         {
@@ -359,7 +401,10 @@
           },
           "labels": {
             "custom_foobar": "baz",
-            "log.file.name": "test.log"
+            "log.file.name": "test.log",
+            "service_instance_id": "my_service_instance_id",
+            "service_name": "my_service_name",
+            "service_namespace": "my_service_namespace"
           }
         },
         {
@@ -383,7 +428,10 @@
           },
           "labels": {
             "custom_foobar": "baz",
-            "log.file.name": "test.log"
+            "log.file.name": "test.log",
+            "service_instance_id": "my_service_instance_id",
+            "service_name": "my_service_name",
+            "service_namespace": "my_service_namespace"
           }
         },
         {
@@ -399,7 +447,10 @@
           "timestamp": "1970-01-01T00:00:00Z",
           "labels": {
             "custom_foobar": "baz",
-            "log.file.name": "test.log"
+            "log.file.name": "test.log",
+            "service_instance_id": "my_service_instance_id",
+            "service_name": "my_service_name",
+            "service_namespace": "my_service_namespace"
           }
         }
       ],