Categorygithub.com/bar-counter/gin-correlation-id

# README

golang-ci license go mod version GoDoc GoReportCard codecov github release

for what

Middleware for reading or generating correlation IDs for each incoming request.

Correlation IDs can then be added to your logs, making it simple to retrieve all logs generated from a single HTTP request.

The middleware checks for the x-request-id header by default, but can be set to any key,like x-correlation-id

Contributing

Contributor Covenant GitHub contributors

We welcome community contributions to this project.

Please read Contributor Guide for more information on how to get started.

depends

in go mod project

# before above global settings
# test version info
$ git ls-remote -q https://github.com/bar-counter/gin-correlation-id.git

# test depends see full version
$ go list -mod readonly -v -m -versions github.com/bar-counter/gin-correlation-id
# or use last version add go.mod by script
$ echo "go mod edit -require=$(go list -mod=readonly -m -versions github.com/bar-counter/gin-correlation-id | awk '{print $1 "@" $NF}')"
$ echo "go mod vendor"

evn

  • minimum go version: go 1.18
  • change go 1.18, ^1.18, 1.18.10 to new go version

libs

libversion
https://github.com/stretchr/testifyv1.8.4
https://github.com/gin-gonic/ginv1.9.1
https://github.com/gofrs/uuid/v5v5.0.0
https://github.com/bwmarrin/snowflakev0.3.0
https://github.com/lithammer/shortuuid/4v4.0.0

Feature

Performance

platformarchmethodtimesns/opB/opallocs/opcpu
linuxamd64Benchmark_gin_correlation_id_uuidv4-21670297124 ns/op2320 B/op32 allocs/opIntel(R) Xeon(R) CPU E5-2673 v4 @ 2.30GHz
linuxamd64BenchmarkParallel_gin_correlation_id_uuidv4-22180675572 ns/op2320 B/op32 allocs/opIntel(R) Xeon(R) CPU E5-2673 v4 @ 2.30GHz
linuxamd64BenchmarkParallel_gin_correlation_id_snowflake-22602054920 ns/op2256 B/op31 allocs/opIntel(R) Xeon(R) CPU E5-2673 v4 @ 2.30GHz
linuxamd64Benchmark_gin_correlation_id_snowflake-21994225922 ns/op2256 B/op31 allocs/opIntel(R) Xeon(R) CPU E5-2673 v4 @ 2.30GHz
linuxamd64BenchmarkGinIdShortUuid-27337115670 ns/op4891 B/op128 allocs/opIntel(R) Xeon(R) CPU E5-2673 v4 @ 2.30GHz
linuxamd64BenchmarkParallelGinIdShortUuid-210329610477 ns/op4891 B/op128 allocs/opIntel(R) Xeon(R) CPU E5-2673 v4 @ 2.30GHz
windowsamd64Benchmark_gin_correlation_id_uuidv4-22156405563 ns/op2344 B/op33 allocs/opIntel(R) Xeon(R) Platinum 8272CL CPU @ 2.60GHz
windowsamd64BenchmarkParallel_gin_correlation_id_uuidv4-22912743558 ns/op2344 B/op33 allocs/opIntel(R) Xeon(R) Platinum 8272CL CPU @ 2.60GHz
windowsamd64Benchmark_gin_correlation_id_snowflake-22281925059 ns/op2256 B/op31 allocs/opIntel(R) Xeon(R) Platinum 8272CL CPU @ 2.60GHz
windowsamd64BenchmarkParallel_gin_correlation_id_snowflake-23473943327 ns/op2256 B/op31 allocs/opIntel(R) Xeon(R) Platinum 8272CL CPU @ 2.60GHz
windowsamd64BenchmarkGinIdShortUuid-29555612331 ns/op4915 B/op129 allocs/opIntel(R) Xeon(R) Platinum 8272CL CPU @ 2.60GHz
windowsamd64BenchmarkParallelGinIdShortUuid-21535238331 ns/op4915 B/op129 allocs/opIntel(R) Xeon(R) Platinum 8272CL CPU @ 2.60GHz
darwinarm64Benchmark_gin_correlation_id_uuidv4-24403732712 ns/op2321 B/op32 allocs/opapple silicon M1 Max core 10
darwinarm64BenchmarkParallel_gin_correlation_id_uuidv4-105131222296 ns/op2324 B/op32 allocs/opapple silicon M1 Max core 10
darwinarm64Benchmark_gin_correlation_id_snowflake-25707522095 ns/op2257 B/op31 allocs/opapple silicon M1 Max core 10
darwinarm64BenchmarkParallel_gin_correlation_id_snowflake-105571272213 ns/op2260 B/op31 allocs/opapple silicon M1 Max core 10
darwinarm64BenchmarkGinIdShortUuid-22027865920 ns/op4893 B/op128 allocs/opapple silicon M1 Max core 10
darwinarm64BenchmarkParallelGinIdShortUuid-103302343614 ns/op4898 B/op128 allocs/opapple silicon M1 Max core 10
  • ns/op is the average time per operation, lower latency better performance
  • B/op is the number of bytes allocated per operation, memory allocation means getting more memory from the operating system, less occupied better
  • allocs/op is the number of allocations per operation, memory allocation means getting more memory from the operating system, less occupied better

