Skip to content

Commit d3e9f91

Browse files
authored
api: add a log for each request (#52)
1 parent f0c3736 commit d3e9f91

File tree

1 file changed

+62
-16
lines changed

1 file changed

+62
-16
lines changed

api/api.go

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ package api
33
import (
44
"encoding/json"
55
"fmt"
6+
"net"
67
"net/http"
78
"os"
9+
"strings"
10+
"time"
811

912
"github.com/contextwtf/lanyard/api/tracing"
1013

@@ -37,13 +40,14 @@ func (s *Server) Handler(env, gitSha string) http.Handler {
3740

3841
h := http.Handler(mux)
3942
h = versionHandler(h, gitSha)
40-
h = tracingHandler(os.Getenv("DD_ENV"), os.Getenv("DD_SERVICE"), gitSha, h)
41-
h = hlog.NewHandler(log.Logger)(h)
4243
h = hlog.UserAgentHandler("user_agent")(h)
4344
h = hlog.RefererHandler("referer")(h)
4445
h = hlog.RequestIDHandler("req_id", "Request-Id")(h)
4546
h = hlog.URLHandler("path")(h)
46-
h = hlog.RequestHandler("req")(h)
47+
h = hlog.MethodHandler("method")(h)
48+
h = tracingHandler(os.Getenv("DD_ENV"), os.Getenv("DD_SERVICE"), gitSha, h)
49+
h = RemoteAddrHandler("ip")(h)
50+
h = hlog.NewHandler(log.Logger)(h) // needs to be last for log values to correctly be passed to context
4751

4852
if env == "production" {
4953
return h
@@ -53,11 +57,48 @@ func (s *Server) Handler(env, gitSha string) http.Handler {
5357
AllowedOrigins: []string{"http://localhost:3000"},
5458
AllowCredentials: true,
5559
})
60+
5661
h = c.Handler(h)
5762

5863
return h
5964
}
6065

66+
func ipFromRequest(r *http.Request) string {
67+
if r.Header.Get("fastly-client-ip") != "" {
68+
return r.Header.Get("fastly-client-ip")
69+
}
70+
71+
if r.Header.Get("x-forwarded-for") != "" {
72+
group := strings.Split(r.Header.Get("x-forwarded-for"), ", ")
73+
if len(group) > 0 {
74+
return group[len(group)-1]
75+
}
76+
}
77+
78+
host, _, err := net.SplitHostPort(r.RemoteAddr)
79+
if err != nil {
80+
return host
81+
}
82+
83+
return ""
84+
}
85+
86+
func RemoteAddrHandler(fieldKey string) func(next http.Handler) http.Handler {
87+
return func(next http.Handler) http.Handler {
88+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
89+
ip := ipFromRequest(r)
90+
if ip != "" {
91+
log := zerolog.Ctx(r.Context())
92+
log.UpdateContext(func(c zerolog.Context) zerolog.Context {
93+
return c.Str("ip", ip)
94+
})
95+
}
96+
97+
next.ServeHTTP(w, r.WithContext(r.Context()))
98+
})
99+
}
100+
}
101+
61102
func versionHandler(h http.Handler, sha string) http.Handler {
62103
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
63104
w.Header().Add("server-version", sha)
@@ -70,27 +111,32 @@ func tracingHandler(env, service, sha string, h http.Handler) http.Handler {
70111
span, ctx := tracing.SpanFromContext(r.Context(), "http.request")
71112

72113
defer span.Finish()
114+
log := zerolog.Ctx(ctx)
73115

74-
log := zerolog.Ctx(r.Context())
75-
log.UpdateContext(func(c zerolog.Context) zerolog.Context {
76-
return c.Uint64("dd.trace_id", span.Context().TraceID())
77-
})
78-
log.UpdateContext(func(c zerolog.Context) zerolog.Context {
79-
return c.Str("dd.service", service)
80-
})
81-
log.UpdateContext(func(c zerolog.Context) zerolog.Context {
82-
return c.Str("dd.env", env)
83-
})
84-
log.UpdateContext(func(c zerolog.Context) zerolog.Context {
85-
return c.Str("dd.version", sha)
86-
})
116+
if env != "" {
117+
log.UpdateContext(func(c zerolog.Context) zerolog.Context {
118+
return c.Uint64("dd.trace_id", span.Context().TraceID()).
119+
Str("dd.service", service).
120+
Str("dd.env", env).
121+
Str("dd.version", sha)
122+
})
123+
}
87124

88125
span.SetTag(ext.ResourceName, r.URL.Path)
89126
span.SetTag(ext.SpanType, ext.SpanTypeWeb)
90127
span.SetTag(ext.HTTPMethod, r.Method)
91128

92129
sc := &statusCapture{ResponseWriter: w}
130+
131+
requestStart := time.Now()
93132
h.ServeHTTP(sc, r.WithContext(ctx))
133+
134+
// log every request
135+
log.Info().
136+
Int("status", sc.status).
137+
Dur("duration", time.Since(requestStart)).
138+
Msg("")
139+
94140
span.SetTag(ext.HTTPCode, sc.status)
95141
})
96142
}

0 commit comments

Comments
 (0)