mirror of
https://github.com/retailcrm/mg-transport-core.git
synced 2024-11-21 20:56:04 +03:00
stream id support, minor tweaks
This commit is contained in:
parent
6bd6171024
commit
38f0296425
@ -19,6 +19,9 @@ const ConnectionAttr = "connection"
|
||||
// AccountAttr represents the attribute name for the account.
|
||||
const AccountAttr = "account"
|
||||
|
||||
// StreamIDAttr represents the workflow stream identifier (for example, all the processes triggered by one request).
|
||||
const StreamIDAttr = "streamId"
|
||||
|
||||
// CounterIDAttr represents the attribute name for the counter ID.
|
||||
const CounterIDAttr = "counterId"
|
||||
|
||||
@ -63,6 +66,16 @@ func HTTPStatusName(code int) zap.Field {
|
||||
return zap.String(HTTPStatusNameAttr, http.StatusText(code))
|
||||
}
|
||||
|
||||
// StreamID returns a zap.Fields with the give stream ID.
|
||||
func StreamID(val any) zap.Field {
|
||||
switch item := val.(type) {
|
||||
case string:
|
||||
return zap.String(StreamIDAttr, item)
|
||||
default:
|
||||
return zap.Any(StreamIDAttr, item)
|
||||
}
|
||||
}
|
||||
|
||||
// Body returns a zap.Field with the given request body value.
|
||||
func Body(val any) zap.Field {
|
||||
switch item := val.(type) {
|
||||
|
@ -60,6 +60,27 @@ func TestHTTPStatusName(t *testing.T) {
|
||||
assert.Equal(t, http.StatusText(http.StatusOK), val.String)
|
||||
}
|
||||
|
||||
func TestStreamID(t *testing.T) {
|
||||
var cases = []struct {
|
||||
input interface{}
|
||||
result interface{}
|
||||
}{
|
||||
{
|
||||
input: "",
|
||||
result: "",
|
||||
},
|
||||
{
|
||||
input: "test body",
|
||||
result: "test body",
|
||||
},
|
||||
}
|
||||
for _, c := range cases {
|
||||
val := StreamID(c.input)
|
||||
assert.Equal(t, StreamIDAttr, val.Key)
|
||||
assert.Equal(t, c.result, val.String)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBody(t *testing.T) {
|
||||
var cases = []struct {
|
||||
input interface{}
|
||||
@ -74,8 +95,8 @@ func TestBody(t *testing.T) {
|
||||
result: nil,
|
||||
},
|
||||
{
|
||||
input: "ooga booga",
|
||||
result: "ooga booga",
|
||||
input: "test body",
|
||||
result: "test body",
|
||||
},
|
||||
{
|
||||
input: `{"success":true}`,
|
||||
@ -90,8 +111,8 @@ func TestBody(t *testing.T) {
|
||||
result: nil,
|
||||
},
|
||||
{
|
||||
input: []byte("ooga booga"),
|
||||
result: "ooga booga",
|
||||
input: []byte("test body"),
|
||||
result: "test body",
|
||||
},
|
||||
{
|
||||
input: []byte(`{"success":true}`),
|
||||
|
@ -458,7 +458,7 @@ func addFields(enc zapcore.ObjectEncoder, fields []zapcore.Field) {
|
||||
hasEntries := false
|
||||
for _, f := range fields {
|
||||
switch f.Key {
|
||||
case HandlerAttr, ConnectionAttr, AccountAttr:
|
||||
case HandlerAttr, ConnectionAttr, AccountAttr, StreamIDAttr:
|
||||
f.AddTo(enc)
|
||||
default:
|
||||
hasEntries = true
|
||||
|
@ -14,12 +14,18 @@ const (
|
||||
mgDebugLogResp = "MG TRANSPORT API Response: %s"
|
||||
)
|
||||
|
||||
// MGTransportClientLogger implements both v1.BasicLogger and v1.DebugLogger.
|
||||
type MGTransportClientLogger interface {
|
||||
v1.BasicLogger
|
||||
v1.DebugLogger
|
||||
}
|
||||
|
||||
type mgTransportClientAdapter struct {
|
||||
log Logger
|
||||
}
|
||||
|
||||
// MGTransportClientAdapter constructs an adapter that will log MG requests and responses.
|
||||
func MGTransportClientAdapter(log Logger) v1.BasicLogger {
|
||||
func MGTransportClientAdapter(log Logger) MGTransportClientLogger {
|
||||
return &mgTransportClientAdapter{log: log}
|
||||
}
|
||||
|
||||
|
@ -44,7 +44,7 @@ func EncoderConfigConsole() zapcore.EncoderConfig {
|
||||
LevelKey: "level",
|
||||
TimeKey: "datetime",
|
||||
NameKey: "logger",
|
||||
CallerKey: "caller",
|
||||
CallerKey: "",
|
||||
FunctionKey: zapcore.OmitKey,
|
||||
StacktraceKey: "",
|
||||
LineEnding: "\n",
|
||||
@ -88,7 +88,7 @@ func EncoderConfigJSON() zapcore.EncoderConfig {
|
||||
LevelKey: "level_name",
|
||||
TimeKey: "datetime",
|
||||
NameKey: "logger",
|
||||
CallerKey: "caller",
|
||||
CallerKey: "",
|
||||
FunctionKey: zapcore.OmitKey,
|
||||
StacktraceKey: "",
|
||||
LineEnding: "\n",
|
||||
|
Loading…
Reference in New Issue
Block a user