-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: jmazionis <[email protected]>
- Loading branch information
Showing
8 changed files
with
230 additions
and
13 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,90 @@ | ||
package log | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"time" | ||
|
||
"castai-agent/internal/castai" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type Exporter interface { | ||
logrus.Hook | ||
Wait() | ||
} | ||
|
||
func SetupLogExporter(logger *logrus.Logger, castaiclient castai.Client, cfg Config) { | ||
logExporter := newExporter(cfg, castaiclient) | ||
logger.AddHook(logExporter) | ||
logrus.RegisterExitHandler(logExporter.Wait) | ||
} | ||
|
||
func newExporter(cfg Config, client castai.Client) Exporter { | ||
return &exporter{ | ||
cfg: cfg, | ||
client: client, | ||
wg: sync.WaitGroup{}, | ||
} | ||
} | ||
|
||
type exporter struct { | ||
cfg Config | ||
client castai.Client | ||
wg sync.WaitGroup | ||
} | ||
|
||
type Config struct { | ||
ClusterID string | ||
MsgSendTimeoutSecs time.Duration | ||
} | ||
|
||
func (ex *exporter) Levels() []logrus.Level { | ||
return []logrus.Level{ | ||
logrus.ErrorLevel, | ||
logrus.FatalLevel, | ||
logrus.PanicLevel, | ||
logrus.InfoLevel, | ||
logrus.WarnLevel, | ||
} | ||
} | ||
|
||
func (ex *exporter) Fire(entry *logrus.Entry) error { | ||
if entry.Context != nil { | ||
if v, _ := entry.Context.Value(castai.DoNotSendLogs).(string); v == "true" { | ||
// Don't fire the hook | ||
return nil | ||
} | ||
} | ||
|
||
ex.wg.Add(1) | ||
|
||
go func(entry *logrus.Entry) { | ||
defer ex.wg.Done() | ||
ex.sendLogEvent(ex.cfg.ClusterID, entry) | ||
}(entry) | ||
|
||
return nil | ||
} | ||
|
||
func (ex *exporter) Wait() { | ||
ex.wg.Wait() | ||
} | ||
|
||
func (ex *exporter) sendLogEvent(clusterID string, e *logrus.Entry) { | ||
ctx, cancel := context.WithTimeout(context.Background(), ex.cfg.MsgSendTimeoutSecs * time.Second) | ||
defer cancel() | ||
|
||
ex.client.SendLogEvent( | ||
ctx, | ||
clusterID, | ||
&castai.IngestAgentLogsRequest{ | ||
LogEvent: castai.LogEvent{ | ||
Level: e.Level.String(), | ||
Time: e.Time, | ||
Message: e.Message, | ||
Fields: e.Data, | ||
}, | ||
}) | ||
} |
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,59 @@ | ||
package log | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/google/uuid" | ||
"github.com/sirupsen/logrus" | ||
"github.com/sirupsen/logrus/hooks/test" | ||
"github.com/stretchr/testify/require" | ||
|
||
"castai-agent/internal/castai" | ||
mock_castai "castai-agent/internal/castai/mock" | ||
) | ||
|
||
func TestSetupLogExporter(t *testing.T) { | ||
logger, hook := test.NewNullLogger() | ||
defer hook.Reset() | ||
mockClusterID := uuid.New().String() | ||
ctrl := gomock.NewController(t) | ||
mockapi := mock_castai.NewMockClient(ctrl) | ||
SetupLogExporter(logger, mockapi, Config{ClusterID: mockClusterID, MsgSendTimeoutSecs: 1}) | ||
|
||
t.Run("sends the log msg", func(t *testing.T) { | ||
r := require.New(t) | ||
mockapi.EXPECT().SendLogEvent(gomock.Any(), gomock.Any(), gomock.Any()). | ||
Do(assertClusterFields(r, mockClusterID, "eks")).Return(&castai.IngestAgentLogsResponse{}).Times(1) | ||
log := logger.WithFields(logrus.Fields{ | ||
"cluster_id": mockClusterID, | ||
"provider": "eks", | ||
}) | ||
log.Log(logrus.ErrorLevel, "failed to discover account id") | ||
time.Sleep(1 * time.Second) | ||
}) | ||
|
||
t.Run("skips sending log msg", func(t *testing.T) { | ||
mockapi.EXPECT().SendLogEvent(gomock.Any(), gomock.Any(), gomock.Any()).Times(0) | ||
log := logrus.WithContext(castai.DoNotSendLogsCtx()) | ||
log = log.WithFields(logrus.Fields{ | ||
"cluster_id": mockClusterID, | ||
"provider": "eks", | ||
}) | ||
log.Log(logrus.ErrorLevel, "failed to discover account id") | ||
time.Sleep(1 * time.Second) | ||
}) | ||
} | ||
|
||
func assertClusterFields( | ||
r *require.Assertions, mockClusterID, provider string, | ||
) func(_ context.Context, clusterID string, req *castai.IngestAgentLogsRequest) *castai.IngestAgentLogsResponse { | ||
return func(_ context.Context, clusterID string, req *castai.IngestAgentLogsRequest) *castai.IngestAgentLogsResponse { | ||
fields := req.LogEvent.Fields | ||
r.Equal(mockClusterID, fields["cluster_id"]) | ||
r.Equal(provider, fields["provider"]) | ||
return &castai.IngestAgentLogsResponse{} | ||
} | ||
} |