0

我正在使用收集器来跟踪我的 java 服务中的跨度,这些服务是 http 和 grpc。收集器端点是 localhost:55680。此 java 服务跟踪成功。

现在,我想使用这个收集器来跟踪我基于 gRPC 的 go 服务。

在我的 go 服务中,我从 repo opentelemetry-go-contrib 复制了这些文件:interceptor.go 和 grpctrace.go,这里https://github.com/open-telemetry/opentelemetry-go-contrib/tree/main/instrumentation/ google.golang.org/grpc/otelgrpc

现在,我创建一个名为 config.go 的文件:

package grpcTracing

import (
  "context"
  "log"

  "go.opentelemetry.io/otel"
  "go.opentelemetry.io/otel/exporters/otlp"
  "go.opentelemetry.io/otel/exporters/otlp/otlpgrpc"
  "go.opentelemetry.io/otel/label"
  "go.opentelemetry.io/otel/propagation"
  "go.opentelemetry.io/otel/sdk/resource"
  sdktrace "go.opentelemetry.io/otel/sdk/trace"
)

// Init configures an OpenTelemetry exporter and trace provider
func InitTracing() {

  ctx := context.Background()

  driver := otlpgrpc.NewDriver(
    otlpgrpc.WithInsecure(),
    otlpgrpc.WithEndpoint("localhost:55680"),
  )

  exporter, err := otlp.NewExporter(ctx, driver) // Configure as needed.
  if err != nil {
    log.Fatal(err)
  }
  defer func() {
    err := exporter.Shutdown(ctx)
    if err != nil {
      log.Fatalf("failed to stop exporter: %v", err)
    }
  }()

  service := "test-service"

  tracerProvider := sdktrace.NewTracerProvider(
    sdktrace.WithConfig(sdktrace.Config{DefaultSampler: sdktrace.AlwaysSample()}),
    sdktrace.WithResource(resource.NewWithAttributes(
      label.Key("service.name").String(service),
    )),
    sdktrace.WithBatcher(exporter),
  )


  if err != nil {
    log.Fatal(err)
  }
  otel.SetTracerProvider(tracerProvider)
  otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
}

现在,当我启动我的 gRPC 服务器时,我会这样做:


        grpcTracing.InitTracing()
        ...
    grpcServer := grpc.NewServer(
        grpc.UnaryInterceptor(grpcTracing.UnaryServerInterceptor()),
    )

这是服务器拦截器,这被称为每个请愿书:

func UnaryServerInterceptor(opts ...Option) grpc.UnaryServerInterceptor {
  return func(
    ctx context.Context,
    req interface{},
    info *grpc.UnaryServerInfo,
    handler grpc.UnaryHandler,
  ) (interface{}, error) {
    requestMetadata, _ := metadata.FromIncomingContext(ctx)
    metadataCopy := requestMetadata.Copy()

    entries, spanCtx := Extract(ctx, &metadataCopy, opts...)
    ctx = baggage.ContextWithValues(ctx, entries...)

    tracer := newConfig(opts).TracerProvider.Tracer(
      instrumentationName,
      trace.WithInstrumentationVersion(otelcontrib.SemVersion()),
    )

    name, attr := spanInfo(info.FullMethod, peerFromCtx(ctx))
    ctx, span := tracer.Start(
      trace.ContextWithRemoteSpanContext(ctx, spanCtx),
      name,
      trace.WithSpanKind(trace.SpanKindServer),
      trace.WithAttributes(attr...),
    )
    defer span.End()

    messageReceived.Event(ctx, 1, req)

    resp, err := handler(ctx, req)
    if err != nil {
      s, _ := status.FromError(err)
      span.SetStatus(codes.Error, s.Message())
      span.SetAttributes(statusCodeAttr(s.Code()))
      messageSent.Event(ctx, 1, s.Proto())
    } else {
      span.SetAttributes(statusCodeAttr(grpc_codes.OK))
      messageSent.Event(ctx, 1, resp)
    }

    return resp, err
  }
}

第一个请愿书,我收到以下消息: rpc error: code = Canceled desc = context canceled 对于下一个请愿书,我收到以下消息: exporter disconnected

我检查收集器日志,似乎没有请求来。

知道为什么它不起作用吗?我究竟做错了什么?

谢谢!!!

4

1 回答 1

0

我的错误在这几行:

defer func() {
    err := exporter.Shutdown(ctx)
    if err != nil {
      log.Fatalf("failed to stop exporter: %v", err)
    }
}()

我删除了这条线,它起作用了!

于 2021-02-05T15:41:16.917 回答