add: jaeger tracing

This commit is contained in:
viletyy 2021-06-14 21:15:55 +08:00
parent 17b4f99a50
commit 36ee60b9fa
17 changed files with 170 additions and 31 deletions

View File

@ -4,6 +4,7 @@ app:
jwt_secret: 'viletyy' jwt_secret: 'viletyy'
jwt_expire: '7200' jwt_expire: '7200'
run_mode: 'debug' run_mode: 'debug'
default_context_timeout: 60
page_size: 10 page_size: 10
upload_save_path: 'tmp/uploads' upload_save_path: 'tmp/uploads'
upload_server_path: '/static' upload_server_path: '/static'

View File

@ -1,7 +1,7 @@
/* /*
* @Date: 2021-03-22 09:46:19 * @Date: 2021-03-22 09:46:19
* @LastEditors: viletyy * @LastEditors: viletyy
* @LastEditTime: 2021-06-13 22:00:01 * @LastEditTime: 2021-06-13 23:00:46
* @FilePath: /potato/config/app.go * @FilePath: /potato/config/app.go
*/ */
package config package config
@ -13,6 +13,7 @@ type App struct {
JwtSecret string `mapstructure:"jwt_secret" json:"jwt_secret" yaml:"jwt_secret"` JwtSecret string `mapstructure:"jwt_secret" json:"jwt_secret" yaml:"jwt_secret"`
JwtExpire int64 `mapstructure:"jwt_expire" json:"jwt_expire" yaml:"jwt_expire"` JwtExpire int64 `mapstructure:"jwt_expire" json:"jwt_expire" yaml:"jwt_expire"`
RunMode string `mapstructure:"run_mode" json:"run_mode" yaml:"run_mode"` RunMode string `mapstructure:"run_mode" json:"run_mode" yaml:"run_mode"`
DefaultContextTimeout int64 `mapstructure:"default_context_timeout" json:"default_context_timeout" yaml:"default_context_timeout"`
UploadSavePath string `mapstructure:"upload_save_path" json:"upload_save_path" yaml:"upload_save_path"` UploadSavePath string `mapstructure:"upload_save_path" json:"upload_save_path" yaml:"upload_save_path"`
UploadServerPath string `mapstructure:"upload_server_path" json:"upload_server_path" yaml:"upload_server_path"` UploadServerPath string `mapstructure:"upload_server_path" json:"upload_server_path" yaml:"upload_server_path"`
UploadImageMaxSize int64 `mapstructure:"upload_image_max_size" json:"upload_image_max_size" yaml:"upload_image_max_size"` UploadImageMaxSize int64 `mapstructure:"upload_image_max_size" json:"upload_image_max_size" yaml:"upload_image_max_size"`

View File

@ -1,7 +1,7 @@
/* /*
* @Date: 2021-03-22 09:54:07 * @Date: 2021-03-22 09:54:07
* @LastEditors: viletyy * @LastEditors: viletyy
* @LastEditTime: 2021-03-22 09:56:38 * @LastEditTime: 2021-06-14 20:40:49
* @FilePath: /potato/config/server.go * @FilePath: /potato/config/server.go
*/ */
package config package config
@ -10,4 +10,5 @@ type Server struct {
HttpPort int64 `mapstructure:"http_port" json:"http_port" yaml:"http_port"` HttpPort int64 `mapstructure:"http_port" json:"http_port" yaml:"http_port"`
ReadTimeout int64 `mapstructure:"read_timeout" json:"read_timeout" yaml:"read_timeout"` ReadTimeout int64 `mapstructure:"read_timeout" json:"read_timeout" yaml:"read_timeout"`
WriteTimeout int64 `mapstructure:"write_timeout" json:"write_timeout" yaml:"write_timeout"` WriteTimeout int64 `mapstructure:"write_timeout" json:"write_timeout" yaml:"write_timeout"`
TracerHostPort string `mapstructure:"tracer_host_port" json:"tracer_host_port" yaml:"tracer_host_port"`
} }

View File

