From 3d98c954bfce4bb27905cfff9834ae87eda2efad Mon Sep 17 00:00:00 2001 From: franklin Date: Mon, 10 Jul 2023 08:46:54 +0200 Subject: [PATCH] feat: add error metric --- .golangci.yaml | 2 +- example/basic/go.mod | 2 +- example/errors/go.mod | 7 +++---- example/errors/go.sum | 7 +------ example/errors/main.go | 20 ++++++++++---------- example/monitor/go.mod | 2 +- example/nullable/go.mod | 2 +- example/time/go.mod | 2 +- gotsrpc.go | 14 ++++++++++++++ stats.go | 16 ++++++++++------ 10 files changed, 43 insertions(+), 31 deletions(-) diff --git a/.golangci.yaml b/.golangci.yaml index b90ce5c..160c43a 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -62,7 +62,7 @@ linters: #- contextcheck # check the function whether to use a non-inherited context [fast: false, auto-fix: false] #- cyclop # checks function and package cyclomatic complexity [fast: false, auto-fix: false] - decorder # check declaration order and count of types, constants, variables and functions [fast: true, auto-fix: false] - - depguard # Go linter that checks if package imports are in a list of acceptable packages [fast: true, auto-fix: false] + #- depguard # Go linter that checks if package imports are in a list of acceptable packages [fast: true, auto-fix: false] - dogsled # Checks assignments with too many blank identifiers (e.g. x, _, _, _, := f()) [fast: true, auto-fix: false] #- dupl # Tool for code clone detection [fast: true, auto-fix: false] - durationcheck # check for two durations multiplied together [fast: false, auto-fix: false] diff --git a/example/basic/go.mod b/example/basic/go.mod index 4379104..07897c8 100644 --- a/example/basic/go.mod +++ b/example/basic/go.mod @@ -3,7 +3,7 @@ module github.com/foomo/gotsrpc/v2/example/basic go 1.16 require ( - github.com/foomo/gotsrpc/v2 v2.0.10 + github.com/foomo/gotsrpc/v2 v2.0.0 github.com/pkg/errors v0.9.1 github.com/valyala/gorpc v0.0.0-20160519171614-908281bef774 ) diff --git a/example/errors/go.mod b/example/errors/go.mod index 29aa1ee..d592d27 100644 --- a/example/errors/go.mod +++ b/example/errors/go.mod @@ -3,11 +3,10 @@ module github.com/foomo/gotsrpc/v2/example/errors go 1.16 require ( - github.com/foomo/gotsrpc/v2 v2.0.10 - github.com/kr/pretty v0.3.0 // indirect + github.com/foomo/gotsrpc/v2 v2.0.0 github.com/pkg/errors v0.9.1 - github.com/rogpeppe/go-internal v1.8.1 // indirect - gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect ) +require gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect + replace github.com/foomo/gotsrpc/v2 => ../../ diff --git a/example/errors/go.sum b/example/errors/go.sum index 860b8e8..6b9944d 100644 --- a/example/errors/go.sum +++ b/example/errors/go.sum @@ -141,9 +141,8 @@ github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxv github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= -github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= -github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= @@ -159,7 +158,6 @@ github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjY github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= -github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= @@ -189,9 +187,6 @@ github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1 github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= github.com/prometheus/procfs v0.8.0/go.mod h1:z7EfXMXOkbkqb9IINtpCn86r/to3BnA0uaxHdg830/4= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= -github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= -github.com/rogpeppe/go-internal v1.8.1 h1:geMPLpDpQOgVyCg5z5GoRwLHepNdb71NXb67XFkP+Eg= -github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= diff --git a/example/errors/main.go b/example/errors/main.go index 2a02bd5..e0317d0 100644 --- a/example/errors/main.go +++ b/example/errors/main.go @@ -52,7 +52,7 @@ func call() { c := backendsvs.NewDefaultServiceGoTSRPCClient("http://localhost:3000") { - fmt.Println("-------------------------") + fmt.Println("--- Error ----------------------") var gotsrpcErr *gotsrpc.Error serviceErr, err := c.Error(ctx) if err != nil { @@ -70,7 +70,7 @@ func call() { } { - fmt.Println("-------------------------") + fmt.Println("--- Scalar ---------------------") scalar, err := c.Scalar(ctx) if err != nil { panic("client error should be nil") @@ -82,7 +82,7 @@ func call() { } { - fmt.Println("-------------------------") + fmt.Println("--- MultiScalar ----------------") scalar, err := c.MultiScalar(ctx) if err != nil { panic("client error should be nil") @@ -94,7 +94,7 @@ func call() { } { - fmt.Println("-------------------------") + fmt.Println("--- Struct ---------------------") strct, err := c.Struct(ctx) if err != nil { panic("client error should be nil") @@ -106,7 +106,7 @@ func call() { } { - fmt.Println("-------------------------") + fmt.Println("--- WrappedError ---------------") var gotsrpcErr *gotsrpc.Error serviceErr, err := c.WrappedError(ctx) if err != nil { @@ -126,7 +126,7 @@ func call() { } { - fmt.Println("-------------------------") + fmt.Println("--- ScalarError ----------------") var scalarErr *backend.ScalarError var gotsrpcErr *gotsrpc.Error serviceErr, err := c.ScalarError(ctx) @@ -148,7 +148,7 @@ func call() { } { - fmt.Println("-------------------------") + fmt.Println("--- CustomError ----------------") var customErr *backend.CustomError var gotsrpcErr *gotsrpc.Error serviceErr, err := c.CustomError(ctx) @@ -170,7 +170,7 @@ func call() { } { - fmt.Println("-------------------------") + fmt.Println("--- TypedError -----------------") serviceErr, err := c.TypedError(ctx) if err != nil { panic("client error should be nil") @@ -185,7 +185,7 @@ func call() { } { - fmt.Println("-------------------------") + fmt.Println("--- TypedWrappedError ----------") serviceErr, err := c.TypedWrappedError(ctx) if err != nil { panic("client error should be nil") @@ -200,7 +200,7 @@ func call() { } { - fmt.Println("-------------------------") + fmt.Println("--- TypedCustomError -----------") serviceErr, err := c.TypedCustomError(ctx) if err != nil { panic("client error should be nil") diff --git a/example/monitor/go.mod b/example/monitor/go.mod index 0774e50..641b86a 100644 --- a/example/monitor/go.mod +++ b/example/monitor/go.mod @@ -3,7 +3,7 @@ module github.com/foomo/gotsrpc/v2/example/monitor go 1.16 require ( - github.com/foomo/gotsrpc/v2 v2.0.10 + github.com/foomo/gotsrpc/v2 v2.0.0 github.com/pkg/errors v0.9.1 github.com/valyala/gorpc v0.0.0-20160519171614-908281bef774 ) diff --git a/example/nullable/go.mod b/example/nullable/go.mod index 0a9f8ea..b23b7c9 100644 --- a/example/nullable/go.mod +++ b/example/nullable/go.mod @@ -3,7 +3,7 @@ module github.com/foomo/gotsrpc/v2/example/nullable go 1.16 require ( - github.com/foomo/gotsrpc/v2 v2.0.10 + github.com/foomo/gotsrpc/v2 v2.0.0 github.com/pkg/errors v0.9.1 github.com/valyala/gorpc v0.0.0-20160519171614-908281bef774 ) diff --git a/example/time/go.mod b/example/time/go.mod index d254319..af1a0f9 100644 --- a/example/time/go.mod +++ b/example/time/go.mod @@ -4,7 +4,7 @@ go 1.16 require ( github.com/davecgh/go-spew v1.1.1 - github.com/foomo/gotsrpc/v2 v2.0.10 + github.com/foomo/gotsrpc/v2 v2.0.0 github.com/pkg/errors v0.9.1 github.com/valyala/gorpc v0.0.0-20160519171614-908281bef774 ) diff --git a/gotsrpc.go b/gotsrpc.go index 78fcd73..8cb041d 100644 --- a/gotsrpc.go +++ b/gotsrpc.go @@ -108,6 +108,20 @@ func Reply(response []interface{}, stats *CallStats, r *http.Request, w http.Res if stats != nil { stats.ResponseSize = writer.length stats.Marshalling = time.Since(serializationStart) + if len(response) > 0 { + errResp := response[len(response)-1] + if v, ok := errResp.(interface { + Error() string + }); ok { + stats.ErrorCode = 1 + stats.ErrorMessage = v.Error() + } + if v, ok := errResp.(interface { + ErrorCode() int + }); ok { + stats.ErrorCode = v.ErrorCode() + } + } } return nil } diff --git a/stats.go b/stats.go index 0779b45..4a4c08e 100644 --- a/stats.go +++ b/stats.go @@ -3,10 +3,14 @@ package gotsrpc import "time" type CallStats struct { - Package, Service, Func string - Execution time.Duration - Marshalling time.Duration - Unmarshalling time.Duration - RequestSize int - ResponseSize int + Package string + Service string + Func string + Execution time.Duration + Marshalling time.Duration + Unmarshalling time.Duration + RequestSize int + ResponseSize int + ErrorCode int + ErrorMessage string }