Skip to content

Commit

Permalink
Add logrus hook for sending SkaffoldLogEvents (#6250)
Browse files Browse the repository at this point in the history
* add logrus hook for sending SkaffoldLogEvents

* add trace loglevel and use default case

* comments

* test

* linters
  • Loading branch information
MarlonGamez authored Jul 20, 2021
1 parent 23440d4 commit ea2b8b5
Show file tree
Hide file tree
Showing 8 changed files with 315 additions and 199 deletions.
2 changes: 2 additions & 0 deletions cmd/skaffold/app/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
sErrors "github.com/GoogleContainerTools/skaffold/pkg/skaffold/errors"
event "github.com/GoogleContainerTools/skaffold/pkg/skaffold/event/v2"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/instrumentation"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/instrumentation/prompt"
kubectx "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/context"
Expand Down Expand Up @@ -261,6 +262,7 @@ func setUpLogs(stdErr io.Writer, level string, timestamp bool) error {
logrus.SetFormatter(&logrus.TextFormatter{
FullTimestamp: timestamp,
})
logrus.AddHook(event.NewLogHook())
return nil
}

Expand Down
1 change: 1 addition & 0 deletions docs/content/en/docs/references/api/grpc.md
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,7 @@ Enum indicating the log level of a line of output
| ERROR | 3 | Error Level |
| FATAL | 4 | Fatal Level |
| PANIC | 5 | Panic Level |
| TRACE | 6 | Trace Level |



Expand Down
53 changes: 52 additions & 1 deletion pkg/skaffold/event/v2/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import (
"fmt"
"io"

"github.com/sirupsen/logrus"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
"github.com/GoogleContainerTools/skaffold/proto/enums"
proto "github.com/GoogleContainerTools/skaffold/proto/v2"
)

Expand All @@ -40,7 +43,7 @@ func (l logger) Write(p []byte) (int, error) {
handler.handleSkaffoldLogEvent(&proto.SkaffoldLogEvent{
TaskId: fmt.Sprintf("%s-%d", l.Phase, handler.iteration),
SubtaskId: l.SubtaskID,
Level: 0,
Level: -1,
Message: string(p),
})

Expand All @@ -54,3 +57,51 @@ func (ev *eventHandler) handleSkaffoldLogEvent(e *proto.SkaffoldLogEvent) {
},
})
}

// logHook is an implementation of logrus.Hook used to send SkaffoldLogEvents
type logHook struct{}

func NewLogHook() logrus.Hook {
return logHook{}
}

// Levels returns all levels as we want to send events for all levels
func (h logHook) Levels() []logrus.Level {
return []logrus.Level{
logrus.PanicLevel,
logrus.FatalLevel,
logrus.ErrorLevel,
logrus.WarnLevel,
logrus.InfoLevel,
logrus.DebugLevel,
logrus.TraceLevel,
}
}

// Fire constructs a SkaffoldLogEvent and sends it to the event channel
func (h logHook) Fire(entry *logrus.Entry) error {
handler.handleSkaffoldLogEvent(&proto.SkaffoldLogEvent{
Level: levelFromEntry(entry),
Message: entry.Message,
})
return nil
}

func levelFromEntry(entry *logrus.Entry) enums.LogLevel {
switch entry.Level {
case logrus.FatalLevel:
return enums.LogLevel_FATAL
case logrus.ErrorLevel:
return enums.LogLevel_ERROR
case logrus.WarnLevel:
return enums.LogLevel_WARN
case logrus.InfoLevel:
return enums.LogLevel_INFO
case logrus.PanicLevel:
return enums.LogLevel_PANIC
case logrus.TraceLevel:
return enums.LogLevel_TRACE
default:
return enums.LogLevel_DEBUG
}
}
54 changes: 54 additions & 0 deletions pkg/skaffold/event/v2/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ package v2
import (
"testing"

"github.com/sirupsen/logrus"

latestV1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v1"
"github.com/GoogleContainerTools/skaffold/proto/enums"
proto "github.com/GoogleContainerTools/skaffold/proto/v2"
"github.com/GoogleContainerTools/skaffold/testutil"
)

func TestHandleSkaffoldLogEvent(t *testing.T) {
Expand Down Expand Up @@ -51,3 +54,54 @@ func TestHandleSkaffoldLogEvent(t *testing.T) {
return logLen == len(messages)
})
}

func TestLevelFromEntry(t *testing.T) {
tests := []struct {
name string
logrusLvl logrus.Level
enumLvl enums.LogLevel
}{
{
name: "panic",
logrusLvl: logrus.PanicLevel,
enumLvl: enums.LogLevel_PANIC,
},
{
name: "fatal",
logrusLvl: logrus.FatalLevel,
enumLvl: enums.LogLevel_FATAL,
},
{
name: "error",
logrusLvl: logrus.ErrorLevel,
enumLvl: enums.LogLevel_ERROR,
},
{
name: "warn",
logrusLvl: logrus.WarnLevel,
enumLvl: enums.LogLevel_WARN,
},
{
name: "info",
logrusLvl: logrus.InfoLevel,
enumLvl: enums.LogLevel_INFO,
},
{
name: "debug",
logrusLvl: logrus.DebugLevel,
enumLvl: enums.LogLevel_DEBUG,
},
{
name: "trace",
logrusLvl: logrus.TraceLevel,
enumLvl: enums.LogLevel_TRACE,
},
}

for _, test := range tests {
testutil.Run(t, test.name, func(t *testutil.T) {
got := levelFromEntry(&logrus.Entry{Level: test.logrusLvl})
t.CheckDeepEqual(test.enumLvl, got)
})
}
}
400 changes: 202 additions & 198 deletions proto/enums/enums.pb.go

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions proto/enums/enums.proto
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ enum LogLevel {
FATAL = 4;
// Panic Level
PANIC = 5;
// Trace Level
TRACE = 6;
}

// Enum for Status codes<br>
Expand Down
1 change: 1 addition & 0 deletions proto/v1/skaffold.pb.go

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

1 change: 1 addition & 0 deletions proto/v2/skaffold.pb.go

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

0 comments on commit ea2b8b5

Please sign in to comment.