Skip to content

Latest commit

 

History

History
104 lines (76 loc) · 2.17 KB

README.md

File metadata and controls

104 lines (76 loc) · 2.17 KB

gojjid GoDoc

gojiid provides a customizable middleware to provide and retrieve a request id string via the http Context

Example

Using the default generator

package main

import(
	"github.com/atlassian/gojiid"
	"github.com/goji/glogrus"
	"goji.io"
	"golang.org/x/net/context"
	"net/http"
)

func main() {
	router := goji.NewMux()
    
    //default request id generation
    router.UseC(gojiid.NewRequestId())
	
	logr := logrus.New()
	logr.Formatter = new(logrus.JSONFormatter)
	
	// add request id support to glogrus
	goji.UseC(glogrus.NewGlogrusWithReqId(logr, "myapp", gojiid.FromContext))

	log.Fatal(http.ListenAndServe(":8080", router))
}

Grabbing the id from an http header

package main

import(
	"github.com/atlassian/gojiid"
	"github.com/goji/glogrus"
	"goji.io"
	"golang.org/x/net/context"
	"net/http"
)

func main() {
	router := goji.NewMux()
    
    // lookup from http headers in order
    router.UseC(gojiid.NewCustomRequestId(
        &gojiid.RequestIdConfig{Headers:[]string{"X-Request-ID", "my-custom-id-header"}}
    ))
	
	logr := logrus.New()
	logr.Formatter = new(logrus.JSONFormatter)
	
	// add request id support to glogrus
	goji.Use(glogrus.NewGlogrusWithReqId(logr, "myapp", gojiid.FromContext))

	log.Fatal(http.ListenAndServe(":8080", router))
}

Grabbing the id from an http header and using a custom generator

package main

import(
	"github.com/atlassian/gojiid"
	"github.com/goji/glogrus"
	"goji.io"
	"golang.org/x/net/context"
	"net/http"
)

func main() {
	router := goji.NewMux()
    
    // lookup from http headers in order
    router.UseC(gojiid.NewCustomRequestId(
        &gojiid.RequestIdConfig{
            Headers:[]string{"X-Request-ID", "my-custom-id-header"},
            Generator: MyGenerator
        }
    ))
	
	logr := logrus.New()
	logr.Formatter = new(logrus.JSONFormatter)
	
	// add request id support to glogrus
	goji.Use(glogrus.NewGlogrusWithReqId(logr, "myapp", gojiid.FromContext))

	log.Fatal(http.ListenAndServe(":8080", router))
	
}

func MyGenerator(req *http.Request) string {
    return "static-id"
}