From 6f96e67adb351c0cf7f0b0011b59678f0f262b3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Thu, 25 Jan 2024 19:21:43 +0100 Subject: [PATCH] example/dice: Do not use semconv (#4849) * example/dice: Do not use semconv * Update docs * Remove resource code and use defaults --- example/dice/doc.go | 6 +++++- example/dice/main.go | 4 +--- example/dice/otel.go | 29 +++++------------------------ 3 files changed, 11 insertions(+), 28 deletions(-) diff --git a/example/dice/doc.go b/example/dice/doc.go index 5fe156fb977..31a3f7bfc20 100644 --- a/example/dice/doc.go +++ b/example/dice/doc.go @@ -12,5 +12,9 @@ // See the License for the specific language governing permissions and // limitations under the License. -// Dice is the "Roll the dice" getting started example application. +// Dice is the "Roll the dice" application. +// +// [Getting Started] uses this example to demonstrate OpenTelemetry Go. +// +// [Getting Started]: https://opentelemetry.io/docs/languages/net/automatic/getting-started/ package main diff --git a/example/dice/main.go b/example/dice/main.go index f7e0242906f..7ba965300d1 100644 --- a/example/dice/main.go +++ b/example/dice/main.go @@ -39,9 +39,7 @@ func run() (err error) { defer stop() // Set up OpenTelemetry. - serviceName := "dice" - serviceVersion := "0.1.0" - otelShutdown, err := setupOTelSDK(ctx, serviceName, serviceVersion) + otelShutdown, err := setupOTelSDK(ctx) if err != nil { return } diff --git a/example/dice/otel.go b/example/dice/otel.go index 0b30735007a..87e64dc6ce8 100644 --- a/example/dice/otel.go +++ b/example/dice/otel.go @@ -24,14 +24,12 @@ import ( "go.opentelemetry.io/otel/exporters/stdout/stdouttrace" "go.opentelemetry.io/otel/propagation" "go.opentelemetry.io/otel/sdk/metric" - "go.opentelemetry.io/otel/sdk/resource" "go.opentelemetry.io/otel/sdk/trace" - semconv "go.opentelemetry.io/otel/semconv/v1.24.0" ) // setupOTelSDK bootstraps the OpenTelemetry pipeline. // If it does not return an error, make sure to call shutdown for proper cleanup. -func setupOTelSDK(ctx context.Context, serviceName, serviceVersion string) (shutdown func(context.Context) error, err error) { +func setupOTelSDK(ctx context.Context) (shutdown func(context.Context) error, err error) { var shutdownFuncs []func(context.Context) error // shutdown calls cleanup functions registered via shutdownFuncs. @@ -51,19 +49,12 @@ func setupOTelSDK(ctx context.Context, serviceName, serviceVersion string) (shut err = errors.Join(inErr, shutdown(ctx)) } - // Set up resource. - res, err := newResource(serviceName, serviceVersion) - if err != nil { - handleErr(err) - return - } - // Set up propagator. prop := newPropagator() otel.SetTextMapPropagator(prop) // Set up trace provider. - tracerProvider, err := newTraceProvider(res) + tracerProvider, err := newTraceProvider() if err != nil { handleErr(err) return @@ -72,7 +63,7 @@ func setupOTelSDK(ctx context.Context, serviceName, serviceVersion string) (shut otel.SetTracerProvider(tracerProvider) // Set up meter provider. - meterProvider, err := newMeterProvider(res) + meterProvider, err := newMeterProvider() if err != nil { handleErr(err) return @@ -83,14 +74,6 @@ func setupOTelSDK(ctx context.Context, serviceName, serviceVersion string) (shut return } -func newResource(serviceName, serviceVersion string) (*resource.Resource, error) { - return resource.Merge(resource.Default(), - resource.NewWithAttributes(semconv.SchemaURL, - semconv.ServiceName(serviceName), - semconv.ServiceVersion(serviceVersion), - )) -} - func newPropagator() propagation.TextMapPropagator { return propagation.NewCompositeTextMapPropagator( propagation.TraceContext{}, @@ -98,7 +81,7 @@ func newPropagator() propagation.TextMapPropagator { ) } -func newTraceProvider(res *resource.Resource) (*trace.TracerProvider, error) { +func newTraceProvider() (*trace.TracerProvider, error) { traceExporter, err := stdouttrace.New( stdouttrace.WithPrettyPrint()) if err != nil { @@ -109,19 +92,17 @@ func newTraceProvider(res *resource.Resource) (*trace.TracerProvider, error) { trace.WithBatcher(traceExporter, // Default is 5s. Set to 1s for demonstrative purposes. trace.WithBatchTimeout(time.Second)), - trace.WithResource(res), ) return traceProvider, nil } -func newMeterProvider(res *resource.Resource) (*metric.MeterProvider, error) { +func newMeterProvider() (*metric.MeterProvider, error) { metricExporter, err := stdoutmetric.New() if err != nil { return nil, err } meterProvider := metric.NewMeterProvider( - metric.WithResource(res), metric.WithReader(metric.NewPeriodicReader(metricExporter, // Default is 1m. Set to 3s for demonstrative purposes. metric.WithInterval(3*time.Second))),