usage

sample uuid-v4

package main

import (
	"github.com/bar-counter/gin-correlation-id/gin_correlation_id_uuidv4"
	"github.com/gin-gonic/gin"
	"net/http"
	"log"
	"fmt"
)

func ginPingRouter() *gin.Engine {
	router := gin.New()

	// add Middleware
	router.Use(gin_correlation_id_uuidv4.Middleware())

	router.GET("/ping", func(c *gin.Context) {
		c.String(http.StatusOK, "pong as correlation ID: %s", gin_correlation_id_uuidv4.GetCorrelationID(c))
	})
	return router
}

func main() {
	// Create the Gin engine. 
	g := ginPingRouter()
	serverPort := "49002"
	log.Printf("=> now server access as: http://127.0.0.1:%s", serverPort)
	err := http.ListenAndServe(fmt.Sprintf(":%v", serverPort), g)
	if err != nil {
		fmt.Printf("run gin server err %v\n", err)
		return
	}
}

integrationTest

  • run server then test as curl
$ curl -v http://127.0.0.1:49002/ping
*   Trying 127.0.0.1:49002...
* Connected to 127.0.0.1 (127.0.0.1) port 49002 (#0)
> GET /ping HTTP/1.1
> Host: 127.0.0.1:49002
> User-Agent: curl/7.80.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Access-Control-Expose-Headers: x-request-id
< Content-Type: text/plain; charset=utf-8
< X-Request-Id: 0b737fda-5439-487b-9f81-a7824b88532e
< Date: Sun, 18 Jun 2023 04:20:27 GMT
< Content-Length: 60
<
* Connection #0 to host 127.0.0.1 left intact
pong as correlation ID: 0b737fda-5439-487b-9f81-a7824b88532e
  1. response header has X-Request-Id key as correlation ID
  2. can get correlation ID as
    // use uuid v4
gin_correlation_id_uuidv4.GetCorrelationID(*gin.Context)

// use snowflake
gin_correlation_id_snowflake.GetCorrelationID(*gin.Context)

// use shortuuid
gin_correlation_id_shortuuid.GetCorrelationID(*gin.Context)

CORS

If you are using cross-origin resource sharing (CORS)

e.g. you are making requests to an API from a frontend JavaScript code served from a different origin, you have to ensure two things:

  • permit correlation ID header in the incoming requests' HTTP headers so the value can be reused by the middleware,
  • add the correlation ID header to the allowlist in responses' HTTP headers so it can be accessed by the browser.

have to include the Access-Control-Allow-Origin and Access-Control-Expose-Headers

dev

# It needs to be executed after the first use or update of dependencies.
$ make init dep
  • test code
$ make test testBenchmark

add main.go file and run

# run at env dev use cmd/main.go
$ make dev
  • ci to fast check
# check style at local
$ make style

# run ci at local
$ make ci

docker

# then test build as test/Dockerfile
$ make dockerTestRestartLatest
# clean test build
$ make dockerTestPruneLatest

# more info see
$ make helpDocker

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author