commit
65facb6015
@ -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
|
@ -0,0 +1,7 @@
|
|||||||
|
.env
|
||||||
|
.git
|
||||||
|
.svn
|
||||||
|
.idea
|
||||||
|
.vscode
|
||||||
|
.log
|
||||||
|
gomod.sh
|
@ -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.
|
@ -0,0 +1,17 @@
|
|||||||
|
<h1>
|
||||||
|
<a href="https://www.dtapp.net/">Golang TraceId</a>
|
||||||
|
</h1>
|
||||||
|
|
||||||
|
📦 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
|
||||||
|
```
|
@ -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"))
|
||||||
|
}
|
@ -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()
|
||||||
|
}
|
@ -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"))
|
||||||
|
}
|
@ -0,0 +1 @@
|
|||||||
|
package gotrace_id
|
Loading…
Reference in new issue