request

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2022 License: BSD-3-Clause Imports: 12 Imported by: 0

Documentation

Overview

Package request provides helper functions to handle Request ID propagation.

Basics

It will be used the following service as example:

type Service interface {
	Do(context.Context, Request) (Response, error)
}
type Response struct {
//(...)
	RequestID request.ID
}

HTTP Layer

Use the function "WithID" to create and put a Request ID in context:

func MakeEndpoint(s Service) endpoint.Endpoint {
	return func(ctx context.Context, r interface{}) (interface{}, error) {
		req := r.(Request)
		ctx = request.WithID(ctx)
		response, err := s.Do(ctx, req)
		return response, err
	}
}

Logging

Use the function "GetIDFromContext" to log the Request ID:

func (l *logging) Do(ctx ontext.Context, req Request) (response Response, err error) {
	logger := log.Logger.With().
		EmbedObject(request.GetIDFromContext(ctx)).
		Logger()
	// (...)
}

Index

Constants

View Source
const (
	// HTTPHeaderID is the HTTP Header to be used when sending ID in
	// HTTP Requests or Responses
	HTTPHeaderID = "X-REQUESTID"
)

Variables

This section is empty.

Functions

func IsEmpty

func IsEmpty(id ID) bool

IsEmpty returns true if an request ID is in it's zero-value format

func SetInHTTPRequest

func SetInHTTPRequest(ctx context.Context, request *http.Request)

SetInHTTPRequest will add the ID registered in @ctx into the given @request as a HTTP Header. If @request is nil, an warning log will be emitted and nothing will be changed.

func SetInHTTPResponse

func SetInHTTPResponse(id ID, response http.ResponseWriter)

SetInHTTPResponse will add the ID registered in @ctx into the given @request as a HTTP Header. If @request is nil, an warning log will be emitted and nothing will be changed.

nolint: interfacer

func WithID

func WithID(ctx context.Context, id ID) context.Context

WithID returns a context with the given ID

If there is already an ID in the context, the context is returned unchanged.

func WithNewID

func WithNewID(ctx context.Context) context.Context

WithNewID calls WithID with a new ID

func WithNewRequestID deprecated

func WithNewRequestID(ctx context.Context) context.Context

WithNewRequestID calls WithNewID

Deprecated: use WithNewID instead

func WithRequestID deprecated

func WithRequestID(ctx context.Context, id ID) context.Context

WithRequestID returns a context with the given ID

If there is already an ID in the context, the context is returned unchanged.

Deprecated: use WithID instead

Types

type ID

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

ID is an identifier to uniquely identify a request

func GetFromHTTPRequest

func GetFromHTTPRequest(r *http.Request) ID

GetFromHTTPRequest attempts to read a Request ID from the given @r http request's header. If no ID is found, or the found ID is ill-formatted, an empty ID is returned.

func GetFromHTTPResponse

func GetFromHTTPResponse(r *http.Response) ID

GetFromHTTPResponse attempts to read a Request ID from the given @r http response's header. If no ID is found, or the found ID is ill-formatted, an empty ID is returned.

func GetIDFromContext

func GetIDFromContext(ctx context.Context) ID

GetIDFromContext returns the request ID in the context. Will return a empty ID if it is not set

func GetRequestIDFromContext deprecated

func GetRequestIDFromContext(ctx context.Context) ID

GetRequestIDFromContext returns the request ID in the context. Will return a empty ID if it is not set

Deprecated: use GetIDFromContext instead

func Parse

func Parse(id string) (ID, error)

Parse read the given @id into an request ID.

func (ID) IsEmpty

func (i ID) IsEmpty() bool

IsEmpty returns true if there is no timestamp or randomID in Request ID @i

func (ID) MarshalJSON

func (i ID) MarshalJSON() ([]byte, error)

MarshalJSON converts ID to a string.

func (ID) MarshalZerologObject

func (i ID) MarshalZerologObject(e *zerolog.Event)

MarshalZerologObject implements the zerolog marshaler so it can be logged using: log.With().EmbededObject(cuf).Msg("Some message")

func (ID) String

func (i ID) String() string

func (*ID) UnmarshalJSON

func (i *ID) UnmarshalJSON(b []byte) error

UnmarshalJSON parses an ID from a json

Jump to

Keyboard shortcuts

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