Skip to content

Commit

Permalink
Merge pull request #2 from duanxinDX/xin_jaeger
Browse files Browse the repository at this point in the history
feat: add jaeger middleware
  • Loading branch information
xyh committed Oct 16, 2018
2 parents c819e6b + 67ca1a2 commit 489483f
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 5 deletions.
39 changes: 36 additions & 3 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ import:
- package: github.com/getsentry/raven-go
- package: github.com/levigross/grequests
version: ~0.9
- package: github.com/CrowdSurge/banner
- package: github.com/CrowdSurge/banner
- package: github.com/uber/jaeger-client-go
version: 2.14.0
- package: github.com/opentracing/opentracing-go
version: 1.0.2
76 changes: 76 additions & 0 deletions jaeger/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package jaeger

import (
"fmt"
"os"
"strings"
"strconv"
"time"

"github.com/lingmiaotech/tonic/configs"
"github.com/opentracing/opentracing-go"
"github.com/uber/jaeger-client-go"
"github.com/uber/jaeger-client-go/config"
"github.com/uber/jaeger-client-go/transport"
)

func Initialize() {

var sampleStrategy string
if sampleStrategy = os.Getenv("SAMPLE_STRATEGY"); sampleStrategy == ""{
sampleStrategy = "probabilistic,0.2"
}
args := strings.Split(sampleStrategy, ",")
if len(args) != 2 {
panic(fmt.Sprintf("ERROR: invalid SAMPLE_STRATEGY format %s", sampleStrategy))
}
samplerType := args[0]
param, err := strconv.ParseFloat(args[1], 64)
if err != nil {
panic(fmt.Sprintf("ERROR: invalid SAMPLE_STRATEGY param %s, param must be string of float64", sampleStrategy))
}

cfg := &config.Configuration{
ServiceName: configs.GetString("app_name"),
Sampler: &config.SamplerConfig{
Type: samplerType,
Param: param,
},
}

var agentURI string
if agentURI = os.Getenv("JAEGER_AGENT"); agentURI == "" {
agentURI = fmt.Sprintf("http://jaeger-agent.%s:6831", configs.GetString("app_name"))
}

var backendHostPort string
if env, _ := configs.Get("env").(string); env == "production" {
backendHostPort = agentURI
}else {
backendHostPort = "127.0.0.1:6831"
}

var sender jaeger.Transport
if strings.HasPrefix(backendHostPort, "http://") {
sender = transport.NewHTTPTransport(
backendHostPort,
transport.HTTPBatchSize(1),
)
} else {
sender, err = jaeger.NewUDPTransport(backendHostPort, 0)
if err != nil {
panic(fmt.Sprintf("ERROR: cannot initialize UDP sender: %v\n", err))
}
}

tracer, _, err := cfg.NewTracer(
config.Reporter(jaeger.NewRemoteReporter(
sender,
jaeger.ReporterOptions.BufferFlushInterval(1*time.Second),
)))
if err != nil {
panic(fmt.Sprintf("ERROR: cannot init Jaeger: %v\n", err))
}
opentracing.InitGlobalTracer(tracer)
return
}
31 changes: 30 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import (

"github.com/CrowdSurge/banner"
"github.com/gin-gonic/gin"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"

"github.com/lingmiaotech/tonic/configs"
"github.com/lingmiaotech/tonic/database"
"github.com/lingmiaotech/tonic/jaeger"
"github.com/lingmiaotech/tonic/kafka"
"github.com/lingmiaotech/tonic/logging"
"github.com/lingmiaotech/tonic/redis"
Expand Down Expand Up @@ -112,13 +115,39 @@ func GetServerMode() string {
return gin.DebugMode
}

func InitJaeger() gin.HandlerFunc {
return func(c *gin.Context) {
jaeger.Initialize()
c.Next()
}
}

func InitJaegerSpan() gin.HandlerFunc {
return func(c *gin.Context) {
tracer := opentracing.GlobalTracer()
var span opentracing.Span
spanContext, err := tracer.Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(c.Request.Header))
if err != nil {
span = tracer.StartSpan("HTTP " + c.Request.Method)
} else {
span = tracer.StartSpan("HTTP "+c.Request.Method, ext.RPCServerOption(spanContext))
}
defer span.Finish()
ext.HTTPMethod.Set(span, c.Request.Method)
ext.HTTPUrl.Set(span, c.Request.URL.String())
ext.Component.Set(span, "net/http")
c.Request = c.Request.WithContext(opentracing.ContextWithSpan(c.Request.Context(), span))
c.Next()
}
}

func InitMiddlewares(app *gin.Engine) {
env, _ := configs.Get("env").(string)

switch env {
case "test":
app.Use(gin.LoggerWithWriter(ioutil.Discard), gin.Recovery())
default:
app.Use(gin.Logger(), gin.Recovery())
app.Use(gin.Logger(), gin.Recovery(), InitJaeger(), InitJaegerSpan())
}
}

0 comments on commit 489483f

Please sign in to comment.