Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/kataras/requestid

Unique Identifier for each HTTP request
https://github.com/kataras/requestid

go golang http iris requestid unique-identifier

Last synced: 2 months ago
JSON representation

Unique Identifier for each HTTP request

Awesome Lists containing this project

README

        

# Request ID

[![build status](https://img.shields.io/github/actions/workflow/status/kataras/requestid/ci.yml?style=for-the-badge)](https://github.com/kataras/requestid/actions) [![report card](https://img.shields.io/badge/report%20card-a%2B-ff3333.svg?style=for-the-badge)](https://goreportcard.com/report/github.com/kataras/requestid) [![godocs](https://img.shields.io/badge/go-%20docs-488AC7.svg?style=for-the-badge)](https://godoc.org/github.com/kataras/requestid)

Unique Identifier for each HTTP request. Useful for logging, propagation and e.t.c.

## Installation

The only requirement is the [Go Programming Language](https://golang.org/dl).

```sh
$ go get github.com/kataras/requestid
```

## Getting Started

Import the package:

```go
package main

import "github.com/kataras/requestid"
```

Wrap a handler with the `Handler` function and retrieve the request ID using the `Get` function:

```go
import "net/http"

func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request){
id:= requestid.Get(r)
w.Write([]byte(id))
})

http.ListenAndServe(":8080", requestid.Handler(mux))
}
```

By-default the `requestid` middleware uses the `X-Request-Id` header to extract and set the request ID.
It generates a [universally unique identifier](https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_(random)) when the request header is missing. Use custom logic to extract and set the request ID using `HandlerWithGenerator`:

```go
import "net/http"

func main() {
// extract from a request header and set to the response header.
gen := func(w http.ResponseWriter, r *http.Request) string {
id:= r.Header.Get("X-Custom-Id")
if id == "" {
// [custom logic to generate ID...]
}
w.Header().Set("X-Custom-Id", id)
return id
}

// [...]
router := requestid.HandlerWithGenerator(mux, gen)
http.ListenAndServe(":8080", router)
}
```

When you want an identifier of request based on the headers, body and e.t.c. use the `HashGenerator` helper. Note that, the request id will be the same if the same client sends the same requests (that's the goal here):

```go
func main() {
// [...]

includeBodyOnHash := false
gen := requestid.HashGenerator(includeBodyOnHash)

requestid.HandlerWithGenerator(mux, gen)

// [...]
}
```

## License

This software is licensed under the [MIT License](LICENSE).