From 6451e0fd06dbce4713c031e5207979ef1a1bc154 Mon Sep 17 00:00:00 2001 From: tx7do Date: Fri, 27 Oct 2023 20:09:56 +0800 Subject: [PATCH] feat: tracer --- api/conf/v1/kratos_conf_tracer.proto | 1 + gen/api/go/conf/v1/kratos_conf_tracer.pb.go | 34 +++++++++++++-------- go.mod | 1 + go.sum | 2 ++ tracer.go | 34 +++++++++++++-------- 5 files changed, 47 insertions(+), 25 deletions(-) diff --git a/api/conf/v1/kratos_conf_tracer.proto b/api/conf/v1/kratos_conf_tracer.proto index 3c74047..468470e 100644 --- a/api/conf/v1/kratos_conf_tracer.proto +++ b/api/conf/v1/kratos_conf_tracer.proto @@ -10,4 +10,5 @@ message Tracer { string endpoint = 2; // 端口 double sampler = 3; // 采样率,默认:1.0 string env = 4; // 运行环境:dev、debug、product + bool insecure = 5; } diff --git a/gen/api/go/conf/v1/kratos_conf_tracer.pb.go b/gen/api/go/conf/v1/kratos_conf_tracer.pb.go index 605e81a..efce9f2 100644 --- a/gen/api/go/conf/v1/kratos_conf_tracer.pb.go +++ b/gen/api/go/conf/v1/kratos_conf_tracer.pb.go @@ -30,6 +30,7 @@ type Tracer struct { Endpoint string `protobuf:"bytes,2,opt,name=endpoint,proto3" json:"endpoint,omitempty"` // 端口 Sampler float64 `protobuf:"fixed64,3,opt,name=sampler,proto3" json:"sampler,omitempty"` // 采样率,默认:1.0 Env string `protobuf:"bytes,4,opt,name=env,proto3" json:"env,omitempty"` // 运行环境:dev、debug、product + Insecure bool `protobuf:"varint,5,opt,name=insecure,proto3" json:"insecure,omitempty"` } func (x *Tracer) Reset() { @@ -92,23 +93,32 @@ func (x *Tracer) GetEnv() string { return "" } +func (x *Tracer) GetInsecure() bool { + if x != nil { + return x.Insecure + } + return false +} + var File_conf_v1_kratos_conf_tracer_proto protoreflect.FileDescriptor var file_conf_v1_kratos_conf_tracer_proto_rawDesc = []byte{ 0x0a, 0x20, 0x63, 0x6f, 0x6e, 0x66, 0x2f, 0x76, 0x31, 0x2f, 0x6b, 0x72, 0x61, 0x74, 0x6f, 0x73, 0x5f, 0x63, 0x6f, 0x6e, 0x66, 0x5f, 0x74, 0x72, 0x61, 0x63, 0x65, 0x72, 0x2e, 0x70, 0x72, 0x6f, - 0x74, 0x6f, 0x12, 0x04, 0x63, 0x6f, 0x6e, 0x66, 0x22, 0x6a, 0x0a, 0x06, 0x54, 0x72, 0x61, 0x63, - 0x65, 0x72, 0x12, 0x18, 0x0a, 0x07, 0x62, 0x61, 0x74, 0x63, 0x68, 0x65, 0x72, 0x18, 0x01, 0x20, - 0x01, 0x28, 0x09, 0x52, 0x07, 0x62, 0x61, 0x74, 0x63, 0x68, 0x65, 0x72, 0x12, 0x1a, 0x0a, 0x08, - 0x65, 0x6e, 0x64, 0x70, 0x6f, 0x69, 0x6e, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, - 0x65, 0x6e, 0x64, 0x70, 0x6f, 0x69, 0x6e, 0x74, 0x12, 0x18, 0x0a, 0x07, 0x73, 0x61, 0x6d, 0x70, - 0x6c, 0x65, 0x72, 0x18, 0x03, 0x20, 0x01, 0x28, 0x01, 0x52, 0x07, 0x73, 0x61, 0x6d, 0x70, 0x6c, - 0x65, 0x72, 0x12, 0x10, 0x0a, 0x03, 0x65, 0x6e, 0x76, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, - 0x03, 0x65, 0x6e, 0x76, 0x42, 0x3b, 0x5a, 0x39, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, - 0x6f, 0x6d, 0x2f, 0x74, 0x78, 0x37, 0x64, 0x6f, 0x2f, 0x6b, 0x72, 0x61, 0x74, 0x6f, 0x73, 0x2d, - 0x62, 0x6f, 0x6f, 0x74, 0x73, 0x74, 0x72, 0x61, 0x70, 0x2f, 0x67, 0x65, 0x6e, 0x2f, 0x61, 0x70, - 0x69, 0x2f, 0x67, 0x6f, 0x2f, 0x63, 0x6f, 0x6e, 0x66, 0x2f, 0x76, 0x31, 0x3b, 0x63, 0x6f, 0x6e, - 0x66, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x74, 0x6f, 0x12, 0x04, 0x63, 0x6f, 0x6e, 0x66, 0x22, 0x86, 0x01, 0x0a, 0x06, 0x54, 0x72, 0x61, + 0x63, 0x65, 0x72, 0x12, 0x18, 0x0a, 0x07, 0x62, 0x61, 0x74, 0x63, 0x68, 0x65, 0x72, 0x18, 0x01, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x62, 0x61, 0x74, 0x63, 0x68, 0x65, 0x72, 0x12, 0x1a, 0x0a, + 0x08, 0x65, 0x6e, 0x64, 0x70, 0x6f, 0x69, 0x6e, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x08, 0x65, 0x6e, 0x64, 0x70, 0x6f, 0x69, 0x6e, 0x74, 0x12, 0x18, 0x0a, 0x07, 0x73, 0x61, 0x6d, + 0x70, 0x6c, 0x65, 0x72, 0x18, 0x03, 0x20, 0x01, 0x28, 0x01, 0x52, 0x07, 0x73, 0x61, 0x6d, 0x70, + 0x6c, 0x65, 0x72, 0x12, 0x10, 0x0a, 0x03, 0x65, 0x6e, 0x76, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, + 0x52, 0x03, 0x65, 0x6e, 0x76, 0x12, 0x1a, 0x0a, 0x08, 0x69, 0x6e, 0x73, 0x65, 0x63, 0x75, 0x72, + 0x65, 0x18, 0x05, 0x20, 0x01, 0x28, 0x08, 0x52, 0x08, 0x69, 0x6e, 0x73, 0x65, 0x63, 0x75, 0x72, + 0x65, 0x42, 0x3b, 0x5a, 0x39, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, + 0x74, 0x78, 0x37, 0x64, 0x6f, 0x2f, 0x6b, 0x72, 0x61, 0x74, 0x6f, 0x73, 0x2d, 0x62, 0x6f, 0x6f, + 0x74, 0x73, 0x74, 0x72, 0x61, 0x70, 0x2f, 0x67, 0x65, 0x6e, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x67, + 0x6f, 0x2f, 0x63, 0x6f, 0x6e, 0x66, 0x2f, 0x76, 0x31, 0x3b, 0x63, 0x6f, 0x6e, 0x66, 0x62, 0x06, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/go.mod b/go.mod index c78b87a..ae44d7a 100644 --- a/go.mod +++ b/go.mod @@ -39,6 +39,7 @@ require ( go.opentelemetry.io/otel v1.19.0 go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.19.0 go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.19.0 + go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.19.0 go.opentelemetry.io/otel/exporters/zipkin v1.19.0 go.opentelemetry.io/otel/sdk v1.19.0 go.uber.org/zap v1.26.0 diff --git a/go.sum b/go.sum index 52ffc83..4ee7a77 100644 --- a/go.sum +++ b/go.sum @@ -842,6 +842,8 @@ go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.19.0 h1:3d+S2 go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.19.0/go.mod h1:0+KuTDyKL4gjKCF75pHOX4wuzYDUZYfAQdSu43o+Z2I= go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.19.0 h1:IeMeyr1aBvBiPVYihXIaeIZba6b8E1bYp7lbdxK8CQg= go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.19.0/go.mod h1:oVdCUtjq9MK9BlS7TtucsQwUcXcymNiEDjgDD2jMtZU= +go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.19.0 h1:Nw7Dv4lwvGrI68+wULbcq7su9K2cebeCUrDjVrUJHxM= +go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.19.0/go.mod h1:1MsF6Y7gTqosgoZvHlzcaaM8DIMNZgJh87ykokoNH7Y= go.opentelemetry.io/otel/exporters/zipkin v1.19.0 h1:EGY0h5mGliP9o/nIkVuLI0vRiQqmsYOcbwCuotksO1o= go.opentelemetry.io/otel/exporters/zipkin v1.19.0/go.mod h1:JQgTGJP11yi3o4GHzIWYodhPisxANdqxF1eHwDSnJrI= go.opentelemetry.io/otel/metric v1.19.0 h1:aTzpGtV0ar9wlV4Sna9sdJyII5jTVJEvKETPiOKwvpE= diff --git a/tracer.go b/tracer.go index af3a9ef..78a5570 100644 --- a/tracer.go +++ b/tracer.go @@ -3,10 +3,10 @@ package bootstrap import ( "context" "errors" - "strings" - + "go.opentelemetry.io/otel/exporters/otlp/otlptrace" "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp" + "go.opentelemetry.io/otel/exporters/stdout/stdouttrace" "go.opentelemetry.io/otel/exporters/zipkin" "go.opentelemetry.io/otel" @@ -19,7 +19,7 @@ import ( ) // NewTracerExporter 创建一个导出器,支持:jaeger和zipkin -func NewTracerExporter(exporterName, endpoint string) (traceSdk.SpanExporter, error) { +func NewTracerExporter(exporterName, endpoint string, insecure bool) (traceSdk.SpanExporter, error) { if exporterName == "" { exporterName = "jaeger" } @@ -31,10 +31,12 @@ func NewTracerExporter(exporterName, endpoint string) (traceSdk.SpanExporter, er return NewZipkinExporter(ctx, endpoint) case "jaeger": fallthrough - case "otlptracehttp": - return NewOtlpHttpExporter(ctx, endpoint) - case "otlptracegrpc": - return NewOtlpGrpcExporter(ctx, endpoint) + case "otlp-http": + return NewOtlpHttpExporter(ctx, endpoint, insecure) + case "otlp-grpc": + return NewOtlpGrpcExporter(ctx, endpoint, insecure) + case "stdout": + return stdouttrace.New() default: return nil, errors.New("exporter type not support") } @@ -94,29 +96,35 @@ func NewZipkinExporter(_ context.Context, endpoint string) (traceSdk.SpanExporte //} // NewOtlpHttpExporter 创建一个OTLP HTTP导出器 -func NewOtlpHttpExporter(ctx context.Context, endpoint string, options ...otlptracehttp.Option) (traceSdk.SpanExporter, error) { +func NewOtlpHttpExporter(ctx context.Context, endpoint string, insecure bool, options ...otlptracehttp.Option) (traceSdk.SpanExporter, error) { var opts []otlptracehttp.Option opts = append(opts, otlptracehttp.WithEndpoint(endpoint)) - if strings.HasPrefix(endpoint, "http://") { + if insecure { opts = append(opts, otlptracehttp.WithInsecure()) } opts = append(opts, options...) - return otlptracehttp.New(ctx, opts...) + return otlptrace.New( + ctx, + otlptracehttp.NewClient(opts...), + ) } // NewOtlpGrpcExporter 创建一个OTLP GRPC导出器 -func NewOtlpGrpcExporter(ctx context.Context, endpoint string, options ...otlptracegrpc.Option) (traceSdk.SpanExporter, error) { +func NewOtlpGrpcExporter(ctx context.Context, endpoint string, insecure bool, options ...otlptracegrpc.Option) (traceSdk.SpanExporter, error) { var opts []otlptracegrpc.Option opts = append(opts, otlptracegrpc.WithEndpoint(endpoint)) - if strings.HasPrefix(endpoint, "http://") { + if insecure { opts = append(opts, otlptracegrpc.WithInsecure()) } opts = append(opts, options...) - return otlptracegrpc.New(ctx, opts...) + return otlptrace.New( + ctx, + otlptracegrpc.NewClient(opts...), + ) }