Skip to content

Commit

Permalink
version: minorMFW 2622 send alerts to cloud (#146)
Browse files Browse the repository at this point in the history
version: minor

* add timestamp to protobuf alert; set timestamp in Send func;

* made credentials manager more mockable
  • Loading branch information
AlexandruBungiu authored Feb 28, 2023
1 parent 535ad08 commit 52234b5
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 37 deletions.
1 change: 1 addition & 0 deletions protobuffersrc/Alerts.proto
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ message Alert {
AlertSeverity severity = 2;
string message = 3;
map<string, string> params = 4;
int64 timestamp = 5;
}
9 changes: 8 additions & 1 deletion services/alerts/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package alerts

import (
"errors"
"sync"
"time"

zmq "github.com/pebbe/zmq4"
"github.com/untangle/golang-shared/services/logger"
"github.com/untangle/golang-shared/structs/protocolbuffers/Alerts"
"google.golang.org/protobuf/proto"
"sync"
)

var alertPublisherSingleton *ZmqAlertPublisher
Expand Down Expand Up @@ -81,6 +83,11 @@ func (publisher *ZmqAlertPublisher) Shutdown() error {

// Send publishes the alert to on the ZMQ publishing socket.
func (publisher *ZmqAlertPublisher) Send(alert *Alerts.Alert) {
// 2 reasons to set the timestamp here:
// - the caller isn't responsible for setting the timestamp so we just need to set it in one place (here)
// - we set it before putting it in queue, which means we have the timestamp of the alert creation, not the timestamp when it was processed
alert.Timestamp = time.Now().Unix()

logger.Debug("Publish alert %v\n", alert)
alertMessage, err := proto.Marshal(alert)
if err != nil {
Expand Down
70 changes: 40 additions & 30 deletions structs/protocolbuffers/Alerts/Alerts.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 14 additions & 6 deletions testing/mocks/credentials_manager.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
package mocks

import "github.com/untangle/golang-shared/services/credentialsmanager"
import (
"github.com/stretchr/testify/mock"
)

type mockCredentialsManager struct{}
type mockCredentialsManager struct {
mock.Mock
}

func NewMockCredentialsManager() credentialsmanager.CredentialsManager {
func NewMockCredentialsManager() *mockCredentialsManager {
return &mockCredentialsManager{}
}

func (m *mockCredentialsManager) Startup() error { return nil }
func (m *mockCredentialsManager) Shutdown() error { return nil }
func (m *mockCredentialsManager) GetToken(key string) string { return "" }
func (m *mockCredentialsManager) Startup() error { return nil }
func (m *mockCredentialsManager) Shutdown() error { return nil }
func (m *mockCredentialsManager) GetToken(key string) string {
// this is required for mocking this function's call result
args := m.Called(key)
return args.String(0)
}
func (m *mockCredentialsManager) Name() string {
return "Mocked Credentials Manager"
}

0 comments on commit 52234b5

Please sign in to comment.