From e14a42c8b66166796568cdfda2dcc803ce3043d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E5=8F=AF?= Date: Wed, 11 Oct 2023 11:46:14 +0800 Subject: [PATCH] feat: improve otel with resource (#1009) improve otel with resource --- pkg/core/xtrace/jaeger/config.go | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/pkg/core/xtrace/jaeger/config.go b/pkg/core/xtrace/jaeger/config.go index 4833e1bda1..9bad2f010c 100644 --- a/pkg/core/xtrace/jaeger/config.go +++ b/pkg/core/xtrace/jaeger/config.go @@ -15,6 +15,8 @@ package jaeger import ( + "context" + "github.com/douyu/jupiter/pkg" "github.com/douyu/jupiter/pkg/conf" "github.com/douyu/jupiter/pkg/xlog" @@ -22,7 +24,7 @@ import ( "go.opentelemetry.io/otel/exporters/jaeger" "go.opentelemetry.io/otel/sdk/resource" tracesdk "go.opentelemetry.io/otel/sdk/trace" - semconv "go.opentelemetry.io/otel/semconv/v1.12.0" + semconv "go.opentelemetry.io/otel/semconv/v1.4.0" "go.opentelemetry.io/otel/trace" ) @@ -55,19 +57,30 @@ func (config *Config) Build() trace.TracerProvider { // Create the Jaeger exporter exp, err := jaeger.New(jaeger.WithCollectorEndpoint(jaeger.WithEndpoint(config.Endpoint))) if err != nil { - xlog.Jupiter().Panic("new jaeger", xlog.FieldMod("build"), xlog.FieldErr(err)) + xlog.Jupiter().Panic("new jaeger", xlog.FieldErr(err)) return nil } + + resource, err := resource.New(context.TODO(), + resource.WithHost(), + resource.WithFromEnv(), + resource.WithTelemetrySDK(), + resource.WithAttributes( + semconv.ServiceNameKey.String(config.Name), + ), + ) + if err != nil { + xlog.Jupiter().Panic("new resource", xlog.FieldErr(err)) + return nil + } + tp := tracesdk.NewTracerProvider( // Set the sampling rate based on the parent span to 100% tracesdk.WithSampler(tracesdk.ParentBased(tracesdk.TraceIDRatioBased(config.Sampler))), // Always be sure to batch in production. tracesdk.WithBatcher(exp), // Record information about this application in an Resource. - tracesdk.WithResource(resource.NewSchemaless( - semconv.TelemetrySDKLanguageGo, - semconv.ServiceNameKey.String(config.Name), - )), + tracesdk.WithResource(resource), ) otel.SetTracerProvider(tp) return tp