-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:Add a logging handler and insert it a few places for help debugg…
…ing things.
- Loading branch information
Showing
3 changed files
with
198 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// SPDX-FileCopyrightText: 2025 Comcast Cable Communications Management, LLC | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package loghandler | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/xmidt-org/wrp-go/v3" | ||
"github.com/xmidt-org/xmidt-agent/internal/wrpkit" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
var ( | ||
ErrInvalidInput = fmt.Errorf("invalid input") | ||
) | ||
|
||
// Handler logs information about the message being processed and sends the | ||
// message to the next handler in the chain. | ||
type Handler struct { | ||
next wrpkit.Handler | ||
logger *zap.Logger | ||
level zapcore.Level | ||
} | ||
|
||
// New creates a new Handler. If the next handler is nil or the logger is nil, | ||
// an error is returned. If no level is provided, DebugLevel is used. | ||
func New(next wrpkit.Handler, logger *zap.Logger, level ...zapcore.Level) (*Handler, error) { | ||
if next == nil || logger == nil { | ||
return nil, ErrInvalidInput | ||
} | ||
|
||
level = append(level, zapcore.DebugLevel) | ||
|
||
return &Handler{ | ||
next: next, | ||
logger: logger, | ||
level: level[0], | ||
}, nil | ||
} | ||
|
||
// HandleWrp is called to process a message. If the next handler fails to | ||
// process the message, a response is sent to the source of the message. | ||
func (h Handler) HandleWrp(msg wrp.Message) error { | ||
fields := []zap.Field{ | ||
zap.String("type", msg.Type.String()), | ||
zap.String("source", msg.Source), | ||
zap.String("dest", msg.Destination), | ||
zap.Strings("partnerids", msg.PartnerIDs), | ||
zap.Int("payload_size", len(msg.Payload)), | ||
} | ||
|
||
if msg.TransactionUUID != "" { | ||
fields = append(fields, zap.String("transaction_uuid", msg.TransactionUUID)) | ||
} | ||
|
||
h.logger.Log(h.level, "Handling message", fields...) | ||
|
||
return h.next.HandleWrp(msg) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// SPDX-FileCopyrightText: 2025 Comcast Cable Communications Management, LLC | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package loghandler | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
"github.com/xmidt-org/wrp-go/v3" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
type MockHandler struct { | ||
mock.Mock | ||
} | ||
|
||
func (m *MockHandler) HandleWrp(msg wrp.Message) error { | ||
args := m.Called(msg) | ||
return args.Error(0) | ||
} | ||
|
||
func TestNew(t *testing.T) { | ||
logger := zap.NewNop() | ||
next := &MockHandler{} | ||
|
||
handler, err := New(next, logger) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, handler) | ||
assert.Equal(t, zapcore.DebugLevel, handler.level) | ||
|
||
handler, err = New(nil, logger) | ||
assert.Error(t, err) | ||
assert.Nil(t, handler) | ||
|
||
handler, err = New(next, nil) | ||
assert.Error(t, err) | ||
assert.Nil(t, handler) | ||
} | ||
|
||
func TestHandleWrp(t *testing.T) { | ||
logger, _ := zap.NewDevelopment() | ||
next := &MockHandler{} | ||
handler, err := New(next, logger) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, handler) | ||
|
||
msg := wrp.Message{ | ||
Type: wrp.SimpleRequestResponseMessageType, | ||
Source: "source", | ||
Destination: "destination", | ||
Payload: []byte("payload"), | ||
TransactionUUID: "uuid", | ||
} | ||
|
||
next.On("HandleWrp", msg).Return(nil) | ||
|
||
err = handler.HandleWrp(msg) | ||
assert.NoError(t, err) | ||
next.AssertExpectations(t) | ||
} |