Giter VIP home page Giter VIP logo

requestid's Introduction

Request ID

build status report card godocs

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

Installation

The only requirement is the Go Programming Language.

$ go get github.com/kataras/requestid

Getting Started

Import the package:

package main

import "github.com/kataras/requestid"

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

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 when the request header is missing. Use custom logic to extract and set the request ID using HandlerWithGenerator:

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):

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

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

    requestid.HandlerWithGenerator(mux, gen)

    // [...]
}

License

This software is licensed under the MIT License.

requestid's People

Contributors

kataras avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.