-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[service] fix bug with inconsistent resources (#11578)
The tracer and logger provider were instantiating different resources objects that didn't have a `service.instance.id` attribute. This change fixes that by instantiating the SDK in the service and passing it to the factory via the telemetry.Settings struct. This also removes the duplicate code to instantiate the SDK multiple times, which will be useful when we move to instantiating MeterProvider via the config SDK. This bug is blocking the change to bump up the dependency on the config package. There are a few alternatives that were considered: 1. could set the resource's service.instance.id on the config object instead. this seemed messy as it would be editing the config struct and the instantiation of the SDK would remain duplicated. 2. update the factory to pass in a resource object option, i didn't want to update the NewFactory func. 3. update the CreateLogger, CreateTracerProvider to receive either a resource or sdk parameter, both of those seemed incorrect as well. --------- Signed-off-by: Alex Boten <[email protected]>
- Loading branch information
Showing
11 changed files
with
167 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: bug_fix | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) | ||
component: service | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: ensure traces and logs emitted by the otel go SDK use the same resource information | ||
|
||
# One or more tracking issues or pull requests related to the change | ||
issues: [11578] | ||
|
||
# (Optional) One or more lines of additional information to render under the primary note. | ||
# These lines will be padded with 2 spaces and then inserted directly into the document. | ||
# Use pipe (|) for multiline entries. | ||
subtext: | ||
|
||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package service // import "go.opentelemetry.io/collector/service" | ||
|
||
import ( | ||
sdkresource "go.opentelemetry.io/otel/sdk/resource" | ||
|
||
"go.opentelemetry.io/collector/service/telemetry" | ||
) | ||
|
||
func attributes(res *sdkresource.Resource, cfg telemetry.Config) map[string]interface{} { | ||
attrs := map[string]interface{}{} | ||
for _, r := range res.Attributes() { | ||
attrs[string(r.Key)] = r.Value.AsString() | ||
} | ||
|
||
for k, v := range cfg.Resource { | ||
if v != nil { | ||
attrs[k] = *v | ||
} else { | ||
// the new value is nil, delete the existing key | ||
delete(attrs, k) | ||
} | ||
} | ||
return attrs | ||
} |
37 changes: 23 additions & 14 deletions
37
service/telemetry/attributes_test.go → service/attributes_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,65 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package telemetry // import "go.opentelemetry.io/collector/service/telemetry" | ||
package service // import "go.opentelemetry.io/collector/service" | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/service/internal/resource" | ||
"go.opentelemetry.io/collector/service/telemetry" | ||
) | ||
|
||
func TestAttributes(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
cfg Config | ||
cfg telemetry.Config | ||
buildInfo component.BuildInfo | ||
wantAttributes map[string]interface{} | ||
}{ | ||
{ | ||
name: "no build info and no resource config", | ||
cfg: Config{}, | ||
wantAttributes: map[string]interface{}{"service.name": "", "service.version": ""}, | ||
cfg: telemetry.Config{}, | ||
wantAttributes: map[string]interface{}{"service.name": "", "service.version": "", "service.instance.id": ""}, | ||
}, | ||
{ | ||
name: "build info and no resource config", | ||
cfg: Config{}, | ||
cfg: telemetry.Config{}, | ||
buildInfo: component.BuildInfo{Command: "otelcoltest", Version: "0.0.0-test"}, | ||
wantAttributes: map[string]interface{}{"service.name": "otelcoltest", "service.version": "0.0.0-test"}, | ||
wantAttributes: map[string]interface{}{"service.name": "otelcoltest", "service.version": "0.0.0-test", "service.instance.id": ""}, | ||
}, | ||
{ | ||
name: "no build info and resource config", | ||
cfg: Config{Resource: map[string]*string{"service.name": ptr("resource.name"), "service.version": ptr("resource.version"), "test": ptr("test")}}, | ||
wantAttributes: map[string]interface{}{"service.name": "resource.name", "service.version": "resource.version", "test": "test"}, | ||
cfg: telemetry.Config{Resource: map[string]*string{"service.name": newPtr("resource.name"), "service.version": newPtr("resource.version"), "test": newPtr("test")}}, | ||
wantAttributes: map[string]interface{}{"service.name": "resource.name", "service.version": "resource.version", "test": "test", "service.instance.id": ""}, | ||
}, | ||
{ | ||
name: "build info and resource config", | ||
buildInfo: component.BuildInfo{Command: "otelcoltest", Version: "0.0.0-test"}, | ||
cfg: Config{Resource: map[string]*string{"service.name": ptr("resource.name"), "service.version": ptr("resource.version"), "test": ptr("test")}}, | ||
wantAttributes: map[string]interface{}{"service.name": "resource.name", "service.version": "resource.version", "test": "test"}, | ||
cfg: telemetry.Config{Resource: map[string]*string{"service.name": newPtr("resource.name"), "service.version": newPtr("resource.version"), "test": newPtr("test")}}, | ||
wantAttributes: map[string]interface{}{"service.name": "resource.name", "service.version": "resource.version", "test": "test", "service.instance.id": ""}, | ||
}, | ||
{ | ||
name: "deleting a nil value", | ||
buildInfo: component.BuildInfo{Command: "otelcoltest", Version: "0.0.0-test"}, | ||
cfg: Config{Resource: map[string]*string{"service.name": nil, "service.version": ptr("resource.version"), "test": ptr("test")}}, | ||
wantAttributes: map[string]interface{}{"service.version": "resource.version", "test": "test"}, | ||
cfg: telemetry.Config{Resource: map[string]*string{"service.name": nil, "service.version": newPtr("resource.version"), "test": newPtr("test")}}, | ||
wantAttributes: map[string]interface{}{"service.version": "resource.version", "test": "test", "service.instance.id": ""}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
attrs := attributes(Settings{BuildInfo: tt.buildInfo}, tt.cfg) | ||
require.Equal(t, tt.wantAttributes, attrs) | ||
attrs := attributes(resource.New(tt.buildInfo, tt.cfg.Resource), tt.cfg) | ||
require.Len(t, attrs, len(tt.wantAttributes)) | ||
for k, v := range tt.wantAttributes { | ||
if k == "service.instance.id" { | ||
require.NotNil(t, attrs[k]) | ||
} else { | ||
require.Equal(t, v, attrs[k]) | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.