https://github.com/djcass44/go-tracer
Simple library for request tracing between GoLang services
https://github.com/djcass44/go-tracer
golang golang-library hacktoberfest http tracing
Last synced: about 1 year ago
JSON representation
Simple library for request tracing between GoLang services
- Host: GitHub
- URL: https://github.com/djcass44/go-tracer
- Owner: djcass44
- License: apache-2.0
- Created: 2020-10-05T10:18:09.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-02-25T07:53:24.000Z (about 3 years ago)
- Last Synced: 2023-07-27T16:00:12.072Z (over 2 years ago)
- Topics: golang, golang-library, hacktoberfest, http, tracing
- Language: Go
- Homepage:
- Size: 26.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Go Tracer
This library provides a method for simplifying request tracing and correlation between GoLang services.
#### What it does
* Extracts the `X-Request-ID` value or creates a new ID from a UUIDv4
* Adds an entry to the `http.Request` context
* Injects the `X-Request-ID` header to outgoing HTTP requests
This allows you to 'follow' requests as they move between various services as they will all have the same ID.
#### Getting started
Have a look at [example.go](example.go) for a simple example.
```shell script
go get github.com/djcass44/go-tracer
```
Using handler functions
```go
http.HandleFunc("/something", tracer.NewFunc(myFunc))
```
Using handlers
```go
type myHandler struct {}
func (mh myHandler) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("Hello World!"))
}
// later on
http.Handle("/something", tracer.NewHandler(myHandler{}))
```
**Accessing the RequestID**
The ID can be pulled from the context directly, or using a helper function
Helper:
```go
requestId := tracer.GetRequestId(request)
```
Direct:
```go
requestId := request.Context().Value(tracer.ContextKeyID).(string)
```
**Enabling outgoing RequestID injection**
This needs to be configured for all instances of `http.Client`
*Note: this will replace the `http.Transport` of the client*
```go
// enable outgoing-tracing for the default http client
tracer.Apply(http.DefaultClient)
var myClient = &http.Client{}
tracer.Apply(myClient)
```
#### Contribution
Contributions are more than welcome! Feel free to open a PR, or an issue if you have questions.