correlation

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2021 License: MIT Imports: 7 Imported by: 4

README

package correlation

Package correlation provides a set of tools to add correlation ID to the context at certain levels (transport, endpoint) of the application.

Note: Currently only server side middleware are implemented.

Usage

Get correlation ID from transport headers

Transport level middleware (HTTPToContext, GRPCToContext) read the correlation ID from headers and add it to the context (if there is any).

// HTTP example
httptransport.NewServer(
    ctx,
    endpoint,
    decoder,
    encoder,
    httptransport.ServerBefore(correlation.HTTPToContext()),
)

// gRPC example
grpctransport.NewServer(
    ctx,
    endpoint,
    decoder,
    encoder,
    grpctransport.ServerBefore(correlation.GRPCToContext()),
)
Generate a correlation ID if none is found in the context

When clients don't pass a correlation ID to the server, one should be generated early of the request lifecycle.

endpoint = correlation.Middleware()(endpoint)

Make sure to seed the global random number generator:

import (
    "math/rand"
    "time"
)

rand.Seed(time.Now().UnixNano())

Documentation

Overview

Package correlation provides a set of tools to add correlation ID to the context at certain levels (transport, endpoint) of the application.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromContext

func FromContext(ctx context.Context) (string, bool)

FromContext returns the correlation ID from the context (if any). Returns false as the second parameter if none is found.

func GRPCToContext

func GRPCToContext(headers ...string) grpc.ServerRequestFunc

GRPCToContext moves a correlation ID from request header to context (if any).

func HTTPToContext

func HTTPToContext(headers ...string) http.RequestFunc

HTTPToContext moves a correlation ID from request header to context (if any).

func Middleware

func Middleware() endpoint.Middleware

Middleware creates a new middleware that ensures the context contains a correlation ID.

func ToContext

func ToContext(ctx context.Context, id string) context.Context

ToContext returns a new context annotated with a correlation ID.

Types

This section is empty.

Jump to

Keyboard shortcuts

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