https://github.com/bose/go-gin-opentracing
Gin Web Framework Open Tracing middleware
https://github.com/bose/go-gin-opentracing
gin gin-gonic gin-middleware go middleware opentracing trace
Last synced: 4 months ago
JSON representation
Gin Web Framework Open Tracing middleware
- Host: GitHub
- URL: https://github.com/bose/go-gin-opentracing
- Owner: Bose
- License: other
- Created: 2018-06-20T17:43:37.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2021-05-25T14:53:37.000Z (over 4 years ago)
- Last Synced: 2025-04-12T00:16:49.926Z (6 months ago)
- Topics: gin, gin-gonic, gin-middleware, go, middleware, opentracing, trace
- Language: Go
- Size: 41 KB
- Stars: 37
- Watchers: 6
- Forks: 9
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-gin-opentracing
[](https://godoc.org/github.com/Bose/go-gin-opentracing)
[](https://goreportcard.com/report/github.com/Bose/go-gin-opentracing)
[](https://github.com/Bose/go-gin-opentracing/releases)OpenTracing middleware from the [Gin http framework](https://github.com/gin-gonic/gin).
Based on [opentracing-go](https://github.com/opentracing/opentracing-go), this middleware adds an OpenTracing span for every http request, using the inbound requestID (if one exists) and adds the request span to the gin.Context as the key: `tracing-context`.
## Required Reading
In order to understand the Go platform API, one must first be familiar with the [OpenTracing project](https://opentracing.io/) and [terminology](https://opentracing.io/specification/) more specifically.## Installation
`$ go get github.com/Bose/go-gin-opentracing`
## Usage as middleware
```go
package mainimport (
"fmt"
"os"ginopentracing "github.com/Bose/go-gin-opentracing"
"github.com/gin-gonic/gin"
"github.com/opentracing/opentracing-go"
)func main() {
r := gin.Default()
hostName, err := os.Hostname()
if err != nil {
hostName = "unknown"
}
// initialize the global singleton for tracing...
tracer, reporter, closer, err := ginopentracing.InitTracing(fmt.Sprintf("go-gin-opentracing-example::%s", hostName), "localhost:5775", ginopentracing.WithEnableInfoLog(true))
if err != nil {
panic("unable to init tracing")
}
defer closer.Close()
defer reporter.Close()
opentracing.SetGlobalTracer(tracer)// create the middleware
p := ginopentracing.OpenTracer([]byte("api-request-"))// tell gin to use the middleware
r.Use(p)r.GET("/", func(c *gin.Context) {
c.JSON(200, "Hello world!")
})r.Run(":29090")
}```
See the [example.go file](https://github.com/Bose/go-gin-opentracing/blob/master/example/example.go)
## Usage within gin.Handler
Within the gin Handler you can access this request span and use it to create additional spans, and/or add span tags.
```go
func HelloWorld(c *gin.Context) {
if cspan, ok := c.Get("tracing-context"); ok {
span = tracing.StartSpanWithParent(cspan.(opentracing.Span).Context(), "helloword", c.Request.Method, c.Request.URL.Path)
} else {
span = tracing.StartSpanWithHeader(&c.Request.Header, "helloworld", c.Request.Method, c.Request.URL.Path)
}
defer span.Finish()
c.String(200, "Hello.")
}
```