libGin

package
v0.22.2 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2026 License: GPL-3.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	TraceIDKey   = "trace_id"
	SpanIDKey    = "span_id"
	RequestIDKey = "id"

	RequestBodyMaxSize  = 64 * 1024 // 64KB
	ResponseBodyMaxSize = 64 * 1024 // 64KB

	HiddenRequestHeaders = map[string]struct{}{
		"authorization": {},
		"cookie":        {},
		"set-cookie":    {},
		"x-auth-token":  {},
		"x-csrf-token":  {},
		"x-xsrf-token":  {},
	}
	HiddenResponseHeaders = map[string]struct{}{
		"set-cookie": {},
	}

	// Formatted with http.CanonicalHeaderKey
	RequestIDHeaderKey  = "X-Request-Id"
	RequestIDContextKey = "slog-gin.request-id"
)

Functions

func AddCustomAttributes added in v0.22.2

func AddCustomAttributes(c *gin.Context, attrs ...slog.Attr)

AddCustomAttributes adds custom attributes to the request context.

func AddSpanAttribute added in v0.18.0

func AddSpanAttribute(c *gin.Context, key, value string)

AddSpanAttribute adds an attribute to the current span in Gin context

func AddSpanAttributes added in v0.18.0

func AddSpanAttributes(c *gin.Context, attrs map[string]string)

AddSpanAttributes adds multiple attributes to the current span

func AddSpanEvent added in v0.18.0

func AddSpanEvent(c *gin.Context, name string, attrs map[string]string)

AddSpanEvent adds an event to the current span

func CustomTracingMiddleware added in v0.18.0

func CustomTracingMiddleware(tm *libTracing.TracingManager) gin.HandlerFunc

CustomTracingMiddleware creates a custom Gin middleware with more control

func GetRequestID added in v0.22.2

func GetRequestID(c *gin.Context) string

GetRequestID returns the request identifier.

func GetSpanFromContext added in v0.18.0

func GetSpanFromContext(c *gin.Context) trace.Span

GetSpanFromContext gets the span from Gin context

func Gin

func Gin(handler any) gin.HandlerFunc

func New added in v0.22.2

func New(logger *slog.Logger) gin.HandlerFunc

New returns a gin.HandlerFunc (middleware) that logs requests using slog.

Requests with errors are logged using slog.Error(). Requests without errors are logged using slog.Info().

func NewWithConfig added in v0.22.2

func NewWithConfig(logger *slog.Logger, config Config) gin.HandlerFunc

NewWithConfig returns a gin.HandlerFunc (middleware) that logs requests using slog.

func RecordSpanError added in v0.18.0

func RecordSpanError(c *gin.Context, err error, attrs map[string]string)

RecordSpanError records an error in the current span

func TracingMiddleware added in v0.18.0

func TracingMiddleware() gin.HandlerFunc

TracingMiddleware creates Gin middleware for OpenTelemetry tracing

Types

type Config added in v0.22.2

type Config struct {
	DefaultLevel     slog.Level
	ClientErrorLevel slog.Level
	ServerErrorLevel slog.Level

	WithUserAgent      bool
	WithRequestID      bool
	WithRequestBody    bool
	WithRequestHeader  bool
	WithResponseBody   bool
	WithResponseHeader bool
	WithSpanID         bool
	WithTraceID        bool
	WithClientIP       bool

	HandleGinDebug bool

	Filters []Filter
}

type Filter added in v0.22.2

type Filter func(ctx *gin.Context) bool

func Accept added in v0.22.2

func Accept(filter Filter) Filter

Basic

func AcceptHost added in v0.22.2

func AcceptHost(hosts ...string) Filter

Host

func AcceptHostContains added in v0.22.2

func AcceptHostContains(parts ...string) Filter

func AcceptHostMatch added in v0.22.2

func AcceptHostMatch(regs ...regexp.Regexp) Filter

func AcceptHostPrefix added in v0.22.2

func AcceptHostPrefix(prefixs ...string) Filter

func AcceptHostSuffix added in v0.22.2

func AcceptHostSuffix(prefixs ...string) Filter

func AcceptMethod added in v0.22.2

func AcceptMethod(methods ...string) Filter

Method

func AcceptPath added in v0.22.2

func AcceptPath(urls ...string) Filter

Path

func AcceptPathContains added in v0.22.2

func AcceptPathContains(parts ...string) Filter

func AcceptPathMatch added in v0.22.2

func AcceptPathMatch(regs ...regexp.Regexp) Filter

func AcceptPathPrefix added in v0.22.2

func AcceptPathPrefix(prefixs ...string) Filter

func AcceptPathSuffix added in v0.22.2

func AcceptPathSuffix(prefixs ...string) Filter

func AcceptStatus added in v0.22.2

func AcceptStatus(statuses ...int) Filter

Status

func AcceptStatusGreaterThan added in v0.22.2

func AcceptStatusGreaterThan(status int) Filter

func AcceptStatusGreaterThanOrEqual added in v0.22.2

func AcceptStatusGreaterThanOrEqual(status int) Filter

func AcceptStatusLessThan added in v0.22.2

func AcceptStatusLessThan(status int) Filter

func AcceptStatusLessThanOrEqual added in v0.22.2

func AcceptStatusLessThanOrEqual(status int) Filter

func Ignore added in v0.22.2

func Ignore(filter Filter) Filter

func IgnoreHost added in v0.22.2

func IgnoreHost(hosts ...string) Filter

func IgnoreHostContains added in v0.22.2

func IgnoreHostContains(parts ...string) Filter

