Skip to content

Commit

Permalink
Add handler key to request logs
Browse files Browse the repository at this point in the history
  • Loading branch information
phillebaba committed Feb 6, 2023
1 parent 38501aa commit 946d0d5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ require (
github.com/spf13/afero v1.9.3
github.com/stretchr/testify v1.8.1
github.com/valyala/fastjson v1.6.4
github.com/xenitab/pkg/gin v0.0.3
github.com/xenitab/pkg/gin v0.0.4
github.com/xenitab/pkg/kubernetes v0.0.2
go.uber.org/multierr v1.9.0
go.uber.org/zap v1.24.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1017,8 +1017,8 @@ github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v0.0.0-20180618132009-1d523034197f/go.mod h1:5yf86TLmAcydyeJq5YvxkGPE2fm/u4myDekKRoLuqhs=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
github.com/xenitab/pkg/gin v0.0.3 h1:QBwxrspDOMhQKAsz6BldiKJsfvSEuB2yDW3dirmP+E4=
github.com/xenitab/pkg/gin v0.0.3/go.mod h1:5SCdGJOxEHVSLAQRyyv01gS6nUQbFrnepsuyLlW41a0=
github.com/xenitab/pkg/gin v0.0.4 h1:FIWZA9P6ICodwOf2A7+OKtQmRI1D+dZrCxZhco4cvXE=
github.com/xenitab/pkg/gin v0.0.4/go.mod h1:5SCdGJOxEHVSLAQRyyv01gS6nUQbFrnepsuyLlW41a0=
github.com/xenitab/pkg/kubernetes v0.0.2 h1:ptZIHsDS7ZtnJmCmgM+qkx3oCosUpJcqq3ulBVJpKIY=
github.com/xenitab/pkg/kubernetes v0.0.2/go.mod h1:thX/AkAMeyuHQ9hQYY49tzY64O1d9JYRqdt6mKnp9JE=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
Expand Down
15 changes: 10 additions & 5 deletions internal/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func NewRegistry(ctx context.Context, addr string, containerdClient *containerd.
PathFilter: regexp.MustCompile("/healthz"),
IncludeLatency: true,
IncludeClientIP: true,
IncludeKeys: []string{"handler"},
},
MetricsConfig: pkggin.MetricsConfig{
HandlerID: "registry",
Expand Down Expand Up @@ -123,7 +124,6 @@ func (r *RegistryHandler) registryHandler(c *gin.Context) {
}

// Serve registry endpoints.
r.log.Info("serving registry request", "path", c.Request.URL.Path)
ref, ok, err := ManifestReference(remoteRegistry, c.Request.URL.Path)
if err != nil {
c.AbortWithError(http.StatusNotFound, err)
Expand All @@ -149,6 +149,8 @@ func (r *RegistryHandler) registryHandler(c *gin.Context) {

// TODO: Retry multiple endoints
func (r *RegistryHandler) handleMirror(c *gin.Context, remoteRegistry, registryPort string) {
c.Set("handler", "mirror")

// Disable mirroring so we dont end with an infinite loop
c.Request.Header[MirrorHeader] = []string{"false"}

Expand All @@ -158,7 +160,7 @@ func (r *RegistryHandler) handleMirror(c *gin.Context, remoteRegistry, registryP
return
}
if !ok {
c.AbortWithStatus(http.StatusNotFound)
c.AbortWithError(http.StatusNotFound, fmt.Errorf("could not parse reference"))
return
}

Expand All @@ -174,21 +176,22 @@ func (r *RegistryHandler) handleMirror(c *gin.Context, remoteRegistry, registryP
return
}
if !ok {
r.log.Info("could not find node to forward", "ref", ref.String())
c.Status(http.StatusNotFound)
c.AbortWithError(http.StatusNotFound, fmt.Errorf("could not find node with ref: %s", ref.String()))
return
}
url, err := url.Parse(fmt.Sprintf("http://%s:%s", ip, registryPort))
if err != nil {
c.AbortWithError(http.StatusNotFound, err)
return
}
r.log.Info("forwarding request", "path", c.Request.URL.Path, "url", url.String())
r.log.V(5).Info("forwarding request", "path", c.Request.URL.Path, "url", url.String())
proxy := httputil.NewSingleHostReverseProxy(url)
proxy.ServeHTTP(c.Writer, c.Request)
}

func (r *RegistryHandler) handleManifest(c *gin.Context, ref reference.Spec) {
c.Set("handler", "manifest")

dgst := ref.Digest()
// Reference is tag so need to resolve digest
if dgst == "" {
Expand Down Expand Up @@ -230,6 +233,8 @@ func (r *RegistryHandler) handleManifest(c *gin.Context, ref reference.Spec) {
}

func (r *RegistryHandler) handleBlob(c *gin.Context, ref reference.Spec) {
c.Set("handler", "blob")

info, err := r.containerdClient.ContentStore().Info(c, ref.Digest())
if err != nil {
c.AbortWithError(http.StatusNotFound, err)
Expand Down

0 comments on commit 946d0d5

Please sign in to comment.