stream id support, minor tweaks

This commit is contained in:
Pavel 2024-09-19 17:14:00 +03:00 committed by GitHub
commit 4dcb31faae
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 48 additions and 8 deletions

View File

@ -19,6 +19,9 @@ const ConnectionAttr = "connection"
// AccountAttr represents the attribute name for the account. // AccountAttr represents the attribute name for the account.
const AccountAttr = "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. // CounterIDAttr represents the attribute name for the counter ID.
const CounterIDAttr = "counterId" const CounterIDAttr = "counterId"
@ -63,6 +66,16 @@ func HTTPStatusName(code int) zap.Field {
return zap.String(HTTPStatusNameAttr, http.StatusText(code)) 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. // Body returns a zap.Field with the given request body value.
func Body(val any) zap.Field { func Body(val any) zap.Field {
switch item := val.(type) { switch item := val.(type) {

View File

@ -60,6 +60,27 @@ func TestHTTPStatusName(t *testing.T) {
assert.Equal(t, http.StatusText(http.StatusOK), val.String) 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) { func TestBody(t *testing.T) {
var cases = []struct { var cases = []struct {
input interface{} input interface{}
@ -74,8 +95,8 @@ func TestBody(t *testing.T) {
result: nil, result: nil,
}, },
{ {
input: "ooga booga", input: "test body",
result: "ooga booga", result: "test body",
}, },
{ {
input: `{"success":true}`, input: `{"success":true}`,
@ -90,8 +111,8 @@ func TestBody(t *testing.T) {
result: nil, result: nil,
}, },
{ {
input: []byte("ooga booga"), input: []byte("test body"),
result: "ooga booga", result: "test body",
}, },
{ {
input: []byte(`{"success":true}`), input: []byte(`{"success":true}`),

View File

@ -458,7 +458,7 @@ func addFields(enc zapcore.ObjectEncoder, fields []zapcore.Field) {
hasEntries := false hasEntries := false
for _, f := range fields { for _, f := range fields {
switch f.Key { switch f.Key {
case HandlerAttr, ConnectionAttr, AccountAttr: case HandlerAttr, ConnectionAttr, AccountAttr, StreamIDAttr:
f.AddTo(enc) f.AddTo(enc)
default: default:
hasEntries = true hasEntries = true

View File

@ -14,12 +14,18 @@ const (
mgDebugLogResp = "MG TRANSPORT API Response: %s" mgDebugLogResp = "MG TRANSPORT API Response: %s"
) )
// MGTransportClientLogger implements both v1.BasicLogger and v1.DebugLogger.
type MGTransportClientLogger interface {
v1.BasicLogger
v1.DebugLogger
}
type mgTransportClientAdapter struct { type mgTransportClientAdapter struct {
log Logger log Logger
} }
// MGTransportClientAdapter constructs an adapter that will log MG requests and responses. // 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} return &mgTransportClientAdapter{log: log}
} }

View File

@ -44,7 +44,7 @@ func EncoderConfigConsole() zapcore.EncoderConfig {
LevelKey: "level", LevelKey: "level",
TimeKey: "datetime", TimeKey: "datetime",
NameKey: "logger", NameKey: "logger",
CallerKey: "caller", CallerKey: "",
FunctionKey: zapcore.OmitKey, FunctionKey: zapcore.OmitKey,
StacktraceKey: "", StacktraceKey: "",
LineEnding: "\n", LineEnding: "\n",
@ -88,7 +88,7 @@ func EncoderConfigJSON() zapcore.EncoderConfig {
LevelKey: "level_name", LevelKey: "level_name",
TimeKey: "datetime", TimeKey: "datetime",
NameKey: "logger", NameKey: "logger",
CallerKey: "caller", CallerKey: "",
FunctionKey: zapcore.OmitKey, FunctionKey: zapcore.OmitKey,
StacktraceKey: "", StacktraceKey: "",
LineEnding: "\n", LineEnding: "\n",