diff --git a/CHANGELOG.md b/CHANGELOG.md index dc2d4bba776..57da5af2985 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Removed the deprecated package `go.opentelemetry.io/otel/exporters/trace/jaeger`. (#2020) - Removed the deprecated package `go.opentelemetry.io/otel/exporters/trace/zipkin`. (#2020) +- Removed the `"go.opentelemetry.io/otel/sdk/resource".WithBuiltinDetectors` function. + The explicit `With*` options for every built-in detector should be used instead. (#2026 #2097) - Removed metrics test package `go.opentelemetry.io/otel/sdk/export/metric/metrictest`. (#2105) ### Fixed diff --git a/sdk/resource/builtin.go b/sdk/resource/builtin.go index b3365ad3c4d..24d5d43bf44 100644 --- a/sdk/resource/builtin.go +++ b/sdk/resource/builtin.go @@ -78,7 +78,8 @@ func StringDetector(schemaURL string, k attribute.Key, f func() (string, error)) return stringDetector{schemaURL: schemaURL, K: k, F: f} } -// Detect implements Detector. +// Detect returns a *Resource that describes the string as a value +// corresponding to attribute.Key as well as the specific schemaURL. func (sd stringDetector) Detect(ctx context.Context) (*Resource, error) { value, err := sd.F() if err != nil { diff --git a/sdk/resource/config.go b/sdk/resource/config.go index 2606de7bb79..5fa45859b53 100644 --- a/sdk/resource/config.go +++ b/sdk/resource/config.go @@ -60,13 +60,6 @@ func (o detectorsOption) apply(cfg *config) { cfg.detectors = append(cfg.detectors, o.detectors...) } -// WithBuiltinDetectors adds the built detectors to the configured resource. -func WithBuiltinDetectors() Option { - return WithDetectors(telemetrySDK{}, - host{}, - fromEnv{}) -} - // WithFromEnv adds attributes from environment variables to the configured resource. func WithFromEnv() Option { return WithDetectors(fromEnv{}) @@ -92,3 +85,87 @@ type schemaURLOption string func (o schemaURLOption) apply(cfg *config) { cfg.schemaURL = string(o) } + +// WithOS adds all the OS attributes to the configured Resource. +// See individual WithOS* functions to configure specific attributes. +func WithOS() Option { + return WithDetectors( + osTypeDetector{}, + osDescriptionDetector{}, + ) +} + +// WithOSType adds an attribute with the operating system type to the configured Resource. +func WithOSType() Option { + return WithDetectors(osTypeDetector{}) +} + +// WithOSDescription adds an attribute with the operating system description to the +// configured Resource. The formatted string is equivalent to the output of the +// `uname -snrvm` command. +func WithOSDescription() Option { + return WithDetectors(osDescriptionDetector{}) +} + +// WithProcess adds all the Process attributes to the configured Resource. +// See individual WithProcess* functions to configure specific attributes. +func WithProcess() Option { + return WithDetectors( + processPIDDetector{}, + processExecutableNameDetector{}, + processExecutablePathDetector{}, + processCommandArgsDetector{}, + processOwnerDetector{}, + processRuntimeNameDetector{}, + processRuntimeVersionDetector{}, + processRuntimeDescriptionDetector{}, + ) +} + +// WithProcessPID adds an attribute with the process identifier (PID) to the +// configured Resource. +func WithProcessPID() Option { + return WithDetectors(processPIDDetector{}) +} + +// WithProcessExecutableName adds an attribute with the name of the process +// executable to the configured Resource. +func WithProcessExecutableName() Option { + return WithDetectors(processExecutableNameDetector{}) +} + +// WithProcessExecutablePath adds an attribute with the full path to the process +// executable to the configured Resource. +func WithProcessExecutablePath() Option { + return WithDetectors(processExecutablePathDetector{}) +} + +// WithProcessCommandArgs adds an attribute with all the command arguments (including +// the command/executable itself) as received by the process the configured Resource. +func WithProcessCommandArgs() Option { + return WithDetectors(processCommandArgsDetector{}) +} + +// WithProcessOwner adds an attribute with the username of the user that owns the process +// to the configured Resource. +func WithProcessOwner() Option { + return WithDetectors(processOwnerDetector{}) +} + +// WithProcessRuntimeName adds an attribute with the name of the runtime of this +// process to the configured Resource. +func WithProcessRuntimeName() Option { + return WithDetectors(processRuntimeNameDetector{}) +} + +// WithProcessRuntimeVersion adds an attribute with the version of the runtime of +// this process to the configured Resource. +func WithProcessRuntimeVersion() Option { + return WithDetectors(processRuntimeVersionDetector{}) +} + +// WithProcessRuntimeDescription adds an attribute with an additional description +// about the runtime of the process to the configured Resource. +func WithProcessRuntimeDescription() Option { + return WithDetectors(processRuntimeDescriptionDetector{}) +} diff --git a/sdk/resource/os.go b/sdk/resource/os.go index 2655837232e..ff0072d852f 100644 --- a/sdk/resource/os.go +++ b/sdk/resource/os.go @@ -67,27 +67,6 @@ func (osDescriptionDetector) Detect(ctx context.Context) (*Resource, error) { ), nil } -// WithOSType adds an attribute with the operating system type to the configured Resource. -func WithOSType() Option { - return WithDetectors(osTypeDetector{}) -} - -// WithOSDescription adds an attribute with the operating system description to the -// configured Resource. The formatted string is equivalent to the output of the -// `uname -snrvm` command. -func WithOSDescription() Option { - return WithDetectors(osDescriptionDetector{}) -} - -// WithOS adds all the OS attributes to the configured Resource. -// See individual WithOS* functions to configure specific attributes. -func WithOS() Option { - return WithDetectors( - osTypeDetector{}, - osDescriptionDetector{}, - ) -} - // mapRuntimeOSToSemconvOSType translates the OS name as provided by the Go runtime // into an OS type attribute with the corresponding value defined by the semantic // conventions. In case the provided OS name isn't mapped, it's transformed to lowercase diff --git a/sdk/resource/os_test.go b/sdk/resource/os_test.go index b212a3bc3fe..3672c2eed79 100644 --- a/sdk/resource/os_test.go +++ b/sdk/resource/os_test.go @@ -15,7 +15,6 @@ package resource_test import ( - "context" "testing" "github.com/stretchr/testify/require" @@ -38,55 +37,6 @@ func mockRuntimeProviders() { ) } -func TestWithOSType(t *testing.T) { - mockRuntimeProviders() - t.Cleanup(restoreAttributesProviders) - - ctx := context.Background() - - res, err := resource.New(ctx, - resource.WithOSType(), - ) - - require.NoError(t, err) - require.EqualValues(t, map[string]string{ - "os.type": "linux", - }, toMap(res)) -} - -func TestWithOSDescription(t *testing.T) { - mockRuntimeProviders() - t.Cleanup(restoreAttributesProviders) - - ctx := context.Background() - - res, err := resource.New(ctx, - resource.WithOSDescription(), - ) - - require.NoError(t, err) - require.EqualValues(t, map[string]string{ - "os.description": "Test", - }, toMap(res)) -} - -func TestWithOS(t *testing.T) { - mockRuntimeProviders() - t.Cleanup(restoreAttributesProviders) - - ctx := context.Background() - - res, err := resource.New(ctx, - resource.WithOS(), - ) - - require.NoError(t, err) - require.EqualValues(t, map[string]string{ - "os.type": "linux", - "os.description": "Test", - }, toMap(res)) -} - func TestMapRuntimeOSToSemconvOSType(t *testing.T) { tt := []struct { Name string diff --git a/sdk/resource/process.go b/sdk/resource/process.go index 171406ec581..c75c5ef0f15 100644 --- a/sdk/resource/process.go +++ b/sdk/resource/process.go @@ -173,66 +173,3 @@ func (processRuntimeDescriptionDetector) Detect(ctx context.Context) (*Resource, semconv.ProcessRuntimeDescriptionKey.String(runtimeDescription), ), nil } - -// WithProcessPID adds an attribute with the process identifier (PID) to the -// configured Resource. -func WithProcessPID() Option { - return WithDetectors(processPIDDetector{}) -} - -// WithProcessExecutableName adds an attribute with the name of the process -// executable to the configured Resource. -func WithProcessExecutableName() Option { - return WithDetectors(processExecutableNameDetector{}) -} - -// WithProcessExecutablePath adds an attribute with the full path to the process -// executable to the configured Resource. -func WithProcessExecutablePath() Option { - return WithDetectors(processExecutablePathDetector{}) -} - -// WithProcessCommandArgs adds an attribute with all the command arguments (including -// the command/executable itself) as received by the process the configured Resource. -func WithProcessCommandArgs() Option { - return WithDetectors(processCommandArgsDetector{}) -} - -// WithProcessOwner adds an attribute with the username of the user that owns the process -// to the configured Resource. -func WithProcessOwner() Option { - return WithDetectors(processOwnerDetector{}) -} - -// WithProcessRuntimeName adds an attribute with the name of the runtime of this -// process to the configured Resource. -func WithProcessRuntimeName() Option { - return WithDetectors(processRuntimeNameDetector{}) -} - -// WithProcessRuntimeVersion adds an attribute with the version of the runtime of -// this process to the configured Resource. -func WithProcessRuntimeVersion() Option { - return WithDetectors(processRuntimeVersionDetector{}) -} - -// WithProcessRuntimeDescription adds an attribute with an additional description -// about the runtime of the process to the configured Resource. -func WithProcessRuntimeDescription() Option { - return WithDetectors(processRuntimeDescriptionDetector{}) -} - -// WithProcess adds all the Process attributes to the configured Resource. -// See individual WithProcess* functions to configure specific attributes. -func WithProcess() Option { - return WithDetectors( - processPIDDetector{}, - processExecutableNameDetector{}, - processExecutablePathDetector{}, - processCommandArgsDetector{}, - processOwnerDetector{}, - processRuntimeNameDetector{}, - processRuntimeVersionDetector{}, - processRuntimeDescriptionDetector{}, - ) -} diff --git a/sdk/resource/process_test.go b/sdk/resource/process_test.go index d6a6dfd503d..d60c48411c4 100644 --- a/sdk/resource/process_test.go +++ b/sdk/resource/process_test.go @@ -104,22 +104,6 @@ func restoreAttributesProviders() { resource.SetDefaultOSDescriptionProvider() } -func TestWithProcessFuncs(t *testing.T) { - mockProcessAttributesProviders() - - t.Run("WithPID", testWithProcessPID) - t.Run("WithExecutableName", testWithProcessExecutableName) - t.Run("WithExecutablePath", testWithProcessExecutablePath) - t.Run("WithCommandArgs", testWithProcessCommandArgs) - t.Run("WithOwner", testWithProcessOwner) - t.Run("WithRuntimeName", testWithProcessRuntimeName) - t.Run("WithRuntimeVersion", testWithProcessRuntimeVersion) - t.Run("WithRuntimeDescription", testWithProcessRuntimeDescription) - t.Run("WithProcess", testWithProcess) - - restoreAttributesProviders() -} - func TestWithProcessFuncsErrors(t *testing.T) { mockProcessAttributesProvidersWithErrors() @@ -145,130 +129,6 @@ func TestRuntimeArch(t *testing.T) { require.EqualValues(t, runtime.GOARCH, resource.RuntimeArch()) } -func testWithProcessPID(t *testing.T) { - ctx := context.Background() - - res, err := resource.New(ctx, - resource.WithProcessPID(), - ) - - require.NoError(t, err) - require.EqualValues(t, map[string]string{ - "process.pid": fmt.Sprint(fakePID), - }, toMap(res)) -} - -func testWithProcessExecutableName(t *testing.T) { - ctx := context.Background() - - res, err := resource.New(ctx, - resource.WithProcessExecutableName(), - ) - - require.NoError(t, err) - require.EqualValues(t, map[string]string{ - "process.executable.name": fakeExecutableName, - }, toMap(res)) -} - -func testWithProcessExecutablePath(t *testing.T) { - ctx := context.Background() - - res, err := resource.New(ctx, - resource.WithProcessExecutablePath(), - ) - - require.NoError(t, err) - require.EqualValues(t, map[string]string{ - "process.executable.path": fakeExecutablePath, - }, toMap(res)) -} - -func testWithProcessCommandArgs(t *testing.T) { - ctx := context.Background() - - res, err := resource.New(ctx, - resource.WithProcessCommandArgs(), - ) - - require.NoError(t, err) - require.EqualValues(t, map[string]string{ - "process.command_args": fmt.Sprint(fakeCommandArgs), - }, toMap(res)) -} - -func testWithProcessOwner(t *testing.T) { - ctx := context.Background() - - res, err := resource.New(ctx, - resource.WithProcessOwner(), - ) - - require.NoError(t, err) - require.EqualValues(t, map[string]string{ - "process.owner": fakeOwner, - }, toMap(res)) -} - -func testWithProcessRuntimeName(t *testing.T) { - ctx := context.Background() - - res, err := resource.New(ctx, - resource.WithProcessRuntimeName(), - ) - - require.NoError(t, err) - require.EqualValues(t, map[string]string{ - "process.runtime.name": fakeRuntimeName, - }, toMap(res)) -} - -func testWithProcessRuntimeVersion(t *testing.T) { - ctx := context.Background() - - res, err := resource.New(ctx, - resource.WithProcessRuntimeVersion(), - ) - - require.NoError(t, err) - require.EqualValues(t, map[string]string{ - "process.runtime.version": fakeRuntimeVersion, - }, toMap(res)) -} - -func testWithProcessRuntimeDescription(t *testing.T) { - ctx := context.Background() - - res, err := resource.New(ctx, - resource.WithProcessRuntimeDescription(), - ) - - require.NoError(t, err) - require.EqualValues(t, map[string]string{ - "process.runtime.description": fakeRuntimeDescription, - }, toMap(res)) -} - -func testWithProcess(t *testing.T) { - ctx := context.Background() - - res, err := resource.New(ctx, - resource.WithProcess(), - ) - - require.NoError(t, err) - require.EqualValues(t, map[string]string{ - "process.pid": fmt.Sprint(fakePID), - "process.executable.name": fakeExecutableName, - "process.executable.path": fakeExecutablePath, - "process.command_args": fmt.Sprint(fakeCommandArgs), - "process.owner": fakeOwner, - "process.runtime.name": fakeRuntimeName, - "process.runtime.version": fakeRuntimeVersion, - "process.runtime.description": fakeRuntimeDescription, - }, toMap(res)) -} - func testWithProcessExecutablePathError(t *testing.T) { ctx := context.Background() diff --git a/sdk/resource/resource.go b/sdk/resource/resource.go index 94b11c6c0e7..0d442f91716 100644 --- a/sdk/resource/resource.go +++ b/sdk/resource/resource.go @@ -43,7 +43,14 @@ var ( otel.Handle(err) } return r - }(Detect(context.Background(), defaultServiceNameDetector{}, fromEnv{}, telemetrySDK{})) + }( + Detect( + context.Background(), + defaultServiceNameDetector{}, + fromEnv{}, + telemetrySDK{}, + ), + ) ) var ( diff --git a/sdk/resource/resource_test.go b/sdk/resource/resource_test.go index c9d8ac6d7ed..20edebd0b48 100644 --- a/sdk/resource/resource_test.go +++ b/sdk/resource/resource_test.go @@ -373,22 +373,6 @@ func TestNew(t *testing.T) { "A": "B", }, }, - { - name: "Builtins", - envars: "key=value,other=attr", - options: []resource.Option{ - resource.WithBuiltinDetectors(), - }, - resourceValues: map[string]string{ - "host.name": hostname(), - "telemetry.sdk.name": "opentelemetry", - "telemetry.sdk.language": "go", - "telemetry.sdk.version": otel.Version(), - "key": "value", - "other": "attr", - }, - schemaURL: semconv.SchemaURL, - }, { name: "With schema url", envars: "", @@ -457,61 +441,186 @@ func TestNew(t *testing.T) { } } -func TestNewWithBuiltinDetectors(t *testing.T) { - tc := []struct { - name string - envars string - detectors []resource.Detector - options []resource.Option +func TestWithOSType(t *testing.T) { + mockRuntimeProviders() + t.Cleanup(restoreAttributesProviders) - resourceValues map[string]string - }{ - { - name: "No Options returns builtin", - envars: "key=value,other=attr", - options: nil, - resourceValues: map[string]string{ - "host.name": hostname(), - "telemetry.sdk.name": "opentelemetry", - "telemetry.sdk.language": "go", - "telemetry.sdk.version": otel.Version(), - "key": "value", - "other": "attr", - }, - }, - { - name: "WithAttributes", - envars: "key=value,other=attr", - options: []resource.Option{ - resource.WithAttributes(attribute.String("A", "B")), - }, - resourceValues: map[string]string{ - "host.name": hostname(), - "telemetry.sdk.name": "opentelemetry", - "telemetry.sdk.language": "go", - "telemetry.sdk.version": otel.Version(), - "key": "value", - "other": "attr", - "A": "B", - }, - }, - } - for _, tt := range tc { - t.Run(tt.name, func(t *testing.T) { - store, err := ottest.SetEnvVariables(map[string]string{ - envVar: tt.envars, - }) - require.NoError(t, err) - defer func() { require.NoError(t, store.Restore()) }() + ctx := context.Background() - ctx := context.Background() - options := append([]resource.Option{resource.WithBuiltinDetectors()}, tt.options...) - res, err := resource.New(ctx, options...) + res, err := resource.New(ctx, + resource.WithOSType(), + ) - require.NoError(t, err) - require.EqualValues(t, tt.resourceValues, toMap(res)) - }) - } + require.NoError(t, err) + require.EqualValues(t, map[string]string{ + "os.type": "linux", + }, toMap(res)) +} + +func TestWithOSDescription(t *testing.T) { + mockRuntimeProviders() + t.Cleanup(restoreAttributesProviders) + + ctx := context.Background() + + res, err := resource.New(ctx, + resource.WithOSDescription(), + ) + + require.NoError(t, err) + require.EqualValues(t, map[string]string{ + "os.description": "Test", + }, toMap(res)) +} + +func TestWithOS(t *testing.T) { + mockRuntimeProviders() + t.Cleanup(restoreAttributesProviders) + + ctx := context.Background() + + res, err := resource.New(ctx, + resource.WithOS(), + ) + + require.NoError(t, err) + require.EqualValues(t, map[string]string{ + "os.type": "linux", + "os.description": "Test", + }, toMap(res)) +} + +func TestWithProcessPID(t *testing.T) { + mockProcessAttributesProvidersWithErrors() + ctx := context.Background() + + res, err := resource.New(ctx, + resource.WithProcessPID(), + ) + + require.NoError(t, err) + require.EqualValues(t, map[string]string{ + "process.pid": fmt.Sprint(fakePID), + }, toMap(res)) +} + +func TestWithProcessExecutableName(t *testing.T) { + mockProcessAttributesProvidersWithErrors() + ctx := context.Background() + + res, err := resource.New(ctx, + resource.WithProcessExecutableName(), + ) + + require.NoError(t, err) + require.EqualValues(t, map[string]string{ + "process.executable.name": fakeExecutableName, + }, toMap(res)) +} + +func TestWithProcessExecutablePath(t *testing.T) { + mockProcessAttributesProviders() + ctx := context.Background() + + res, err := resource.New(ctx, + resource.WithProcessExecutablePath(), + ) + + require.NoError(t, err) + require.EqualValues(t, map[string]string{ + "process.executable.path": fakeExecutablePath, + }, toMap(res)) +} + +func TestWithProcessCommandArgs(t *testing.T) { + mockProcessAttributesProvidersWithErrors() + ctx := context.Background() + + res, err := resource.New(ctx, + resource.WithProcessCommandArgs(), + ) + + require.NoError(t, err) + require.EqualValues(t, map[string]string{ + "process.command_args": fmt.Sprint(fakeCommandArgs), + }, toMap(res)) +} + +func TestWithProcessOwner(t *testing.T) { + mockProcessAttributesProviders() + ctx := context.Background() + + res, err := resource.New(ctx, + resource.WithProcessOwner(), + ) + + require.NoError(t, err) + require.EqualValues(t, map[string]string{ + "process.owner": fakeOwner, + }, toMap(res)) +} + +func TestWithProcessRuntimeName(t *testing.T) { + mockProcessAttributesProvidersWithErrors() + ctx := context.Background() + + res, err := resource.New(ctx, + resource.WithProcessRuntimeName(), + ) + + require.NoError(t, err) + require.EqualValues(t, map[string]string{ + "process.runtime.name": fakeRuntimeName, + }, toMap(res)) +} + +func TestWithProcessRuntimeVersion(t *testing.T) { + mockProcessAttributesProvidersWithErrors() + ctx := context.Background() + + res, err := resource.New(ctx, + resource.WithProcessRuntimeVersion(), + ) + + require.NoError(t, err) + require.EqualValues(t, map[string]string{ + "process.runtime.version": fakeRuntimeVersion, + }, toMap(res)) +} + +func TestWithProcessRuntimeDescription(t *testing.T) { + mockProcessAttributesProvidersWithErrors() + ctx := context.Background() + + res, err := resource.New(ctx, + resource.WithProcessRuntimeDescription(), + ) + + require.NoError(t, err) + require.EqualValues(t, map[string]string{ + "process.runtime.description": fakeRuntimeDescription, + }, toMap(res)) +} + +func TestWithProcess(t *testing.T) { + mockProcessAttributesProviders() + ctx := context.Background() + + res, err := resource.New(ctx, + resource.WithProcess(), + ) + + require.NoError(t, err) + require.EqualValues(t, map[string]string{ + "process.pid": fmt.Sprint(fakePID), + "process.executable.name": fakeExecutableName, + "process.executable.path": fakeExecutablePath, + "process.command_args": fmt.Sprint(fakeCommandArgs), + "process.owner": fakeOwner, + "process.runtime.name": fakeRuntimeName, + "process.runtime.version": fakeRuntimeVersion, + "process.runtime.description": fakeRuntimeDescription, + }, toMap(res)) } func toMap(res *resource.Resource) map[string]string {