Golang/Gin 学习

Golang-06 为gin框架添加zipkin

2019-09-18  本文已影响0人  国服最坑开发
package main

import (
    "os"
    "time"

    "github.com/gin-gonic/gin"
    "github.com/opentracing/opentracing-go"
    zkOt "github.com/openzipkin-contrib/zipkin-go-opentracing"
    "github.com/openzipkin/zipkin-go"
    zkHttp "github.com/openzipkin/zipkin-go/reporter/http"
)

// 第一步: 开一个全局变量
var zkTracer opentracing.Tracer

func main() {
        // 第二步: 初始化 tracer
    {
        reporter := zkHttp.NewReporter("http://localhost:9411/api/v2/spans")
        defer reporter.Close()
        endpoint, err := zipkin.NewEndpoint("main3", "localhost:80")
        if err != nil {
            log.Fatalf("unable to create local endpoint: %+v\n", err)
        }
        nativeTracer, err := zipkin.NewTracer(reporter, zipkin.WithLocalEndpoint(endpoint))
        if err != nil {
            log.Fatalf("unable to create tracer: %+v\n", err)
        }
        zkTracer = zkOt.Wrap(nativeTracer)
        opentracing.SetGlobalTracer(zkTracer)
    }

    r := gin.Default()
        // 第三步: 添加一个 middleWare, 为每一个请求添加span
    r.Use(func(c *gin.Context) {
        span := zkTracer.StartSpan(c.FullPath())
        defer span.Finish()
        c.Next()
    })
    r.GET("/",
        func(c *gin.Context) {
            time.Sleep(500 * time.Millisecond)
            c.JSON(200, gin.H{"code": 200, "msg": "OK"})
        })
    r.GET("/app",
        func(c *gin.Context) {
            time.Sleep(500 * time.Millisecond)
            c.JSON(200, gin.H{"code": 200, "msg": "OK"})
        })
    r.Run(":80")
}
上一篇下一篇

猜你喜欢

热点阅读