func IgnoreHostMatch added in v0.22.2

func IgnoreHostMatch(regs ...regexp.Regexp) Filter

func IgnoreHostPrefix added in v0.22.2

func IgnoreHostPrefix(prefixs ...string) Filter

func IgnoreHostSuffix added in v0.22.2

func IgnoreHostSuffix(suffixs ...string) Filter

func IgnoreMethod added in v0.22.2

func IgnoreMethod(methods ...string) Filter

func IgnorePath added in v0.22.2

func IgnorePath(urls ...string) Filter

func IgnorePathContains added in v0.22.2

func IgnorePathContains(parts ...string) Filter

func IgnorePathMatch added in v0.22.2

func IgnorePathMatch(regs ...regexp.Regexp) Filter

func IgnorePathPrefix added in v0.22.2

func IgnorePathPrefix(prefixs ...string) Filter

func IgnorePathSuffix added in v0.22.2

func IgnorePathSuffix(suffixs ...string) Filter

func IgnoreStatus added in v0.22.2

func IgnoreStatus(statuses ...int) Filter

func IgnoreStatusGreaterThan added in v0.22.2

func IgnoreStatusGreaterThan(status int) Filter

func IgnoreStatusGreaterThanOrEqual added in v0.22.2

func IgnoreStatusGreaterThanOrEqual(status int) Filter

func IgnoreStatusLessThan added in v0.22.2

func IgnoreStatusLessThan(status int) Filter

func IgnoreStatusLessThanOrEqual added in v0.22.2

func IgnoreStatusLessThanOrEqual(status int) Filter

type GinParser

type GinParser struct {
	Ctx *gin.Context
}

func InitContext

func InitContext(c any) GinParser

func (GinParser) Abort added in v0.7.0

func (c GinParser) Abort() error

func (GinParser) AddCustomAttributes added in v0.13.2

func (c GinParser) AddCustomAttributes(attr slog.Attr)

func (GinParser) AddSpanAttribute added in v0.18.0

func (c GinParser) AddSpanAttribute(key, value string)

func (GinParser) AddSpanAttributes added in v0.18.0

func (c GinParser) AddSpanAttributes(attrs map[string]string)

func (GinParser) AddSpanEvent added in v0.18.0

func (c GinParser) AddSpanEvent(name string, attrs map[string]string)

func (GinParser) CheckUrlParam

func (c GinParser) CheckUrlParam(name string) (string, bool)

func (GinParser) FileAttachment added in v0.10.4

func (c GinParser) FileAttachment(path, fileName string)

func (GinParser) FormValue added in v0.10.1

func (c GinParser) FormValue(name string) string

func (GinParser) GetArgs

func (c GinParser) GetArgs(args ...any) map[string]string

func (GinParser) GetBody

func (c GinParser) GetBody(target any) error

func (GinParser) GetContext added in v0.22.0

func (c GinParser) GetContext() context.Context

GetContext returns the context from the Gin request

func (GinParser) GetHeader

func (c GinParser) GetHeader(target webFramework.HeaderInterface) error

func (GinParser) GetHeaderValue

func (c GinParser) GetHeaderValue(name string) string

func (GinParser) GetHttpHeader

func (c GinParser) GetHttpHeader() http.Header

func (GinParser) GetLocal

func (c GinParser) GetLocal(name string) any

func (GinParser) GetLocalString

func (c GinParser) GetLocalString(name string) string

func (GinParser) GetMethod

func (c GinParser) GetMethod() string

func (GinParser) GetPath

func (c GinParser) GetPath() string

func (GinParser) GetRawUrlQuery

func (c GinParser) GetRawUrlQuery() string

func (GinParser) GetTraceContext added in v0.18.0

func (c GinParser) GetTraceContext() trace.SpanContext

Tracing methods for GinParser

func (GinParser) GetUri added in v0.5.7

func (c GinParser) GetUri(target any) error

func (GinParser) GetUrlParam

func (c GinParser) GetUrlParam(name string) string

func (GinParser) GetUrlParams

func (c GinParser) GetUrlParams() map[string]string

func (GinParser) GetUrlQuery

func (c GinParser) GetUrlQuery(target any) error

func (GinParser) Next added in v0.7.0

func (c GinParser) Next() error

func (GinParser) ParseCommand

func (c GinParser) ParseCommand(command, title string, request webFramework.RecordData, parser webFramework.FieldParser) string

func (GinParser) RecordSpanError added in v0.18.0

func (c GinParser) RecordSpanError(err error, attrs map[string]string)

func (GinParser) SaveFile added in v0.10.1

func (c GinParser) SaveFile(
	formTagName, path string,
) error

func (GinParser) SendJSONRespBody added in v0.7.0

func (c GinParser) SendJSONRespBody(status int, resp any) error

func (GinParser) SetContext added in v0.22.0

func (c GinParser) SetContext(ctx context.Context)

SetContext updates the context in the Gin request

func (GinParser) SetLocal

func (c GinParser) SetLocal(name string, value any)

func (GinParser) SetReqHeader added in v0.7.5

func (c GinParser) SetReqHeader(name string, value string)

func (GinParser) SetRespHeader added in v0.10.28

func (c GinParser) SetRespHeader(name string, value string)

func (GinParser) SetTraceContext added in v0.18.0

func (c GinParser) SetTraceContext(spanCtx trace.SpanContext)

func (GinParser) StartSpan added in v0.18.0

func (c GinParser) StartSpan(name string, opts ...trace.SpanStartOption) (context.Context, trace.Span)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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