Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Refactored more logging - standardized the interface
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Apr 14, 2022
1 parent 49ddae7 commit 8e44a02
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 68 deletions.
30 changes: 16 additions & 14 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ type (

// notificationsOptions holds the configuration for notifications
notificationsOptions struct {
client notifications.ClientInterface // Notifications client
webhookEndpoint string // Webhook endpoint
notifications.ClientInterface // Notifications client
options []notifications.ClientOps // List of options
webhookEndpoint string // Webhook endpoint
}

// paymailOptions holds the configuration for Paymail
Expand Down Expand Up @@ -153,7 +154,7 @@ func NewClient(ctx context.Context, opts ...ClientOps) (ClientInterface, error)
return nil, err
}

// Load the Paymail client (if client does not exist)
// Load the Notification client (if client does not exist)
if err = client.loadNotificationClient(); err != nil {
return nil, err
}
Expand Down Expand Up @@ -289,26 +290,27 @@ func (c *Client) Debug(on bool) {
c.options.debug = on

// Set debugging on the Cachestore
cs := c.Cachestore()
if cs != nil {
if cs := c.Cachestore(); cs != nil {
cs.Debug(on)
}

// Set debugging on the Chainstate
ch := c.Chainstate()
if ch != nil {
if ch := c.Chainstate(); ch != nil {
ch.Debug(on)
}

// Set debugging on the Datastore
ds := c.Datastore()
if ds != nil {
if ds := c.Datastore(); ds != nil {
ds.Debug(on)
}

// Set debugging on the Notifications
if n := c.Notifications(); n != nil {
n.Debug(on)
}

// Set debugging on the Taskmanager
tm := c.Taskmanager()
if tm != nil {
if tm := c.Taskmanager(); tm != nil {
tm.Debug(on)
}
}
Expand Down Expand Up @@ -400,15 +402,15 @@ func (c *Client) ModifyTaskPeriod(name string, period time.Duration) error {

// Notifications will return the Notifications if it exists
func (c *Client) Notifications() notifications.ClientInterface {
if c.options.notifications != nil && c.options.notifications.client != nil {
return c.options.notifications.client
if c.options.notifications != nil && c.options.notifications.ClientInterface != nil {
return c.options.notifications.ClientInterface
}
return nil
}

// SetNotificationsClient will overwrite the notification's client with the given client
func (c *Client) SetNotificationsClient(client notifications.ClientInterface) {
c.options.notifications.client = client
c.options.notifications.ClientInterface = client
}

// Taskmanager will return the Taskmanager if it exists
Expand Down
16 changes: 4 additions & 12 deletions client_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,10 @@ func (c *Client) loadDatastore(ctx context.Context) (err error) {

// loadNotificationClient will load the notifications client
func (c *Client) loadNotificationClient() (err error) {
// Only load if it's not set (the client can be overloaded)
if c.options.notifications.client == nil {
opts := []notifications.ClientOps{
notifications.WithNotifications(c.options.notifications.webhookEndpoint),
}
if c.options.logger != nil {
opts = append(opts, notifications.WithLogger(c.options.logger))
}
if c.options.debug {
opts = append(opts, notifications.WithDebug())
}
c.options.notifications.client, err = notifications.NewClient(opts...)

// Load notification if a custom interface was NOT provided
if c.options.notifications.ClientInterface == nil {
c.options.notifications.ClientInterface, err = notifications.NewClient(c.options.notifications.options...)
}
return
}
Expand Down
16 changes: 12 additions & 4 deletions client_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ func defaultClientOptions() *clientOptions {
newRelic: &newRelicOptions{},

// Blank notifications config
notifications: &notificationsOptions{},
notifications: &notificationsOptions{
ClientInterface: nil,
webhookEndpoint: "",
},

// Blank Paymail config
paymail: &paymailOptions{
Expand Down Expand Up @@ -182,6 +185,7 @@ func WithNewRelic(app *newrelic.Application) ClientOps {
c.chainstate.options = append(c.chainstate.options, chainstate.WithNewRelic())
c.dataStore.options = append(c.dataStore.options, datastore.WithNewRelic())
c.taskManager.options = append(c.taskManager.options, taskmanager.WithNewRelic())
// c.notifications.options = append(c.notifications.options, notifications.WithNewRelic())

// Enable the service
c.newRelic.enabled = true
Expand All @@ -197,6 +201,7 @@ func WithDebugging() ClientOps {
c.cacheStore.options = append(c.cacheStore.options, cachestore.WithDebugging())
c.chainstate.options = append(c.chainstate.options, chainstate.WithDebugging())
c.dataStore.options = append(c.dataStore.options, datastore.WithDebugging())
c.notifications.options = append(c.notifications.options, notifications.WithDebugging())
c.taskManager.options = append(c.taskManager.options, taskmanager.WithDebugging())
}
}
Expand Down Expand Up @@ -246,6 +251,7 @@ func WithLogger(customLogger logger.Interface) ClientOps {
c.chainstate.options = append(c.chainstate.options, chainstate.WithLogger(c.logger))
c.dataStore.options = append(c.dataStore.options, datastore.WithLogger(&datastore.DatabaseLogWrapper{Interface: c.logger}))
c.taskManager.options = append(c.taskManager.options, taskmanager.WithLogger(c.logger))
c.notifications.options = append(c.notifications.options, notifications.WithLogger(c.logger))
}
}
}
Expand Down Expand Up @@ -581,9 +587,11 @@ func WithNotifications(webhookEndpoint string) ClientOps {
}
}

// WithNotificationsInterface will set a notifications interface
func WithNotificationsInterface(notify notifications.ClientInterface) ClientOps {
// WithCustomNotifications will set a custom notifications interface
func WithCustomNotifications(customNotifications notifications.ClientInterface) ClientOps {
return func(c *clientOptions) {
c.notifications.client = notify
if customNotifications != nil {
c.notifications.ClientInterface = customNotifications
}
}
}
5 changes: 3 additions & 2 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
func TestClient_Debug(t *testing.T) {
t.Parallel()

t.Run("load basic Datastore and Cachestore with debug", func(t *testing.T) {
t.Run("load basic with debug", func(t *testing.T) {
tc, err := NewClient(
tester.GetNewRelicCtx(t, defaultNewRelicApp, defaultNewRelicTx),
DefaultClientOpts(false, true)...,
Expand All @@ -32,8 +32,9 @@ func TestClient_Debug(t *testing.T) {
tc.Debug(true)

assert.Equal(t, true, tc.IsDebug())
assert.Equal(t, true, tc.Datastore().IsDebug())
assert.Equal(t, true, tc.Cachestore().IsDebug())
assert.Equal(t, true, tc.Datastore().IsDebug())
assert.Equal(t, true, tc.Notifications().IsDebug())
assert.Equal(t, true, tc.Taskmanager().IsDebug())
})
}
Expand Down
2 changes: 1 addition & 1 deletion interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ type ClientServices interface {
Chainstate() chainstate.ClientInterface
Datastore() datastore.ClientInterface
Logger() logger.Interface
Notifications() notifications.ClientInterface
PaymailClient() paymail.ClientInterface
Taskmanager() taskmanager.ClientInterface
}
Expand Down Expand Up @@ -112,7 +113,6 @@ type ClientInterface interface {
IsIUCEnabled() bool
IsNewRelicEnabled() bool
ModifyTaskPeriod(name string, period time.Duration) error
Notifications() notifications.ClientInterface
SetNotificationsClient(notifications.ClientInterface)
UserAgent() string
Version() string
Expand Down
24 changes: 22 additions & 2 deletions notifications/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package notifications

import "gorm.io/gorm/logger"
import "github.com/BuxOrg/bux/logger"

// EventType event types thrown in Bux
type EventType string
Expand Down Expand Up @@ -29,8 +29,8 @@ type (
// clientOptions holds all the configuration for the client
clientOptions struct {
config *notificationsConfig // Configuration for broadcasting and other chain-state actions
httpClient HTTPInterface
debug bool
httpClient HTTPInterface
logger logger.Interface
}

Expand All @@ -53,6 +53,26 @@ func NewClient(opts ...ClientOps) (ClientInterface, error) {
opt(client.options)
}

// Set logger if not set
if client.options.logger == nil {
client.options.logger = logger.NewLogger(client.IsDebug())
}

// Return the client
return client, nil
}

// IsDebug will return if debugging is enabled
func (c *Client) IsDebug() bool {
return c.options.debug
}

// Debug will set the debug flag
func (c *Client) Debug(on bool) {
c.options.debug = on
}

// Logger get the logger
func (c *Client) Logger() logger.Interface {
return c.options.logger
}
11 changes: 6 additions & 5 deletions notifications/client_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"net/http"
"time"

"gorm.io/gorm/logger"
"github.com/BuxOrg/bux/logger"
)

const (
Expand All @@ -25,6 +25,7 @@ func defaultClientOptions() *clientOptions {
config: &notificationsConfig{
webhookEndpoint: "",
},
logger: nil,
httpClient: &http.Client{
Timeout: defaultHTTPTimeout,
},
Expand All @@ -39,14 +40,14 @@ func WithNotifications(webhookEndpoint string) ClientOps {
}

// WithLogger will set the logger
func WithLogger(log logger.Interface) ClientOps {
func WithLogger(customLogger logger.Interface) ClientOps {
return func(c *clientOptions) {
c.logger = log
c.logger = customLogger
}
}

// WithDebug will set debugging on notifications
func WithDebug() ClientOps {
// WithDebugging will set debugging on notifications
func WithDebugging() ClientOps {
return func(c *clientOptions) {
c.debug = true
}
Expand Down
11 changes: 2 additions & 9 deletions notifications/client_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
package notifications

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewClient(t *testing.T) {
/*func TestNewClient(t *testing.T) {
type args struct {
opts []ClientOps
}
Expand All @@ -33,4 +26,4 @@ func TestNewClient(t *testing.T) {
assert.Equalf(t, tt.want, got, "NewClient(%v)", tt.args.opts)
})
}
}
}*/
6 changes: 4 additions & 2 deletions notifications/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"net/http"

"gorm.io/gorm/logger"
"github.com/BuxOrg/bux/logger"
)

// HTTPInterface is the HTTP client interface
Expand All @@ -14,7 +14,9 @@ type HTTPInterface interface {

// ClientInterface is the notification client interface
type ClientInterface interface {
Notify(ctx context.Context, modelType string, eventType EventType, model interface{}, id string) error
Debug(on bool)
GetWebhookEndpoint() string
IsDebug() bool
Logger() logger.Interface
Notify(ctx context.Context, modelType string, eventType EventType, model interface{}, id string) error
}
26 changes: 10 additions & 16 deletions notifications/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,27 @@ import (
"encoding/json"
"fmt"
"net/http"

"gorm.io/gorm/logger"
)

// GetWebhookEndpoint get the configured webhook endpoint
// GetWebhookEndpoint will get the configured webhook endpoint
func (c *Client) GetWebhookEndpoint() string {
return c.options.config.webhookEndpoint
}

// Logger get the logger
func (c *Client) Logger() logger.Interface {
return c.options.logger
}

// Notify create a new notification
func (c *Client) Notify(ctx context.Context, modelType string, eventType EventType, model interface{}, id string) error {
// Notify will create a new notification
func (c *Client) Notify(ctx context.Context, modelType string, eventType EventType,
model interface{}, id string) error {

if len(c.options.config.webhookEndpoint) == 0 {
if c.options.debug && c.Logger() != nil {
if c.IsDebug() {
c.Logger().Info(ctx, fmt.Sprintf("NOTIFY %s: %s - %v", eventType, id, model))
}
} else {
jsonData, err := json.Marshal(map[string]interface{}{
"modelType": modelType,
"eventType": eventType,
"id": id,
"model": model,
"modelType": modelType,
})
if err != nil {
return err
Expand All @@ -57,10 +51,10 @@ func (c *Client) Notify(ctx context.Context, modelType string, eventType EventTy

if response.StatusCode != http.StatusOK {
// todo queue notification for another try ...
if c.Logger() != nil {
c.Logger().Error(ctx, fmt.Sprintf("%s: %d", "received invalid response from notification endpoint: ",
response.StatusCode))
}
c.Logger().Error(ctx, fmt.Sprintf(
"%s: %d",
"received invalid response from notification endpoint: ",
response.StatusCode))
}
}

Expand Down
2 changes: 1 addition & 1 deletion notifications/notifications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestClient_Notify(t *testing.T) {
httpMock: func() {
httpmock.RegisterResponder(http.MethodPost, webhookURL,
httpmock.NewStringResponder(
200,
http.StatusOK,
`OK`,
),
)
Expand Down

0 comments on commit 8e44a02

Please sign in to comment.