correlation

package module
v2.0.0+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 27, 2019 License: MIT Imports: 8 Imported by: 0

README

Correlation GoDoc

Correlation is a HTTP middleware for Go that adds correlation ids to incoming requests. It can be used with a standard net/http Handler and also be integrated with Negroni.

Usage

 package main

  import (
      "net/http"

      "gitlab.com/JanMa/correlation"
  )

  var myHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
      w.Write([]byte("hello world"))
  })

  func main() {
      correlationMiddleware := correlation.New(correlation.Options{
          CorrelationIDType: correlation.UUID,
      })

      http.ListenAndServe(":8080", correlationMiddleware.Handler(myHandler))
  }

The above example will add a X-Correlation-ID header to each incoming request containing a random UUID.

Available options
c := correlation.New(correlation.Options{
    // HeaderName the name of the header to be used as correlation id. Defaults to `X-Correlation-ID`.
	CorrelationHeaderName: "X-Correlation-ID",
	// IDType the type of correlation id to generate. Defaults to `correlation.UUID`.
	CorrelationIDType: correlation.UUID,
	// CustomString the value to use when using a custom correlation id with the type correlation.Custom. Default is empty.
	CorrelationCustomString: "",
})

Integration example

package main

import (
    "net/http"

    "github.com/urfave/negroni"
    "gitlab.com/JanMa/correlation"
)

func main() {
    mux := http.NewServeMux()
    mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
        w.Write([]byte("hello world!"))
    })

    correlationMiddleware := correlation.New(correlation.Options{
        CorrelationHeaderName: "Correlation-ID",
    })

    n := negroni.Classic()
    n.Use(negroni.HandlerFunc(correlationMiddleware.HandlerFuncWithNext))
    n.UseHandler(mux)

    n.Run(":8080")
}

Documentation

Overview

Package correlation is a HTTP middleware that adds correlation ids to incoming requests.

package main

import (
    "net/http"

    "gitlab.com/JanMa/correlation"
)

var myHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("hello world"))
})

func main() {
    correlationMiddleware := correlation.New(correlation.Options{
        CorrelationIDType: correlation.UUID,
    })

    http.ListenAndServe(":8080", correlationMiddleware.Handler(myHandler))
}

Index

Constants

View Source
const (
	// UUID generate a random UUID as correlation id.
	UUID correlationType = iota + 1
	// CUID generate a CUID as correlation id.
	CUID
	// Random generate a pseudo random Int64 as correlation id.
	Random
	// Custom pass a custom string as correlation id.
	Custom
	// Time use the elapsed nanoseconds since the Epoch as correlation id.
	Time
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Correlation

type Correlation struct {
	// contains filtered or unexported fields
}

Correlation is a middleware that adds correlation ids to requests. A correlation.Options struct can be provided t override the default configuration values.

func New

func New(opt Options) *Correlation

New returns a new correlation struct with the provides options.

func (*Correlation) Handler

func (c *Correlation) Handler(h http.Handler) http.Handler

Handler for integration with net/http.

func (*Correlation) HandlerForRequestOnly

func (c *Correlation) HandlerForRequestOnly(h http.Handler) http.Handler

HandlerForRequestOnly for integration with net/http.

func (*Correlation) HandlerFuncWithNext

func (c *Correlation) HandlerFuncWithNext(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)

HandlerFuncWithNext for integration with github.com/urfave/negroni.

func (*Correlation) HandlerFuncWithNextForRequestOnly

func (c *Correlation) HandlerFuncWithNextForRequestOnly(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)

HandlerFuncWithNextForRequestOnly for integration with github.com/urfave/negroni.

func (*Correlation) ModifyResponseHeaders

func (c *Correlation) ModifyResponseHeaders(res *http.Response) error

ModifyResponseHeaders modifies the response for integration with net/http/httputil ReverseProxy.

func (*Correlation) Process

func (c *Correlation) Process(w http.ResponseWriter, r *http.Request) error

Process processes the incoming request.

type Options

type Options struct {
	// CorrelationHeaderName the name of the header to be used as correlation id. Defaults to `X-Correlation-ID`.
	CorrelationHeaderName string
	// CorrelationIDType the type of correlation id to generate. Defaults to `correlation.UUID`.
	CorrelationIDType correlationType
	// CorrelationCustomString the value to use when using a custom correlation id. Default is empty.
	CorrelationCustomString string
}

Options is a struct for specifying configuration options.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL