From 65facb60150d12ce84a7954486bc511e6d4f53e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Sat, 13 Aug 2022 09:33:54 +0800 Subject: [PATCH] - init --- .drone.yml | 17 +++++++++++++++++ .gitignore | 7 +++++++ LICENSE | 21 +++++++++++++++++++++ README.md | 17 +++++++++++++++++ const.go | 3 +++ context.go | 23 +++++++++++++++++++++++ context_test.go | 16 ++++++++++++++++ gin_use.go | 25 +++++++++++++++++++++++++ gin_use_test.go | 1 + go.mod | 3 +++ 10 files changed, 133 insertions(+) create mode 100644 .drone.yml create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 const.go create mode 100644 context.go create mode 100644 context_test.go create mode 100644 gin_use.go create mode 100644 gin_use_test.go create mode 100644 go.mod diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..c56c479 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,17 @@ +kind: pipeline +type: docker +name: clone + +steps: + - name: Test + image: golang:1.18 + commands: + - go env -w GO111MODULE=on + - go env -w GOPROXY=https://goproxy.cn,direct + - go test -v ./... + - name: Benchmark + image: golang:1.18 + commands: + - go env -w GO111MODULE=on + - go env -w GOPROXY=https://goproxy.cn,direct + - go test -bench=. -benchmem \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2ee9f26 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +.env +.git +.svn +.idea +.vscode +.log +gomod.sh \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ff84bbf --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2018 李光春 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..8bd2588 --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +

+Golang TraceId +

+ +📦 Golang TraceId + +[comment]: <> (go) +[![godoc](https://pkg.go.dev/badge/go.dtapp.net/gotrace_id?status.svg)](https://pkg.go.dev/go.dtapp.net/gotrace_id) +[![goproxy.cn](https://goproxy.cn/stats/go.dtapp.net/gotrace_id/badges/download-count.svg)](https://goproxy.cn/stats/go.dtapp.net/gotrace_id) +[![goreportcard.com](https://goreportcard.com/badge/go.dtapp.net/gotrace_id)](https://goreportcard.com/report/go.dtapp.net/gotrace_id) +[![deps.dev](https://img.shields.io/badge/deps-go-red.svg)](https://deps.dev/go/go.dtapp.net%2Fgotrace_id) + +#### 安装 + +```go +go get -v -u go.dtapp.net/gotrace_id +``` diff --git a/const.go b/const.go new file mode 100644 index 0000000..8ee3b7d --- /dev/null +++ b/const.go @@ -0,0 +1,3 @@ +package gotrace_id + +const Version = "1.0.0" diff --git a/context.go b/context.go new file mode 100644 index 0000000..e75ef0d --- /dev/null +++ b/context.go @@ -0,0 +1,23 @@ +package gotrace_id + +import ( + "context" + "fmt" + "github.com/gin-gonic/gin" + "go.dtapp.net/gostring" +) + +// CustomTraceIdContext 自定义设置跟踪编号上下文 +func CustomTraceIdContext() context.Context { + return context.WithValue(context.Background(), "trace_id", gostring.GetUuId()) +} + +// SetGinTraceIdContext 设置跟踪编号上下文 +func SetGinTraceIdContext(c *gin.Context) context.Context { + return context.WithValue(context.Background(), "trace_id", GetGinTraceId(c)) +} + +// GetTraceIdContext 通过上下文获取跟踪编号 +func GetTraceIdContext(ctx context.Context) string { + return fmt.Sprintf("%s", ctx.Value("trace_id")) +} diff --git a/context_test.go b/context_test.go new file mode 100644 index 0000000..c38c1da --- /dev/null +++ b/context_test.go @@ -0,0 +1,16 @@ +package gotrace_id + +import ( + "context" + "testing" +) + +func TestContext(t *testing.T) { + + ctx := context.Background() + t.Log(ctx) + + ctx = context.WithValue(ctx, "k1", "测试") + + t.Log() +} diff --git a/gin_use.go b/gin_use.go new file mode 100644 index 0000000..95d222d --- /dev/null +++ b/gin_use.go @@ -0,0 +1,25 @@ +package gotrace_id + +import ( + "fmt" + "github.com/gin-gonic/gin" + "go.dtapp.net/gostring" +) + +// SetGinTraceId 设置跟踪编号 https://www.jianshu.com/p/2a1a74ad3c3a +func SetGinTraceId() gin.HandlerFunc { + return func(c *gin.Context) { + requestId := c.Request.Header.Get("X-Request-Id") + if requestId == "" { + requestId = gostring.GetUuId() + } + c.Set("trace_id", requestId) + c.Writer.Header().Set("X-Request-Id", requestId) + c.Next() + } +} + +// GetGinTraceId 通过gin中间件获取跟踪编号 +func GetGinTraceId(c *gin.Context) string { + return fmt.Sprintf("%s", c.MustGet("trace_id")) +} diff --git a/gin_use_test.go b/gin_use_test.go new file mode 100644 index 0000000..35a44ee --- /dev/null +++ b/gin_use_test.go @@ -0,0 +1 @@ +package gotrace_id diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..95a30d4 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module go.dtapp.net/gotrace_id + +go 1.19