Skip to content
Permalink
Browse files

链路追踪

  • Loading branch information...
hb-chen committed Oct 13, 2019
1 parent 8573ae8 commit d3f79f6c427822c1646ee7ccb2a4ba68c81681fd
Showing with 135 additions and 26 deletions.
  1. +44 −25 example/api/handler/example.go
  2. +14 −0 example/api/main.go
  3. +12 −0 example/srv/main.go
  4. +3 −1 go.mod
  5. +3 −0 go.sum
  6. +59 −0 pkg/opentracing/jaeger.go
@@ -3,13 +3,14 @@ package handler
import (
"context"
"encoding/json"
"github.com/micro/go-micro/util/log"
"sync"

"github.com/hb-go/micro-quick-start/example/api/client"
api "github.com/micro/go-micro/api/proto"
mc "github.com/micro/go-micro/client"
"github.com/micro/go-micro/client/selector"
"github.com/micro/go-micro/errors"
"github.com/micro/go-micro/util/log"

example "github.com/hb-go/micro-quick-start/example/srv/proto/example"
)
@@ -37,32 +38,50 @@ func (e *Example) Call(ctx context.Context, req *api.Request, rsp *api.Response)
}

// make request
response, err := exampleClient.Call(ctx, &example.Request{
Name: extractValue(req.Post["name"]) + " 1",
})
if err != nil {
return errors.InternalServerError("go.micro.api.example.example.call", err.Error())
}
responses := []*example.Response{}
mtx := sync.RWMutex{}
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
response, err := exampleClient.Call(
ctx,
&example.Request{
Name: extractValue(req.Post["name"]) + " 1",
},
)
if err != nil {
log.Logf("go.micro.api.example.example.call", err.Error())
} else {
mtx.Lock()
responses = append(responses, response)
mtx.Unlock()
}
wg.Done()
}()

