trace

package module
v0.0.0-...-26a7c9b Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2023 License: MIT Imports: 2 Imported by: 10

README

Trace library

import "github.com/best-expendables/trace"
example in router:
func RequestID(prefix string) func(http.Handler) http.Handler {
	return func(next http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			requestID := trace.RequestIDFromHeader(r.Header)

			if requestID == "" {
				if prefix != "" {
					requestID = prefix + "-"
				}

				requestID += fmt.Sprintf("%06d", middleware.NextRequestID())
			}
			ctx := trace.ContextWithRequestID(r.Context(), requestID)
			// Send old or new response id back to the client
			trace.RequestIDToHeader(w.Header(), requestID)

			next.ServeHTTP(w, r.WithContext(ctx))
		})
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextWithRequestID

func ContextWithRequestID(ctx context.Context, requestId string) context.Context

ContextWithRequestID set request id into the context

func ContextWithUserID

func ContextWithUserID(ctx context.Context, UserId string) context.Context

ContextWithUserID set User id into the context

func RequestIDFromContext

func RequestIDFromContext(ctx context.Context) string

RequestIDFromContext get request id from context

func RequestIDFromHeader

func RequestIDFromHeader(header http.Header) string

RequestIDFromHeader get request id from header

func RequestIDToHeader

func RequestIDToHeader(header http.Header, requestID string)

RequestIDToHeader set request id to the header

func UserIDFromContext

func UserIDFromContext(ctx context.Context) string

UserIDFromContext get User id from context

func UserIDFromHeader

func UserIDFromHeader(header http.Header) string

UserIDFromHeader get user id from header

func UserIDToHeader

func UserIDToHeader(header http.Header, UserID string)

UserIDToHeader set user id to the header

Types

This section is empty.

Jump to

Keyboard shortcuts

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