Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased]

### Added

- Add service detection with `WithService` in `go.opentelemetry.io/otel/sdk/resource`. (#7642)

<!-- Released section -->
<!-- Don't change this section unless doing release -->

Expand Down
8 changes: 8 additions & 0 deletions sdk/resource/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,11 @@ func WithContainer() Option {
func WithContainerID() Option {
return WithDetectors(cgroupContainerIDDetector{})
}

// WithService adds all the Service attributes to the configured Resource.
func WithService() Option {
return WithDetectors(
defaultServiceInstanceIDDetector{},
defaultServiceNameDetector{},
)
}
42 changes: 31 additions & 11 deletions sdk/resource/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ var (
kv42 = attribute.String("k4", "")
)

const v121 = "https://opentelemetry.io/schemas/1.21.0"

func TestNewWithAttributes(t *testing.T) {
cases := []struct {
name string
Expand Down Expand Up @@ -155,21 +157,21 @@ func TestMerge(t *testing.T) {
},
{
name: "Merge with first resource with schema",
a: resource.NewWithAttributes("https://opentelemetry.io/schemas/1.21.0", kv41),
a: resource.NewWithAttributes(v121, kv41),
b: resource.NewSchemaless(kv42),
want: []attribute.KeyValue{kv42},
schemaURL: "https://opentelemetry.io/schemas/1.21.0",
schemaURL: v121,
},
{
name: "Merge with second resource with schema",
a: resource.NewSchemaless(kv41),
b: resource.NewWithAttributes("https://opentelemetry.io/schemas/1.21.0", kv42),
b: resource.NewWithAttributes(v121, kv42),
want: []attribute.KeyValue{kv42},
schemaURL: "https://opentelemetry.io/schemas/1.21.0",
schemaURL: v121,
},
{
name: "Merge with different schemas",
a: resource.NewWithAttributes("https://opentelemetry.io/schemas/1.21.0", kv41),
a: resource.NewWithAttributes(v121, kv41),
b: resource.NewWithAttributes("https://opentelemetry.io/schemas/1.20.0", kv42),
want: []attribute.KeyValue{kv42},
isErr: true,
Expand Down Expand Up @@ -209,7 +211,7 @@ func TestMergeIdempotent(t *testing.T) {

func TestMergeIdempotentWithSchema(t *testing.T) {
r := resource.NewWithAttributes(
"https://opentelemetry.io/schemas/1.21.0",
v121,
attribute.String("k1", "v1"),
attribute.String("k2", "v2"),
)
Expand Down Expand Up @@ -420,12 +422,12 @@ func TestNew(t *testing.T) {
envars: "",
options: []resource.Option{
resource.WithAttributes(attribute.String("A", "B")),
resource.WithSchemaURL("https://opentelemetry.io/schemas/1.21.0"),
resource.WithSchemaURL(v121),
},
resourceValues: map[string]string{
"A": "B",
},
schemaURL: "https://opentelemetry.io/schemas/1.21.0",
schemaURL: v121,
},
{
name: "With conflicting schema urls",
Expand All @@ -438,7 +440,7 @@ func TestNew(t *testing.T) {
os.Hostname,
),
),
resource.WithSchemaURL("https://opentelemetry.io/schemas/1.21.0"),
resource.WithSchemaURL(v121),
},
resourceValues: map[string]string{
string(semconv.HostNameKey): func() (hostname string) {
Expand All @@ -465,7 +467,7 @@ func TestNew(t *testing.T) {
func() (string, error) { return "", errors.New("fail") },
),
),
resource.WithSchemaURL("https://opentelemetry.io/schemas/1.21.0"),
resource.WithSchemaURL(v121),
},
resourceValues: map[string]string{
string(semconv.HostNameKey): func() (hostname string) {
Expand Down Expand Up @@ -820,6 +822,24 @@ func TestWithContainer(t *testing.T) {
}, toMap(res))
}

func TestWithService(t *testing.T) {
res, err := resource.New(t.Context(),
resource.WithService(),
)

assert.NoError(t, err)

resMap := toMap(res)

// Verify service.name exists
_, ok := resMap[string(semconv.ServiceNameKey)]
require.True(t, ok, "service.name should be present")

// Verify service.instance.id exists
_, ok = resMap[string(semconv.ServiceInstanceIDKey)]
require.True(t, ok, "service.instance.id should be present")
}

func TestResourceConcurrentSafe(t *testing.T) {
// Creating Resources should also be free of any data races,
// because Resources are immutable.
Expand All @@ -839,7 +859,7 @@ type fakeDetector struct{}
func (fakeDetector) Detect(context.Context) (*resource.Resource, error) {
// A bit pedantic, but resource.NewWithAttributes returns an empty Resource when
// no attributes specified. We want to make sure that this is concurrent-safe.
return resource.NewWithAttributes("https://opentelemetry.io/schemas/1.21.0"), nil
return resource.NewWithAttributes(v121), nil
}

var _ resource.Detector = &fakeDetector{}
Loading