Fix Context Values are not logged.
Only the Sentry hook uses the values of the passed context. Therefore, we removed the values from our log statements when we shifted them from an extra `WithField` call to the context. We fix this behavior by introducing a Logrus Hook that copies a fixed set of context values to the logging data.
This commit is contained in:
@ -26,7 +26,7 @@ const (
|
|||||||
// runnerContextKey is the key used to store runners in context.Context.
|
// runnerContextKey is the key used to store runners in context.Context.
|
||||||
runnerContextKey dto.ContextKey = "runner"
|
runnerContextKey dto.ContextKey = "runner"
|
||||||
// destroyReasonContextKey is the key used to store the reason of the destruction in the context.Context.
|
// destroyReasonContextKey is the key used to store the reason of the destruction in the context.Context.
|
||||||
destroyReasonContextKey dto.ContextKey = "destroyReason"
|
destroyReasonContextKey dto.ContextKey = dto.KeyRunnerDestroyReason
|
||||||
// SIGQUIT is the character that causes a tty to send the SIGQUIT signal to the controlled process.
|
// SIGQUIT is the character that causes a tty to send the SIGQUIT signal to the controlled process.
|
||||||
SIGQUIT = 0x1c
|
SIGQUIT = 0x1c
|
||||||
// executionTimeoutGracePeriod is the time to wait after sending a SIGQUIT signal to a timed out execution.
|
// executionTimeoutGracePeriod is the time to wait after sending a SIGQUIT signal to a timed out execution.
|
||||||
|
@ -194,10 +194,14 @@ type ContextKey string
|
|||||||
|
|
||||||
// Keys to reference information (for logging or monitoring).
|
// Keys to reference information (for logging or monitoring).
|
||||||
const (
|
const (
|
||||||
KeyRunnerID = "runner_id"
|
KeyRunnerID = "runner_id"
|
||||||
KeyEnvironmentID = "environment_id"
|
KeyEnvironmentID = "environment_id"
|
||||||
|
KeyRunnerDestroyReason = "destroy_reason"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// LoggedContextKeys defines which keys will be logged if a context is passed to logrus. See ContextHook.
|
||||||
|
var LoggedContextKeys = []ContextKey{KeyRunnerID, KeyEnvironmentID, KeyRunnerDestroyReason}
|
||||||
|
|
||||||
// WebSocketMessageType is the type for the messages from Poseidon to the client.
|
// WebSocketMessageType is the type for the messages from Poseidon to the client.
|
||||||
type WebSocketMessageType string
|
type WebSocketMessageType string
|
||||||
|
|
||||||
|
41
pkg/logging/context_hook.go
Normal file
41
pkg/logging/context_hook.go
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
package logging
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/openHPI/poseidon/pkg/dto"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ContextHook logs the values referenced by the of dto.LoggedContextKeys.
|
||||||
|
// By default Logrus does not log the values stored in the passed context.
|
||||||
|
type ContextHook struct{}
|
||||||
|
|
||||||
|
// Fire is triggered on new log entries.
|
||||||
|
func (hook *ContextHook) Fire(entry *logrus.Entry) error {
|
||||||
|
if entry.Context != nil {
|
||||||
|
injectContextValuesIntoData(entry)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func injectContextValuesIntoData(entry *logrus.Entry) {
|
||||||
|
for _, key := range dto.LoggedContextKeys {
|
||||||
|
value := entry.Context.Value(key)
|
||||||
|
_, valueExisting := entry.Data[string(key)]
|
||||||
|
if !valueExisting && value != nil {
|
||||||
|
entry.Data[string(key)] = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Levels returns all levels this hook should be registered to.
|
||||||
|
func (hook *ContextHook) Levels() []logrus.Level {
|
||||||
|
return []logrus.Level{
|
||||||
|
logrus.PanicLevel,
|
||||||
|
logrus.FatalLevel,
|
||||||
|
logrus.ErrorLevel,
|
||||||
|
logrus.WarnLevel,
|
||||||
|
logrus.InfoLevel,
|
||||||
|
logrus.DebugLevel,
|
||||||
|
logrus.TraceLevel,
|
||||||
|
}
|
||||||
|
}
|
@ -40,6 +40,7 @@ func InitializeLogging(logLevel string, formatter dto.Formatter) {
|
|||||||
TimestampFormat: TimestampFormat,
|
TimestampFormat: TimestampFormat,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
log.AddHook(&ContextHook{})
|
||||||
log.AddHook(&SentryHook{})
|
log.AddHook(&SentryHook{})
|
||||||
log.ExitFunc = func(i int) {
|
log.ExitFunc = func(i int) {
|
||||||
sentry.Flush(GracefulSentryShutdown)
|
sentry.Flush(GracefulSentryShutdown)
|
||||||
|
@ -19,9 +19,7 @@ func (hook *SentryHook) Fire(entry *logrus.Entry) error {
|
|||||||
var hub *sentry.Hub
|
var hub *sentry.Hub
|
||||||
if entry.Context != nil {
|
if entry.Context != nil {
|
||||||
hub = sentry.GetHubFromContext(entry.Context)
|
hub = sentry.GetHubFromContext(entry.Context)
|
||||||
// This might overwrite valid data when not the request context is passed.
|
injectContextValuesIntoData(entry)
|
||||||
entry.Data[dto.KeyRunnerID] = entry.Context.Value(dto.ContextKey(dto.KeyRunnerID))
|
|
||||||
entry.Data[dto.KeyEnvironmentID] = entry.Context.Value(dto.ContextKey(dto.KeyEnvironmentID))
|
|
||||||
}
|
}
|
||||||
if hub == nil {
|
if hub == nil {
|
||||||
hub = sentry.CurrentHub()
|
hub = sentry.CurrentHub()
|
||||||
|
Reference in New Issue
Block a user