Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gin-contrib/opengintracing
trace requests using opentracing specification
https://github.com/gin-contrib/opengintracing
gin gin-contrib golang middleware
Last synced: 5 days ago
JSON representation
trace requests using opentracing specification
- Host: GitHub
- URL: https://github.com/gin-contrib/opengintracing
- Owner: gin-contrib
- License: mit
- Created: 2018-01-28T05:59:22.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2024-12-29T01:39:02.000Z (about 1 month ago)
- Last Synced: 2025-01-19T02:25:51.217Z (12 days ago)
- Topics: gin, gin-contrib, golang, middleware
- Language: Go
- Size: 91.8 KB
- Stars: 95
- Watchers: 7
- Forks: 8
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# tracing
[![Run Tests](https://github.com/gin-contrib/opengintracing/actions/workflows/go.yml/badge.svg)](https://github.com/gin-contrib/opengintracing/actions/workflows/go.yml)
[![Go Report Card](https://goreportcard.com/badge/github.com/gin-contrib/opengintracing)](https://goreportcard.com/report/github.com/gin-contrib/opengintracing)
[![GoDoc](https://godoc.org/github.com/gin-contrib/opengintracing?status.png)](https://pkg.go.dev/github.com/gin-contrib/opengintracing)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)trace requests using opentracing specification, Download:
```bash
go get -u github.com/gin-contrib/opengintracing
```See [opentracing/opentracing-go](https://github.com/opentracing/opentracing-go) for more information.
## Usage
For example you have architecture like this
![Example architecture](images/example_architecture.png)
To start requests tracing you have to:
* On "API Gateway": start span, inject headers and pass it to services
```go
package main
import (
...
"github.com/gin-gonic/gin"
"github.com/gin-contrib/opengintracing"
"github.com/opentracing/opentracing-go"
...
)var trace = /* setup tracer */
func main() {
...
app := gin.Default()app.POST("/service1",
opengintracing.NewSpan(trace, "forward to service 1"),
opengintracing.InjectToHeaders(trace, true),
service1handler)
app.POST("/service2",
opengintracing.NewSpan(trace, "forward to service 2"),
opengintracing.InjectToHeaders(trace, true),
service2handler)
...
}
```* On "Service 1", "Service 2" start span inherited from "API Gateway"`s span
```go
package main
import (
...
"github.com/gin-gonic/gin"
"github.com/gin-contrib/opengintracing"
"github.com/opentracing/opentracing-go"
...
)var trace = /* setup tracer */
func main() {
...
refFunc := opentracing.FollowsFrom
app := gin.Default()app.POST("",
opengintracing.SpanFromHeaders(trace, "operation", refFunc, true),
// don`t forget to inject if you want continue tracing in other service
opengintracing.InjectToHeaders(trace, true),
handler)
...
}
```Also don`t forget to forward headers from "Service 1" to "Service 3"
* On "Service 3" injecting to headers is not required
```go
package main
import (
...
"github.com/gin-gonic/gin"
"github.com/gin-contrib/opengintracing"
"github.com/opentracing/opentracing-go"
...
)var trace = /* setup tracer */
func main() {
...
refFunc := opentracing.ChildOf
app := gin.Default()app.POST("",
opengintracing.SpanFromHeaders(trace, "operation", refFunc, true),
handler)
...
}
```## TODO
* [x] add code sample
* [ ] maybe add sample with SpanFromContext
* [ ] add buildable example (needed simple logging tracer)