You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2021/08/10 12:31:13 UTC

[GitHub] [dubbo-go-pixiu] LvBay commented on a change in pull request #236: [Ftr] tracing

LvBay commented on a change in pull request #236:
URL: https://github.com/apache/dubbo-go-pixiu/pull/236#discussion_r685970689



##########
File path: pkg/filter/tracing/tracing.go
##########
@@ -0,0 +1,120 @@
+package tracing
+
+import (
+	"context"
+	"log"
+	"net/http"
+	"time"
+
+	"go.opentelemetry.io/otel/trace"
+)
+
+import (
+	fc "github.com/dubbogo/dubbo-go-pixiu-filter/pkg/context"
+	"github.com/dubbogo/dubbo-go-pixiu-filter/pkg/filter"
+	"go.opentelemetry.io/otel"
+	"go.opentelemetry.io/otel/attribute"
+	"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.4.0"
+)
+
+import (
+	"github.com/apache/dubbo-go-pixiu/pkg/common/constant"
+	"github.com/apache/dubbo-go-pixiu/pkg/common/extension"
+	"github.com/apache/dubbo-go-pixiu/pkg/config"
+	contexthttp "github.com/apache/dubbo-go-pixiu/pkg/context/http"
+)
+
+const (
+	TracingType_Jaeger = "jaeger"
+
+	traceName      = "http-server"
+	spanNamePrefix = "HTTP SERVER"
+	spanTagBody    = "body"
+
+	jaegerTraceIDInHeader = "uber-trace-id"
+)
+
+// nolint
+func Init() {
+	extension.SetFilterFunc(constant.TracingFilter, tracerFilterFunc())
+}
+
+// tracerFilter is a filter for tracer
+type tracerFilter struct {
+	// global tracer
+	waitTime time.Duration
+}
+
+func tracerFilterFunc() fc.FilterFunc {
+	return New().Do()
+}
+
+func newTracerProvider(url string) (*tracesdk.TracerProvider, error) {
+	// Create the Jaeger exporter
+	exp, err := jaeger.New(jaeger.WithCollectorEndpoint(jaeger.WithEndpoint(url)))
+	if err != nil {
+		return nil, err
+	}
+	tp := tracesdk.NewTracerProvider(
+		// Always be sure to batch in production.
+		tracesdk.WithBatcher(exp),
+		// Record information about this application in an Resource.
+		tracesdk.WithResource(resource.NewWithAttributes(
+			semconv.SchemaURL,
+			semconv.ServiceNameKey.String("pixiu"),
+		)),
+	)
+
+	return tp, nil
+}
+
+// New create tracer filter.
+func New() filter.Filter {
+	tc := config.GetBootstrap().Tracing
+	switch tc.Type {
+	case TracingType_Jaeger:
+		tp, err := newTracerProvider(tc.URL)
+		if err != nil {
+			log.Fatal(err)
+		}
+		otel.SetTracerProvider(tp)
+	default:
+		panic("unsupported tracing")
+	}
+
+	return tracerFilter{}
+}
+
+// Do execute tracerFilter filter logic.
+func (f tracerFilter) Do() fc.FilterFunc {
+	return func(c fc.Context) {
+		hc := c.(*contexthttp.HttpContext)
+		spanName := spanNamePrefix + hc.Request.Method + " " + hc.Request.URL.Path
+		tr := otel.Tracer(traceName)
+		ctx := extractTraceCtxRequest(hc.Request)
+		ctxWithTid, span := tr.Start(ctx, spanName)
+
+		body := contexthttp.ExtractRequestBody(hc.Request)
+		span.SetAttributes(attribute.Key(spanTagBody).String(string(body)))
+		hc.Request = hc.Request.WithContext(ctxWithTid)
+		hc.Next()
+		span.End()

Review comment:
       先执行hc.Next(),再span.End()




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org