1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-01-03 22:52:30 +02:00

Replace sdktrace.WithResourceAttributes() with WithResource()

This commit is contained in:
Anthony J Mirabella 2020-05-20 16:08:43 -04:00
parent 5461669733
commit 923a6c8afc
6 changed files with 25 additions and 14 deletions

View File

@ -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)
}

View File

@ -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
)

View File

@ -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 {

View File

@ -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")

View File

@ -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
}
}

View File

@ -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)