Compare commits

...

2 Commits

Author SHA1 Message Date
e4886b4a7d v0.0.445 added CtxData() and ExtendGinMeta/ExtendContextMeta to exerr
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m55s
2024-05-03 15:28:53 +02:00
dcb5d3d7cd v0.0.444 change gin values in exerr auto meta to not include dots in keys (fucks up mongo)
Some checks failed
Build Docker and Deploy / Run goext test-suite (push) Failing after 3m9s
2024-05-03 13:24:08 +02:00
3 changed files with 81 additions and 34 deletions

View File

@@ -71,6 +71,7 @@ type Builder struct {
wrappedErr error wrappedErr error
errorData *ExErr errorData *ExErr
containsGinData bool containsGinData bool
containsContextData bool
noLog bool noLog bool
} }
@@ -309,27 +310,27 @@ func (b *Builder) Errs(key string, val []error) *Builder {
func (b *Builder) GinReq(ctx context.Context, g *gin.Context, req *http.Request) *Builder { func (b *Builder) GinReq(ctx context.Context, g *gin.Context, req *http.Request) *Builder {
if v := ctx.Value("start_timestamp"); v != nil { if v := ctx.Value("start_timestamp"); v != nil {
if t, ok := v.(time.Time); ok { if t, ok := v.(time.Time); ok {
b.Time("ctx.startTimestamp", t) b.Time("ctx_startTimestamp", t)
b.Time("ctx.endTimestamp", time.Now()) b.Time("ctx_endTimestamp", time.Now())
} }
} }
b.Str("gin.method", req.Method) b.Str("gin_method", req.Method)
b.Str("gin.path", g.FullPath()) b.Str("gin_path", g.FullPath())
b.Strs("gin.header", extractHeader(g.Request.Header)) b.Strs("gin_header", extractHeader(g.Request.Header))
if req.URL != nil { if req.URL != nil {
b.Str("gin.url", req.URL.String()) b.Str("gin_url", req.URL.String())
} }
if ctxVal := g.GetString("apiversion"); ctxVal != "" { if ctxVal := g.GetString("apiversion"); ctxVal != "" {
b.Str("gin.context.apiversion", ctxVal) b.Str("gin_context_apiversion", ctxVal)
} }
if ctxVal := g.GetString("uid"); ctxVal != "" { if ctxVal := g.GetString("uid"); ctxVal != "" {
b.Str("gin.context.uid", ctxVal) b.Str("gin_context_uid", ctxVal)
} }
if ctxVal := g.GetString("fcmId"); ctxVal != "" { if ctxVal := g.GetString("fcmId"); ctxVal != "" {
b.Str("gin.context.fcmid", ctxVal) b.Str("gin_context_fcmid", ctxVal)
} }
if ctxVal := g.GetString("reqid"); ctxVal != "" { if ctxVal := g.GetString("reqid"); ctxVal != "" {
b.Str("gin.context.reqid", ctxVal) b.Str("gin_context_reqid", ctxVal)
} }
if req.Method != "GET" && req.Body != nil { if req.Method != "GET" && req.Body != nil {
@@ -340,12 +341,12 @@ func (b *Builder) GinReq(ctx context.Context, g *gin.Context, req *http.Request)
var prettyJSON bytes.Buffer var prettyJSON bytes.Buffer
err = json.Indent(&prettyJSON, bin, "", " ") err = json.Indent(&prettyJSON, bin, "", " ")
if err == nil { if err == nil {
b.Str("gin.body", string(prettyJSON.Bytes())) b.Str("gin_body", string(prettyJSON.Bytes()))
} else { } else {
b.Bytes("gin.body", bin) b.Bytes("gin_body", bin)
} }
} else { } else {
b.Str("gin.body", fmt.Sprintf("[[%v bytes | %s]]", len(bin), req.Header.Get("Content-Type"))) b.Str("gin_body", fmt.Sprintf("[[%v bytes | %s]]", len(bin), req.Header.Get("Content-Type")))
} }
} }
} }
@@ -355,9 +356,9 @@ func (b *Builder) GinReq(ctx context.Context, g *gin.Context, req *http.Request)
if brc, ok := req.Body.(dataext.BufferedReadCloser); ok { if brc, ok := req.Body.(dataext.BufferedReadCloser); ok {
if bin, err := brc.BufferedAll(); err == nil { if bin, err := brc.BufferedAll(); err == nil {
if len(bin) < 16*1024 { if len(bin) < 16*1024 {
b.Bytes("gin.body", bin) b.Bytes("gin_body", bin)
} else { } else {
b.Str("gin.body", fmt.Sprintf("[[%v bytes | %s]]", len(bin), req.Header.Get("Content-Type"))) b.Str("gin_body", fmt.Sprintf("[[%v bytes | %s]]", len(bin), req.Header.Get("Content-Type")))
} }
} }
} }
@@ -365,10 +366,20 @@ func (b *Builder) GinReq(ctx context.Context, g *gin.Context, req *http.Request)
} }
pkgconfig.ExtendGinMeta(ctx, b, g, req)
b.containsGinData = true b.containsGinData = true
return b return b
} }
func (b *Builder) CtxData(method Method, ctx context.Context) *Builder {
pkgconfig.ExtendContextMeta(b, method, ctx)
b.containsContextData = true
return b
}
func formatHeader(header map[string][]string) string { func formatHeader(header map[string][]string) string {
ml := 1 ml := 1
for k, _ := range header { for k, _ := range header {
@@ -412,9 +423,13 @@ func extractHeader(header map[string][]string) []string {
// If the errors is not SevWarn or SevInfo it gets also logged (in short form, without stacktrace) onto stdout // If the errors is not SevWarn or SevInfo it gets also logged (in short form, without stacktrace) onto stdout
// Can be gloablly configured with ZeroLogErrTraces and ZeroLogAllTraces // Can be gloablly configured with ZeroLogErrTraces and ZeroLogAllTraces
// Can be locally suppressed with Builder.NoLog() // Can be locally suppressed with Builder.NoLog()
func (b *Builder) Build() error { func (b *Builder) Build(ctxs ...context.Context) error {
warnOnPkgConfigNotInitialized() warnOnPkgConfigNotInitialized()
for _, dctx := range ctxs {
b.CtxData(MethodBuild, dctx)
}
if pkgconfig.DisableErrorWrapping && b.wrappedErr != nil { if pkgconfig.DisableErrorWrapping && b.wrappedErr != nil {
return b.wrappedErr return b.wrappedErr
} }
@@ -439,6 +454,8 @@ func (b *Builder) Output(ctx context.Context, g *gin.Context) {
b.GinReq(ctx, g, g.Request) b.GinReq(ctx, g, g.Request)
} }
b.CtxData(MethodOutput, ctx)
b.errorData.Output(g) b.errorData.Output(g)
if (b.errorData.Severity == SevErr || b.errorData.Severity == SevFatal) && (pkgconfig.ZeroLogErrGinOutput || pkgconfig.ZeroLogAllGinOutput) { if (b.errorData.Severity == SevErr || b.errorData.Severity == SevFatal) && (pkgconfig.ZeroLogErrGinOutput || pkgconfig.ZeroLogAllGinOutput) {
@@ -452,7 +469,13 @@ func (b *Builder) Output(ctx context.Context, g *gin.Context) {
// Print prints the error // Print prints the error
// If the error is SevErr we also send it to the error-service // If the error is SevErr we also send it to the error-service
func (b *Builder) Print() { func (b *Builder) Print(ctxs ...context.Context) {
warnOnPkgConfigNotInitialized()
for _, dctx := range ctxs {
b.CtxData(MethodPrint, dctx)
}
if b.errorData.Severity == SevErr || b.errorData.Severity == SevFatal { if b.errorData.Severity == SevErr || b.errorData.Severity == SevFatal {
b.errorData.Log(stackSkipLogger.Error()) b.errorData.Log(stackSkipLogger.Error())
} else if b.errorData.Severity == SevWarn { } else if b.errorData.Severity == SevWarn {
@@ -468,7 +491,12 @@ func (b *Builder) Format(level LogPrintLevel) string {
// Fatal prints the error and terminates the program // Fatal prints the error and terminates the program
// If the error is SevErr we also send it to the error-service // If the error is SevErr we also send it to the error-service
func (b *Builder) Fatal() { func (b *Builder) Fatal(ctxs ...context.Context) {
for _, dctx := range ctxs {
b.CtxData(MethodFatal, dctx)
}
b.errorData.Severity = SevFatal b.errorData.Severity = SevFatal
b.errorData.Log(stackSkipLogger.WithLevel(zerolog.FatalLevel)) b.errorData.Log(stackSkipLogger.WithLevel(zerolog.FatalLevel))

View File

@@ -1,8 +1,11 @@
package exerr package exerr
import ( import (
"context"
"fmt" "fmt"
"github.com/gin-gonic/gin"
"gogs.mikescher.com/BlackForestBytes/goext/langext" "gogs.mikescher.com/BlackForestBytes/goext/langext"
"net/http"
) )
type ErrorPackageConfig struct { type ErrorPackageConfig struct {
@@ -16,6 +19,8 @@ type ErrorPackageConfig struct {
DisableErrorWrapping bool // Disables the exerr.Wrap()...Build() function - will always return the original error DisableErrorWrapping bool // Disables the exerr.Wrap()...Build() function - will always return the original error
ZeroLogErrGinOutput bool // autom print zerolog logs on ginext.Error() / .Output(gin) (for SevErr and SevFatal) ZeroLogErrGinOutput bool // autom print zerolog logs on ginext.Error() / .Output(gin) (for SevErr and SevFatal)
ZeroLogAllGinOutput bool // autom print zerolog logs on ginext.Error() / .Output(gin) (for all Severities) ZeroLogAllGinOutput bool // autom print zerolog logs on ginext.Error() / .Output(gin) (for all Severities)
ExtendGinMeta func(ctx context.Context, b *Builder, g *gin.Context, req *http.Request) // (Optionally) extend the final error meta values with additional data from the gin context (a few are automatically added, here more can be included)
ExtendContextMeta func(b *Builder, method Method, dctx context.Context) // (Optionally) extend the final error meta values with additional data from the context (a few are automatically added, here more can be included)
} }
type ErrorPackageConfigInit struct { type ErrorPackageConfigInit struct {
@@ -29,6 +34,8 @@ type ErrorPackageConfigInit struct {
DisableErrorWrapping *bool DisableErrorWrapping *bool
ZeroLogErrGinOutput *bool ZeroLogErrGinOutput *bool
ZeroLogAllGinOutput *bool ZeroLogAllGinOutput *bool
ExtendGinMeta func(ctx context.Context, b *Builder, g *gin.Context, req *http.Request)
ExtendContextMeta func(b *Builder, method Method, dctx context.Context)
} }
var initialized = false var initialized = false
@@ -44,6 +51,8 @@ var pkgconfig = ErrorPackageConfig{
DisableErrorWrapping: false, DisableErrorWrapping: false,
ZeroLogErrGinOutput: true, ZeroLogErrGinOutput: true,
ZeroLogAllGinOutput: false, ZeroLogAllGinOutput: false,
ExtendGinMeta: func(ctx context.Context, b *Builder, g *gin.Context, req *http.Request) {},
ExtendContextMeta: func(b *Builder, method Method, dctx context.Context) {},
} }
// Init initializes the exerr packages // Init initializes the exerr packages
@@ -56,6 +65,8 @@ func Init(cfg ErrorPackageConfigInit) {
ego := func(err *ExErr, json map[string]any) {} ego := func(err *ExErr, json map[string]any) {}
egdo := func(err *ExErr, depth int, json map[string]any) {} egdo := func(err *ExErr, depth int, json map[string]any) {}
egm := func(ctx context.Context, b *Builder, g *gin.Context, req *http.Request) {}
egcm := func(b *Builder, method Method, dctx context.Context) {}
if cfg.ExtendGinOutput != nil { if cfg.ExtendGinOutput != nil {
ego = cfg.ExtendGinOutput ego = cfg.ExtendGinOutput
@@ -63,6 +74,12 @@ func Init(cfg ErrorPackageConfigInit) {
if cfg.ExtendGinDataOutput != nil { if cfg.ExtendGinDataOutput != nil {
egdo = cfg.ExtendGinDataOutput egdo = cfg.ExtendGinDataOutput
} }
if cfg.ExtendGinMeta != nil {
egm = cfg.ExtendGinMeta
}
if cfg.ExtendContextMeta != nil {
egcm = cfg.ExtendContextMeta
}
pkgconfig = ErrorPackageConfig{ pkgconfig = ErrorPackageConfig{
ZeroLogErrTraces: langext.Coalesce(cfg.ZeroLogErrTraces, pkgconfig.ZeroLogErrTraces), ZeroLogErrTraces: langext.Coalesce(cfg.ZeroLogErrTraces, pkgconfig.ZeroLogErrTraces),
@@ -75,6 +92,8 @@ func Init(cfg ErrorPackageConfigInit) {
DisableErrorWrapping: langext.Coalesce(cfg.DisableErrorWrapping, pkgconfig.DisableErrorWrapping), DisableErrorWrapping: langext.Coalesce(cfg.DisableErrorWrapping, pkgconfig.DisableErrorWrapping),
ZeroLogAllGinOutput: langext.Coalesce(cfg.ZeroLogAllGinOutput, pkgconfig.ZeroLogAllGinOutput), ZeroLogAllGinOutput: langext.Coalesce(cfg.ZeroLogAllGinOutput, pkgconfig.ZeroLogAllGinOutput),
ZeroLogErrGinOutput: langext.Coalesce(cfg.ZeroLogErrGinOutput, pkgconfig.ZeroLogErrGinOutput), ZeroLogErrGinOutput: langext.Coalesce(cfg.ZeroLogErrGinOutput, pkgconfig.ZeroLogErrGinOutput),
ExtendGinMeta: egm,
ExtendContextMeta: egcm,
} }
initialized = true initialized = true

View File

@@ -1,5 +1,5 @@
package goext package goext
const GoextVersion = "0.0.443" const GoextVersion = "0.0.445"
const GoextVersionTimestamp = "2024-05-03T11:56:29+0200" const GoextVersionTimestamp = "2024-05-03T15:28:53+0200"