Skip to content

Commit

Permalink
pass proper logger down to datatx handler and tus
Browse files Browse the repository at this point in the history
Signed-off-by: Jörn Friedrich Dreyer <[email protected]>
  • Loading branch information
butonic committed Nov 13, 2024
1 parent b36aa05 commit 06e7921
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 17 deletions.
11 changes: 6 additions & 5 deletions internal/http/services/dataprovider/dataprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import (
"fmt"
"net/http"

"github.com/mitchellh/mapstructure"
"github.com/rs/zerolog"

"github.com/cs3org/reva/v2/pkg/appctx"
"github.com/cs3org/reva/v2/pkg/events"
"github.com/cs3org/reva/v2/pkg/events/stream"
Expand All @@ -30,8 +33,6 @@ import (
"github.com/cs3org/reva/v2/pkg/rhttp/router"
"github.com/cs3org/reva/v2/pkg/storage"
"github.com/cs3org/reva/v2/pkg/storage/fs/registry"
"github.com/mitchellh/mapstructure"
"github.com/rs/zerolog"
)

func init() {
Expand Down Expand Up @@ -103,7 +104,7 @@ func New(m map[string]interface{}, log *zerolog.Logger) (global.Service, error)
return nil, err
}

dataTXs, err := getDataTXs(conf, fs, evstream)
dataTXs, err := getDataTXs(conf, fs, evstream, log)
if err != nil {
return nil, err
}
Expand All @@ -125,7 +126,7 @@ func getFS(c *config, stream events.Stream) (storage.FS, error) {
return nil, fmt.Errorf("driver not found: %s", c.Driver)
}

func getDataTXs(c *config, fs storage.FS, publisher events.Publisher) (map[string]http.Handler, error) {
func getDataTXs(c *config, fs storage.FS, publisher events.Publisher, log *zerolog.Logger) (map[string]http.Handler, error) {
if c.DataTXs == nil {
c.DataTXs = make(map[string]map[string]interface{})
}
Expand All @@ -144,7 +145,7 @@ func getDataTXs(c *config, fs storage.FS, publisher events.Publisher) (map[strin
txs := make(map[string]http.Handler)
for t := range c.DataTXs {
if f, ok := datatxregistry.NewFuncs[t]; ok {
if tx, err := f(c.DataTXs[t], publisher); err == nil {
if tx, err := f(c.DataTXs[t], publisher, log); err == nil {
if handler, err := tx.Handler(fs); err == nil {
txs[t] = handler
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/rhttp/datatx/manager/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
package registry

import (
"github.com/rs/zerolog"

"github.com/cs3org/reva/v2/pkg/events"
"github.com/cs3org/reva/v2/pkg/rhttp/datatx"
)

// NewFunc is the function that data transfer implementations
// should register at init time.
type NewFunc func(map[string]interface{}, events.Publisher) (datatx.DataTX, error)
type NewFunc func(map[string]interface{}, events.Publisher, *zerolog.Logger) (datatx.DataTX, error)

// NewFuncs is a map containing all the registered data transfers.
var NewFuncs = map[string]NewFunc{}
Expand Down
10 changes: 7 additions & 3 deletions pkg/rhttp/datatx/manager/simple/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ import (
"net/http"
"time"

userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
"github.com/rs/zerolog"

userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/cs3org/reva/v2/internal/http/services/owncloud/ocdav/net"
"github.com/cs3org/reva/v2/pkg/appctx"
"github.com/cs3org/reva/v2/pkg/errtypes"
Expand All @@ -48,6 +49,7 @@ func init() {
type manager struct {
conf *cache.Config
publisher events.Publisher
log *zerolog.Logger
}

func parseConfig(m map[string]interface{}) (*cache.Config, error) {
Expand All @@ -60,7 +62,7 @@ func parseConfig(m map[string]interface{}) (*cache.Config, error) {
}

// New returns a datatx manager implementation that relies on HTTP PUT/GET.
func New(m map[string]interface{}, publisher events.Publisher) (datatx.DataTX, error) {
func New(m map[string]interface{}, publisher events.Publisher, log *zerolog.Logger) (datatx.DataTX, error) {
c, err := parseConfig(m)
if err != nil {
return nil, err
Expand All @@ -69,11 +71,13 @@ func New(m map[string]interface{}, publisher events.Publisher) (datatx.DataTX, e
return &manager{
conf: c,
publisher: publisher,
log: log,
}, nil
}

func (m *manager) Handler(fs storage.FS) (http.Handler, error) {
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r = r.WithContext(appctx.WithLogger(r.Context(), m.log))
ctx := r.Context()
sublog := appctx.GetLogger(ctx).With().Str("datatx", "simple").Logger()

Expand Down
11 changes: 8 additions & 3 deletions pkg/rhttp/datatx/manager/spaces/spaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ import (

userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
"github.com/rs/zerolog"

"github.com/cs3org/reva/v2/internal/http/services/owncloud/ocdav/net"
"github.com/cs3org/reva/v2/pkg/appctx"
"github.com/cs3org/reva/v2/pkg/errtypes"
Expand All @@ -39,8 +43,6 @@ import (
"github.com/cs3org/reva/v2/pkg/storage/cache"
"github.com/cs3org/reva/v2/pkg/storagespace"
"github.com/cs3org/reva/v2/pkg/utils"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
)

func init() {
Expand All @@ -50,6 +52,7 @@ func init() {
type manager struct {
conf *cache.Config
publisher events.Publisher
log *zerolog.Logger
}

func parseConfig(m map[string]interface{}) (*cache.Config, error) {
Expand All @@ -62,7 +65,7 @@ func parseConfig(m map[string]interface{}) (*cache.Config, error) {
}

// New returns a datatx manager implementation that relies on HTTP PUT/GET.
func New(m map[string]interface{}, publisher events.Publisher) (datatx.DataTX, error) {
func New(m map[string]interface{}, publisher events.Publisher, log *zerolog.Logger) (datatx.DataTX, error) {
c, err := parseConfig(m)
if err != nil {
return nil, err
Expand All @@ -71,11 +74,13 @@ func New(m map[string]interface{}, publisher events.Publisher) (datatx.DataTX, e
return &manager{
conf: c,
publisher: publisher,
log: log,
}, nil
}

func (m *manager) Handler(fs storage.FS) (http.Handler, error) {
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r = r.WithContext(appctx.WithLogger(r.Context(), m.log))
ctx := r.Context()
var spaceID string
spaceID, r.URL.Path = router.ShiftPath(r.URL.Path)
Expand Down
13 changes: 8 additions & 5 deletions pkg/rhttp/datatx/manager/tus/tus.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ import (
"path"
"regexp"

provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
"github.com/rs/zerolog"
tusd "github.com/tus/tusd/v2/pkg/handler"
"golang.org/x/exp/slog"

provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/cs3org/reva/v2/internal/http/services/owncloud/ocdav/net"
"github.com/cs3org/reva/v2/pkg/appctx"
"github.com/cs3org/reva/v2/pkg/errtypes"
Expand All @@ -38,8 +40,6 @@ import (
"github.com/cs3org/reva/v2/pkg/rhttp/datatx/metrics"
"github.com/cs3org/reva/v2/pkg/storage"
"github.com/cs3org/reva/v2/pkg/storagespace"
"github.com/mitchellh/mapstructure"
"golang.org/x/exp/slog"
)

func init() {
Expand All @@ -59,6 +59,7 @@ type TusConfig struct {
type manager struct {
conf *TusConfig
publisher events.Publisher
log *zerolog.Logger
}

func parseConfig(m map[string]interface{}) (*TusConfig, error) {
Expand All @@ -71,14 +72,15 @@ func parseConfig(m map[string]interface{}) (*TusConfig, error) {
}

// New returns a datatx manager implementation that relies on HTTP PUT/GET.
func New(m map[string]interface{}, publisher events.Publisher) (datatx.DataTX, error) {
func New(m map[string]interface{}, publisher events.Publisher, log *zerolog.Logger) (datatx.DataTX, error) {
c, err := parseConfig(m)
if err != nil {
return nil, err
}
return &manager{
conf: c,
publisher: publisher,
log: log,
}, nil
}

Expand All @@ -100,7 +102,7 @@ func (m *manager) Handler(fs storage.FS) (http.Handler, error) {
config := tusd.Config{
StoreComposer: composer,
NotifyCompleteUploads: true,
Logger: slog.New(tusdLogger{log: appctx.GetLogger(context.Background())}), // Note: this is a noop logger
Logger: slog.New(tusdLogger{log: m.log}),
}

if m.conf.CorsEnabled {
Expand Down Expand Up @@ -154,6 +156,7 @@ func (m *manager) Handler(fs storage.FS) (http.Handler, error) {
}

h := handler.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r = r.WithContext(appctx.WithLogger(r.Context(), m.log))
method := r.Method
// https://github.com/tus/tus-resumable-upload-protocol/blob/master/protocol.md#x-http-method-override
if r.Header.Get("X-HTTP-Method-Override") != "" {
Expand Down

0 comments on commit 06e7921

Please sign in to comment.