1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-28 21:09:17 +02:00
opentelemetry-go/sdk/resource/resource_experimental_test.go
Johannes Tax 4e94f405ab
Populate service.instance.id with a default value when OTEL_GO_X_RESOURCE is set (#5520)
As discussed in a previous SIG meeting, this PR adds support for setting
a default value for
[`service.instance.id`](https://github.com/open-telemetry/semantic-conventions/tree/main/docs/resource#service-experimental)
according to semantic conventions:

> Implementations, such as SDKs, are recommended to generate a random
Version 1 or Version 4 [RFC 4122](https://www.ietf.org/rfc/rfc4122.txt)
UUID, but are free to use an inherent unique ID as the source of this
value if stability is desirable. In that case, the ID SHOULD be used as
source of a UUID Version 5 and SHOULD use the following UUID as the
namespace: `4d63009a-8d0f-11ee-aad7-4c796ed8e320`.

This PR follows the recommendation and populates `service.instance.id`
with a random Version 4 UUID. The functionality is guarded by the
`OTEL_GO_X_RESOURCE` feature flag environment variable.

There are plans to declare `service.instance.id` stable and also make it
a required attribute (similar to `service.name`). Once this happens, the
functionality can be made available regardless of whether
`OTEL_GO_X_RESOURCE` is set.

Closes
https://github.com/open-telemetry/opentelemetry-go-contrib/issues/5423

---------

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
Co-authored-by: Tyler Yahn <codingalias@gmail.com>
2024-06-24 08:25:49 -07:00

36 lines
923 B
Go

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package resource
import (
"regexp"
"sync"
"testing"
"github.com/stretchr/testify/require"
semconv "go.opentelemetry.io/otel/semconv/v1.25.0"
)
func TestDefaultExperimental(t *testing.T) {
// Experimental attributes aren't present by default
res := Default()
require.False(t, res.Set().HasValue(semconv.ServiceInstanceIDKey))
// Reset cache and enable experimental resources
defaultResourceOnce = sync.Once{}
t.Setenv("OTEL_GO_X_RESOURCE", "true")
res = Default()
require.True(t, res.Set().HasValue(semconv.ServiceInstanceIDKey))
serviceInstanceID, ok := res.Set().Value(semconv.ServiceInstanceIDKey)
require.True(t, ok)
matched, err := regexp.MatchString("^[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}$", serviceInstanceID.AsString())
require.NoError(t, err)
require.True(t, matched)
}