response1, err := exampleClient.Call(
ctx,
&example.Request{
Name: extractValue(req.Post["name"]) + " 2",
},
mc.WithSelectOption(
selector.WithFilter(
selector.FilterLabel("key", "value1"),
wg.Add(1)
go func() {
response1, err := exampleClient.Call(
ctx,
&example.Request{
Name: extractValue(req.Post["name"]) + " 2",
},
mc.WithSelectOption(
selector.WithFilter(
selector.FilterLabel("key", "value1"),
),
),
),
)
if err != nil {
return errors.InternalServerError("go.micro.api.example.example.call", err.Error())
}

responses := []*example.Response{
response,
response1,
}
)
if err != nil {
log.Logf("go.micro.api.example.example.call", err.Error())
} else {
mtx.Lock()
responses = append(responses, response1)
mtx.Unlock()
}
wg.Done()
}()
wg.Wait()

b, _ := json.Marshal(responses)

@@ -23,6 +23,9 @@ import (
"github.com/micro/go-micro/config"
"github.com/micro/go-micro/config/source/consul"

tracer "github.com/hb-go/micro-quick-start/pkg/opentracing"
"github.com/micro/go-plugins/wrapper/trace/opentracing"

example "github.com/hb-go/micro-quick-start/example/api/proto/example"
)

@@ -94,6 +97,17 @@ func main() {
micro.WrapClient(NewClientWrapper()),
)

// 链路追踪
t, closer, err := tracer.NewJaegerTracer("example.api", "127.0.0.1:6831")
if err != nil {
log.Fatalf("opentracing tracer create error:%v", err)
}
defer closer.Close()
service.Init(
micro.WrapCall(opentracing.NewCallWrapper(t)),
micro.WrapHandler(opentracing.NewHandlerWrapper(t)),
)

// Register Handler
example.RegisterExampleHandler(
service.Server(),
@@ -5,8 +5,10 @@ import (
"github.com/hb-go/micro-quick-start/example/srv/subscriber"
"github.com/micro/go-micro"
"github.com/micro/go-micro/util/log"
"github.com/micro/go-plugins/wrapper/trace/opentracing"

example "github.com/hb-go/micro-quick-start/example/srv/proto/example"
tracer "github.com/hb-go/micro-quick-start/pkg/opentracing"
)

func main() {
@@ -27,6 +29,16 @@ func main() {
micro.Metadata(metadata),
)

// 链路追踪
t, closer, err := tracer.NewJaegerTracer("example.srv", "127.0.0.1:6831")
if err != nil {
log.Fatalf("opentracing tracer create error:%v", err)
}
defer closer.Close()
service.Init(
micro.WrapHandler(opentracing.NewHandlerWrapper(t)),
)

// Register Handler
example.RegisterExampleHandler(service.Server(), new(handler.Example))

4 go.mod
@@ -7,6 +7,8 @@ require (
github.com/golang/protobuf v1.3.2
github.com/micro/go-micro v1.11.3
github.com/micro/go-plugins v1.3.0
github.com/nats-io/nats-server/v2 v2.1.0 // indirect
github.com/opentracing/opentracing-go v1.1.0
github.com/prometheus/client_golang v1.1.0
github.com/uber/jaeger-client-go v2.16.0+incompatible
github.com/uber/jaeger-lib v2.0.0+incompatible
)
3 go.sum
@@ -448,6 +448,7 @@ github.com/opencontainers/go-digest v1.0.0-rc1/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQ
github.com/opencontainers/image-spec v1.0.1/go.mod h1:BtxoFyWECRxE4U/7sNtV5W15zMzWCbyJoFRP3s7yZA0=
github.com/opencontainers/runc v0.1.1/go.mod h1:qT5XzbpPznkRYVz/mWwUaVBUv2rmF59PVA73FjuZG0U=
github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
github.com/opentracing/opentracing-go v1.1.0 h1:pWlfV3Bxv7k65HYwkikxat0+s3pV4bsqf19k25Ur8rU=
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
github.com/openzipkin/zipkin-go v0.1.6/go.mod h1:QgAqvLzwWbR/WpD4A3cGpPtJrZXNIiJc5AZX7/PBEpw=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
@@ -539,8 +540,10 @@ github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqri
github.com/uber-go/atomic v1.3.2/go.mod h1:/Ct5t2lcmbJ4OSe/waGBoaVvVqtO0bmtfVNex1PFV8g=
github.com/uber-go/atomic v1.4.0/go.mod h1:/Ct5t2lcmbJ4OSe/waGBoaVvVqtO0bmtfVNex1PFV8g=
github.com/uber/jaeger-client-go v2.15.0+incompatible/go.mod h1:WVhlPFC8FDjOFMMWRy2pZqQJSXxYSwNYOkTr/Z6d3Kk=
github.com/uber/jaeger-client-go v2.16.0+incompatible h1:Q2Pp6v3QYiocMxomCaJuwQGFt7E53bPYqEgug/AoBtY=
github.com/uber/jaeger-client-go v2.16.0+incompatible/go.mod h1:WVhlPFC8FDjOFMMWRy2pZqQJSXxYSwNYOkTr/Z6d3Kk=
github.com/uber/jaeger-lib v1.5.0/go.mod h1:ComeNDZlWwrWnDv8aPp0Ba6+uUTzImX/AauajbLI56U=
github.com/uber/jaeger-lib v2.0.0+incompatible h1:iMSCV0rmXEogjNWPh2D0xk9YVKvrtGoHJNe9ebLu/pw=
github.com/uber/jaeger-lib v2.0.0+incompatible/go.mod h1:ComeNDZlWwrWnDv8aPp0Ba6+uUTzImX/AauajbLI56U=
github.com/willf/bitset v1.1.9/go.mod h1:RjeCKbqT1RxIR/KWY6phxZiaY1IyutSBfGjNPySAYV4=
github.com/willf/bitset v1.1.10/go.mod h1:RjeCKbqT1RxIR/KWY6phxZiaY1IyutSBfGjNPySAYV4=
@@ -0,0 +1,59 @@
package opentracing

import (
"io"

"github.com/micro/go-micro/util/log"
"github.com/opentracing/opentracing-go"
"github.com/uber/jaeger-client-go"
jaegercfg "github.com/uber/jaeger-client-go/config"
"github.com/uber/jaeger-lib/metrics"
)

func NewJaegerTracer(serviceName, addr string) (opentracing.Tracer, io.Closer, error) {
// Sample configuration for testing. Use constant sampling to sample every trace
// and enable LogSpan to log every span via configured Logger.
cfg := jaegercfg.Configuration{
Sampler: &jaegercfg.SamplerConfig{
Type: jaeger.SamplerTypeConst,
Param: 1,
},
Reporter: &jaegercfg.ReporterConfig{
LogSpans: true,
},
}

cfg.ServiceName = serviceName

// Example logger and metrics factory. Use github.com/uber/jaeger-client-go/log
// and github.com/uber/jaeger-lib/metrics respectively to bind to real logging and metrics
// frameworks.
jLogger := &jaegerLogger{}
jMetricsFactory := metrics.NullFactory

sender, err := jaeger.NewUDPTransport(addr, 0)
if err != nil {
log.Logf("could not initialize jaeger sender: %s", err.Error())
return nil, nil, err
}

repoter := jaeger.NewRemoteReporter(sender)

return cfg.NewTracer(
jaegercfg.Logger(jLogger),
jaegercfg.Metrics(jMetricsFactory),
jaegercfg.Reporter(repoter),
)

}

type jaegerLogger struct{}

func (l *jaegerLogger) Error(msg string) {
log.Logf("ERROR: %s", msg)
}

// Infof logs a message at info priority
func (l *jaegerLogger) Infof(msg string, args ...interface{}) {
log.Logf(msg, args...)
}

0 comments on commit d3f79f6

Please sign in to comment.
You can’t perform that action at this time.