Skip to content

A Goji Middleware For adding Request Id to Context

Notifications You must be signed in to change notification settings

atlassian/gojiid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 

Repository files navigation

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"
}

About

A Goji Middleware For adding Request Id to Context

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages