Skip to content

Commit

Permalink
Fix error casting on metrics report
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre Hahn committed Aug 8, 2019
1 parent a2fab1a commit b07f08b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
9 changes: 3 additions & 6 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"sync/atomic"
"time"

opentracing "github.com/opentracing/opentracing-go"
"github.com/topfreegames/pitaya/conn/codec"
"github.com/topfreegames/pitaya/conn/message"
"github.com/topfreegames/pitaya/conn/packet"
Expand All @@ -45,6 +44,8 @@ import (
"github.com/topfreegames/pitaya/tracing"
"github.com/topfreegames/pitaya/util"
"github.com/topfreegames/pitaya/util/compression"

opentracing "github.com/opentracing/opentracing-go"
)

var (
Expand Down Expand Up @@ -404,13 +405,9 @@ func (a *Agent) write() {
tracing.FinishSpan(data.ctx, e)
if data.typ == message.Response {
var rErr error

if m.Err {
// default code is overwritten, if any
rErr = &errors.Error{Code: errors.ErrUnknownCode}
_ = a.serializer.Unmarshal(payload, rErr)
rErr = util.GetErrorFromPayload(a.serializer, payload)
}

metrics.ReportTimingFromCtx(data.ctx, a.metricsReporters, handlerType, rErr)
}
case <-a.chStopWrite:
Expand Down
23 changes: 20 additions & 3 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,20 @@ import (
"reflect"
"runtime/debug"

"github.com/google/uuid"
opentracing "github.com/opentracing/opentracing-go"
"github.com/sirupsen/logrus"
"github.com/topfreegames/pitaya/conn/message"
"github.com/topfreegames/pitaya/constants"
pcontext "github.com/topfreegames/pitaya/context"
e "github.com/topfreegames/pitaya/errors"
"github.com/topfreegames/pitaya/logger"
"github.com/topfreegames/pitaya/protos"
"github.com/topfreegames/pitaya/serialize"
"github.com/topfreegames/pitaya/serialize/json"
"github.com/topfreegames/pitaya/serialize/protobuf"
"github.com/topfreegames/pitaya/tracing"

"github.com/google/uuid"
opentracing "github.com/opentracing/opentracing-go"
"github.com/sirupsen/logrus"
)

func getLoggerFromArgs(args []reflect.Value) logger.Logger {
Expand Down Expand Up @@ -116,6 +119,20 @@ func FileExists(filename string) bool {
return err == nil || os.IsExist(err)
}

// GetErrorFromPayload gets the error from payload
func GetErrorFromPayload(serializer serialize.Serializer, payload []byte) error {
err := &e.Error{Code: e.ErrUnknownCode}
switch serializer.(type) {
case *json.Serializer:
_ = serializer.Unmarshal(payload, err)
case *protobuf.Serializer:
pErr := &protos.Error{Code: e.ErrUnknownCode}
_ = serializer.Unmarshal(payload, pErr)
err = &e.Error{Code: pErr.Code, Message: pErr.Msg, Metadata: pErr.Metadata}
}
return err
}

// GetErrorPayload creates and serializes an error payload
func GetErrorPayload(serializer serialize.Serializer, err error) ([]byte, error) {
code := e.ErrUnknownCode
Expand Down

0 comments on commit b07f08b

Please sign in to comment.