Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logging: add similar logging to v3 #1041

Merged
merged 3 commits into from
Sep 17, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions httptransport/logginghandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package httptransport

import (
"net/http"
"strconv"
"time"

"github.com/rs/zerolog"
)

type httpStatusWriter struct {
http.ResponseWriter

StatusCode int
}

// LoggingHandler will log HTTP requests using the pre initialized zerolog.
func LoggingHandler(next http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
start := time.Now()

log := zerolog.Ctx(r.Context())

next.ServeHTTP(w, r)

log.Info().
Str("remote addr", r.RemoteAddr).
Str("method", r.Method).
Str("request uri", r.RequestURI).
Str("status", strconv.Itoa(http.StatusOK)).
Copy link
Collaborator

@Allda Allda Sep 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this always log 200?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's embarrassing runs I hate how that works. Readding

Float64("elapsed time (md)", float64(time.Since(start).Nanoseconds())*1e-6).
mcoops marked this conversation as resolved.
Show resolved Hide resolved
Msg("handled HTTP request")
}
}
22 changes: 11 additions & 11 deletions httptransport/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func New(ctx context.Context, conf config.Config, indexer indexer.Service, match
func (t *Server) configureDiscovery(_ context.Context) error {
h := intromw.Handler(
othttp.NewHandler(
DiscoveryHandler(),
LoggingHandler(DiscoveryHandler()),
OpenAPIV1Path,
t.traceOpt,
),
Expand Down Expand Up @@ -171,7 +171,7 @@ func (t *Server) configureIndexerMode(_ context.Context) error {
// affected manifest handler register
affectedH := intromw.Handler(
othttp.NewHandler(
AffectedManifestHandler(t.indexer),
LoggingHandler(AffectedManifestHandler(t.indexer)),
AffectedManifestAPIPath,
t.traceOpt,
),
Expand All @@ -182,7 +182,7 @@ func (t *Server) configureIndexerMode(_ context.Context) error {
// index handler register
indexH := intromw.Handler(
othttp.NewHandler(
IndexHandler(t.indexer),
LoggingHandler(IndexHandler(t.indexer)),
IndexAPIPath,
t.traceOpt,
),
Expand All @@ -193,7 +193,7 @@ func (t *Server) configureIndexerMode(_ context.Context) error {
// index report handler register
indexReportH := intromw.Handler(
othttp.NewHandler(
IndexReportHandler(t.indexer),
LoggingHandler(IndexReportHandler(t.indexer)),
IndexReportAPIPath,
t.traceOpt,
),
Expand All @@ -204,7 +204,7 @@ func (t *Server) configureIndexerMode(_ context.Context) error {
// index state handler register
stateH := intromw.Handler(
othttp.NewHandler(
IndexStateHandler(t.indexer),
LoggingHandler(IndexStateHandler(t.indexer)),
IndexStateAPIPath,
t.traceOpt,
),
Expand All @@ -226,7 +226,7 @@ func (t *Server) configureMatcherMode(_ context.Context) error {
// vulnerability report handler register
vulnReportH := intromw.Handler(
othttp.NewHandler(
VulnerabilityReportHandler(t.matcher, t.indexer),
LoggingHandler(VulnerabilityReportHandler(t.matcher, t.indexer)),
VulnerabilityReportPath,
t.traceOpt,
),
Expand All @@ -237,7 +237,7 @@ func (t *Server) configureMatcherMode(_ context.Context) error {
// update operation handler register
opH := intromw.Handler(
othttp.NewHandler(
UpdateOperationHandler(t.matcher),
LoggingHandler(UpdateOperationHandler(t.matcher)),
UpdateOperationAPIPath,
t.traceOpt,
),
Expand All @@ -248,7 +248,7 @@ func (t *Server) configureMatcherMode(_ context.Context) error {
// update diff handler register
diffH := intromw.Handler(
othttp.NewHandler(
UpdateDiffHandler(t.matcher),
LoggingHandler(UpdateDiffHandler(t.matcher)),
UpdateDiffAPIPath,
t.traceOpt,
),
Expand All @@ -270,7 +270,7 @@ func (t *Server) configureNotifierMode(ctx context.Context) error {
// notifications callback handler
callbackH := intromw.Handler(
othttp.NewHandler(
NotificationHandler(t.notifier),
LoggingHandler(NotificationHandler(t.notifier)),
NotificationAPIPath,
t.traceOpt,
),
Expand All @@ -286,7 +286,7 @@ func (t *Server) configureNotifierMode(ctx context.Context) error {
// keys handler
keysH := intromw.Handler(
othttp.NewHandler(
KeysHandler(ks),
LoggingHandler(KeysHandler(ks)),
KeysAPIPath,
t.traceOpt,
),
Expand All @@ -297,7 +297,7 @@ func (t *Server) configureNotifierMode(ctx context.Context) error {
// key by ID handler
keyByIDH := intromw.Handler(
othttp.NewHandler(
KeyByIDHandler(ks),
LoggingHandler(KeyByIDHandler(ks)),
KeyByIDAPIPath,
t.traceOpt,
),
Expand Down