@ -24,7 +24,7 @@ var doc = `{
"host": "{{.Host}}", "host": "{{.Host}}",
"basePath": "{{.BasePath}}", "basePath": "{{.BasePath}}",
"paths": { "paths": {
"/v1/auth": { "/auth": {
"post": { "post": {
"consumes": [ "consumes": [
"multipart/form-data" "multipart/form-data"

View File

@ -8,7 +8,7 @@
}, },
"basePath": "/api", "basePath": "/api",
"paths": { "paths": {
"/v1/auth": { "/auth": {
"post": { "post": {
"consumes": [ "consumes": [
"multipart/form-data" "multipart/form-data"

View File

@ -17,7 +17,7 @@ info:
title: Potato Api title: Potato Api
version: "1.0" version: "1.0"
paths: paths:
/v1/auth: /auth:
post: post:
consumes: consumes:
- multipart/form-data - multipart/form-data

View File

@ -1,7 +1,7 @@
/* /*
* @Date: 2021-03-22 09:42:09 * @Date: 2021-03-22 09:42:09
* @LastEditors: viletyy * @LastEditors: viletyy
* @LastEditTime: 2021-04-06 18:18:15 * @LastEditTime: 2021-06-13 23:53:51
* @FilePath: /potato/global/global.go * @FilePath: /potato/global/global.go
*/ */
package global package global
@ -9,6 +9,7 @@ package global
import ( import (
"github.com/go-redis/redis" "github.com/go-redis/redis"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
"github.com/opentracing/opentracing-go"
"github.com/spf13/viper" "github.com/spf13/viper"
"github.com/viletyy/potato/config" "github.com/viletyy/potato/config"
"go.uber.org/zap" "go.uber.org/zap"
@ -20,4 +21,5 @@ var (
GO_CONFIG *config.Config GO_CONFIG *config.Config
GO_VP *viper.Viper GO_VP *viper.Viper
GO_LOG *zap.Logger GO_LOG *zap.Logger
GO_TRACER opentracing.Tracer
) )

6
go.mod
View File

@ -3,8 +3,10 @@ module github.com/viletyy/potato
go 1.15 go 1.15
require ( require (
github.com/HdrHistogram/hdrhistogram-go v1.1.0 // indirect
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
github.com/dgrijalva/jwt-go v3.2.0+incompatible github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/eddycjy/opentracing-gorm v0.0.0-20200209122056-516a807d2182
github.com/fsnotify/fsnotify v1.4.9 github.com/fsnotify/fsnotify v1.4.9
github.com/gin-gonic/gin v1.7.2 github.com/gin-gonic/gin v1.7.2
github.com/go-playground/locales v0.13.0 github.com/go-playground/locales v0.13.0
@ -17,10 +19,14 @@ require (
github.com/lestrrat-go/strftime v1.0.4 // indirect github.com/lestrrat-go/strftime v1.0.4 // indirect
github.com/onsi/ginkgo v1.16.4 // indirect github.com/onsi/ginkgo v1.16.4 // indirect
github.com/onsi/gomega v1.13.0 // indirect github.com/onsi/gomega v1.13.0 // indirect
github.com/opentracing/opentracing-go v1.2.0
github.com/spf13/viper v1.7.1 github.com/spf13/viper v1.7.1
github.com/swaggo/gin-swagger v1.3.0 github.com/swaggo/gin-swagger v1.3.0
github.com/swaggo/swag v1.7.0 github.com/swaggo/swag v1.7.0
github.com/uber/jaeger-client-go v2.22.1+incompatible
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
github.com/viletyy/yolk v1.0.1 github.com/viletyy/yolk v1.0.1
go.uber.org/atomic v1.8.0 // indirect
go.uber.org/zap v1.17.0 go.uber.org/zap v1.17.0
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df

View File

@ -1,7 +1,7 @@
/* /*
* @Date: 2021-03-22 10:12:38 * @Date: 2021-03-22 10:12:38
* @LastEditors: viletyy * @LastEditors: viletyy
* @LastEditTime: 2021-06-10 21:53:31 * @LastEditTime: 2021-06-14 21:11:29
* @FilePath: /potato/initialize/gorm.go * @FilePath: /potato/initialize/gorm.go
*/ */
package initialize package initialize
@ -9,6 +9,7 @@ package initialize
import ( import (
"fmt" "fmt"
otgorm "github.com/eddycjy/opentracing-gorm"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql" _ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/postgres" _ "github.com/jinzhu/gorm/dialects/postgres"
@ -68,4 +69,7 @@ func GormSet(db *gorm.DB) {
// 设置打开数据库连接的最大数量 // 设置打开数据库连接的最大数量
db.DB().SetMaxOpenConns(100) db.DB().SetMaxOpenConns(100)
// 设置链路追踪
otgorm.AddGormCallbacks(db)
} }

25
initialize/tracer.go Normal file
View File

@ -0,0 +1,25 @@
/*
* @Date: 2021-06-13 23:25:52
* @LastEditors: viletyy
* @LastEditTime: 2021-06-14 20:42:40
* @FilePath: /potato/initialize/tracer.go
*/
package initialize
import (
"github.com/opentracing/opentracing-go"
"github.com/viletyy/potato/global"
"github.com/viletyy/potato/pkg/tracer"
)
func Tracer() opentracing.Tracer {
jaegerTracer, _, err := tracer.NewJaegerTracer(
global.GO_CONFIG.App.Name,
global.GO_CONFIG.Server.TracerHostPort,
)
if err != nil {
global.GO_LOG.Sugar().Errorf("tracer.NewJaegerTracer err: %v", err)
}
return jaegerTracer
}

View File

@ -1,7 +1,7 @@
/* /*
* @Date: 2021-06-10 18:58:25 * @Date: 2021-06-10 18:58:25
* @LastEditors: viletyy * @LastEditors: viletyy
* @LastEditTime: 2021-06-13 22:41:01 * @LastEditTime: 2021-06-14 21:02:33
* @FilePath: /potato/internal/controller/api/auth.go * @FilePath: /potato/internal/controller/api/auth.go
*/ */
package api package api

View File

@ -1,7 +1,7 @@
/* /*
* @Date: 2021-06-12 21:55:50 * @Date: 2021-06-12 21:55:50
* @LastEditors: viletyy * @LastEditors: viletyy
* @LastEditTime: 2021-06-12 22:22:31 * @LastEditTime: 2021-06-14 21:00:47
* @FilePath: /potato/internal/middleware/access_log.go * @FilePath: /potato/internal/middleware/access_log.go
*/ */
package middleware package middleware
@ -40,6 +40,8 @@ func AccessLog() gin.HandlerFunc {
global.GO_LOG.With( global.GO_LOG.With(
zap.String("request", c.Request.PostForm.Encode()), zap.String("request", c.Request.PostForm.Encode()),
zap.String("response", bodyWriter.body.String()), zap.String("response", bodyWriter.body.String()),
zap.String("trace_id", c.GetString("X-Trace-ID")),
zap.String("span_id", c.GetString("X-Span-ID")),
).Sugar().Infof("access log: method: %s, status_code: %d, begin_time: %d, end_time: %d", ).Sugar().Infof("access log: method: %s, status_code: %d, begin_time: %d, end_time: %d",
c.Request.Method, c.Request.Method,
bodyWriter.Status(), bodyWriter.Status(),

View File

@ -0,0 +1,57 @@
/*
* @Date: 2021-06-13 23:33:05
* @LastEditors: viletyy
* @LastEditTime: 2021-06-14 20:45:47
* @FilePath: /potato/internal/middleware/tracer.go
*/
package middleware
import (
"context"
"github.com/gin-gonic/gin"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/uber/jaeger-client-go"
"github.com/viletyy/potato/global"
)
func Tracing() gin.HandlerFunc {
return func(c *gin.Context) {
var newCtx context.Context
var span opentracing.Span
spanCtx, err := opentracing.GlobalTracer().Extract(
opentracing.HTTPHeaders,
opentracing.HTTPHeadersCarrier(c.Request.Header),
)
if err != nil {
span, newCtx = opentracing.StartSpanFromContextWithTracer(
c.Request.Context(),
global.GO_TRACER,
c.Request.URL.Path,
)
} else {
span, newCtx = opentracing.StartSpanFromContextWithTracer(
c.Request.Context(),
global.GO_TRACER,
c.Request.URL.Path,
opentracing.ChildOf(spanCtx),
opentracing.Tag{Key: string(ext.Component), Value: "HTTP"},
)
}
defer span.Finish()
var traceID string
var spanID string
var spanContext = span.Context()
switch spanContext.(type) {
case jaeger.SpanContext:
jaegerContext := spanContext.(jaeger.SpanContext)
traceID = jaegerContext.TraceID().String()
spanID = jaegerContext.SpanID().String()
}
c.Set("X-Trace-ID", traceID)
c.Set("X-Span-ID", spanID)
c.Request = c.Request.WithContext(newCtx)
c.Next()
}
}

View File

@ -1,7 +1,7 @@
/* /*
* @Date: 2021-03-21 19:54:57 * @Date: 2021-03-21 19:54:57
* @LastEditors: viletyy * @LastEditors: viletyy
* @LastEditTime: 2021-06-13 22:46:46 * @LastEditTime: 2021-06-13 23:39:38
* @FilePath: /potato/internal/routers/router.go * @FilePath: /potato/internal/routers/router.go
*/ */
package routers package routers
@ -46,9 +46,10 @@ func InitRouter() *gin.Engine {
} }
Engine.Use(middleware.AppInfo()) // 设置app信息 Engine.Use(middleware.AppInfo()) // 设置app信息
Engine.Use(middleware.RateLimiter(methodLimiters)) // 设置限流控制 Engine.Use(middleware.RateLimiter(methodLimiters)) // 设置限流控制
Engine.Use(middleware.ContextTimeout(60 * time.Second)) // 设置统一超时控制 Engine.Use(middleware.ContextTimeout(time.Duration(global.GO_CONFIG.App.DefaultContextTimeout))) // 设置统一超时控制
Engine.Use(middleware.Translations()) // 设置自定义验证 Engine.Use(middleware.Translations()) // 设置自定义验证
Engine.Use(middleware.CORS()) // 设置跨域 Engine.Use(middleware.CORS()) // 设置跨域
Engine.Use(middleware.Tracing())
Engine.StaticFS("/static", http.Dir(global.GO_CONFIG.App.UploadSavePath)) Engine.StaticFS("/static", http.Dir(global.GO_CONFIG.App.UploadSavePath))

View File

@ -1,7 +1,7 @@
/* /*
* @Date: 2021-06-10 18:51:48 * @Date: 2021-06-10 18:51:48
* @LastEditors: viletyy * @LastEditors: viletyy
* @LastEditTime: 2021-06-10 18:53:10 * @LastEditTime: 2021-06-14 21:13:01
* @FilePath: /potato/internal/service/service.go * @FilePath: /potato/internal/service/service.go
*/ */
package service package service
@ -9,6 +9,7 @@ package service
import ( import (
"context" "context"
otgorm "github.com/eddycjy/opentracing-gorm"
"github.com/viletyy/potato/global" "github.com/viletyy/potato/global"
"github.com/viletyy/potato/internal/dao" "github.com/viletyy/potato/internal/dao"
) )
@ -20,7 +21,7 @@ type Service struct {
func New(ctx context.Context) Service { func New(ctx context.Context) Service {
svc := Service{Ctx: ctx} svc := Service{Ctx: ctx}
svc.dao = dao.New(global.GO_DB) svc.dao = dao.New(otgorm.WithContext(svc.Ctx, global.GO_DB))
return svc return svc
} }

View File

@ -1,7 +1,7 @@
/* /*
* @Date: 2021-03-21 19:54:57 * @Date: 2021-03-21 19:54:57
* @LastEditors: viletyy * @LastEditors: viletyy
* @LastEditTime: 2021-06-10 10:28:27 * @LastEditTime: 2021-06-13 23:32:06
* @FilePath: /potato/main.go * @FilePath: /potato/main.go
*/ */
package main package main
@ -21,6 +21,7 @@ func main() {
global.GO_LOG = initialize.Zap() global.GO_LOG = initialize.Zap()
global.GO_DB = initialize.Gorm() global.GO_DB = initialize.Gorm()
global.GO_REDIS = initialize.Redis() global.GO_REDIS = initialize.Redis()
global.GO_TRACER = initialize.Tracer()
defer global.GO_DB.Close() defer global.GO_DB.Close()
defer global.GO_REDIS.Close() defer global.GO_REDIS.Close()

37
pkg/tracer/tracer.go Normal file
View File

@ -0,0 +1,37 @@
/*
* @Date: 2021-06-13 23:16:55
* @LastEditors: viletyy
* @LastEditTime: 2021-06-13 23:24:34
* @FilePath: /potato/pkg/tracer/tracer.go
*/
package tracer
import (
"io"
"time"
opentracing "github.com/opentracing/opentracing-go"
"github.com/uber/jaeger-client-go/config"
)
func NewJaegerTracer(serviceName, agentHostPort string) (opentracing.Tracer, io.Closer, error) {
// 设置应用的基本信息
cfg := &config.Configuration{
ServiceName: serviceName,
Sampler: &config.SamplerConfig{
Type: "const",
Param: 1,
}, // 固定采样、对所有数据都进行采样
Reporter: &config.ReporterConfig{
LogSpans: true,
BufferFlushInterval: 1 * time.Second,
LocalAgentHostPort: agentHostPort,
}, // 是否启用LoggingReporter、刷新缓冲区都频率、上报的Agent地址
}
tracer, closer, err := cfg.NewTracer()
if err != nil {
return nil, nil, err
}
opentracing.SetGlobalTracer(tracer)
return tracer, closer, nil
}