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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- Add `IsRandom` and `WithRandom` on `TraceFlags`, and `IsRandom` on `SpanContext` in `go.opentelemetry.io/otel/trace` for [W3C Trace Context Level 2 Random Trace ID Flag](https://www.w3.org/TR/trace-context-2/#random-trace-id-flag) support. (#8012)
- Add service detection with `WithService` in `go.opentelemetry.io/otel/sdk/resource`. (#7642)
- Add `DefaultWithContext` and `EnvironmentWithContext` in `go.opentelemetry.io/otel/sdk/resource` to support plumbing `context.Context` through default and environment detectors. (#8051)
- Support attributes with empty value (`attribute.EMPTY`) in OTLP exporters (`go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc`, `go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc`, `go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc`, `go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp`, `go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp`, `go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp`). (#8038)
- Support attributes with empty value (`attribute.EMPTY`) in `go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest`. (#8038)

Expand Down
19 changes: 17 additions & 2 deletions sdk/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,15 @@ func Empty() *Resource {
// Default returns an instance of Resource with a default
// "service.name" and OpenTelemetrySDK attributes.
func Default() *Resource {
return DefaultWithContext(context.Background())
}

// DefaultWithContext returns an instance of Resource with a default
// "service.name" and OpenTelemetrySDK attributes.
//
// If the default resource has already been initialized, the provided ctx
// is ignored and the cached resource is returned.
func DefaultWithContext(ctx context.Context) *Resource {
defaultResourceOnce.Do(func() {
Comment thread
dmathieu marked this conversation as resolved.
var err error
defaultDetectors := []Detector{
Expand All @@ -243,7 +252,7 @@ func Default() *Resource {
defaultDetectors = append([]Detector{defaultServiceInstanceIDDetector{}}, defaultDetectors...)
}
defaultResource, err = Detect(
context.Background(),
ctx,
defaultDetectors...,
)
if err != nil {
Expand All @@ -260,8 +269,14 @@ func Default() *Resource {
// Environment returns an instance of Resource with attributes
// extracted from the OTEL_RESOURCE_ATTRIBUTES environment variable.
func Environment() *Resource {
return EnvironmentWithContext(context.Background())
}

// EnvironmentWithContext returns an instance of Resource with attributes
// extracted from the OTEL_RESOURCE_ATTRIBUTES environment variable.
func EnvironmentWithContext(ctx context.Context) *Resource {
detector := &fromEnv{}
resource, err := detector.Detect(context.Background())
resource, err := detector.Detect(ctx)
if err != nil {
otel.Handle(err)
}
Expand Down
14 changes: 14 additions & 0 deletions sdk/resource/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,20 @@ func TestDefault(t *testing.T) {
require.Contains(t, res.Attributes(), semconv.TelemetrySDKName("opentelemetry"))
}

func TestDefaultWithContext(t *testing.T) {
ctx := t.Context()
res1 := resource.DefaultWithContext(ctx)
res2 := resource.DefaultWithContext(ctx)
assert.Same(t, res1, res2)
}

func TestEnvironmentWithContext(t *testing.T) {
t.Setenv(envVar, "key=value")
ctx := t.Context()
res := resource.EnvironmentWithContext(ctx)
assert.Equal(t, map[string]string{"key": "value"}, toMap(res))
}

func TestEquivalentStability(t *testing.T) {
r1 := resource.NewSchemaless(
attribute.String("a", "1"),
Expand Down
Loading