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

add request id to important http handlers #3376

Merged
merged 1 commit into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions changelog/unreleased/logging-improvements.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Enhancement: Improve logging

We improved the logging by adding the request id to ocdav, ocs and several other http services.

https://github.com/cs3org/reva/pull/3376
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ require (
github.com/emvi/iso-639-1 v1.0.1
github.com/eventials/go-tus v0.0.0-20220610120217-05d0564bb571
github.com/gdexlab/go-render v1.0.1
github.com/go-chi/chi v4.1.2+incompatible
github.com/go-chi/chi/v5 v5.0.7
github.com/go-ldap/ldap/v3 v3.4.4
github.com/go-micro/plugins/v4/events/natsjs v1.1.0
Expand Down
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,6 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/cs3org/cato v0.0.0-20200828125504-e418fc54dd5e h1:tqSPWQeueWTKnJVMJffz4pz0o1WuQxJ28+5x5JgaHD8=
github.com/cs3org/cato v0.0.0-20200828125504-e418fc54dd5e/go.mod h1:XJEZ3/EQuI3BXTp/6DUzFr850vlxq11I6satRtz0YQ4=
github.com/cs3org/go-cs3apis v0.0.0-20220929083235-bb0b1a236d6c h1:b+YTmOGlf43mnF8MzO0fsy8/Ho8JLu44Iq5Y0fKLJMM=
github.com/cs3org/go-cs3apis v0.0.0-20220929083235-bb0b1a236d6c/go.mod h1:UXha4TguuB52H14EMoSsCqDj7k8a/t7g4gVP+bgY5LY=
github.com/cs3org/go-cs3apis v0.0.0-20221005085457-19ea8088a512 h1:xTvaIsLu1ezoWOJKnV0ehgiowkOiEhMaylaI1lD/Axw=
github.com/cs3org/go-cs3apis v0.0.0-20221005085457-19ea8088a512/go.mod h1:UXha4TguuB52H14EMoSsCqDj7k8a/t7g4gVP+bgY5LY=
github.com/cs3org/go-cs3apis v0.0.0-20221012090518-ef2996678965 h1:y4n2j68LLnvac+zw/al8MfPgO5aQiIwLmHM/JzYN8AM=
github.com/cs3org/go-cs3apis v0.0.0-20221012090518-ef2996678965/go.mod h1:UXha4TguuB52H14EMoSsCqDj7k8a/t7g4gVP+bgY5LY=
github.com/cubewise-code/go-mime v0.0.0-20200519001935-8c5762b177d8 h1:Z9lwXumT5ACSmJ7WGnFl+OMLLjpz5uR2fyz7dC255FI=
Expand Down Expand Up @@ -247,8 +243,6 @@ github.com/gliderlabs/ssh v0.2.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aev
github.com/go-acme/lego/v4 v4.4.0 h1:uHhU5LpOYQOdp3aDU+XY2bajseu8fuExphTL1Ss6/Fc=
github.com/go-asn1-ber/asn1-ber v1.5.4 h1:vXT6d/FNDiELJnLb6hGNa309LMsrCoYFvpwHDF0+Y1A=
github.com/go-asn1-ber/asn1-ber v1.5.4/go.mod h1:hEBeB/ic+5LoWskz+yKT7vGhhPYkProFKoKdwZRWMe0=
github.com/go-chi/chi v4.1.2+incompatible h1:fGFk2Gmi/YKXk0OmGfBh0WgmN3XB8lVnEyNz34tQRec=
github.com/go-chi/chi v4.1.2+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ=
github.com/go-chi/chi/v5 v5.0.7 h1:rDTPXLDHGATaeHvVlLcR4Qe0zftYethFucbjVQ1PxU8=
github.com/go-chi/chi/v5 v5.0.7/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-git/gcfg v1.5.0 h1:Q5ViNfGF8zFgyJWPqYwA7qGFoMTEiBmdlkcfRmpIMa4=
Expand Down
1 change: 1 addition & 0 deletions internal/http/interceptors/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ import (
_ "github.com/cs3org/reva/v2/internal/http/interceptors/cors"
_ "github.com/cs3org/reva/v2/internal/http/interceptors/prometheus"
_ "github.com/cs3org/reva/v2/internal/http/interceptors/providerauthorizer"
_ "github.com/cs3org/reva/v2/internal/http/interceptors/requestid"
// Add your own middleware.
)
54 changes: 54 additions & 0 deletions internal/http/interceptors/requestid/requestid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2018-2021 CERN
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

package requestid

import (
"net/http"

"github.com/cs3org/reva/v2/pkg/rhttp/global"
"github.com/go-chi/chi/v5/middleware"
)

const (
defaultPriority = 100
)

func init() {
global.RegisterMiddleware("requestid", New)
}

// New returns a new HTTP middleware that adds the X-Request-ID to the context
func New(m map[string]interface{}) (global.Middleware, int, error) {
rh := requestIDHandler{}
return rh.handler, defaultPriority, nil
}

type requestIDHandler struct {
h http.Handler
}

// handler is a request id middleware
func (rh requestIDHandler) handler(h http.Handler) http.Handler {
rh.h = middleware.RequestID(h)
return rh
}

func (rh requestIDHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
rh.h.ServeHTTP(w, r)
}
2 changes: 1 addition & 1 deletion internal/http/services/appprovider/appprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import (
"github.com/cs3org/reva/v2/pkg/storagespace"
"github.com/cs3org/reva/v2/pkg/utils"
iso6391 "github.com/emvi/iso-639-1"
"github.com/go-chi/chi"
"github.com/go-chi/chi/v5"
ua "github.com/mileusna/useragent"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
Expand Down
4 changes: 2 additions & 2 deletions internal/http/services/archiver/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ import (
"net/http"
"time"

"regexp"

gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"

"regexp"

"github.com/cs3org/reva/v2/internal/http/services/archiver/manager"
"github.com/cs3org/reva/v2/pkg/errtypes"
"github.com/cs3org/reva/v2/pkg/rgrpc/todo/pool"
Expand Down
3 changes: 2 additions & 1 deletion internal/http/services/owncloud/ocdav/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func (h *MetaHandler) handlePathForUser(w http.ResponseWriter, r *http.Request,

id := storagespace.FormatResourceID(*rid)
sublog := appctx.GetLogger(ctx).With().Str("path", r.URL.Path).Str("resourceid", id).Logger()
sublog.Info().Msg("calling get path for user")
client, err := s.getClient()
if err != nil {
sublog.Error().Err(err).Msg("error getting grpc client")
Expand All @@ -118,7 +119,7 @@ func (h *MetaHandler) handlePathForUser(w http.ResponseWriter, r *http.Request,
pathReq := &provider.GetPathRequest{ResourceId: rid}
pathRes, err := client.GetPath(ctx, pathReq)
if err != nil {
sublog.Error().Err(err).Msg("error sending GetPath grpc request")
sublog.Error().Err(err).Msg("could not send GetPath grpc request: transport error")
w.WriteHeader(http.StatusInternalServerError)
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ func (h *Handler) GetShare(w http.ResponseWriter, r *http.Request) {
// the path (/users/u-u-i-d/foo) will not be accessible

// in a global namespace we can access the share using the full path
// in a jailed namespace we have to point to the mount point in the users /Shares jail
// in a jailed namespace we have to point to the mount point in the users /Shares Jail
// - needed for oc10 hot migration
// or use the /dav/spaces/<space id> endpoint?

Expand All @@ -619,7 +619,7 @@ func (h *Handler) GetShare(w http.ResponseWriter, r *http.Request) {
// - no, the gateway only sees the same list any has the same options as the ocs service
// - we would need to have a list of mountpoints for the shares -> owncloudstorageprovider for hot migration migration

// best we can do for now is stat the /Shares jail if it is set and return those paths
// best we can do for now is stat the /Shares Jail if it is set and return those paths

// if we are in a jail and the current share has been accepted use the stat from the share jail
// Needed because received shares can be jailed in a folder in the users home
Expand Down Expand Up @@ -934,7 +934,7 @@ func (h *Handler) listSharesWithMe(w http.ResponseWriter, r *http.Request) {
// the path (/users/u-u-i-d/foo) will not be accessible

// in a global namespace we can access the share using the full path
// in a jailed namespace we have to point to the mount point in the users /Shares jail
// in a jailed namespace we have to point to the mount point in the users /Shares Jail
// - needed for oc10 hot migration
// or use the /dav/spaces/<space id> endpoint?

Expand All @@ -946,7 +946,7 @@ func (h *Handler) listSharesWithMe(w http.ResponseWriter, r *http.Request) {
// - no, the gateway only sees the same list any has the same options as the ocs service
// - we would need to have a list of mountpoints for the shares -> owncloudstorageprovider for hot migration migration

// best we can do for now is stat the /Shares jail if it is set and return those paths
// best we can do for now is stat the /Shares Jail if it is set and return those paths

// if we are in a jail and the current share has been accepted use the stat from the share jail
// Needed because received shares can be jailed in a folder in the users home
Expand Down
11 changes: 10 additions & 1 deletion pkg/appctx/appctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"context"

rtrace "github.com/cs3org/reva/v2/pkg/trace"
"github.com/go-chi/chi/v5/middleware"
"github.com/rs/zerolog"
"go.opentelemetry.io/otel/trace"
)
Expand All @@ -37,7 +38,15 @@ func WithLogger(ctx context.Context, l *zerolog.Logger) context.Context {
// GetLogger returns the logger associated with the given context
// or a disabled logger in case no logger is stored inside the context.
func GetLogger(ctx context.Context) *zerolog.Logger {
return zerolog.Ctx(ctx)
logger := zerolog.Ctx(ctx)
reqID := middleware.GetReqID(ctx)

if reqID != "" {
sublogger := logger.With().Str("request-id", reqID).Logger()
logger = &sublogger
}

return logger
}

// WithTracerProvider returns a context with an associated TracerProvider
Expand Down
6 changes: 5 additions & 1 deletion pkg/micro/ocdav/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/cs3org/reva/v2/pkg/storage/favorite/memory"
rtrace "github.com/cs3org/reva/v2/pkg/trace"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
httpServer "github.com/go-micro/plugins/v4/server/http"
"github.com/owncloud/ocis/v2/ocis-pkg/registry"
"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -191,8 +192,11 @@ func useMiddlewares(r *chi.Mux, sopts *Options, svc global.Service, tp trace.Tra
// ctx
cm := appctx.New(sopts.Logger, tp)

// request-id
rm := middleware.RequestID
Copy link
Member Author

Choose a reason for hiding this comment

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

@rhafer Could not find another way. Any ideas? the ocdav service starts differently


// actually register
r.Use(pm, tm, lm, authMiddle, cm)
r.Use(pm, tm, lm, authMiddle, rm, cm)
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/rhttp/datatx/manager/tus/tus.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,14 @@ type composable interface {
}

func setExpiresHeader(fs storage.FS, w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
ctx := r.Context()
id := path.Base(r.URL.Path)
datastore, ok := fs.(tusd.DataStore)
if !ok {
appctx.GetLogger(ctx).Error().Interface("fs", fs).Msg("storage is not a tus datastore")
return
}
upload, err := datastore.GetUpload(context.Background(), id)
upload, err := datastore.GetUpload(ctx, id)
if err != nil {
appctx.GetLogger(ctx).Error().Err(err).Msg("could not get upload from storage")
return
Expand Down
12 changes: 2 additions & 10 deletions pkg/storage/utils/decomposedfs/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ import (
"github.com/cs3org/reva/v2/pkg/appctx"
ctxpkg "github.com/cs3org/reva/v2/pkg/ctx"
"github.com/cs3org/reva/v2/pkg/errtypes"
"github.com/cs3org/reva/v2/pkg/logger"
"github.com/cs3org/reva/v2/pkg/storage"
"github.com/cs3org/reva/v2/pkg/storage/utils/chunking"
"github.com/cs3org/reva/v2/pkg/storage/utils/decomposedfs/lookup"
Expand Down Expand Up @@ -398,6 +397,8 @@ func (fs *Decomposedfs) readInfo(id string) (tusd.FileInfo, error) {

// GetUpload returns the Upload for the given upload id
func (fs *Decomposedfs) GetUpload(ctx context.Context, id string) (tusd.Upload, error) {
l := appctx.GetLogger(ctx)
sub := l.With().Int("pid", os.Getpid()).Logger()
info, err := fs.readInfo(id)
if err != nil {
return nil, err
Expand All @@ -413,15 +414,6 @@ func (fs *Decomposedfs) GetUpload(ctx context.Context, id string) (tusd.Upload,
}

ctx = ctxpkg.ContextSetUser(ctx, u)
// TODO configure the logger the same way ... store and add traceid in file info

var opts []logger.Option
opts = append(opts, logger.WithLevel(info.Storage["LogLevel"]))
opts = append(opts, logger.WithWriter(os.Stderr, logger.ConsoleMode))
l := logger.New(opts...)

sub := l.With().Int("pid", os.Getpid()).Logger()

ctx = appctx.WithLogger(ctx, &sub)

return &fileUpload{
Expand Down