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/09/02 02:44:09 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_r700702284



##########
File path: pkg/filter/tracing/tracing.go
##########
@@ -0,0 +1,138 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+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 "":
+	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)))

Review comment:
       那就不记录body了~




-- 
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