Skip to content

Commit

Permalink
Include service attributes in logs exporter by default
Browse files Browse the repository at this point in the history
  • Loading branch information
damemi committed Jan 10, 2023
1 parent cb447ae commit 399b4d0
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 19 deletions.
7 changes: 7 additions & 0 deletions exporter/collector/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,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.
Expand All @@ -165,6 +169,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",
Expand Down
3 changes: 2 additions & 1 deletion exporter/collector/integrationtest/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ func TestLoadConfig(t *testing.T) {
ClientConfig: collector.ClientConfig{
GRPCPoolSize: 1,
},
DefaultLogName: "foo-log",
DefaultLogName: "foo-log",
ServiceResourceLabels: true,
},
},
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
},
{
Expand All @@ -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"
}
},
{
Expand All @@ -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"
}
},
{
Expand All @@ -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"
}
},
{
Expand All @@ -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"
}
},
{
Expand All @@ -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"
}
},
{
Expand All @@ -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"
}
},
{
Expand All @@ -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"
}
},
{
Expand All @@ -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"
}
},
{
Expand All @@ -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"
}
},
{
Expand All @@ -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"
}
},
{
Expand All @@ -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"
}
},
{
Expand All @@ -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"
}
},
{
Expand All @@ -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"
}
},
{
Expand All @@ -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"
}
},
{
Expand All @@ -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"
}
},
{
Expand All @@ -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"
}
}
],
Expand Down
2 changes: 1 addition & 1 deletion exporter/collector/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 399b4d0

Please sign in to comment.