Skip to content

Commit

Permalink
feat: add referer
Browse files Browse the repository at this point in the history
  • Loading branch information
franklinkim committed Jun 1, 2023
1 parent 6f5d8f9 commit 8366985
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
7 changes: 7 additions & 0 deletions log/fields_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ const (

// HTTPTrackingIDKey represents the HTTP tracking id if known (e.g. from X-Tracking-ID).
HTTPTrackingIDKey = "http_tracking_id"

// HTTPRefererKey identifies the address of the web page (i.e., the URI or IRI), from which the resource has been requested.
HTTPRefererKey = "http_referer"
)

func FHTTPServerName(id string) zap.Field {
Expand Down Expand Up @@ -98,6 +101,10 @@ func FHTTPUserAgent(userAgent string) zap.Field {
return zap.String(HTTPUserAgentKey, userAgent)
}

func FHTTPReferer(host string) zap.Field {
return zap.String(HTTPRefererKey, host)
}

func FHTTPHost(host string) zap.Field {
return zap.String(HTTPHostKey, host)
}
Expand Down
11 changes: 11 additions & 0 deletions log/with.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ func WithHTTPRequestID(l *zap.Logger, r *http.Request) *zap.Logger {
}
}

func WithHTTPReferer(l *zap.Logger, r *http.Request) *zap.Logger {
if value := r.Header.Get("X-Referer"); value != "" {
return With(l, FHTTPReferer(value))
} else if value := r.Referer(); value != "" {
return With(l, FHTTPHost(value))
} else {
return l
}
}

func WithHTTPHost(l *zap.Logger, r *http.Request) *zap.Logger {
if value := r.Header.Get("X-Forwarded-Host"); value != "" {
return With(l, FHTTPHost(value))
Expand Down Expand Up @@ -120,6 +130,7 @@ func WithHTTPClientIP(l *zap.Logger, r *http.Request) *zap.Logger {

func WithHTTPRequest(l *zap.Logger, r *http.Request) *zap.Logger {
l = WithHTTPHost(l, r)
l = WithHTTPReferer(l, r)
l = WithHTTPRequestID(l, r)
l = WithHTTPSessionID(l, r)
l = WithHTTPTrackingID(l, r)
Expand Down

0 comments on commit 8366985

Please sign in to comment.