-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Log result of HTTP requests & artifacts load/saves. Closes #8257 (
#8394) Signed-off-by: Alex Collins <alex_collins@intuit.com>
- Loading branch information
Showing
6 changed files
with
125 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package accesslog | ||
|
||
import ( | ||
"net/http" | ||
"time" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
// Interceptor returns a handler that provides access logging. | ||
// | ||
// github.com/gorilla/handlers/logging.go | ||
// https://arunvelsriram.medium.com/simple-golang-http-logging-middleware-315656ff8722 | ||
func Interceptor(h http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
t := time.Now() | ||
|
||
rcw := &resultCapturingWriter{ResponseWriter: w} | ||
|
||
h.ServeHTTP(rcw, r) | ||
|
||
log.WithFields(log.Fields{ | ||
"path": r.URL.Path, // log the path not the URL, to avoid logging sensitive data that could be in the query params | ||
"method": r.Method, // log the method, so we can differentiate create/update from get/list | ||
"status": rcw.status, | ||
"size": rcw.size, | ||
"duration": time.Since(t), | ||
}).Info() | ||
}) | ||
} |
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,29 @@ | ||
package accesslog | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
// resultCapturingWriter captures the size and status code of the response. | ||
// Because http.response implements http.Flusher, we must do so too, otherwise Watch* methods don't work. | ||
// We do not implement http.Hijacker, as HTTP/2 requests should not allow it. | ||
type resultCapturingWriter struct { | ||
http.ResponseWriter // MUST also be http.Flusher | ||
status int | ||
size int | ||
} | ||
|
||
func (r *resultCapturingWriter) Write(b []byte) (int, error) { | ||
size, err := r.ResponseWriter.Write(b) | ||
r.size += size | ||
return size, err | ||
} | ||
|
||
func (r *resultCapturingWriter) WriteHeader(v int) { | ||
r.ResponseWriter.WriteHeader(v) | ||
r.status = v | ||
} | ||
|
||
func (r *resultCapturingWriter) Flush() { | ||
r.ResponseWriter.(http.Flusher).Flush() | ||
} |
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,55 @@ | ||
package logging | ||
|
||
import ( | ||
"time" | ||
|
||
log "github.com/sirupsen/logrus" | ||
|
||
wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1" | ||
"github.com/argoproj/argo-workflows/v3/workflow/artifacts/common" | ||
) | ||
|
||
// driver adds a logging interceptor to help diagnose issues with artifacts | ||
type driver struct { | ||
common.ArtifactDriver | ||
} | ||
|
||
func New(d common.ArtifactDriver) common.ArtifactDriver { | ||
return &driver{d} | ||
} | ||
|
||
func (d driver) Load(a *wfv1.Artifact, path string) error { | ||
t := time.Now() | ||
key, _ := a.GetKey() | ||
err := d.ArtifactDriver.Load(a, path) | ||
log.WithField("artifactName", a.Name). | ||
WithField("key", key). | ||
WithField("duration", time.Since(t)). | ||
WithError(err). | ||
Info("Load artifact") | ||
return err | ||
} | ||
|
||
func (d driver) Save(path string, a *wfv1.Artifact) error { | ||
t := time.Now() | ||
key, _ := a.GetKey() | ||
err := d.ArtifactDriver.Save(path, a) | ||
log.WithField("artifactName", a.Name). | ||
WithField("key", key). | ||
WithField("duration", time.Since(t)). | ||
WithError(err). | ||
Info("Save artifact") | ||
return err | ||
} | ||
|
||
func (d driver) ListObjects(a *wfv1.Artifact) ([]string, error) { | ||
t := time.Now() | ||
key, _ := a.GetKey() | ||
list, err := d.ArtifactDriver.ListObjects(a) | ||
log.WithField("artifactName", a.Name). | ||
WithField("key", key). | ||
WithField("duration", time.Since(t)). | ||
WithError(err). | ||
Info("List objects") | ||
return list, err | ||
} |