From 923a6c8afccbcaf20c8654388da484e68535d8a8 Mon Sep 17 00:00:00 2001 From: Anthony J Mirabella Date: Wed, 20 May 2020 16:08:43 -0400 Subject: [PATCH] Replace sdktrace.WithResourceAttributes() with WithResource() --- example/basic/main.go | 3 ++- example/otel-collector/go.mod | 5 +++++ example/otel-collector/main.go | 7 ++++--- exporters/otlp/otlp_integration_test.go | 13 +++++++++---- sdk/trace/provider.go | 9 ++++----- sdk/trace/trace_test.go | 2 +- 6 files changed, 25 insertions(+), 14 deletions(-) diff --git a/example/basic/main.go b/example/basic/main.go index fee62818a..6d42fd4d1 100644 --- a/example/basic/main.go +++ b/example/basic/main.go @@ -26,6 +26,7 @@ import ( metricstdout "go.opentelemetry.io/otel/exporters/metric/stdout" tracestdout "go.opentelemetry.io/otel/exporters/trace/stdout" "go.opentelemetry.io/otel/sdk/metric/controller/push" + "go.opentelemetry.io/otel/sdk/resource" sdktrace "go.opentelemetry.io/otel/sdk/trace" ) @@ -46,7 +47,7 @@ func initTracer() { } tp, err := sdktrace.NewProvider(sdktrace.WithSyncer(exp), sdktrace.WithConfig(sdktrace.Config{DefaultSampler: sdktrace.AlwaysSample()}), - sdktrace.WithResourceAttributes(kv.String("rk1", "rv11"), kv.Int64("rk2", 5))) + sdktrace.WithResource(resource.New(kv.String("rk1", "rv11"), kv.Int64("rk2", 5)))) if err != nil { log.Panicf("failed to initialize trace provider %v", err) } diff --git a/example/otel-collector/go.mod b/example/otel-collector/go.mod index 0ceb645ec..c81c00eb0 100644 --- a/example/otel-collector/go.mod +++ b/example/otel-collector/go.mod @@ -8,3 +8,8 @@ require ( go.opentelemetry.io/otel/exporters/otlp v0.5.0 google.golang.org/grpc v1.29.1 ) + +replace ( + go.opentelemetry.io/otel => ../.. + go.opentelemetry.io/otel/exporters/otlp => ../../exporters/otlp +) diff --git a/example/otel-collector/main.go b/example/otel-collector/main.go index ddbdb124d..c16002f7e 100644 --- a/example/otel-collector/main.go +++ b/example/otel-collector/main.go @@ -25,6 +25,7 @@ import ( "go.opentelemetry.io/otel/api/kv" "go.opentelemetry.io/otel/exporters/otlp" + "go.opentelemetry.io/otel/sdk/resource" sdktrace "go.opentelemetry.io/otel/sdk/trace" "github.com/open-telemetry/opentelemetry-collector/translator/conventions" @@ -50,12 +51,12 @@ func main() { tp, err := sdktrace.NewProvider( sdktrace.WithConfig(sdktrace.Config{DefaultSampler: sdktrace.AlwaysSample()}), - sdktrace.WithResourceAttributes( + sdktrace.WithResource(resource.New( // the service name used to display traces in Jaeger kv.Key(conventions.AttributeServiceName).String("test-service"), - ), + )), sdktrace.WithBatcher(exp, // add following two options to ensure flush - sdktrace.WithScheduleDelayMillis(5), + sdktrace.WithBatchTimeout(5), sdktrace.WithMaxExportBatchSize(2), )) if err != nil { diff --git a/exporters/otlp/otlp_integration_test.go b/exporters/otlp/otlp_integration_test.go index 081437c71..41f5bb195 100644 --- a/exporters/otlp/otlp_integration_test.go +++ b/exporters/otlp/otlp_integration_test.go @@ -34,6 +34,7 @@ import ( "go.opentelemetry.io/otel/sdk/metric/controller/push" integrator "go.opentelemetry.io/otel/sdk/metric/integrator/simple" "go.opentelemetry.io/otel/sdk/metric/selector/simple" + "go.opentelemetry.io/otel/sdk/resource" sdktrace "go.opentelemetry.io/otel/sdk/trace" ) @@ -86,13 +87,17 @@ func newExporterEndToEndTest(t *testing.T, additionalOpts []otlp.ExporterOption) ), } tp1, err := sdktrace.NewProvider(append(pOpts, - sdktrace.WithResourceAttributes(kv.String("rk1", "rv11)"), - kv.Int64("rk2", 5)))...) + sdktrace.WithResource(resource.New( + kv.String("rk1", "rv11)"), + kv.Int64("rk2", 5), + )))...) assert.NoError(t, err) tp2, err := sdktrace.NewProvider(append(pOpts, - sdktrace.WithResourceAttributes(kv.String("rk1", "rv12)"), - kv.Float32("rk3", 6.5)))...) + sdktrace.WithResource(resource.New( + kv.String("rk1", "rv12)"), + kv.Float32("rk3", 6.5), + )))...) assert.NoError(t, err) tr1 := tp1.Tracer("test-tracer1") diff --git a/sdk/trace/provider.go b/sdk/trace/provider.go index 6b596e4d1..b493c7d53 100644 --- a/sdk/trace/provider.go +++ b/sdk/trace/provider.go @@ -21,7 +21,6 @@ import ( export "go.opentelemetry.io/otel/sdk/export/trace" "go.opentelemetry.io/otel/sdk/resource" - "go.opentelemetry.io/otel/api/kv" apitrace "go.opentelemetry.io/otel/api/trace" ) @@ -195,10 +194,10 @@ func WithConfig(config Config) ProviderOption { } } -// WithResourceAttributes option sets the resource attributes to the provider. -// Resource is added to the span when it is started. -func WithResourceAttributes(attrs ...kv.KeyValue) ProviderOption { +// WithResource option attaches a resource to the provider. +// The resource is added to the span when it is started. +func WithResource(r *resource.Resource) ProviderOption { return func(opts *ProviderOptions) { - opts.config.Resource = resource.New(attrs...) + opts.config.Resource = r } } diff --git a/sdk/trace/trace_test.go b/sdk/trace/trace_test.go index a2dc2af54..786f504a0 100644 --- a/sdk/trace/trace_test.go +++ b/sdk/trace/trace_test.go @@ -1067,7 +1067,7 @@ func TestWithResource(t *testing.T) { var te testExporter tp, _ := NewProvider(WithSyncer(&te), WithConfig(Config{DefaultSampler: AlwaysSample()}), - WithResourceAttributes(kv.String("rk1", "rv1"), kv.Int64("rk2", 5))) + WithResource(resource.New(kv.String("rk1", "rv1"), kv.Int64("rk2", 5)))) span := startSpan(tp, "WithResource") span.SetAttributes(kv.String("key1", "value1")) got, err := endSpan(&te, span)