// Init配置OpenTelemetryfunc Init() *sdktrace.TracerProvider {if ctx == nil {ctx = context.Background()}//创建新的exporter,设置基本的endpointopts := []otlptracegrpc.Option{otlptracegrpc.WithEndpoint("<接入点>"),otlptracegrpc.WithInsecure(),}exporter, err := otlptracegrpc.New(ctx, opts...)if err != nil {log.Fatal(err)}//设置Token,也可以设置环境变量:OTEL_RESOURCE_ATTRIBUTES=token=xxxxxxxxxr, err := resource.New(ctx, []resource.Option{//设置Token值resource.WithAttributes(attribute.KeyValue{Key: "token", Value: attribute.StringValue("<Token>"),}),//设置服务名resource.WithAttributes(attribute.KeyValue{Key: "service.name", Value: attribute.StringValue("audotanggrpcdemo"),}),}...)if err != nil {log.Fatal(err)}//创建新的TracerProvidertp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()),sdktrace.WithBatcher(exporter),sdktrace.WithResource(r),)otel.SetTracerProvider(tp)otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))return tp}
//初始化tracetp := trace.Init()defer func() {if err := tp.Shutdown(context.Background()); err != nil {log.Printf("Error shutting down tracer provider: %v", err)}}()//指定hosthost := os.Getenv("grpc1")lis, err := net.Listen("tcp", host+":7778")if err != nil {log.Fatalf("failed to listen: %v", err)}
s := grpc.NewServer(grpc.UnaryInterceptor(otelgrpc.UnaryServerInterceptor()),grpc.StreamInterceptor(otelgrpc.StreamServerInterceptor()),)
//在gRPC服务器处注册我们的服务api.RegisterHelloServiceServer(s, &server{})reflection.Register(s)if err := s.Serve(lis); err != nil {log.Fatalf("failed to serve: %v", err)}
//初始化tracetp := trace.Init()defer func() {if err := tp.Shutdown(context.Background()); err != nil {log.Printf("Error shutting down tracer provider: %v", err)}}()
//向服务端建立连接,配置拦截器conn, err := grpc.DialContext(context.Background(), "localhost:7778", grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock())
c := api.NewHelloServiceClient(conn)for {callSayHelloClientStream(c)time.Sleep(100 * time.Millisecond)}
本页内容是否解决了您的问题?