From 38f0296425fc7b013f7b573d68abba50fd48d644 Mon Sep 17 00:00:00 2001 From: Neur0toxine Date: Thu, 19 Sep 2024 17:09:58 +0300 Subject: [PATCH] stream id support, minor tweaks --- core/logger/attrs.go | 13 ++++++++++ core/logger/attrs_test.go | 29 +++++++++++++++++++--- core/logger/json_with_context_encoder.go | 2 +- core/logger/mg_transport_client_adapter.go | 8 +++++- core/logger/zap.go | 4 +-- 5 files changed, 48 insertions(+), 8 deletions(-) diff --git a/core/logger/attrs.go b/core/logger/attrs.go index 06ec6e0..66ce7b0 100644 --- a/core/logger/attrs.go +++ b/core/logger/attrs.go @@ -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) { diff --git a/core/logger/attrs_test.go b/core/logger/attrs_test.go index 2bb519d..376a719 100644 --- a/core/logger/attrs_test.go +++ b/core/logger/attrs_test.go @@ -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}`), diff --git a/core/logger/json_with_context_encoder.go b/core/logger/json_with_context_encoder.go index cc3a8a1..79b1a2f 100644 --- a/core/logger/json_with_context_encoder.go +++ b/core/logger/json_with_context_encoder.go @@ -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 diff --git a/core/logger/mg_transport_client_adapter.go b/core/logger/mg_transport_client_adapter.go index 0b8569b..63fb9bd 100644 --- a/core/logger/mg_transport_client_adapter.go +++ b/core/logger/mg_transport_client_adapter.go @@ -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} } diff --git a/core/logger/zap.go b/core/logger/zap.go index 1b8730b..e8bd2a9 100644 --- a/core/logger/zap.go +++ b/core/logger/zap.go @@ -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",