little-cui closed pull request #319: SCB-430 Wrong parentId in tracing data
URL: https://github.com/apache/incubator-servicecomb-service-center/pull/319
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/server/infra/tracing/tracing.go b/server/infra/tracing/tracing.go
index 3e5d674a..a7d855e0 100644
--- a/server/infra/tracing/tracing.go
+++ b/server/infra/tracing/tracing.go
@@ -16,6 +16,11 @@
  */
 package tracing
 
+import (
+       
"github.com/apache/incubator-servicecomb-service-center/server/infra/registry"
+       "golang.org/x/net/context"
+)
+
 const CTX_TRACE_SPAN = "x-trace-span"
 
 type Request interface{}
@@ -27,3 +32,9 @@ type Tracing interface {
        ClientBegin(operationName string, r Request) Span
        ClientEnd(span Span, code int, message string)
 }
+
+type RegistryRequest struct {
+       Ctx      context.Context
+       Endpoint string
+       Options  registry.PluginOp
+}
diff --git a/server/plugin/infra/registry/etcd/tracing.go 
b/server/plugin/infra/registry/etcd/tracing.go
index 72a38b08..2ac751f9 100644
--- a/server/plugin/infra/registry/etcd/tracing.go
+++ b/server/plugin/infra/registry/etcd/tracing.go
@@ -17,7 +17,6 @@
 package etcd
 
 import (
-       "github.com/apache/incubator-servicecomb-service-center/pkg/util"
        
"github.com/apache/incubator-servicecomb-service-center/server/infra/registry"
        
"github.com/apache/incubator-servicecomb-service-center/server/infra/tracing"
        "github.com/apache/incubator-servicecomb-service-center/server/plugin"
@@ -25,26 +24,12 @@ import (
        "net/http"
 )
 
-func ToRequest(op registry.PluginOp) (*http.Request, error) {
-       var action string
-       switch op.Action {
-       case registry.Get:
-               action = http.MethodGet
-       case registry.Put:
-               action = http.MethodPut
-       case registry.Delete:
-               action = http.MethodDelete
-       }
-       return http.NewRequest(action, endpoint+"/?"+op.FormatUrlParams(), nil)
-}
-
 func TracingBegin(ctx context.Context, operationName string, op 
registry.PluginOp) tracing.Span {
-       r, err := ToRequest(op)
-       if err != nil {
-               util.Logger().Errorf(err, "new backend request failed")
-               return nil
+       r := &tracing.RegistryRequest{
+               Ctx:      ctx,
+               Options:  op,
+               Endpoint: endpoint,
        }
-       r = r.WithContext(ctx)
        return plugin.Plugins().Tracing().ClientBegin(operationName, r)
 }
 
diff --git a/server/plugin/infra/tracing/buildin/buildin.go 
b/server/plugin/infra/tracing/buildin/buildin.go
index b4616f21..0150cee2 100644
--- a/server/plugin/infra/tracing/buildin/buildin.go
+++ b/server/plugin/infra/tracing/buildin/buildin.go
@@ -25,6 +25,7 @@ import (
        "github.com/opentracing/opentracing-go/ext"
        "github.com/openzipkin/zipkin-go-opentracing/thrift/gen-go/zipkincore"
        "net/http"
+       "net/url"
        "sync"
 )
 
@@ -88,14 +89,12 @@ func (zp *Zipkin) ServerEnd(itf tracing.Span, code int, 
message string) {
 
 func (zp *Zipkin) ClientBegin(operationName string, itf tracing.Request) 
tracing.Span {
        var (
-               span    opentracing.Span
-               ctx     context.Context
-               carrier interface{}
+               span opentracing.Span
        )
        switch itf.(type) {
        case *http.Request:
                r := itf.(*http.Request)
-               ctx = r.Context()
+               ctx := r.Context()
 
                parentSpan, ok := 
ctx.Value(tracing.CTX_TRACE_SPAN).(opentracing.Span)
                if !ok {
@@ -110,20 +109,50 @@ func (zp *Zipkin) ClientBegin(operationName string, itf 
tracing.Request) tracing
                span.SetTag(zipkincore.HTTP_PATH, r.URL.Path)
                span.SetTag(zipkincore.HTTP_HOST, r.URL.Host)
 
-               carrier = opentracing.HTTPHeadersCarrier(r.Header)
-       default:
-               // grpc?
-               return nil
-       }
+               carrier := opentracing.HTTPHeadersCarrier(r.Header)
 
-       util.SetContext(ctx, tracing.CTX_TRACE_SPAN, span)
+               if err := ZipkinTracer().Inject(
+                       span.Context(),
+                       opentracing.HTTPHeaders,
+                       carrier,
+               ); err != nil {
+                       util.Logger().Errorf(err, "tracer inject request 
failed")
+               }
+       case *tracing.RegistryRequest:
+               r := itf.(*tracing.RegistryRequest)
+               ctx := r.Ctx
+
+               parentSpan, ok := 
ctx.Value(tracing.CTX_TRACE_SPAN).(opentracing.Span)
+               if !ok {
+                       return nil
+               }
 
-       if err := ZipkinTracer().Inject(
-               span.Context(),
-               opentracing.HTTPHeaders,
-               carrier,
-       ); err != nil {
-               util.Logger().Errorf(err, "tracer inject request failed")
+               u, _ := url.Parse(r.Endpoint + "/?" + 
r.Options.FormatUrlParams())
+
+               span = ZipkinTracer().StartSpan(operationName, 
opentracing.ChildOf(parentSpan.Context()))
+               ext.SpanKindRPCClient.Set(span)
+               ext.HTTPMethod.Set(span, r.Options.Action.String())
+               ext.HTTPUrl.Set(span, u.String())
+
+               span.SetTag("protocol", "gRPC")
+               span.SetTag(zipkincore.HTTP_PATH, u.Path)
+               span.SetTag(zipkincore.HTTP_HOST, u.Host)
+
+               carrier := opentracing.HTTPHeadersCarrier{}
+               if err := ZipkinTracer().Inject(
+                       span.Context(),
+                       opentracing.HTTPHeaders,
+                       carrier,
+               ); err != nil {
+                       util.Logger().Errorf(err, "tracer inject request 
failed")
+               }
+               // inject context
+               carrier.ForeachKey(func(key, val string) error {
+                       util.SetContext(ctx, key, val)
+                       return nil
+               })
+       default:
+               return nil
        }
 
        return span
diff --git a/server/plugin/infra/tracing/buildin/file_collector.go 
b/server/plugin/infra/tracing/buildin/file_collector.go
index 6c5ef1fc..bd48e5bd 100644
--- a/server/plugin/infra/tracing/buildin/file_collector.go
+++ b/server/plugin/infra/tracing/buildin/file_collector.go
@@ -107,6 +107,7 @@ func (f *FileCollector) Run(stopCh <-chan struct{}) {
                i     = f.Interval * 10
                t     = time.NewTicker(f.Interval)
                nr    = time.Now().Add(i)
+               max   = f.BatchSize * 2
        )
        for {
                select {
@@ -116,10 +117,10 @@ func (f *FileCollector) Run(stopCh <-chan struct{}) {
                case span := <-f.c:
                        batch = append(batch, span)
                        if len(batch) >= f.BatchSize {
-                               if len(batch) > f.BatchSize {
+                               if len(batch) > max {
                                        dispose := len(batch) - f.BatchSize
                                        util.Logger().Errorf(nil, "backlog is 
full, dispose %d span(s), max: %d",
-                                               dispose, f.BatchSize)
+                                               dispose, max)
                                        batch = batch[dispose:] // allocate more
                                }
                                if c := f.write(batch); c == 0 {


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to