https://github.com/dmytrohridin/correlation-id
Golang http handler that can be used as middleware to generate correlation id
https://github.com/dmytrohridin/correlation-id
correlation golang microservices
Last synced: 5 months ago
JSON representation
Golang http handler that can be used as middleware to generate correlation id
- Host: GitHub
- URL: https://github.com/dmytrohridin/correlation-id
- Owner: dmytrohridin
- License: mit
- Created: 2022-04-17T14:50:35.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2022-04-28T21:07:10.000Z (about 4 years ago)
- Last Synced: 2024-06-21T19:21:50.798Z (almost 2 years ago)
- Topics: correlation, golang, microservices
- Language: Go
- Homepage:
- Size: 6.84 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Correlation Id [](https://github.com/dmytrohridin/correlation-id/actions) [](https://godoc.org/github.com/dmytrohridin/correlation-id)
Correlations Id is used in distributed applications to trace requests across multiple services. This package provides a lightweight correlation id middlware. Request headers are checked for a correlation id. If found or generated, this correlation id is attached to the request context which can be used to access the current correlation id where it is required for logging etc. Based on middleware settings correlation id can be returned with response headers.
## Install and update
`go get -u github.com/dmytrohridin/correlation-id`
## Get Correlation Id value
To get correlation id value use `FromContext` function.
```go
id := correlationid.FromContext(req.Context())
```
## HeaderName setting
By default `Correlation-Id` key used as header name. Behavior can be overridden.
```go
middleware := correlationid.New()
middleware.HeaderName = "Request-Id"
```
## IdGenerator setting
By default [google/uuid package](https://github.com/google/uuid) is used for correlation id when `Correlation-Id` header is not included in request headers. Behavior can be overridden by applying a custom function.
```go
middleware := correlationid.New()
middleware.IdGenerator = func() string {
return "any_id"
}
```
## IncludeInResponse setting
By default `Correlation-Id` is sent in response header. Behavior can be overridden.
```go
middleware := correlationid.New()
middleware.IncludeInResponse = false
```
## EnforceHeader setting
By default `Correlation-Id` is not required in request headers. Behavior can be overridden.
```go
middleware := correlationid.New()
middleware.EnforceHeader = true
```
If header is enforced and client does not send it - `BadRequest` will be returned.