Add context to log statements.

This commit is contained in:
Maximilian Paß
2023-04-11 19:24:06 +01:00
parent 43221c717e
commit 0c8fa9ccfa
16 changed files with 97 additions and 88 deletions

View File

@ -77,7 +77,7 @@ func HTTPLoggingMiddleware(next http.Handler) http.Handler {
next.ServeHTTP(lrw, r)
latency := time.Now().UTC().Sub(start)
logEntry := log.WithFields(logrus.Fields{
logEntry := log.WithContext(r.Context()).WithFields(logrus.Fields{
"code": lrw.StatusCode,
"method": r.Method,
"path": path,

View File

@ -130,12 +130,12 @@ func addEnvironmentID(r *http.Request, id dto.EnvironmentID) {
func AddRequestSize(r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
log.WithError(err).Warn("Failed to read request body")
log.WithContext(r.Context()).WithError(err).Warn("Failed to read request body")
}
err = r.Body.Close()
if err != nil {
log.WithError(err).Warn("Failed to close request body")
log.WithContext(r.Context()).WithError(err).Warn("Failed to close request body")
}
r.Body = io.NopCloser(bytes.NewBuffer(body))
@ -185,7 +185,7 @@ func addInfluxDBField(r *http.Request, key string, value interface{}) {
func dataPointFromRequest(r *http.Request) *write.Point {
p, ok := r.Context().Value(influxdbContextKey).(*write.Point)
if !ok {
log.Error("All http request must contain an influxdb data point!")
log.WithContext(r.Context()).Error("All http request must contain an influxdb data point!")
}
return p
}

View File

@ -69,7 +69,7 @@ func (w *Ls2JsonWriter) Write(p []byte) (int, error) {
if len(line) != 0 {
count, err := w.writeLine(line)
if err != nil {
log.WithError(err).Warn("Could not write line to Target")
log.WithContext(w.Ctx).WithError(err).Warn("Could not write line to Target")
return count, err
}
}
@ -87,7 +87,7 @@ func (w *Ls2JsonWriter) initializeJSONObject() (count int, err error) {
if !w.jsonStartSent {
count, err = w.Target.Write([]byte("{\"files\": ["))
if count == 0 || err != nil {
log.WithError(err).Warn("Could not write to target")
log.WithContext(w.Ctx).WithError(err).Warn("Could not write to target")
err = fmt.Errorf("could not write to target: %w", err)
} else {
w.jsonStartSent = true
@ -102,7 +102,7 @@ func (w *Ls2JsonWriter) Close() {
if w.jsonStartSent {
count, err := w.Target.Write([]byte("]}"))
if count == 0 || err != nil {
log.WithError(err).Warn("Could not Close ls2json writer")
log.WithContext(w.Ctx).WithError(err).Warn("Could not Close ls2json writer")
}
w.sentrySpan.Finish()
}
@ -163,7 +163,7 @@ func (w *Ls2JsonWriter) parseFileHeader(matches [][]byte) ([]byte, error) {
name = dto.FilePath(parts[0])
linkTarget = dto.FilePath(parts[1])
} else {
log.Error("could not split link into name and target")
log.WithContext(w.Ctx).Error("could not split link into name and target")
}
}