diff options
author | Kamil Trzciński <ayufan@ayufan.eu> | 2020-10-08 12:38:42 +0300 |
---|---|---|
committer | Kamil Trzciński <ayufan@ayufan.eu> | 2020-10-13 14:08:30 +0300 |
commit | dc54f4215620da74b70dbd07d0f1369e409d3e0b (patch) | |
tree | 69e26a6cde0b614079a2ab0b361f1efe81a74066 /internal/logging | |
parent | 5f461b42e39419795b2669278398a9a7c6ed2cd9 (diff) |
Log serving type used for requestslog-serving-type
This extends our structured logging
with information about how the given
request was served
Diffstat (limited to 'internal/logging')
-rw-r--r-- | internal/logging/logging.go | 18 | ||||
-rw-r--r-- | internal/logging/logging_test.go | 86 |
2 files changed, 78 insertions, 26 deletions
diff --git a/internal/logging/logging.go b/internal/logging/logging.go index 3432cdf7..6643e169 100644 --- a/internal/logging/logging.go +++ b/internal/logging/logging.go @@ -53,16 +53,20 @@ func getAccessLogger(format string) (*logrus.Logger, error) { // getExtraLogFields is used to inject additional fields into the // HTTP access logger middleware. func getExtraLogFields(r *http.Request) log.Fields { - var projectID uint64 - if d := request.GetDomain(r); d != nil { - projectID = d.GetProjectID(r) + logFields := log.Fields{ + "pages_https": request.IsHTTPS(r), + "pages_host": request.GetHost(r), } - return log.Fields{ - "pages_https": request.IsHTTPS(r), - "pages_host": request.GetHost(r), - "pages_project_id": projectID, + if d := request.GetDomain(r); d != nil { + if lp := d.GetLookupPath(r); lp != nil { + logFields["pages_project_serving_type"] = lp.ServingType + logFields["pages_project_prefix"] = lp.Prefix + logFields["pages_project_id"] = lp.ProjectID + } } + + return logFields } // BasicAccessLogger configures the GitLab pages basic HTTP access logger middleware diff --git a/internal/logging/logging_test.go b/internal/logging/logging_test.go index ec8837b6..e87a8c0d 100644 --- a/internal/logging/logging_test.go +++ b/internal/logging/logging_test.go @@ -8,32 +8,78 @@ import ( "gitlab.com/gitlab-org/gitlab-pages/internal/domain" "gitlab.com/gitlab-org/gitlab-pages/internal/request" + "gitlab.com/gitlab-org/gitlab-pages/internal/serving" ) +type lookupPathFunc func(*http.Request) *serving.LookupPath + +func (f lookupPathFunc) Resolve(r *http.Request) (*serving.Request, error) { + return &serving.Request{LookupPath: f(r)}, nil +} + func TestGetExtraLogFields(t *testing.T) { + domainWithResolver := &domain.Domain{ + Resolver: lookupPathFunc(func(*http.Request) *serving.LookupPath { + return &serving.LookupPath{ + ServingType: "file", + ProjectID: 100, + Prefix: "/prefix", + } + }), + } + tests := []struct { - name string - scheme string - host string - domain *domain.Domain + name string + scheme string + host string + domain *domain.Domain + expectedHTTPS interface{} + expectedHost interface{} + expectedProjectID interface{} + expectedProjectPrefix interface{} + expectedServingType interface{} }{ { - name: "https", - scheme: request.SchemeHTTPS, - host: "githost.io", - domain: &domain.Domain{}, + name: "https", + scheme: request.SchemeHTTPS, + host: "githost.io", + domain: domainWithResolver, + expectedHTTPS: true, + expectedHost: "githost.io", + expectedProjectID: uint64(100), + expectedProjectPrefix: "/prefix", + expectedServingType: "file", + }, + { + name: "http", + scheme: request.SchemeHTTP, + host: "githost.io", + domain: domainWithResolver, + expectedHTTPS: false, + expectedHost: "githost.io", + expectedProjectID: uint64(100), + expectedProjectPrefix: "/prefix", + expectedServingType: "file", }, { - name: "http", - scheme: request.SchemeHTTP, - host: "githost.io", - domain: &domain.Domain{}, + name: "domain_without_resolved", + scheme: request.SchemeHTTP, + host: "githost.io", + domain: &domain.Domain{}, + expectedHTTPS: false, + expectedHost: "githost.io", + expectedProjectID: nil, + expectedServingType: nil, }, { - name: "no_domain", - scheme: request.SchemeHTTP, - host: "githost.io", - domain: nil, + name: "no_domain", + scheme: request.SchemeHTTP, + host: "githost.io", + domain: nil, + expectedHTTPS: false, + expectedHost: "githost.io", + expectedProjectID: nil, + expectedServingType: nil, }, } @@ -46,9 +92,11 @@ func TestGetExtraLogFields(t *testing.T) { req = request.WithHostAndDomain(req, tt.host, tt.domain) got := getExtraLogFields(req) - require.Equal(t, got["pages_https"], tt.scheme == request.SchemeHTTPS) - require.Equal(t, got["pages_host"], tt.host) - require.Equal(t, got["pages_project_id"], uint64(0x0)) + require.Equal(t, tt.expectedHTTPS, got["pages_https"]) + require.Equal(t, tt.expectedHost, got["pages_host"]) + require.Equal(t, tt.expectedProjectID, got["pages_project_id"]) + require.Equal(t, tt.expectedProjectPrefix, got["pages_project_prefix"]) + require.Equal(t, tt.expectedServingType, got["pages_project_serving_type"]) }) } } |