Documentation
¶
Overview ¶
Package libGin provides a Gin web framework adapter for requestCore.
Index ¶
- Variables
- func AddCustomAttributes(c *gin.Context, attrs ...slog.Attr)
- func AddSpanAttribute(c *gin.Context, key, value string)
- func AddSpanAttributes(c *gin.Context, attrs map[string]string)
- func AddSpanEvent(c *gin.Context, name string, attrs map[string]string)
- func CustomTracingMiddleware(tm *libTracing.TracingManager) gin.HandlerFunc
- func GetRequestID(c *gin.Context) string
- func GetSpanFromContext(c *gin.Context) trace.Span
- func Gin(handler any) gin.HandlerFunc
- func New(logger *slog.Logger) gin.HandlerFunc
- func NewWithConfig(logger *slog.Logger, config Config) gin.HandlerFunc
- func RecordSpanError(c *gin.Context, err error, attrs map[string]string)
- func TracingMiddleware() gin.HandlerFunc
- type Config
- type Filter
- func Accept(filter Filter) Filter
- func AcceptHost(hosts ...string) Filter
- func AcceptHostContains(parts ...string) Filter
- func AcceptHostMatch(regs ...regexp.Regexp) Filter
- func AcceptHostPrefix(prefixs ...string) Filter
- func AcceptHostSuffix(prefixs ...string) Filter
- func AcceptMethod(methods ...string) Filter
- func AcceptPath(urls ...string) Filter
- func AcceptPathContains(parts ...string) Filter
- func AcceptPathMatch(regs ...regexp.Regexp) Filter
- func AcceptPathPrefix(prefixs ...string) Filter
- func AcceptPathSuffix(prefixs ...string) Filter
- func AcceptStatus(statuses ...int) Filter
- func AcceptStatusGreaterThan(status int) Filter
- func AcceptStatusGreaterThanOrEqual(status int) Filter
- func AcceptStatusLessThan(status int) Filter
- func AcceptStatusLessThanOrEqual(status int) Filter
- func Ignore(filter Filter) Filter
- func IgnoreHost(hosts ...string) Filter
- func IgnoreHostContains(parts ...string) Filter
- func IgnoreHostMatch(regs ...regexp.Regexp) Filter
- func IgnoreHostPrefix(prefixs ...string) Filter
- func IgnoreHostSuffix(suffixs ...string) Filter
- func IgnoreMethod(methods ...string) Filter
- func IgnorePath(urls ...string) Filter
- func IgnorePathContains(parts ...string) Filter
- func IgnorePathMatch(regs ...regexp.Regexp) Filter
- func IgnorePathPrefix(prefixs ...string) Filter
- func IgnorePathSuffix(suffixs ...string) Filter
- func IgnoreStatus(statuses ...int) Filter
- func IgnoreStatusGreaterThan(status int) Filter
- func IgnoreStatusGreaterThanOrEqual(status int) Filter
- func IgnoreStatusLessThan(status int) Filter
- func IgnoreStatusLessThanOrEqual(status int) Filter
- type GinParser
- func (c GinParser) Abort() error
- func (c GinParser) AddCustomAttributes(attr slog.Attr)
- func (c GinParser) AddSpanAttribute(key, value string)
- func (c GinParser) AddSpanAttributes(attrs map[string]string)
- func (c GinParser) AddSpanEvent(name string, attrs map[string]string)
- func (c GinParser) CheckURLParam(name string) (string, bool)
- func (c GinParser) FileAttachment(path, fileName string)
- func (c GinParser) FormValue(name string) string
- func (c GinParser) GetArgs(args ...any) map[string]string
- func (c GinParser) GetBody(target any) error
- func (c GinParser) GetContext() context.Context
- func (c GinParser) GetHTTPHeader() http.Header
- func (c GinParser) GetHeader(target webFramework.HeaderInterface) error
- func (c GinParser) GetHeaderValue(name string) string
- func (c GinParser) GetLocal(name string) any
- func (c GinParser) GetLocalString(name string) string
- func (c GinParser) GetMethod() string
- func (c GinParser) GetPath() string
- func (c GinParser) GetRawURLQuery() string
- func (c GinParser) GetTraceContext() trace.SpanContext
- func (c GinParser) GetURI(target any) error
- func (c GinParser) GetURLParam(name string) string
- func (c GinParser) GetURLParams() map[string]string
- func (c GinParser) GetURLQuery(target any) error
- func (c GinParser) Next() error
- func (c GinParser) ParseCommand(command, title string, request webFramework.RecordData, ...) string
- func (c GinParser) RecordSpanError(err error, attrs map[string]string)
- func (c GinParser) SaveFile(formTagName, path string) error
- func (c GinParser) SendJSONRespBody(status int, resp any) error
- func (c GinParser) SetContext(ctx context.Context)
- func (c GinParser) SetLocal(name string, value any)
- func (c GinParser) SetReqHeader(name string, value string)
- func (c GinParser) SetRespHeader(name string, value string)
- func (c GinParser) SetTraceContext(_ trace.SpanContext)
- func (c GinParser) StartSpan(_ string, _ ...trace.SpanStartOption) (context.Context, trace.Span)
Constants ¶
This section is empty.
Variables ¶
var ( // TraceIDKey is the slog attribute key for the OpenTelemetry trace ID. TraceIDKey = "trace_id" // SpanIDKey is the slog attribute key for the OpenTelemetry span ID. SpanIDKey = "span_id" // RequestIDKey is the slog attribute key for the request ID. RequestIDKey = "id" // RequestBodyMaxSize is the maximum number of bytes captured from the request body. RequestBodyMaxSize = 64 * 1024 // 64KB // ResponseBodyMaxSize is the maximum number of bytes captured from the response body. ResponseBodyMaxSize = 64 * 1024 // 64KB // HiddenRequestHeaders is the set of request headers excluded from logs. HiddenRequestHeaders = map[string]struct{}{ "authorization": {}, "cookie": {}, "set-cookie": {}, "x-auth-token": {}, "x-csrf-token": {}, "x-xsrf-token": {}, } // HiddenResponseHeaders is the set of response headers excluded from logs. HiddenResponseHeaders = map[string]struct{}{ "set-cookie": {}, } // RequestIDHeaderKey is the canonical HTTP header key for the request ID. RequestIDHeaderKey = "X-Request-Id" // RequestIDContextKey is the gin context key for storing the request ID. RequestIDContextKey = "slog-gin.request-id" )
Functions ¶
func AddCustomAttributes ¶ added in v0.22.2
AddCustomAttributes adds custom attributes to the request context.
func AddSpanAttribute ¶ added in v0.18.0
AddSpanAttribute adds an attribute to the current span in Gin context
func AddSpanAttributes ¶ added in v0.18.0
AddSpanAttributes adds multiple attributes to the current span
func AddSpanEvent ¶ added in v0.18.0
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
GetRequestID returns the request identifier.
func GetSpanFromContext ¶ added in v0.18.0
GetSpanFromContext gets the span from Gin context
func Gin ¶
func Gin(handler any) gin.HandlerFunc
Gin adapts a requestCore handler into a 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
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
}
Config holds the configuration for the slog Gin middleware.
type Filter ¶ added in v0.22.2
Filter is a function that decides whether a request should be logged.
func AcceptHost ¶ added in v0.22.2
AcceptHost returns a filter that accepts requests whose host matches any of the given hosts.
func AcceptHostContains ¶ added in v0.22.2
AcceptHostContains returns a filter that accepts requests whose host contains any of the given parts.
func AcceptHostMatch ¶ added in v0.22.2
AcceptHostMatch returns a filter that accepts requests whose host matches any of the given regexps.
func AcceptHostPrefix ¶ added in v0.22.2
AcceptHostPrefix returns a filter that accepts requests whose host starts with any of the given prefixes.
func AcceptHostSuffix ¶ added in v0.22.2
AcceptHostSuffix returns a filter that accepts requests whose host ends with any of the given suffixes.
func AcceptMethod ¶ added in v0.22.2
AcceptMethod returns a filter that accepts requests matching any of the given HTTP methods.
func AcceptPath ¶ added in v0.22.2
AcceptPath returns a filter that accepts requests whose path matches any of the given URLs.
func AcceptPathContains ¶ added in v0.22.2
AcceptPathContains returns a filter that accepts requests whose path contains any of the given parts.
func AcceptPathMatch ¶ added in v0.22.2
AcceptPathMatch returns a filter that accepts requests whose path matches any of the given regexps.
func AcceptPathPrefix ¶ added in v0.22.2
AcceptPathPrefix returns a filter that accepts requests whose path starts with any of the given prefixes.
func AcceptPathSuffix ¶ added in v0.22.2
AcceptPathSuffix returns a filter that accepts requests whose path ends with any of the given suffixes.
func AcceptStatus ¶ added in v0.22.2
AcceptStatus returns a filter that accepts responses matching any of the given statuses.
func AcceptStatusGreaterThan ¶ added in v0.22.2
AcceptStatusGreaterThan returns a filter that accepts responses with status greater than the given value.
func AcceptStatusGreaterThanOrEqual ¶ added in v0.22.2
AcceptStatusGreaterThanOrEqual returns a filter that accepts responses with status greater than or equal to the given value.
func AcceptStatusLessThan ¶ added in v0.22.2
AcceptStatusLessThan returns a filter that accepts responses with status less than the given value.
func AcceptStatusLessThanOrEqual ¶ added in v0.22.2
AcceptStatusLessThanOrEqual returns a filter that accepts responses with status less than or equal to the given value.
func IgnoreHost ¶ added in v0.22.2
IgnoreHost returns a filter that rejects requests whose host matches any of the given hosts.
func IgnoreHostContains ¶ added in v0.22.2
IgnoreHostContains returns a filter that rejects requests whose host contains any of the given parts.
func IgnoreHostMatch ¶ added in v0.22.2
IgnoreHostMatch returns a filter that rejects requests whose host matches any of the given regexps.
func IgnoreHostPrefix ¶ added in v0.22.2
IgnoreHostPrefix returns a filter that rejects requests whose host starts with any of the given prefixes.
func IgnoreHostSuffix ¶ added in v0.22.2
IgnoreHostSuffix returns a filter that rejects requests whose host ends with any of the given suffixes.
func IgnoreMethod ¶ added in v0.22.2
IgnoreMethod returns a filter that rejects requests matching any of the given methods.
func IgnorePath ¶ added in v0.22.2
IgnorePath returns a filter that rejects requests whose path matches any of the given URLs.
func IgnorePathContains ¶ added in v0.22.2
IgnorePathContains returns a filter that rejects requests whose path contains any of the given parts.
func IgnorePathMatch ¶ added in v0.22.2
IgnorePathMatch returns a filter that rejects requests whose path matches any of the given regexps.
func IgnorePathPrefix ¶ added in v0.22.2
IgnorePathPrefix returns a filter that rejects requests whose path starts with any of the given prefixes.
func IgnorePathSuffix ¶ added in v0.22.2
IgnorePathSuffix returns a filter that rejects requests whose path ends with any of the given suffixes.
func IgnoreStatus ¶ added in v0.22.2
IgnoreStatus returns a filter that rejects responses matching any of the given statuses.
func IgnoreStatusGreaterThan ¶ added in v0.22.2
IgnoreStatusGreaterThan returns a filter that rejects responses with status greater than the given value.
func IgnoreStatusGreaterThanOrEqual ¶ added in v0.22.2
IgnoreStatusGreaterThanOrEqual returns a filter that rejects responses with status greater than or equal to the given value.
func IgnoreStatusLessThan ¶ added in v0.22.2
IgnoreStatusLessThan returns a filter that rejects responses with status less than the given value.
func IgnoreStatusLessThanOrEqual ¶ added in v0.22.2
IgnoreStatusLessThanOrEqual returns a filter that rejects responses with status less than or equal to the given value.
type GinParser ¶
GinParser wraps a Gin context to implement the webFramework parser interface.
func InitContext ¶
InitContext creates a GinParser from a Gin context.
func (GinParser) AddCustomAttributes ¶ added in v0.13.2
AddCustomAttributes adds a custom slog attribute to the request context.
func (GinParser) AddSpanAttribute ¶ added in v0.18.0
AddSpanAttribute adds a string key-value attribute to the current span.
func (GinParser) AddSpanAttributes ¶ added in v0.18.0
AddSpanAttributes adds multiple string key-value attributes to the current span.
func (GinParser) AddSpanEvent ¶ added in v0.18.0
AddSpanEvent records an event with attributes on the current span.
func (GinParser) CheckURLParam ¶ added in v0.28.1
CheckURLParam returns the URL parameter value and whether it exists.
func (GinParser) FileAttachment ¶ added in v0.10.4
FileAttachment sends a file as an attachment in the response.
func (GinParser) GetArgs ¶
GetArgs returns a map of common request arguments and additional URL parameters.
func (GinParser) GetContext ¶ added in v0.22.0
GetContext returns the context from the Gin request
func (GinParser) GetHTTPHeader ¶ added in v0.28.1
GetHTTPHeader returns the HTTP request headers.
func (GinParser) GetHeader ¶
func (c GinParser) GetHeader(target webFramework.HeaderInterface) error
GetHeader binds request headers to the given target struct.
func (GinParser) GetHeaderValue ¶
GetHeaderValue returns the value of the named HTTP request header.
func (GinParser) GetLocal ¶
GetLocal returns the value stored in the Gin context under the given name.
func (GinParser) GetLocalString ¶
GetLocalString returns the string value stored in the Gin context under the given name.
func (GinParser) GetRawURLQuery ¶ added in v0.28.1
GetRawURLQuery returns the raw query string from the request URL.
func (GinParser) GetTraceContext ¶ added in v0.18.0
func (c GinParser) GetTraceContext() trace.SpanContext
GetTraceContext returns the trace span context from the Gin request context.
func (GinParser) GetURLParam ¶ added in v0.28.1
GetURLParam returns the value of the named URL path parameter.
func (GinParser) GetURLParams ¶ added in v0.28.1
GetURLParams returns all URL path parameters as a map.
func (GinParser) GetURLQuery ¶ added in v0.28.1
GetURLQuery binds URL query parameters to the given target.
func (GinParser) ParseCommand ¶
func (c GinParser) ParseCommand(command, title string, request webFramework.RecordData, parser webFramework.FieldParser) string
ParseCommand parses a query command using request context values and field parsers.
func (GinParser) RecordSpanError ¶ added in v0.18.0
RecordSpanError records an error with attributes on the current span.
func (GinParser) SaveFile ¶ added in v0.10.1
SaveFile saves an uploaded file from the form to the given path.
func (GinParser) SendJSONRespBody ¶ added in v0.7.0
SendJSONRespBody sends a JSON response with the given status code and body.
func (GinParser) SetContext ¶ added in v0.22.0
SetContext updates the context in the Gin request
func (GinParser) SetReqHeader ¶ added in v0.7.5
SetReqHeader sets a value on the HTTP request header.
func (GinParser) SetRespHeader ¶ added in v0.10.28
SetRespHeader sets a value on the HTTP response header.
func (GinParser) SetTraceContext ¶ added in v0.18.0
func (c GinParser) SetTraceContext(_ trace.SpanContext)
SetTraceContext is a no-op for Gin as trace context is handled by the context.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package gininitiator provides Gin engine initialization with middleware setup.
|
Package gininitiator provides Gin engine initialization with middleware setup. |
|
Package logger provides Gin middleware logging integration.
|
Package logger provides Gin middleware logging integration. |
|
ginsplunk
Package ginsplunk provides Gin Splunk logging middleware integration.
|
Package ginsplunk provides Gin Splunk logging middleware integration. |