-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: report logs back to the api (#6)
- Loading branch information
1 parent
0dc6200
commit 233bf0e
Showing
8 changed files
with
244 additions
and
66 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
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,52 @@ | ||
package mock | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"github.com/castai/cluster-controller/castai" | ||
) | ||
|
||
func NewMockAPIClient(actions []*castai.ClusterAction) *mockClient { | ||
return &mockClient{Actions: actions} | ||
} | ||
|
||
type mockAck struct { | ||
ActionID string | ||
Err *string | ||
} | ||
|
||
type mockClient struct { | ||
Actions []*castai.ClusterAction | ||
Logs []*castai.LogEvent | ||
Acks []*mockAck | ||
mu sync.Mutex | ||
} | ||
|
||
func (m *mockClient) GetActions(_ context.Context) ([]*castai.ClusterAction, error) { | ||
return m.Actions, nil | ||
} | ||
|
||
func (m *mockClient) SendLogs(_ context.Context, req *castai.LogEvent) error { | ||
m.mu.Lock() | ||
m.Logs = append(m.Logs, req) | ||
m.mu.Unlock() | ||
return nil | ||
} | ||
|
||
func (m *mockClient) AckAction(_ context.Context, actionID string, req *castai.AckClusterActionRequest) error { | ||
m.removeAckedActions(actionID) | ||
|
||
m.Acks = append(m.Acks, &mockAck{ActionID: actionID, Err: req.Error}) | ||
return nil | ||
} | ||
|
||
func (m *mockClient) removeAckedActions(actionID string) { | ||
var remaining []*castai.ClusterAction | ||
for _, action := range m.Actions { | ||
if action.ID != actionID { | ||
remaining = append(remaining, action) | ||
} | ||
} | ||
m.Actions = remaining | ||
} |
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,81 @@ | ||
package log | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"time" | ||
|
||
"github.com/cenkalti/backoff/v4" | ||
"github.com/sirupsen/logrus" | ||
|
||
"github.com/castai/cluster-controller/castai" | ||
) | ||
|
||
const ( | ||
sendTimeout = 15 * time.Second | ||
) | ||
|
||
type Exporter interface { | ||
logrus.Hook | ||
Wait() | ||
} | ||
|
||
func NewExporter(logger *logrus.Logger, client castai.Client) Exporter { | ||
return &exporter{ | ||
logger: logger, | ||
client: client, | ||
wg: sync.WaitGroup{}, | ||
} | ||
} | ||
|
||
type exporter struct { | ||
logger *logrus.Logger | ||
client castai.Client | ||
wg sync.WaitGroup | ||
} | ||
|
||
func (e *exporter) Levels() []logrus.Level { | ||
return []logrus.Level{ | ||
logrus.ErrorLevel, | ||
logrus.FatalLevel, | ||
logrus.PanicLevel, | ||
logrus.InfoLevel, | ||
logrus.WarnLevel, | ||
} | ||
} | ||
|
||
func (e *exporter) Fire(entry *logrus.Entry) error { | ||
e.wg.Add(1) | ||
|
||
go func(entry *logrus.Entry) { | ||
defer e.wg.Done() | ||
e.sendLogEvent(entry) | ||
}(entry) | ||
|
||
return nil | ||
} | ||
|
||
func (e *exporter) Wait() { | ||
e.wg.Wait() | ||
} | ||
|
||
func (e *exporter) sendLogEvent(log *logrus.Entry) { | ||
ctx, cancel := context.WithTimeout(context.Background(), sendTimeout) | ||
defer cancel() | ||
|
||
req := &castai.LogEvent{ | ||
Level: log.Level.String(), | ||
Time: log.Time, | ||
Message: log.Message, | ||
Fields: log.Data, | ||
} | ||
|
||
b := backoff.WithContext(backoff.WithMaxRetries(backoff.NewExponentialBackOff(), 3), ctx) | ||
err := backoff.Retry(func() error { | ||
return e.client.SendLogs(ctx, req) | ||
}, b) | ||
|
||
if err != nil { | ||
e.logger.Debugf("sending logs: %v", err) | ||
} | ||
} |
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,42 @@ | ||
package log | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/sirupsen/logrus" | ||
"github.com/sirupsen/logrus/hooks/test" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/goleak" | ||
|
||
"github.com/castai/cluster-controller/castai/mock" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
goleak.VerifyTestMain(m) | ||
} | ||
|
||
func TestLogExporter(t *testing.T) { | ||
it := require.New(t) | ||
logger, hook := test.NewNullLogger() | ||
defer hook.Reset() | ||
|
||
client := mock.NewMockAPIClient(nil) | ||
e := NewExporter(nil, client) | ||
logger.AddHook(e) | ||
log := logger.WithFields(logrus.Fields{ | ||
"cluster_id": "test-cluster", | ||
}) | ||
|
||
log.Log(logrus.InfoLevel, "deleting kubernetes node") | ||
log.Log(logrus.ErrorLevel, "failed to add node") | ||
log.Log(logrus.DebugLevel, "sending logs") | ||
e.Wait() | ||
|
||
it.Len(client.Logs, 2) | ||
it.Equal(client.Logs[0].Message, "deleting kubernetes node") | ||
it.Equal(client.Logs[0].Level, "info") | ||
it.Equal(client.Logs[0].Fields, logrus.Fields{"cluster_id": "test-cluster"}) | ||
it.Equal(client.Logs[1].Message, "failed to add node") | ||
it.Equal(client.Logs[1].Level, "error") | ||
it.Equal(client.Logs[1].Fields, logrus.Fields{"cluster_id": "test-cluster"}) | ||
} |
Oops, something went wrong.