From 8081c95a6088ba92a6dde385e1e6469af8968d60 Mon Sep 17 00:00:00 2001 From: rot1024 Date: Thu, 29 Jun 2023 12:49:30 +0900 Subject: [PATCH] chore(server): print request ID to logs (#530) --- server/go.mod | 6 +- server/go.sum | 7 +- server/internal/adapter/gql/loader_plugin.go | 3 - .../internal/adapter/gql/loader_property.go | 3 - server/internal/app/app.go | 24 ++-- server/internal/app/auth_client.go | 6 +- server/internal/app/config/auth.go | 8 +- server/internal/app/config/auth_server.go | 2 +- server/internal/app/config/config_test.go | 2 +- server/internal/app/main.go | 6 +- server/internal/app/profiler.go | 2 +- server/internal/app/repo.go | 11 +- server/internal/app/tracer.go | 2 +- server/internal/app/web.go | 2 +- .../infrastructure/adapter/property_schema.go | 8 +- .../infrastructure/auth0/authenticator.go | 10 +- .../auth0/authenticator_test.go | 3 +- server/internal/infrastructure/fs/file.go | 20 +-- server/internal/infrastructure/fs/plugin.go | 18 +-- .../infrastructure/fs/property_schema.go | 10 +- server/internal/infrastructure/gcs/file.go | 56 ++++---- .../internal/infrastructure/google/fetch.go | 4 +- .../internal/infrastructure/google/google.go | 2 +- .../infrastructure/marketplace/marketplace.go | 24 ++-- server/internal/infrastructure/mongo/asset.go | 12 +- .../internal/infrastructure/mongo/config.go | 6 +- .../infrastructure/mongo/container.go | 31 ++--- .../internal/infrastructure/mongo/dataset.go | 18 +-- .../infrastructure/mongo/dataset_schema.go | 8 +- server/internal/infrastructure/mongo/layer.go | 6 +- server/internal/infrastructure/mongo/lock.go | 10 +- .../201217132559_add_scene_widget_id.go | 4 +- .../201217193948_add_scene_default_tile.go | 4 +- .../210310145844_remove_preview_token.go | 4 +- .../210730175108_add_scene_align_system.go | 4 +- ...220214180713_split_schema_of_properties.go | 4 +- ...4648_add_scene_field_to_property_schema.go | 4 +- .../221028204300_move_terrain_properties.go | 4 +- .../internal/infrastructure/mongo/plugin.go | 9 +- .../internal/infrastructure/mongo/policy.go | 4 +- .../internal/infrastructure/mongo/project.go | 14 +- .../internal/infrastructure/mongo/property.go | 11 +- .../infrastructure/mongo/property_schema.go | 4 +- server/internal/infrastructure/mongo/scene.go | 4 +- .../infrastructure/mongo/scene_lock.go | 4 +- server/internal/infrastructure/mongo/tag.go | 6 +- server/internal/infrastructure/mongo/user.go | 4 +- .../infrastructure/mongo/workspace.go | 4 +- server/internal/infrastructure/s3/s3.go | 31 +++-- .../internal/usecase/gateway/authenticator.go | 4 +- server/internal/usecase/gateway/google.go | 2 +- server/internal/usecase/interactor/dataset.go | 10 +- .../usecase/interactor/plugin_common.go | 6 +- .../usecase/interactor/plugin_upload.go | 6 +- .../internal/usecase/interactor/published.go | 6 +- server/internal/usecase/interactor/user.go | 4 +- .../usecase/interactor/user_signup.go | 1 + server/pkg/layer/decoding/shp_test.go | 26 ++-- server/pkg/log/gceformatter.go | 123 ------------------ server/pkg/log/log.go | 108 --------------- server/pkg/plugin/pluginpack/package.go | 3 +- server/pkg/scene/sceneops/plugin_installer.go | 20 +-- server/pkg/scene/sceneops/plugin_migrator.go | 2 +- 63 files changed, 269 insertions(+), 505 deletions(-) delete mode 100644 server/pkg/log/gceformatter.go delete mode 100644 server/pkg/log/log.go diff --git a/server/go.mod b/server/go.mod index 6c74cdf0fb..2f1ecd5b78 100644 --- a/server/go.mod +++ b/server/go.mod @@ -22,14 +22,12 @@ require ( github.com/kelseyhightower/envconfig v1.4.0 github.com/kennygrant/sanitize v1.2.4 github.com/labstack/echo/v4 v4.7.2 - github.com/labstack/gommon v0.3.1 github.com/mitchellh/mapstructure v1.5.0 github.com/paulmach/go.geojson v1.4.0 github.com/pkg/errors v0.9.1 github.com/ravilushqa/otelgqlgen v0.8.0 - github.com/reearth/reearthx v0.0.0-20230530055440-86a8d4675853 + github.com/reearth/reearthx v0.0.0-20230629030550-b8c5785d95e2 github.com/samber/lo v1.27.0 - github.com/sirupsen/logrus v1.8.1 github.com/spf13/afero v1.9.3 github.com/square/mongo-lock v0.0.0-20201208161834-4db518ed7fb2 github.com/stretchr/testify v1.8.0 @@ -112,6 +110,7 @@ require ( github.com/imkira/go-interpol v1.0.0 // indirect github.com/jmespath/go-jmespath v0.4.0 // indirect github.com/klauspost/compress v1.13.6 // indirect + github.com/labstack/gommon v0.3.1 // indirect github.com/maruel/panicparse/v2 v2.3.1 // indirect github.com/matryer/moq v0.2.7 // indirect github.com/mattn/go-colorable v0.1.13 // indirect @@ -125,6 +124,7 @@ require ( github.com/sendgrid/rest v2.6.9+incompatible // indirect github.com/sendgrid/sendgrid-go v3.12.0+incompatible // indirect github.com/sergi/go-diff v1.1.0 // indirect + github.com/sirupsen/logrus v1.8.1 // indirect github.com/smartystreets/assertions v1.1.1 // indirect github.com/smartystreets/goconvey v1.6.4 // indirect github.com/urfave/cli/v2 v2.8.1 // indirect diff --git a/server/go.sum b/server/go.sum index 57d0d5ee55..bbb84701cd 100644 --- a/server/go.sum +++ b/server/go.sum @@ -80,6 +80,7 @@ github.com/Masterminds/goutils v1.1.0/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy86 github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y= github.com/Masterminds/sprig v2.22.0+incompatible/go.mod h1:y6hNFY5UBTIWBxnzTeuNhlNS5hqE0NB0E6fgfo2Br3o= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= +github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d h1:licZJFw2RwpHMqeKTCYkitsPqHNxTmd4SNR5r94FGM8= github.com/agnivade/levenshtein v1.0.1/go.mod h1:CURSv5d9Uaml+FovSIICkLbAUZ9S4RqaHDIsdSBg7lM= github.com/agnivade/levenshtein v1.1.1 h1:QY8M92nrzkmr798gCo3kmMyqXFzdQVpxLlGPRBij0P8= github.com/agnivade/levenshtein v1.1.1/go.mod h1:veldBMzWxcCG2ZvUTKD2kJNRdCk5hVbJomOvKkmgYbo= @@ -492,8 +493,10 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/ravilushqa/otelgqlgen v0.8.0 h1:x48k+D1GMgm87xhMO2Lekrr9YGzFbpG3yijn9GpxuAY= github.com/ravilushqa/otelgqlgen v0.8.0/go.mod h1:6JO5YO2iY4POC7R6yB/L/RKXCcyISL8qQt5NnHOhh0o= -github.com/reearth/reearthx v0.0.0-20230530055440-86a8d4675853 h1:eO1u6WJ/B1j/uC0ZnHS21EYuaREbdYDgHAExy7LLAnw= -github.com/reearth/reearthx v0.0.0-20230530055440-86a8d4675853/go.mod h1:Rh7MJPKq43f+HZ/PwjZ5vEbGPpllNFvUrxn9sBn2b+s= +github.com/reearth/reearthx v0.0.0-20230628105450-fa673eee6505 h1:lTPbUsI7F1W5drXgWhPkSYRprC2j5xS/Fq2c2d+eIys= +github.com/reearth/reearthx v0.0.0-20230628105450-fa673eee6505/go.mod h1:O4zFr6ks8jDKfgy9NBpZKp31jpmAPnfOtMQK9B6xX54= +github.com/reearth/reearthx v0.0.0-20230629030550-b8c5785d95e2 h1:hVSBdiK+0/1WsTkANFncN0hq/xa4xJZ7Sl75Dxwr5Cw= +github.com/reearth/reearthx v0.0.0-20230629030550-b8c5785d95e2/go.mod h1:O4zFr6ks8jDKfgy9NBpZKp31jpmAPnfOtMQK9B6xX54= github.com/robertkrimen/godocdown v0.0.0-20130622164427-0bfa04905481/go.mod h1:C9WhFzY47SzYBIvzFqSvHIR6ROgDo4TtdTuRaOMjF/s= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= diff --git a/server/internal/adapter/gql/loader_plugin.go b/server/internal/adapter/gql/loader_plugin.go index bf85b1d561..0c56adce83 100644 --- a/server/internal/adapter/gql/loader_plugin.go +++ b/server/internal/adapter/gql/loader_plugin.go @@ -6,7 +6,6 @@ import ( "github.com/reearth/reearth/server/internal/adapter/gql/gqldataloader" "github.com/reearth/reearth/server/internal/adapter/gql/gqlmodel" "github.com/reearth/reearth/server/internal/usecase/interfaces" - "github.com/reearth/reearth/server/pkg/log" "github.com/reearth/reearthx/util" ) @@ -25,8 +24,6 @@ func (c *PluginLoader) Fetch(ctx context.Context, ids []gqlmodel.ID) ([]*gqlmode } op := getOperator(ctx) - log.Infof("TEMP: PluginLoader.Fetch op: %#v", op) - log.Infof("TEMP: PluginLoader.Fetch ids: %v", ids) res, err := c.usecase.Fetch(ctx, ids2, op) if err != nil { diff --git a/server/internal/adapter/gql/loader_property.go b/server/internal/adapter/gql/loader_property.go index c3c34a29e4..9abe9db187 100644 --- a/server/internal/adapter/gql/loader_property.go +++ b/server/internal/adapter/gql/loader_property.go @@ -7,7 +7,6 @@ import ( "github.com/reearth/reearth/server/internal/adapter/gql/gqlmodel" "github.com/reearth/reearth/server/internal/usecase/interfaces" "github.com/reearth/reearth/server/pkg/id" - "github.com/reearth/reearth/server/pkg/log" "github.com/reearth/reearthx/util" ) @@ -26,8 +25,6 @@ func (c *PropertyLoader) Fetch(ctx context.Context, ids []gqlmodel.ID) ([]*gqlmo } op := getOperator(ctx) - log.Infof("TEMP: PropertyLoader.Fetch op: %#v", op) - log.Infof("TEMP: PropertyLoader.Fetch ids: %v", ids) res, err := c.usecase.Fetch(ctx, ids2, op) if err != nil { diff --git a/server/internal/app/app.go b/server/internal/app/app.go index 382261ace8..54130ba0c3 100644 --- a/server/internal/app/app.go +++ b/server/internal/app/app.go @@ -23,7 +23,7 @@ import ( func initEcho(ctx context.Context, cfg *ServerConfig) *echo.Echo { if cfg.Config == nil { - log.Fatalln("ServerConfig.Config is nil") + log.Fatalf("ServerConfig.Config is nil") } e := echo.New() @@ -36,9 +36,10 @@ func initEcho(ctx context.Context, cfg *ServerConfig) *echo.Echo { logger := log.NewEcho() e.Logger = logger e.Use( - logger.AccessLogger(), middleware.Recover(), otelecho.Middleware("reearth"), + echo.WrapMiddleware(appx.RequestIDMiddleware()), + logger.AccessLogger(), ) if cfg.Config.HTTPSREDIRECT { e.Use(middleware.HTTPSRedirectWithConfig(middleware.RedirectConfig{ @@ -54,8 +55,11 @@ func initEcho(ctx context.Context, cfg *ServerConfig) *echo.Echo { ) } + // auth + authConfig := cfg.Config.JWTProviders() + log.Infof("auth: config: %#v", authConfig) e.Use( - echo.WrapMiddleware(lo.Must(appx.AuthMiddleware(cfg.Config.JWTProviders(), adapter.ContextAuthInfo, true))), + echo.WrapMiddleware(lo.Must(appx.AuthMiddleware(authConfig, adapter.ContextAuthInfo, true))), attachOpMiddleware(cfg), ) @@ -75,7 +79,7 @@ func initEcho(ctx context.Context, cfg *ServerConfig) *echo.Echo { e.GET("/graphql", echo.WrapHandler( playground.Handler("reearth", "/api/graphql"), )) - log.Infof("gql: GraphQL Playground is available") + log.Infofc(ctx, "gql: GraphQL Playground is available") } // init usecases @@ -139,7 +143,6 @@ func initEcho(ctx context.Context, cfg *ServerConfig) *echo.Echo { func errorHandler(next func(error, echo.Context)) func(error, echo.Context) { return func(err error, c echo.Context) { - ctx := c.Request().Context() if c.Response().Committed { return } @@ -148,17 +151,8 @@ func errorHandler(next func(error, echo.Context)) func(error, echo.Context) { err = rerror.ErrNotFound } - if err := rerror.UnwrapErrInternal(err); err != nil { - au := adapter.GetAuthInfo(ctx) - u := adapter.User(ctx) - op := adapter.Operator(ctx) - c.Echo().Logger.Errorf("AUTH: %#v", au) - c.Echo().Logger.Errorf("USER: %#v", u) - c.Echo().Logger.Errorf("OP: %#v", op) - } - code, msg := errorMessage(err, func(f string, args ...interface{}) { - c.Echo().Logger.Errorf(f, args...) + log.Errorfc(c.Request().Context(), f, args...) }) if err := c.JSON(code, map[string]string{ "error": msg, diff --git a/server/internal/app/auth_client.go b/server/internal/app/auth_client.go index c013c92722..28dfe2c339 100644 --- a/server/internal/app/auth_client.go +++ b/server/internal/app/auth_client.go @@ -9,6 +9,7 @@ import ( "github.com/reearth/reearth/server/pkg/id" "github.com/reearth/reearth/server/pkg/user" "github.com/reearth/reearth/server/pkg/workspace" + "github.com/reearth/reearthx/log" "github.com/reearth/reearthx/rerror" "github.com/reearth/reearthx/util" ) @@ -78,13 +79,16 @@ func attachOpMiddleware(cfg *ServerConfig) echo.MiddlewareFunc { } if u != nil { + ctx = adapter.AttachUser(ctx, u) + log.Debugfc(ctx, "auth: user: id=%s name=%s email=%s", u.ID(), u.Name(), u.Email()) + op, err := generateOperator(ctx, cfg, u) if err != nil { return err } - ctx = adapter.AttachUser(ctx, u) ctx = adapter.AttachOperator(ctx, op) + log.Debugfc(ctx, "auth: op: %#v", op) } c.SetRequest(req.WithContext(ctx)) diff --git a/server/internal/app/config/auth.go b/server/internal/app/config/auth.go index 686f0d2d11..2b1082939d 100644 --- a/server/internal/app/config/auth.go +++ b/server/internal/app/config/auth.go @@ -67,7 +67,7 @@ func (c Auth0Config) AuthConfigForWeb() *AuthConfig { if c.Domain == "" || c.WebClientID == "" { return nil } - domain := prepareUrl(c.Domain) + domain := getAuthDomain(c.Domain) var aud []string if len(c.Audience) > 0 { aud = []string{c.Audience} @@ -83,7 +83,7 @@ func (c Auth0Config) AuthConfigForM2M() *AuthConfig { if c.Domain == "" || c.ClientID == "" { return nil } - domain := prepareUrl(c.Domain) + domain := getAuthDomain(c.Domain) var aud []string if len(c.Audience) > 0 { aud = []string{c.Audience} @@ -125,11 +125,11 @@ func (c CognitoConfig) Configs() AuthConfigs { } } -func prepareUrl(url string) string { +func getAuthDomain(url string) string { if !strings.HasPrefix(url, "https://") && !strings.HasPrefix(url, "http://") { url = "https://" + url } - // url = strings.TrimSuffix(url, "/") + // Auth0 JS library adds slash to the end of the domain (issuer) if !strings.HasSuffix(url, "/") { url = url + "/" } diff --git a/server/internal/app/config/auth_server.go b/server/internal/app/config/auth_server.go index e9cf27e48b..881b581700 100644 --- a/server/internal/app/config/auth_server.go +++ b/server/internal/app/config/auth_server.go @@ -37,7 +37,7 @@ func (c AuthSrvConfig) AuthConfig(debug bool, host string) *AuthConfig { } return &AuthConfig{ - ISS: domain, + ISS: getAuthDomain(domain), AUD: aud, ClientID: lo.ToPtr(AuthServerDefaultClientID), } diff --git a/server/internal/app/config/config_test.go b/server/internal/app/config/config_test.go index fee1d218a8..7d3845147b 100644 --- a/server/internal/app/config/config_test.go +++ b/server/internal/app/config/config_test.go @@ -9,7 +9,7 @@ import ( func TestReadConfig(t *testing.T) { clientID := AuthServerDefaultClientID localAuth := AuthConfig{ - ISS: "http://localhost:8080", + ISS: "http://localhost:8080/", AUD: []string{"http://localhost:8080"}, ClientID: &clientID, } diff --git a/server/internal/app/main.go b/server/internal/app/main.go index 0ed971509e..1f5f805d24 100644 --- a/server/internal/app/main.go +++ b/server/internal/app/main.go @@ -23,7 +23,7 @@ func Start(debug bool, version string) { // Load config conf, cerr := config.ReadConfig(debug) if cerr != nil { - log.Fatal(cerr) + log.Fatalf("failed to load config: %v", cerr) } log.Infof("config: %s", conf.Print()) @@ -89,7 +89,7 @@ func NewServer(ctx context.Context, cfg *ServerConfig) *WebServer { } func (w *WebServer) Run() { - defer log.Infoln("Server shutdown") + defer log.Infof("Server shutdown") debugLog := "" if w.appServer.Debug { @@ -99,7 +99,7 @@ func (w *WebServer) Run() { go func() { err := w.appServer.StartH2CServer(w.address, &http2.Server{}) - log.Fatalln(err.Error()) + log.Fatalf("failed to run server: %v", err) }() quit := make(chan os.Signal, 1) diff --git a/server/internal/app/profiler.go b/server/internal/app/profiler.go index f04ca1f9a3..bc6607c2a5 100644 --- a/server/internal/app/profiler.go +++ b/server/internal/app/profiler.go @@ -22,6 +22,6 @@ func initGCPProfiler(version string) { Service: "reearth", ServiceVersion: version, }); err != nil { - log.Fatalln(err) + log.Fatalf("failed to init GCP profiler: %v", err) } } diff --git a/server/internal/app/repo.go b/server/internal/app/repo.go index 0a3a3b3ed1..864b0a6f9f 100644 --- a/server/internal/app/repo.go +++ b/server/internal/app/repo.go @@ -2,7 +2,6 @@ package app import ( "context" - "fmt" "time" "github.com/reearth/reearth/server/internal/app/config" @@ -63,7 +62,7 @@ func initReposAndGateways(ctx context.Context, conf *config.Config, debug bool) // release lock of all scenes if err := repos.SceneLock.ReleaseAllLock(context.Background()); err != nil { - log.Fatalln(fmt.Sprintf("repo initialization error: %+v", err)) + log.Fatalf("repo initialization error: %v", err) } return repos, gateways @@ -72,7 +71,7 @@ func initReposAndGateways(ctx context.Context, conf *config.Config, debug bool) func initFile(ctx context.Context, conf *config.Config) (fileRepo gateway.File) { var err error if conf.GCS.IsConfigured() { - log.Infof("file: GCS storage is used: %s\n", conf.GCS.BucketName) + log.Infofc(ctx, "file: GCS storage is used: %s\n", conf.GCS.BucketName) fileRepo, err = gcs.NewFile(conf.GCS.BucketName, conf.AssetBaseURL, conf.GCS.PublicationCacheControl) if err != nil { log.Warnf("file: failed to init GCS storage: %s\n", err.Error()) @@ -82,7 +81,7 @@ func initFile(ctx context.Context, conf *config.Config) (fileRepo gateway.File) } if conf.S3.IsConfigured() { - log.Infof("file: S3 storage is used: %s\n", conf.S3.BucketName) + log.Infofc(ctx, "file: S3 storage is used: %s\n", conf.S3.BucketName) fileRepo, err = s3.NewS3(ctx, conf.S3.BucketName, conf.AssetBaseURL, conf.S3.PublicationCacheControl) if err != nil { log.Warnf("file: failed to init S3 storage: %s\n", err.Error()) @@ -90,11 +89,11 @@ func initFile(ctx context.Context, conf *config.Config) (fileRepo gateway.File) return } - log.Infoln("file: local storage is used") + log.Infof("file: local storage is used") afs := afero.NewBasePathFs(afero.NewOsFs(), "data") fileRepo, err = fs.NewFile(afs, conf.AssetBaseURL) if err != nil { - log.Fatalln(fmt.Sprintf("file: init error: %+v", err)) + log.Fatalf("file: init error: %+v", err) } return fileRepo } diff --git a/server/internal/app/tracer.go b/server/internal/app/tracer.go index 991a7c9a15..a48e52b0f9 100644 --- a/server/internal/app/tracer.go +++ b/server/internal/app/tracer.go @@ -27,7 +27,7 @@ func initTracer(ctx context.Context, conf *config.Config) io.Closer { func initGCPTracer(ctx context.Context, conf *config.Config) { exporter, err := texporter.New(texporter.WithProjectID(conf.GCPProject)) if err != nil { - log.Fatalln(err) + log.Fatalf("failed to init GCP tracer: %v", err) } tp := sdktrace.NewTracerProvider(sdktrace.WithSyncer(exporter), sdktrace.WithSampler(sdktrace.TraceIDRatioBased(conf.TracerSample))) diff --git a/server/internal/app/web.go b/server/internal/app/web.go index 70832280d1..c7a79fc802 100644 --- a/server/internal/app/web.go +++ b/server/internal/app/web.go @@ -55,7 +55,7 @@ func (w *WebHandler) Handler(e *echo.Echo) { return } - e.Logger.Info("web: web directory will be delivered\n") + log.Infof("web: web directory will be delivered") cfg := map[string]any{} if w.AuthConfig != nil { diff --git a/server/internal/infrastructure/adapter/property_schema.go b/server/internal/infrastructure/adapter/property_schema.go index 43e6bef51a..bfb6b6081c 100644 --- a/server/internal/infrastructure/adapter/property_schema.go +++ b/server/internal/infrastructure/adapter/property_schema.go @@ -64,28 +64,28 @@ func (r *propertySchema) FindByIDs(ctx context.Context, ids []id.PropertySchemaI func (r *propertySchema) Save(ctx context.Context, p *property.Schema) error { if r.writer == nil { - return rerror.ErrInternalBy(errors.New("writer is not set")) + return rerror.ErrInternalByWithContext(ctx, errors.New("writer is not set")) } return r.writer.Save(ctx, p) } func (r *propertySchema) SaveAll(ctx context.Context, p property.SchemaList) error { if r.writer == nil { - return rerror.ErrInternalBy(errors.New("writer is not set")) + return rerror.ErrInternalByWithContext(ctx, errors.New("writer is not set")) } return r.writer.SaveAll(ctx, p) } func (r *propertySchema) Remove(ctx context.Context, p id.PropertySchemaID) error { if r.writer == nil { - return rerror.ErrInternalBy(errors.New("writer is not set")) + return rerror.ErrInternalByWithContext(ctx, errors.New("writer is not set")) } return r.writer.Remove(ctx, p) } func (r *propertySchema) RemoveAll(ctx context.Context, p []id.PropertySchemaID) error { if r.writer == nil { - return rerror.ErrInternalBy(errors.New("writer is not set")) + return rerror.ErrInternalByWithContext(ctx, errors.New("writer is not set")) } return r.writer.RemoveAll(ctx, p) } diff --git a/server/internal/infrastructure/auth0/authenticator.go b/server/internal/infrastructure/auth0/authenticator.go index 5d7527e724..30b629436b 100644 --- a/server/internal/infrastructure/auth0/authenticator.go +++ b/server/internal/infrastructure/auth0/authenticator.go @@ -72,7 +72,7 @@ func New(domain, clientID, clientSecret string) *Auth0 { } } -func (a *Auth0) UpdateUser(p gateway.AuthenticatorUpdateUserParam) (data gateway.AuthenticatorUser, err error) { +func (a *Auth0) UpdateUser(ctx context.Context, p gateway.AuthenticatorUpdateUserParam) (data gateway.AuthenticatorUser, err error) { if err != nil { return } @@ -93,7 +93,7 @@ func (a *Auth0) UpdateUser(p gateway.AuthenticatorUpdateUserParam) (data gateway } var r response - r, err = a.exec(http.MethodPatch, "api/v2/users/"+p.ID, payload) + r, err = a.exec(ctx, http.MethodPatch, "api/v2/users/"+p.ID, payload) if err != nil { err = rerror.ErrInternalByWith("failed to update user", err) return @@ -103,7 +103,7 @@ func (a *Auth0) UpdateUser(p gateway.AuthenticatorUpdateUserParam) (data gateway return } -func (a *Auth0) exec(method, path string, b any) (r response, err error) { +func (a *Auth0) exec(ctx context.Context, method, path string, b any) (r response, err error) { if a == nil || a.base == "" { err = errors.New("auth0: domain is not set") return @@ -124,7 +124,7 @@ func (a *Auth0) exec(method, path string, b any) (r response, err error) { } var req *http.Request - req, err = http.NewRequest(method, a.base+path, body) + req, err = http.NewRequestWithContext(ctx, method, a.base+path, body) if err != nil { return } @@ -144,7 +144,7 @@ func (a *Auth0) exec(method, path string, b any) (r response, err error) { } if !a.disableLogging { - log.Infof("auth0: path: %s, status: %d, resp: %s", path, resp.StatusCode, respb) + log.Infofc(ctx, "auth0: path: %s, status: %d, resp: %s", path, resp.StatusCode, respb) } if err = json.Unmarshal(respb, &r); err != nil { diff --git a/server/internal/infrastructure/auth0/authenticator_test.go b/server/internal/infrastructure/auth0/authenticator_test.go index 84c2a4b4f1..67c9059fe1 100644 --- a/server/internal/infrastructure/auth0/authenticator_test.go +++ b/server/internal/infrastructure/auth0/authenticator_test.go @@ -1,6 +1,7 @@ package auth0 import ( + "context" "encoding/json" "net/http" "strings" @@ -88,7 +89,7 @@ func TestAuth0(t *testing.T) { }) newEmail := "xxxxx" - r, err := a.UpdateUser(gateway.AuthenticatorUpdateUserParam{ + r, err := a.UpdateUser(context.Background(), gateway.AuthenticatorUpdateUserParam{ ID: userID, Email: &newEmail, }) diff --git a/server/internal/infrastructure/fs/file.go b/server/internal/infrastructure/fs/file.go index 8171b4d886..2a8226da4d 100644 --- a/server/internal/infrastructure/fs/file.go +++ b/server/internal/infrastructure/fs/file.go @@ -102,7 +102,7 @@ func (f *fileRepo) RemoveBuiltScene(ctx context.Context, name string) error { // helpers -func (f *fileRepo) read(_ context.Context, filename string) (io.ReadCloser, error) { +func (f *fileRepo) read(ctx context.Context, filename string) (io.ReadCloser, error) { if filename == "" { return nil, rerror.ErrNotFound } @@ -112,25 +112,25 @@ func (f *fileRepo) read(_ context.Context, filename string) (io.ReadCloser, erro if os.IsNotExist(err) { return nil, rerror.ErrNotFound } - return nil, rerror.ErrInternalBy(err) + return nil, rerror.ErrInternalByWithContext(ctx, err) } return file, nil } -func (f *fileRepo) upload(_ context.Context, filename string, content io.Reader) (int64, error) { +func (f *fileRepo) upload(ctx context.Context, filename string, content io.Reader) (int64, error) { if filename == "" { return 0, gateway.ErrFailedToUploadFile } if fnd := filepath.Dir(filename); fnd != "" { if err := f.fs.MkdirAll(fnd, 0755); err != nil { - return 0, rerror.ErrInternalBy(err) + return 0, rerror.ErrInternalByWithContext(ctx, err) } } dest, err := f.fs.Create(filename) if err != nil { - return 0, rerror.ErrInternalBy(err) + return 0, rerror.ErrInternalByWithContext(ctx, err) } defer func() { _ = dest.Close() @@ -144,14 +144,14 @@ func (f *fileRepo) upload(_ context.Context, filename string, content io.Reader) return size, nil } -func (f *fileRepo) move(_ context.Context, from, dest string) error { +func (f *fileRepo) move(ctx context.Context, from, dest string) error { if from == "" || dest == "" || from == dest { return gateway.ErrInvalidFile } if destd := filepath.Dir(dest); destd != "" { if err := f.fs.MkdirAll(destd, 0755); err != nil { - return rerror.ErrInternalBy(err) + return rerror.ErrInternalByWithContext(ctx, err) } } @@ -159,13 +159,13 @@ func (f *fileRepo) move(_ context.Context, from, dest string) error { if os.IsNotExist(err) { return rerror.ErrNotFound } - return rerror.ErrInternalBy(err) + return rerror.ErrInternalByWithContext(ctx, err) } return nil } -func (f *fileRepo) delete(_ context.Context, filename string) error { +func (f *fileRepo) delete(ctx context.Context, filename string) error { if filename == "" { return gateway.ErrFailedToUploadFile } @@ -174,7 +174,7 @@ func (f *fileRepo) delete(_ context.Context, filename string) error { if os.IsNotExist(err) { return nil } - return rerror.ErrInternalBy(err) + return rerror.ErrInternalByWithContext(ctx, err) } return nil } diff --git a/server/internal/infrastructure/fs/plugin.go b/server/internal/infrastructure/fs/plugin.go index a7b01eb625..cfceea75a5 100644 --- a/server/internal/infrastructure/fs/plugin.go +++ b/server/internal/infrastructure/fs/plugin.go @@ -33,7 +33,7 @@ func (r *pluginRepo) Filtered(f repo.SceneFilter) repo.Plugin { } func (r *pluginRepo) FindByID(ctx context.Context, pid id.PluginID) (*plugin.Plugin, error) { - m, err := readPluginManifest(r.fs, pid) + m, err := readPluginManifest(ctx, r.fs, pid) if err != nil { return nil, err } @@ -58,25 +58,25 @@ func (r *pluginRepo) FindByIDs(ctx context.Context, ids []id.PluginID) ([]*plugi } func (r *pluginRepo) Save(ctx context.Context, p *plugin.Plugin) error { - return rerror.ErrInternalBy(errors.New("read only")) + return rerror.ErrInternalByWithContext(ctx, errors.New("read only")) } func (r *pluginRepo) Remove(ctx context.Context, pid id.PluginID) error { - return rerror.ErrInternalBy(errors.New("read only")) + return rerror.ErrInternalByWithContext(ctx, errors.New("read only")) } var translationFileNameRegexp = regexp.MustCompile(`reearth_([a-zA-Z]+(?:-[a-zA-Z]+)?).yml`) -func readPluginManifest(fs afero.Fs, pid id.PluginID) (*manifest.Manifest, error) { +func readPluginManifest(ctx context.Context, fs afero.Fs, pid id.PluginID) (*manifest.Manifest, error) { base := filepath.Join(pluginDir, pid.String()) - translationMap, err := readPluginTranslation(fs, base) + translationMap, err := readPluginTranslation(ctx, fs, base) if err != nil { return nil, err } f, err := fs.Open(filepath.Join(base, manifestFilePath)) if err != nil { - return nil, rerror.ErrInternalBy(err) + return nil, rerror.ErrInternalByWithContext(ctx, err) } defer func() { _ = f.Close() @@ -90,10 +90,10 @@ func readPluginManifest(fs afero.Fs, pid id.PluginID) (*manifest.Manifest, error return m, nil } -func readPluginTranslation(fs afero.Fs, base string) (manifest.TranslationMap, error) { +func readPluginTranslation(ctx context.Context, fs afero.Fs, base string) (manifest.TranslationMap, error) { d, err := afero.ReadDir(fs, base) if err != nil { - return nil, rerror.ErrInternalBy(err) + return nil, rerror.ErrInternalByWithContext(ctx, err) } translationMap := manifest.TranslationMap{} @@ -108,7 +108,7 @@ func readPluginTranslation(fs afero.Fs, base string) (manifest.TranslationMap, e } langfile, err := fs.Open(filepath.Join(base, name)) if err != nil { - return nil, rerror.ErrInternalBy(err) + return nil, rerror.ErrInternalByWithContext(ctx, err) } defer func() { _ = langfile.Close() diff --git a/server/internal/infrastructure/fs/property_schema.go b/server/internal/infrastructure/fs/property_schema.go index 3257c13e0c..e9819f960f 100644 --- a/server/internal/infrastructure/fs/property_schema.go +++ b/server/internal/infrastructure/fs/property_schema.go @@ -30,7 +30,7 @@ func (r *propertySchema) Filtered(f repo.SceneFilter) repo.PropertySchema { } func (r *propertySchema) FindByID(ctx context.Context, i id.PropertySchemaID) (*property.Schema, error) { - m, err := readPluginManifest(r.fs, i.Plugin()) + m, err := readPluginManifest(ctx, r.fs, i.Plugin()) if err != nil { return nil, err } @@ -66,17 +66,17 @@ func (r *propertySchema) FindByIDs(ctx context.Context, ids []id.PropertySchemaI } func (r *propertySchema) Save(ctx context.Context, p *property.Schema) error { - return rerror.ErrInternalBy(errors.New("read only")) + return rerror.ErrInternalByWithContext(ctx, errors.New("read only")) } func (r *propertySchema) SaveAll(ctx context.Context, p property.SchemaList) error { - return rerror.ErrInternalBy(errors.New("read only")) + return rerror.ErrInternalByWithContext(ctx, errors.New("read only")) } func (r *propertySchema) Remove(ctx context.Context, pid id.PropertySchemaID) error { - return rerror.ErrInternalBy(errors.New("read only")) + return rerror.ErrInternalByWithContext(ctx, errors.New("read only")) } func (r *propertySchema) RemoveAll(ctx context.Context, pid []id.PropertySchemaID) error { - return rerror.ErrInternalBy(errors.New("read only")) + return rerror.ErrInternalByWithContext(ctx, errors.New("read only")) } diff --git a/server/internal/infrastructure/gcs/file.go b/server/internal/infrastructure/gcs/file.go index 28d0fe8923..9453adfd2e 100644 --- a/server/internal/infrastructure/gcs/file.go +++ b/server/internal/infrastructure/gcs/file.go @@ -90,7 +90,7 @@ func (f *fileRepo) UploadAsset(ctx context.Context, file *file.File) (*url.URL, } func (f *fileRepo) RemoveAsset(ctx context.Context, u *url.URL) error { - log.Infof("gcs: asset deleted: %s", u) + log.Infofc(ctx, "gcs: asset deleted: %s", u) sn := getGCSObjectNameFromURL(f.base, u) if sn == "" { @@ -119,7 +119,7 @@ func (f *fileRepo) UploadPluginFile(ctx context.Context, pid id.PluginID, file * } func (f *fileRepo) RemovePlugin(ctx context.Context, pid id.PluginID) error { - log.Infof("gcs: plugin deleted: %s", pid) + log.Infofc(ctx, "gcs: plugin deleted: %s", pid) return f.deleteAll(ctx, path.Join(gcsPluginBasePath, pid.String())) } @@ -152,7 +152,7 @@ func (f *fileRepo) MoveBuiltScene(ctx context.Context, oldName, name string) err } func (f *fileRepo) RemoveBuiltScene(ctx context.Context, name string) error { - log.Infof("gcs: built scene deleted: %s", name) + log.Infofc(ctx, "gcs: built scene deleted: %s", name) sn := sanitize.Path(name + ".json") if sn == "" { @@ -179,8 +179,8 @@ func (f *fileRepo) read(ctx context.Context, filename string) (io.ReadCloser, er bucket, err := f.bucket(ctx) if err != nil { - log.Errorf("gcs: read bucket err: %+v\n", err) - return nil, rerror.ErrInternalBy(err) + log.Errorfc(ctx, "gcs: read bucket err: %+v\n", err) + return nil, rerror.ErrInternalByWithContext(ctx, err) } reader, err := bucket.Object(filename).NewReader(ctx) @@ -188,8 +188,8 @@ func (f *fileRepo) read(ctx context.Context, filename string) (io.ReadCloser, er if errors.Is(err, storage.ErrObjectNotExist) { return nil, rerror.ErrNotFound } - log.Errorf("gcs: read err: %+v\n", err) - return nil, rerror.ErrInternalBy(err) + log.Errorfc(ctx, "gcs: read err: %+v\n", err) + return nil, rerror.ErrInternalByWithContext(ctx, err) } return reader, nil @@ -202,13 +202,13 @@ func (f *fileRepo) upload(ctx context.Context, filename string, content io.Reade bucket, err := f.bucket(ctx) if err != nil { - log.Errorf("gcs: upload bucket err: %+v\n", err) - return 0, rerror.ErrInternalBy(err) + log.Errorfc(ctx, "gcs: upload bucket err: %+v\n", err) + return 0, rerror.ErrInternalByWithContext(ctx, err) } object := bucket.Object(filename) if err := object.Delete(ctx); err != nil && !errors.Is(err, storage.ErrObjectNotExist) { - log.Errorf("gcs: upload delete err: %+v\n", err) + log.Errorfc(ctx, "gcs: upload delete err: %+v\n", err) return 0, gateway.ErrFailedToUploadFile } @@ -217,12 +217,12 @@ func (f *fileRepo) upload(ctx context.Context, filename string, content io.Reade size, err := io.Copy(writer, content) if err != nil { - log.Errorf("gcs: upload err: %+v\n", err) + log.Errorfc(ctx, "gcs: upload err: %+v\n", err) return 0, gateway.ErrFailedToUploadFile } if err := writer.Close(); err != nil { - log.Errorf("gcs: upload close err: %+v\n", err) + log.Errorfc(ctx, "gcs: upload close err: %+v\n", err) return 0, gateway.ErrFailedToUploadFile } @@ -236,8 +236,8 @@ func (f *fileRepo) move(ctx context.Context, from, dest string) error { bucket, err := f.bucket(ctx) if err != nil { - log.Errorf("gcs: move bucket err: %+v\n", err) - return rerror.ErrInternalBy(err) + log.Errorfc(ctx, "gcs: move bucket err: %+v\n", err) + return rerror.ErrInternalByWithContext(ctx, err) } object := bucket.Object(from) @@ -246,13 +246,13 @@ func (f *fileRepo) move(ctx context.Context, from, dest string) error { if errors.Is(err, storage.ErrObjectNotExist) { return rerror.ErrNotFound } - log.Errorf("gcs: move copy err: %+v\n", err) - return rerror.ErrInternalBy(err) + log.Errorfc(ctx, "gcs: move copy err: %+v\n", err) + return rerror.ErrInternalByWithContext(ctx, err) } if err := object.Delete(ctx); err != nil { - log.Errorf("gcs: move delete err: %+v\n", err) - return rerror.ErrInternalBy(err) + log.Errorfc(ctx, "gcs: move delete err: %+v\n", err) + return rerror.ErrInternalByWithContext(ctx, err) } return nil @@ -265,8 +265,8 @@ func (f *fileRepo) delete(ctx context.Context, filename string) error { bucket, err := f.bucket(ctx) if err != nil { - log.Errorf("gcs: delete bucket err: %+v\n", err) - return rerror.ErrInternalBy(err) + log.Errorfc(ctx, "gcs: delete bucket err: %+v\n", err) + return rerror.ErrInternalByWithContext(ctx, err) } object := bucket.Object(filename) @@ -275,8 +275,8 @@ func (f *fileRepo) delete(ctx context.Context, filename string) error { return nil } - log.Errorf("gcs: delete err: %+v\n", err) - return rerror.ErrInternalBy(err) + log.Errorfc(ctx, "gcs: delete err: %+v\n", err) + return rerror.ErrInternalByWithContext(ctx, err) } return nil } @@ -288,8 +288,8 @@ func (f *fileRepo) deleteAll(ctx context.Context, path string) error { bucket, err := f.bucket(ctx) if err != nil { - log.Errorf("gcs: deleteAll bucket err: %+v\n", err) - return rerror.ErrInternalBy(err) + log.Errorfc(ctx, "gcs: deleteAll bucket err: %+v\n", err) + return rerror.ErrInternalByWithContext(ctx, err) } it := bucket.Objects(ctx, &storage.Query{ @@ -302,12 +302,12 @@ func (f *fileRepo) deleteAll(ctx context.Context, path string) error { break } if err != nil { - log.Errorf("gcs: deleteAll next err: %+v\n", err) - return rerror.ErrInternalBy(err) + log.Errorfc(ctx, "gcs: deleteAll next err: %+v\n", err) + return rerror.ErrInternalByWithContext(ctx, err) } if err := bucket.Object(attrs.Name).Delete(ctx); err != nil { - log.Errorf("gcs: deleteAll err: %+v\n", err) - return rerror.ErrInternalBy(err) + log.Errorfc(ctx, "gcs: deleteAll err: %+v\n", err) + return rerror.ErrInternalByWithContext(ctx, err) } } return nil diff --git a/server/internal/infrastructure/google/fetch.go b/server/internal/infrastructure/google/fetch.go index c0309836ea..3e164b2da4 100644 --- a/server/internal/infrastructure/google/fetch.go +++ b/server/internal/infrastructure/google/fetch.go @@ -22,7 +22,7 @@ func sheetURL(fileId string, sheetName string) string { return gurl.String() } -func fetchCSV(token string, fileId string, sheetName string) (*io.ReadCloser, error) { +func fetchCSV(token string, fileId string, sheetName string) (io.ReadCloser, error) { u := sheetURL(fileId, sheetName) req, err := http.NewRequest("GET", u, nil) if err != nil { @@ -38,5 +38,5 @@ func fetchCSV(token string, fileId string, sheetName string) (*io.ReadCloser, er return nil, fmt.Errorf("StatusCode=%d", res.StatusCode) } - return &res.Body, nil + return res.Body, nil } diff --git a/server/internal/infrastructure/google/google.go b/server/internal/infrastructure/google/google.go index fb1c985471..6a9eb016a5 100644 --- a/server/internal/infrastructure/google/google.go +++ b/server/internal/infrastructure/google/google.go @@ -13,6 +13,6 @@ func NewGoogle() gateway.Google { return &google{} } -func (g google) FetchCSV(token string, fileId string, sheetName string) (*io.ReadCloser, error) { +func (g google) FetchCSV(token string, fileId string, sheetName string) (io.ReadCloser, error) { return fetchCSV(token, fileId, sheetName) } diff --git a/server/internal/infrastructure/marketplace/marketplace.go b/server/internal/infrastructure/marketplace/marketplace.go index 3278264ae8..a8199be2ca 100644 --- a/server/internal/infrastructure/marketplace/marketplace.go +++ b/server/internal/infrastructure/marketplace/marketplace.go @@ -7,8 +7,8 @@ import ( "strings" "github.com/reearth/reearth/server/pkg/id" - "github.com/reearth/reearth/server/pkg/log" "github.com/reearth/reearth/server/pkg/plugin/pluginpack" + "github.com/reearth/reearthx/log" "github.com/reearth/reearthx/rerror" "golang.org/x/oauth2/clientcredentials" ) @@ -38,18 +38,18 @@ func (m *Marketplace) FetchPluginPackage(ctx context.Context, pid id.PluginID) ( return nil, err } - log.Infof("marketplace: downloading plugin package from \"%s\"", url) + log.Infofc(ctx, "marketplace: downloading plugin package from \"%s\"", url) req, err := http.NewRequest("GET", url, nil) if err != nil { - return nil, rerror.ErrInternalBy(err) + return nil, rerror.ErrInternalByWithContext(ctx, err) } if m.secret != "" { req.Header.Set(secretHeader, m.secret) } res, err := m.client(ctx).Do(req) if err != nil { - return nil, rerror.ErrInternalBy(err) + return nil, rerror.ErrInternalByWithContext(ctx, err) } defer func() { @@ -61,10 +61,14 @@ func (m *Marketplace) FetchPluginPackage(ctx context.Context, pid id.PluginID) ( } if res.StatusCode != http.StatusOK { - return nil, rerror.ErrInternalBy(fmt.Errorf("status code is %d", res.StatusCode)) + return nil, rerror.ErrInternalByWithContext(ctx, fmt.Errorf("status code is %d", res.StatusCode)) } - return pluginpack.PackageFromZip(res.Body, nil, pluginPackageSizeLimit) + p, err := pluginpack.PackageFromZip(res.Body, nil, pluginPackageSizeLimit) + if err != nil { + return nil, rerror.ErrInternalByWithContext(ctx, err) + } + return p, nil } func (m *Marketplace) NotifyDownload(ctx context.Context, pid id.PluginID) error { @@ -74,11 +78,11 @@ func (m *Marketplace) NotifyDownload(ctx context.Context, pid id.PluginID) error } url = url + "/download" - log.Infof("marketplace: notify donwload to \"%s\"", url) + log.Infofc(ctx, "marketplace: notify donwload to \"%s\"", url) req, err := http.NewRequest("POST", url, nil) if err != nil { - return rerror.ErrInternalBy(err) + return rerror.ErrInternalByWithContext(ctx, err) } if m.secret != "" { req.Header.Set(secretHeader, m.secret) @@ -86,7 +90,7 @@ func (m *Marketplace) NotifyDownload(ctx context.Context, pid id.PluginID) error res, err := m.client(ctx).Do(req) if err != nil { - return rerror.ErrInternalBy(err) + return rerror.ErrInternalByWithContext(ctx, err) } defer func() { @@ -94,7 +98,7 @@ func (m *Marketplace) NotifyDownload(ctx context.Context, pid id.PluginID) error }() if res.StatusCode != http.StatusOK && res.StatusCode != http.StatusNotFound { - return rerror.ErrInternalBy(fmt.Errorf("status code is %d", res.StatusCode)) + return rerror.ErrInternalByWithContext(ctx, fmt.Errorf("status code is %d", res.StatusCode)) } return nil } diff --git a/server/internal/infrastructure/mongo/asset.go b/server/internal/infrastructure/mongo/asset.go index bd4d3d59c8..43512468b0 100644 --- a/server/internal/infrastructure/mongo/asset.go +++ b/server/internal/infrastructure/mongo/asset.go @@ -30,8 +30,8 @@ func NewAsset(client *mongox.Client) *Asset { return &Asset{client: client.WithCollection("asset")} } -func (r *Asset) Init() error { - return createIndexes(context.Background(), r.client, assetIndexes, assetUniqueIndexes) +func (r *Asset) Init(ctx context.Context) error { + return createIndexes(ctx, r.client, assetIndexes, assetUniqueIndexes) } func (r *Asset) Filtered(f repo.WorkspaceFilter) repo.Asset { @@ -89,7 +89,7 @@ func (r *Asset) TotalSizeByWorkspace(ctx context.Context, wid id.WorkspaceID) (i {"$group": bson.M{"_id": nil, "size": bson.M{"$sum": "$size"}}}, }) if err != nil { - return 0, rerror.ErrInternalBy(err) + return 0, rerror.ErrInternalByWithContext(ctx, err) } defer func() { _ = c.Close(ctx) @@ -104,7 +104,7 @@ func (r *Asset) TotalSizeByWorkspace(ctx context.Context, wid id.WorkspaceID) (i } var res resp if err := c.Decode(&res); err != nil { - return 0, rerror.ErrInternalBy(err) + return 0, rerror.ErrInternalByWithContext(ctx, err) } return res.Size, nil } @@ -134,7 +134,7 @@ func (r *Asset) paginate(ctx context.Context, filter any, sort *asset.SortType, c := mongodoc.NewAssetConsumer() pageInfo, err := r.client.Paginate(ctx, r.readFilter(filter), usort, pagination, c) if err != nil { - return nil, nil, rerror.ErrInternalBy(err) + return nil, nil, rerror.ErrInternalByWithContext(ctx, err) } return c.Result, pageInfo, nil @@ -143,7 +143,7 @@ func (r *Asset) paginate(ctx context.Context, filter any, sort *asset.SortType, func (r *Asset) find(ctx context.Context, filter any) ([]*asset.Asset, error) { c := mongodoc.NewAssetConsumer() if err2 := r.client.Find(ctx, r.readFilter(filter), c); err2 != nil { - return nil, rerror.ErrInternalBy(err2) + return nil, rerror.ErrInternalByWithContext(ctx, err2) } return c.Result, nil } diff --git a/server/internal/infrastructure/mongo/config.go b/server/internal/infrastructure/mongo/config.go index bfad7ff218..9ec64c3906 100644 --- a/server/internal/infrastructure/mongo/config.go +++ b/server/internal/infrastructure/mongo/config.go @@ -32,7 +32,7 @@ func (r *Config) LockAndLoad(ctx context.Context) (cfg *config.Config, err error cfgd := &mongodoc.ConfigDocument{} if err := r.client.FindOne(ctx, bson.M{}).Decode(cfgd); err != nil { if !errors.Is(err, mongo.ErrNilDocument) && !errors.Is(err, mongo.ErrNoDocuments) { - return nil, rerror.ErrInternalBy(err) + return nil, rerror.ErrInternalByWithContext(ctx, err) } } return cfgd.Model(), nil @@ -49,7 +49,7 @@ func (r *Config) Save(ctx context.Context, cfg *config.Config) error { bson.M{"$set": mongodoc.NewConfig(*cfg)}, (&options.UpdateOptions{}).SetUpsert(true), ); err != nil { - return rerror.ErrInternalBy(err) + return rerror.ErrInternalByWithContext(ctx, err) } return nil @@ -68,7 +68,7 @@ func (r *Config) SaveAuth(ctx context.Context, cfg *config.Auth) error { }}, (&options.UpdateOptions{}).SetUpsert(true), ); err != nil { - return rerror.ErrInternalBy(err) + return rerror.ErrInternalByWithContext(ctx, err) } return nil diff --git a/server/internal/infrastructure/mongo/container.go b/server/internal/infrastructure/mongo/container.go index 9dd9ae7606..125498814f 100644 --- a/server/internal/infrastructure/mongo/container.go +++ b/server/internal/infrastructure/mongo/container.go @@ -65,21 +65,22 @@ func Init(r *repo.Container) error { return nil } + ctx := context.Background() return util.Try( - r.Asset.(*Asset).Init, - r.AuthRequest.(*authserver.Mongo).Init, - r.Dataset.(*Dataset).Init, - r.DatasetSchema.(*DatasetSchema).Init, - r.Layer.(*Layer).Init, - r.Plugin.(*Plugin).Init, - r.Policy.(*Policy).Init, - r.Project.(*Project).Init, - r.Property.(*Property).Init, - r.PropertySchema.(*PropertySchema).Init, - r.Scene.(*Scene).Init, - r.Tag.(*Tag).Init, - r.User.(*User).Init, - r.Workspace.(*Workspace).Init, + func() error { return r.Asset.(*Asset).Init(ctx) }, + func() error { return r.AuthRequest.(*authserver.Mongo).Init(ctx) }, + func() error { return r.Dataset.(*Dataset).Init(ctx) }, + func() error { return r.DatasetSchema.(*DatasetSchema).Init(ctx) }, + func() error { return r.Layer.(*Layer).Init(ctx) }, + func() error { return r.Plugin.(*Plugin).Init(ctx) }, + func() error { return r.Policy.(*Policy).Init(ctx) }, + func() error { return r.Project.(*Project).Init(ctx) }, + func() error { return r.Property.(*Property).Init(ctx) }, + func() error { return r.PropertySchema.(*PropertySchema).Init(ctx) }, + func() error { return r.Scene.(*Scene).Init(ctx) }, + func() error { return r.Tag.(*Tag).Init(ctx) }, + func() error { return r.User.(*User).Init(ctx) }, + func() error { return r.Workspace.(*Workspace).Init(ctx) }, ) } @@ -111,7 +112,7 @@ func applyOptionalSceneFilter(filter interface{}, ids scene.IDList) interface{} func createIndexes(ctx context.Context, c *mongox.ClientCollection, keys, uniqueKeys []string) error { created, deleted, err := c.Indexes(ctx, keys, uniqueKeys) if len(created) > 0 || len(deleted) > 0 { - log.Infof("mongo: %s: index deleted: %v, created: %v\n", c.Client().Name(), deleted, created) + log.Infofc(ctx, "mongo: %s: index deleted: %v, created: %v\n", c.Client().Name(), deleted, created) } return err } diff --git a/server/internal/infrastructure/mongo/dataset.go b/server/internal/infrastructure/mongo/dataset.go index df18aee3ba..77d6a68aeb 100644 --- a/server/internal/infrastructure/mongo/dataset.go +++ b/server/internal/infrastructure/mongo/dataset.go @@ -38,8 +38,8 @@ func NewDataset(client *mongox.Client) *Dataset { } } -func (r *Dataset) Init() error { - return createIndexes(context.Background(), r.client, datasetIndexes, datasetUniqueIndexes) +func (r *Dataset) Init(ctx context.Context) error { + return createIndexes(ctx, r.client, datasetIndexes, datasetUniqueIndexes) } func (r *Dataset) Filtered(f repo.SceneFilter) repo.Dataset { @@ -238,7 +238,7 @@ func (r *Dataset) FindGraph(ctx context.Context, did id.DatasetID, fields id.Dat cursor, err2 := r.client.Client().Aggregate(ctx, pipeline) if err2 != nil { - return nil, rerror.ErrInternalBy(err2) + return nil, rerror.ErrInternalByWithContext(ctx, err2) } defer func() { _ = cursor.Close(ctx) @@ -246,7 +246,7 @@ func (r *Dataset) FindGraph(ctx context.Context, did id.DatasetID, fields id.Dat doc := mongodoc.DatasetExtendedDocument{} if err2 := bson.Unmarshal(cursor.Current, &doc); err2 != nil { - return nil, rerror.ErrInternalBy(err2) + return nil, rerror.ErrInternalByWithContext(ctx, err2) } docs := make([]*mongodoc.DatasetExtendedDocument, 0, len(fields)) for i := 0; i < len(fields); i++ { @@ -265,11 +265,11 @@ func (r *Dataset) FindGraph(ctx context.Context, did id.DatasetID, fields id.Dat res := make(dataset.List, 0, len(docs)) for i, d := range docs { if i > 0 && i-1 != d.Depth { - return nil, rerror.ErrInternalBy(errors.New("invalid order")) + return nil, rerror.ErrInternalByWithContext(ctx, errors.New("invalid order")) } ds, err2 := d.DatasetDocument.Model() if err2 != nil { - return nil, rerror.ErrInternalBy(err2) + return nil, rerror.ErrInternalByWithContext(ctx, err2) } res = append(res, ds) } @@ -313,7 +313,7 @@ func (r *Dataset) RemoveByScene(ctx context.Context, sceneID id.SceneID) error { {Key: "scene", Value: sceneID.String()}, }) if err != nil { - return rerror.ErrInternalBy(err) + return rerror.ErrInternalByWithContext(ctx, err) } return nil } @@ -321,7 +321,7 @@ func (r *Dataset) RemoveByScene(ctx context.Context, sceneID id.SceneID) error { func (r *Dataset) find(ctx context.Context, dst dataset.List, filter interface{}) (dataset.List, error) { c := mongodoc.NewDatasetConsumer() if err2 := r.client.Find(ctx, r.readFilter(filter), c); err2 != nil { - return nil, rerror.ErrInternalBy(err2) + return nil, rerror.ErrInternalByWithContext(ctx, err2) } return c.Result, nil } @@ -338,7 +338,7 @@ func (r *Dataset) paginate(ctx context.Context, filter bson.M, pagination *useca c := mongodoc.NewDatasetConsumer() pageInfo, err := r.client.Paginate(ctx, r.readFilter(filter), nil, pagination, c) if err != nil { - return nil, nil, rerror.ErrInternalBy(err) + return nil, nil, rerror.ErrInternalByWithContext(ctx, err) } return c.Result, pageInfo, nil } diff --git a/server/internal/infrastructure/mongo/dataset_schema.go b/server/internal/infrastructure/mongo/dataset_schema.go index 8e8b35eb1a..c31f5c02ef 100644 --- a/server/internal/infrastructure/mongo/dataset_schema.go +++ b/server/internal/infrastructure/mongo/dataset_schema.go @@ -30,8 +30,8 @@ func NewDatasetSchema(client *mongox.Client) *DatasetSchema { } } -func (r *DatasetSchema) Init() error { - return createIndexes(context.Background(), r.client, datasetSchemaIndexes, datasetSchemaUniqueIndexes) +func (r *DatasetSchema) Init(ctx context.Context) error { + return createIndexes(ctx, r.client, datasetSchemaIndexes, datasetSchemaUniqueIndexes) } func (r *DatasetSchema) Filtered(f repo.SceneFilter) repo.DatasetSchema { @@ -155,7 +155,7 @@ func (r *DatasetSchema) RemoveByScene(ctx context.Context, sceneID id.SceneID) e if _, err := r.client.Client().DeleteMany(ctx, bson.M{ "scene": sceneID.String(), }); err != nil { - return rerror.ErrInternalBy(err) + return rerror.ErrInternalByWithContext(ctx, err) } return nil } @@ -180,7 +180,7 @@ func (r *DatasetSchema) paginate(ctx context.Context, filter bson.M, pagination c := mongodoc.NewDatasetSchemaConsumer() pageInfo, err := r.client.Paginate(ctx, r.readFilter(filter), nil, pagination, c) if err != nil { - return nil, nil, rerror.ErrInternalBy(err) + return nil, nil, rerror.ErrInternalByWithContext(ctx, err) } return c.Result, pageInfo, nil } diff --git a/server/internal/infrastructure/mongo/layer.go b/server/internal/infrastructure/mongo/layer.go index 42ab6fae36..25cd2a913d 100644 --- a/server/internal/infrastructure/mongo/layer.go +++ b/server/internal/infrastructure/mongo/layer.go @@ -46,8 +46,8 @@ func NewLayer(client *mongox.Client) *Layer { } } -func (r *Layer) Init() error { - return createIndexes(context.Background(), r.client, layerIndexes, layerUniqueIndexes) +func (r *Layer) Init(ctx context.Context) error { + return createIndexes(ctx, r.client, layerIndexes, layerUniqueIndexes) } func (r *Layer) Filtered(f repo.SceneFilter) repo.Layer { @@ -263,7 +263,7 @@ func (r *Layer) RemoveByScene(ctx context.Context, sceneID id.SceneID) error { } _, err := r.client.Client().DeleteMany(ctx, filter) if err != nil { - return rerror.ErrInternalBy(err) + return rerror.ErrInternalByWithContext(ctx, err) } return nil } diff --git a/server/internal/infrastructure/mongo/lock.go b/server/internal/infrastructure/mongo/lock.go index 29a27d8df5..0034119235 100644 --- a/server/internal/infrastructure/mongo/lock.go +++ b/server/internal/infrastructure/mongo/lock.go @@ -40,18 +40,18 @@ func (r *Lock) Lock(ctx context.Context, name string) error { } lockID := uuid.NewString() - log.Infof("lock: trying to lock: id=%s, name=%s, host=%s", name, lockID, r.hostid) + log.Infofc(ctx, "lock: trying to lock: id=%s, name=%s, host=%s", name, lockID, r.hostid) if err := retry.Do( func() error { return r.l.XLock(ctx, name, lockID, r.details()) }, retry.RetryIf(func(err error) bool { return errors.Is(err, lock.ErrAlreadyLocked) }), ); err != nil { - log.Infof("lock: failed to lock: name=%s, id=%s, host=%s, err=%s", name, lockID, r.hostid, err) + log.Infofc(ctx, "lock: failed to lock: name=%s, id=%s, host=%s, err=%s", name, lockID, r.hostid, err) return repo.ErrFailedToLock } r.setLockID(name, lockID) - log.Infof("lock: locked: name=%s, id=%s, host=%s", name, lockID, r.hostid) + log.Infofc(ctx, "lock: locked: name=%s, id=%s, host=%s", name, lockID, r.hostid) return nil } @@ -62,11 +62,11 @@ func (r *Lock) Unlock(ctx context.Context, name string) error { } if _, err := r.l.Unlock(ctx, lockID); err != nil { - return rerror.ErrInternalBy(err) + return rerror.ErrInternalByWithContext(ctx, err) } r.deleteLockID(name) - log.Infof("lock: unlocked: name=%s, id=%s, host=%s", name, lockID, r.hostid) + log.Infofc(ctx, "lock: unlocked: name=%s, id=%s, host=%s", name, lockID, r.hostid) return nil } diff --git a/server/internal/infrastructure/mongo/migration/201217132559_add_scene_widget_id.go b/server/internal/infrastructure/mongo/migration/201217132559_add_scene_widget_id.go index bcd2e94bba..f6b17b9db4 100644 --- a/server/internal/infrastructure/mongo/migration/201217132559_add_scene_widget_id.go +++ b/server/internal/infrastructure/mongo/migration/201217132559_add_scene_widget_id.go @@ -3,9 +3,9 @@ package migration import ( "context" - "github.com/labstack/gommon/log" "github.com/reearth/reearth/server/internal/infrastructure/mongo/mongodoc" "github.com/reearth/reearth/server/pkg/id" + "github.com/reearth/reearthx/log" "github.com/reearth/reearthx/mongox" "go.mongodb.org/mongo-driver/bson" ) @@ -20,7 +20,7 @@ func AddSceneWidgetId(ctx context.Context, c DBClient) error { ids := make([]string, 0, len(rows)) newRows := make([]interface{}, 0, len(rows)) - log.Infof("migration: AddSceneWidgetId: hit scenes: %d\n", len(rows)) + log.Infofc(ctx, "migration: AddSceneWidgetId: hit scenes: %d\n", len(rows)) for _, row := range rows { var doc mongodoc.SceneDocument diff --git a/server/internal/infrastructure/mongo/migration/201217193948_add_scene_default_tile.go b/server/internal/infrastructure/mongo/migration/201217193948_add_scene_default_tile.go index 81274a36b2..4fbf7598b2 100644 --- a/server/internal/infrastructure/mongo/migration/201217193948_add_scene_default_tile.go +++ b/server/internal/infrastructure/mongo/migration/201217193948_add_scene_default_tile.go @@ -44,12 +44,12 @@ func AddSceneDefaultTile(ctx context.Context, c DBClient) error { }, } - log.Infof("migration: AddSceneDefaultTile: filter: %+v\n", filter) + log.Infofc(ctx, "migration: AddSceneDefaultTile: filter: %+v\n", filter) return col.Find(ctx, filter, &mongodoc.PropertyBatchConsumer{ Size: 1000, Callback: func(properties []*property.Property) error { - log.Infof("migration: AddSceneDefaultTile: hit properties: %d\n", len(properties)) + log.Infofc(ctx, "migration: AddSceneDefaultTile: hit properties: %d\n", len(properties)) for _, p := range properties { g := p.GetOrCreateGroupList(scenePropertySchema, property.PointItemBySchema(id.PropertySchemaGroupID("tiles"))) diff --git a/server/internal/infrastructure/mongo/migration/210310145844_remove_preview_token.go b/server/internal/infrastructure/mongo/migration/210310145844_remove_preview_token.go index ecc4fb298f..f72e33f4c2 100644 --- a/server/internal/infrastructure/mongo/migration/210310145844_remove_preview_token.go +++ b/server/internal/infrastructure/mongo/migration/210310145844_remove_preview_token.go @@ -3,7 +3,7 @@ package migration import ( "context" - "github.com/labstack/gommon/log" + "github.com/reearth/reearthx/log" "github.com/reearth/reearthx/mongox" "go.mongodb.org/mongo-driver/bson" ) @@ -18,7 +18,7 @@ func RemovePreviewToken(ctx context.Context, c DBClient) error { ids := make([]string, 0, len(rows)) newRows := make([]interface{}, 0, len(rows)) - log.Infof("migration: RemoveProjectPreviewToken: hit projects: %d\n", len(rows)) + log.Infofc(ctx, "migration: RemoveProjectPreviewToken: hit projects: %d\n", len(rows)) for _, row := range rows { doc := bson.M{} diff --git a/server/internal/infrastructure/mongo/migration/210730175108_add_scene_align_system.go b/server/internal/infrastructure/mongo/migration/210730175108_add_scene_align_system.go index a98bd08b27..1752f4ab8b 100644 --- a/server/internal/infrastructure/mongo/migration/210730175108_add_scene_align_system.go +++ b/server/internal/infrastructure/mongo/migration/210730175108_add_scene_align_system.go @@ -3,12 +3,12 @@ package migration import ( "context" - "github.com/labstack/gommon/log" "github.com/reearth/reearth/server/internal/infrastructure/mongo/mongodoc" "github.com/reearth/reearth/server/pkg/builtin" "github.com/reearth/reearth/server/pkg/id" "github.com/reearth/reearth/server/pkg/plugin" "github.com/reearth/reearth/server/pkg/scene" + "github.com/reearth/reearthx/log" "github.com/reearth/reearthx/mongox" "go.mongodb.org/mongo-driver/bson" ) @@ -22,7 +22,7 @@ func AddSceneAlignSystem(ctx context.Context, c DBClient) error { ids := make([]string, 0, len(rows)) newRows := make([]interface{}, 0, len(rows)) - log.Infof("migration: AddSceneAlignSystem: hit scenes: %d\n", len(rows)) + log.Infofc(ctx, "migration: AddSceneAlignSystem: hit scenes: %d\n", len(rows)) for _, row := range rows { var doc mongodoc.SceneDocument diff --git a/server/internal/infrastructure/mongo/migration/220214180713_split_schema_of_properties.go b/server/internal/infrastructure/mongo/migration/220214180713_split_schema_of_properties.go index 8b7bbde8b6..93aacd5c49 100644 --- a/server/internal/infrastructure/mongo/migration/220214180713_split_schema_of_properties.go +++ b/server/internal/infrastructure/mongo/migration/220214180713_split_schema_of_properties.go @@ -3,9 +3,9 @@ package migration import ( "context" - "github.com/labstack/gommon/log" "github.com/reearth/reearth/server/internal/infrastructure/mongo/mongodoc" "github.com/reearth/reearth/server/pkg/id" + "github.com/reearth/reearthx/log" "github.com/reearth/reearthx/mongox" "go.mongodb.org/mongo-driver/bson" ) @@ -21,7 +21,7 @@ func SplitSchemaOfProperties(ctx context.Context, c DBClient) error { ids := make([]string, 0, len(rows)) newRows := make([]interface{}, 0, len(rows)) - log.Infof("migration: SplitSchemaOfProperties: hit properties: %d\n", len(rows)) + log.Infofc(ctx, "migration: SplitSchemaOfProperties: hit properties: %d\n", len(rows)) for _, row := range rows { var doc mongodoc.PropertyDocument diff --git a/server/internal/infrastructure/mongo/migration/220309174648_add_scene_field_to_property_schema.go b/server/internal/infrastructure/mongo/migration/220309174648_add_scene_field_to_property_schema.go index cded96a344..19d2b8ac4f 100644 --- a/server/internal/infrastructure/mongo/migration/220309174648_add_scene_field_to_property_schema.go +++ b/server/internal/infrastructure/mongo/migration/220309174648_add_scene_field_to_property_schema.go @@ -3,9 +3,9 @@ package migration import ( "context" - "github.com/labstack/gommon/log" "github.com/reearth/reearth/server/internal/infrastructure/mongo/mongodoc" "github.com/reearth/reearth/server/pkg/id" + "github.com/reearth/reearthx/log" "github.com/reearth/reearthx/mongox" "go.mongodb.org/mongo-driver/bson" ) @@ -19,7 +19,7 @@ func AddSceneFieldToPropertySchema(ctx context.Context, c DBClient) error { ids := make([]string, 0, len(rows)) newRows := make([]interface{}, 0, len(rows)) - log.Infof("migration: AddSceneFieldToPropertySchema: hit property schemas: %d\n", len(rows)) + log.Infofc(ctx, "migration: AddSceneFieldToPropertySchema: hit property schemas: %d\n", len(rows)) for _, row := range rows { var doc mongodoc.PropertySchemaDocument diff --git a/server/internal/infrastructure/mongo/migration/221028204300_move_terrain_properties.go b/server/internal/infrastructure/mongo/migration/221028204300_move_terrain_properties.go index 728ed71bc2..d14632083b 100644 --- a/server/internal/infrastructure/mongo/migration/221028204300_move_terrain_properties.go +++ b/server/internal/infrastructure/mongo/migration/221028204300_move_terrain_properties.go @@ -3,9 +3,9 @@ package migration import ( "context" - "github.com/labstack/gommon/log" "github.com/reearth/reearth/server/internal/infrastructure/mongo/mongodoc" "github.com/reearth/reearth/server/pkg/property" + "github.com/reearth/reearthx/log" "github.com/reearth/reearthx/mongox" "github.com/samber/lo" "go.mongodb.org/mongo-driver/bson" @@ -52,7 +52,7 @@ func MoveTerrainProperties(ctx context.Context, c DBClient) error { ids := make([]string, 0, len(rows)) newRows := make([]interface{}, 0, len(rows)) - log.Infof("migration: MoveTerrainProperties: hit properties: %d\n", len(rows)) + log.Infofc(ctx, "migration: MoveTerrainProperties: hit properties: %d\n", len(rows)) for _, row := range rows { var doc mongodoc.PropertyDocument diff --git a/server/internal/infrastructure/mongo/plugin.go b/server/internal/infrastructure/mongo/plugin.go index c4e0dd7257..b1ca64bae1 100644 --- a/server/internal/infrastructure/mongo/plugin.go +++ b/server/internal/infrastructure/mongo/plugin.go @@ -11,7 +11,6 @@ import ( "github.com/reearth/reearth/server/pkg/builtin" "github.com/reearth/reearth/server/pkg/id" "github.com/reearth/reearth/server/pkg/plugin" - "github.com/reearth/reearthx/log" "github.com/reearth/reearthx/mongox" "github.com/reearth/reearthx/rerror" ) @@ -32,8 +31,8 @@ func NewPlugin(client *mongox.Client) *Plugin { } } -func (r *Plugin) Init() error { - return createIndexes(context.Background(), r.client, pluginIndexes, pluginUniqueIndexes) +func (r *Plugin) Init(ctx context.Context) error { + return createIndexes(ctx, r.client, pluginIndexes, pluginUniqueIndexes) } func (r *Plugin) Filtered(f repo.SceneFilter) repo.Plugin { @@ -77,16 +76,12 @@ func (r *Plugin) FindByIDs(ctx context.Context, ids []id.PluginID) ([]*plugin.Pl var err error if len(ids2) > 0 { - log.Infof("TEMP: plugin mongo find by ids %v", ids2) - res, err = r.find(ctx, bson.M{ "id": bson.M{"$in": id.PluginIDsToStrings(ids2)}, }) if err != nil { return nil, err } - - log.Infof("TEMP: plugin mongo find by ids OK") } return res.Concat(b.List()).MapToIDs(ids), nil diff --git a/server/internal/infrastructure/mongo/policy.go b/server/internal/infrastructure/mongo/policy.go index f2813200bf..8a5745127c 100644 --- a/server/internal/infrastructure/mongo/policy.go +++ b/server/internal/infrastructure/mongo/policy.go @@ -25,8 +25,8 @@ func NewPolicy(c *mongox.Client) *Policy { } } -func (r *Policy) Init() error { - return createIndexes(context.Background(), r.client, policyIndexes, policyUniqueIndexes) +func (r *Policy) Init(ctx context.Context) error { + return createIndexes(ctx, r.client, policyIndexes, policyUniqueIndexes) } func (r *Policy) FindByID(ctx context.Context, id workspace.PolicyID) (*workspace.Policy, error) { diff --git a/server/internal/infrastructure/mongo/project.go b/server/internal/infrastructure/mongo/project.go index 24aacdcea9..28256f82e9 100644 --- a/server/internal/infrastructure/mongo/project.go +++ b/server/internal/infrastructure/mongo/project.go @@ -2,7 +2,6 @@ package mongo import ( "context" - "errors" "go.mongodb.org/mongo-driver/bson" @@ -10,7 +9,6 @@ import ( "github.com/reearth/reearth/server/internal/usecase/repo" "github.com/reearth/reearth/server/pkg/id" "github.com/reearth/reearth/server/pkg/project" - "github.com/reearth/reearthx/log" "github.com/reearth/reearthx/mongox" "github.com/reearth/reearthx/rerror" "github.com/reearth/reearthx/usecasex" @@ -32,8 +30,8 @@ func NewProject(client *mongox.Client) *Project { } } -func (r *Project) Init() error { - return createIndexes(context.Background(), r.client, projectIndexes, projectUniqueIndexes) +func (r *Project) Init(ctx context.Context) error { + return createIndexes(ctx, r.client, projectIndexes, projectUniqueIndexes) } func (r *Project) Filtered(f repo.WorkspaceFilter) repo.Project { @@ -90,11 +88,7 @@ func (r *Project) FindByPublicName(ctx context.Context, name string) (*project.P }, } - res, err := r.findOneWithoutReadFilter(ctx, f) - if errors.Is(err, rerror.ErrNotFound) { - log.Infof("mongo: project.FindByPublicName: name=%s err=%v filter=%v q=%#v", name, err, r.f.Readable.Strings(), f) - } - return res, err + return r.findOneWithoutReadFilter(ctx, f) } func (r *Project) CountByWorkspace(ctx context.Context, ws id.WorkspaceID) (int, error) { @@ -158,7 +152,7 @@ func (r *Project) paginate(ctx context.Context, filter bson.M, pagination *useca c := mongodoc.NewProjectConsumer() pageInfo, err := r.client.Paginate(ctx, r.readFilter(filter), nil, pagination, c) if err != nil { - return nil, nil, rerror.ErrInternalBy(err) + return nil, nil, rerror.ErrInternalByWithContext(ctx, err) } return c.Result, pageInfo, nil } diff --git a/server/internal/infrastructure/mongo/property.go b/server/internal/infrastructure/mongo/property.go index f4028cc16d..6e9265dbb6 100644 --- a/server/internal/infrastructure/mongo/property.go +++ b/server/internal/infrastructure/mongo/property.go @@ -7,7 +7,6 @@ import ( "github.com/reearth/reearth/server/internal/usecase/repo" "github.com/reearth/reearth/server/pkg/id" "github.com/reearth/reearth/server/pkg/property" - "github.com/reearth/reearthx/log" "github.com/reearth/reearthx/mongox" "github.com/reearth/reearthx/rerror" "go.mongodb.org/mongo-driver/bson" @@ -47,8 +46,8 @@ func NewProperty(client *mongox.Client) *Property { } } -func (r *Property) Init() error { - return createIndexes(context.Background(), r.client, propertyIndexes, propertyUniqueIndexes) +func (r *Property) Init(ctx context.Context) error { + return createIndexes(ctx, r.client, propertyIndexes, propertyUniqueIndexes) } func (r *Property) Filtered(f repo.SceneFilter) repo.Property { @@ -69,8 +68,6 @@ func (r *Property) FindByIDs(ctx context.Context, ids id.PropertyIDList) (proper return nil, nil } - log.Infof("TEMP: property mongo find by ids %v", ids) - filter := bson.M{ "id": bson.M{ "$in": ids.Strings(), @@ -81,8 +78,6 @@ func (r *Property) FindByIDs(ctx context.Context, ids id.PropertyIDList) (proper return nil, err } - log.Infof("TEMP: property mongo find by ids OK") - return filterProperties(ids, res), nil } @@ -194,7 +189,7 @@ func (r *Property) RemoveByScene(ctx context.Context, sceneID id.SceneID) error "scene": sceneID.String(), }) if err != nil { - return rerror.ErrInternalBy(err) + return rerror.ErrInternalByWithContext(ctx, err) } return nil } diff --git a/server/internal/infrastructure/mongo/property_schema.go b/server/internal/infrastructure/mongo/property_schema.go index fdcd544cd5..d4ccbeeebe 100644 --- a/server/internal/infrastructure/mongo/property_schema.go +++ b/server/internal/infrastructure/mongo/property_schema.go @@ -29,8 +29,8 @@ func NewPropertySchema(client *mongox.Client) *PropertySchema { } } -func (r *PropertySchema) Init() error { - return createIndexes(context.Background(), r.client, propertySchemaIndexes, propertySchemaUniqueIndexes) +func (r *PropertySchema) Init(ctx context.Context) error { + return createIndexes(ctx, r.client, propertySchemaIndexes, propertySchemaUniqueIndexes) } func (r *PropertySchema) Filtered(f repo.SceneFilter) repo.PropertySchema { diff --git a/server/internal/infrastructure/mongo/scene.go b/server/internal/infrastructure/mongo/scene.go index 39e6b7f994..837091018b 100644 --- a/server/internal/infrastructure/mongo/scene.go +++ b/server/internal/infrastructure/mongo/scene.go @@ -29,8 +29,8 @@ func NewScene(client *mongox.Client) *Scene { } } -func (r *Scene) Init() error { - return createIndexes(context.Background(), r.client, sceneIndexes, sceneUniqueIndexes) +func (r *Scene) Init(ctx context.Context) error { + return createIndexes(ctx, r.client, sceneIndexes, sceneUniqueIndexes) } func (r *Scene) Filtered(f repo.WorkspaceFilter) repo.Scene { diff --git a/server/internal/infrastructure/mongo/scene_lock.go b/server/internal/infrastructure/mongo/scene_lock.go index c176638cc2..694f523691 100644 --- a/server/internal/infrastructure/mongo/scene_lock.go +++ b/server/internal/infrastructure/mongo/scene_lock.go @@ -60,7 +60,7 @@ func (r *SceneLock) SaveLock(ctx context.Context, sceneID id.SceneID, lock scene }, &options.UpdateOptions{ Upsert: &upsert, }); err2 != nil { - return rerror.ErrInternalBy(err2) + return rerror.ErrInternalByWithContext(ctx, err2) } return nil } @@ -68,7 +68,7 @@ func (r *SceneLock) SaveLock(ctx context.Context, sceneID id.SceneID, lock scene func (r *SceneLock) ReleaseAllLock(ctx context.Context) error { if _, err2 := r.client.Client().DeleteMany(ctx, bson.D{}); err2 != nil { if err2 != mongo.ErrNilDocument && err2 != mongo.ErrNoDocuments { - return rerror.ErrInternalBy(err2) + return rerror.ErrInternalByWithContext(ctx, err2) } } return nil diff --git a/server/internal/infrastructure/mongo/tag.go b/server/internal/infrastructure/mongo/tag.go index 6508edd6a5..271420525c 100644 --- a/server/internal/infrastructure/mongo/tag.go +++ b/server/internal/infrastructure/mongo/tag.go @@ -29,8 +29,8 @@ func NewTag(client *mongox.Client) *Tag { } } -func (r *Tag) Init() error { - return createIndexes(context.Background(), r.client, tagIndexes, tagUniqueIndexes) +func (r *Tag) Init(ctx context.Context) error { + return createIndexes(ctx, r.client, tagIndexes, tagUniqueIndexes) } func (r *Tag) Filtered(f repo.SceneFilter) repo.Tag { @@ -160,7 +160,7 @@ func (r *Tag) RemoveByScene(ctx context.Context, sceneID id.SceneID) error { "scene": sceneID.String(), }) if err != nil { - return rerror.ErrInternalBy(err) + return rerror.ErrInternalByWithContext(ctx, err) } return nil } diff --git a/server/internal/infrastructure/mongo/user.go b/server/internal/infrastructure/mongo/user.go index 86ddcaf750..8e109e64f2 100644 --- a/server/internal/infrastructure/mongo/user.go +++ b/server/internal/infrastructure/mongo/user.go @@ -26,8 +26,8 @@ func NewUser(client *mongox.Client) *User { } } -func (r *User) Init() error { - return createIndexes(context.Background(), r.client, userIndexes, userUniqueIndexes) +func (r *User) Init(ctx context.Context) error { + return createIndexes(ctx, r.client, userIndexes, userUniqueIndexes) } func (r *User) FindByIDs(ctx context.Context, ids id.UserIDList) ([]*user.User, error) { diff --git a/server/internal/infrastructure/mongo/workspace.go b/server/internal/infrastructure/mongo/workspace.go index ff505fef6d..87e00ea221 100644 --- a/server/internal/infrastructure/mongo/workspace.go +++ b/server/internal/infrastructure/mongo/workspace.go @@ -27,8 +27,8 @@ func NewWorkspace(client *mongox.Client) repo.Workspace { } } -func (r *Workspace) Init() error { - return createIndexes(context.Background(), r.client, workspaceIndexes, workspaceUniqueIndexes) +func (r *Workspace) Init(ctx context.Context) error { + return createIndexes(ctx, r.client, workspaceIndexes, workspaceUniqueIndexes) } func (r *Workspace) FindByUser(ctx context.Context, id id.UserID) (workspace.List, error) { diff --git a/server/internal/infrastructure/s3/s3.go b/server/internal/infrastructure/s3/s3.go index 4a98079dc0..673aeeb8e3 100644 --- a/server/internal/infrastructure/s3/s3.go +++ b/server/internal/infrastructure/s3/s3.go @@ -99,7 +99,7 @@ func (f *fileRepo) UploadAsset(ctx context.Context, file *file.File) (*url.URL, } func (f *fileRepo) RemoveAsset(ctx context.Context, u *url.URL) error { - log.Infof("s3: asset deleted: %s", u) + log.Infofc(ctx, "s3: asset deleted: %s", u) sn := getObjectNameFromURL(f.base, u) if sn == "" { @@ -128,7 +128,7 @@ func (f *fileRepo) UploadPluginFile(ctx context.Context, pid id.PluginID, file * } func (f *fileRepo) RemovePlugin(ctx context.Context, pid id.PluginID) error { - log.Infof("s3: plugin deleted: %s", pid) + log.Infofc(ctx, "s3: plugin deleted: %s", pid) return f.deleteAll(ctx, path.Join(pluginBasePath, pid.String())) } @@ -161,7 +161,7 @@ func (f *fileRepo) MoveBuiltScene(ctx context.Context, oldName, name string) err } func (f *fileRepo) RemoveBuiltScene(ctx context.Context, name string) error { - log.Infof("s3: built scene deleted: %s", name) + log.Infofc(ctx, "s3: built scene deleted: %s", name) sn := sanitize.Path(name + ".json") if sn == "" { @@ -182,8 +182,7 @@ func (f *fileRepo) read(ctx context.Context, filename string) (io.ReadCloser, er Key: aws.String(filename), }) if err != nil { - log.Errorf("s3: read err: %+v\n", err) - return nil, rerror.ErrInternalBy(err) + return nil, rerror.ErrInternalByWithContext(ctx, fmt.Errorf("s3: read err: %+v", err)) } return obj.Body, nil @@ -199,7 +198,7 @@ func (f *fileRepo) upload(ctx context.Context, filename string, content io.Reade ba, err := io.ReadAll(content) if err != nil { - return 0, rerror.ErrInternalBy(err) + return 0, rerror.ErrInternalByWithContext(ctx, err) } body := bytes.NewReader(ba) @@ -211,7 +210,7 @@ func (f *fileRepo) upload(ctx context.Context, filename string, content io.Reade ContentLength: body.Size(), }) if err != nil { - log.Errorf("s3: upload err: %+v\n", err) + log.Errorfc(ctx, "s3: upload err: %v", err) return 0, gateway.ErrFailedToUploadFile } @@ -220,7 +219,7 @@ func (f *fileRepo) upload(ctx context.Context, filename string, content io.Reade Key: aws.String(filename), }) if err != nil { - log.Errorf("s3: check file size after upload err: %+v\n", err) + log.Errorfc(ctx, "s3: check file size after upload err: %v", err) return 0, gateway.ErrFailedToUploadFile } @@ -239,8 +238,8 @@ func (f *fileRepo) copy(ctx context.Context, from, dest string) error { Key: aws.String(dest), }) if err != nil { - log.Errorf("s3: copy err: %+v\n", err) - return rerror.ErrInternalBy(err) + log.Errorfc(ctx, "s3: copy err: %+v\n", err) + return rerror.ErrInternalByWithContext(ctx, err) } return nil @@ -274,8 +273,8 @@ func (f *fileRepo) delete(ctx context.Context, filename string) error { Key: aws.String(filename), }) if err != nil { - log.Errorf("s3: delete err: %+v\n", err) - return rerror.ErrInternalBy(err) + log.Errorfc(ctx, "s3: delete err: %v", err) + return rerror.ErrInternalByWithContext(ctx, err) } return nil @@ -291,8 +290,8 @@ func (f *fileRepo) deleteAll(ctx context.Context, path string) error { Prefix: aws.String(path), }) if err != nil { - log.Errorf("s3: Unable to list object for delete %v", err) - return rerror.ErrInternalBy(err) + log.Errorfc(ctx, "s3: unable to list object for delete %v", err) + return rerror.ErrInternalByWithContext(ctx, err) } keys := lo.Map(l.Contents, func(obj types.Object, _ int) types.ObjectIdentifier { @@ -305,8 +304,8 @@ func (f *fileRepo) deleteAll(ctx context.Context, path string) error { }, }) if err != nil { - log.Errorf("s3: Unable to delete object %v", err) - return rerror.ErrInternalBy(err) + log.Errorfc(ctx, "s3: unable to delete object: %v", err) + return rerror.ErrInternalByWithContext(ctx, err) } return nil diff --git a/server/internal/usecase/gateway/authenticator.go b/server/internal/usecase/gateway/authenticator.go index 86a30b6a76..7a2c6ad0b8 100644 --- a/server/internal/usecase/gateway/authenticator.go +++ b/server/internal/usecase/gateway/authenticator.go @@ -1,5 +1,7 @@ package gateway +import "context" + type AuthenticatorUpdateUserParam struct { ID string Name *string @@ -15,5 +17,5 @@ type AuthenticatorUser struct { } type Authenticator interface { - UpdateUser(AuthenticatorUpdateUserParam) (AuthenticatorUser, error) + UpdateUser(context.Context, AuthenticatorUpdateUserParam) (AuthenticatorUser, error) } diff --git a/server/internal/usecase/gateway/google.go b/server/internal/usecase/gateway/google.go index 26655fbabb..92eba62a44 100644 --- a/server/internal/usecase/gateway/google.go +++ b/server/internal/usecase/gateway/google.go @@ -5,5 +5,5 @@ import ( ) type Google interface { - FetchCSV(token string, fileId string, sheetName string) (*io.ReadCloser, error) + FetchCSV(token string, fileId string, sheetName string) (io.ReadCloser, error) } diff --git a/server/internal/usecase/interactor/dataset.go b/server/internal/usecase/interactor/dataset.go index cfe4658385..14af06c30a 100644 --- a/server/internal/usecase/interactor/dataset.go +++ b/server/internal/usecase/interactor/dataset.go @@ -137,13 +137,13 @@ func (i *Dataset) ImportDatasetFromGoogleSheet(ctx context.Context, inp interfac } defer func() { - err = (*csvFile).Close() + err = csvFile.Close() if err != nil { - log.Fatal(err) + log.Errorfc(ctx, "failed to close: %v", err) } }() - return i.importDataset(ctx, *csvFile, inp.SheetName, ',', inp.SceneId, inp.SchemaId, operator) + return i.importDataset(ctx, csvFile, inp.SheetName, ',', inp.SceneId, inp.SchemaId, operator) } func (i *Dataset) importDataset(ctx context.Context, content io.Reader, name string, separator rune, sceneId id.SceneID, schemaId *id.DatasetSchemaID, o *usecase.Operator) (_ *dataset.Schema, err error) { @@ -392,7 +392,7 @@ func (i *Dataset) GraphFetch(ctx context.Context, id id.DatasetID, depth int, _ res = append(res, d) next, done = it.Next(d) if next.IsNil() { - return nil, rerror.ErrInternalBy(errors.New("next id is nil")) + return nil, rerror.ErrInternalByWithContext(ctx, errors.New("next id is nil")) } if done { break @@ -422,7 +422,7 @@ func (i *Dataset) GraphFetchSchema(ctx context.Context, id id.DatasetSchemaID, d res = append(res, d) next, done = it.Next(d) if next.IsNil() { - return nil, rerror.ErrInternalBy(errors.New("next id is nil")) + return nil, rerror.ErrInternalByWithContext(ctx, errors.New("next id is nil")) } if done { break diff --git a/server/internal/usecase/interactor/plugin_common.go b/server/internal/usecase/interactor/plugin_common.go index 52beffaaf5..9f09dfb146 100644 --- a/server/internal/usecase/interactor/plugin_common.go +++ b/server/internal/usecase/interactor/plugin_common.go @@ -28,7 +28,7 @@ func (i *pluginCommon) SavePluginPack(ctx context.Context, p *pluginpack.Package for { f, err := p.Files.Next() if err != nil { - log.Errorf("failed to read a plugin file (%s): %s", id, err) + log.Errorfc(ctx, "failed to read a plugin file (%s): %s", id, err) return interfaces.ErrInvalidPluginPackage } @@ -38,12 +38,12 @@ func (i *pluginCommon) SavePluginPack(ctx context.Context, p *pluginpack.Package read = true if err := i.file.UploadPluginFile(ctx, p.Manifest.Plugin.ID(), f); err != nil { - return rerror.ErrInternalBy(err) + return rerror.ErrInternalByWithContext(ctx, err) } } if !read { - log.Errorf("no plugin files (%s)", id) + log.Errorfc(ctx, "no plugin files (%s)", id) return interfaces.ErrInvalidPluginPackage } diff --git a/server/internal/usecase/interactor/plugin_upload.go b/server/internal/usecase/interactor/plugin_upload.go index 67ab6dd788..688ba17393 100644 --- a/server/internal/usecase/interactor/plugin_upload.go +++ b/server/internal/usecase/interactor/plugin_upload.go @@ -66,7 +66,11 @@ func (i *Plugin) UploadFromRemote(ctx context.Context, u *url.URL, sid id.SceneI p, err := pluginpack.PackageFromZip(res.Body, &sid, pluginPackageSizeLimit) if err != nil { _ = res.Body.Close() - return nil, nil, interfaces.ErrInvalidPluginPackage + return nil, nil, &rerror.Error{ + Label: interfaces.ErrInvalidPluginPackage, + Err: err, + Separate: true, + } } return i.upload(ctx, p, sid, operator) diff --git a/server/internal/usecase/interactor/published.go b/server/internal/usecase/interactor/published.go index f05558a737..d5008eff15 100644 --- a/server/internal/usecase/interactor/published.go +++ b/server/internal/usecase/interactor/published.go @@ -47,19 +47,19 @@ func NewPublishedWithURL(project repo.Project, file gateway.File, indexHTMLURL * } res, err := http.DefaultClient.Do(req) if err != nil { - log.Errorf("published index: conn err: %s", err) + log.Errorfc(c, "published index: conn err: %s", err) return "", errors.New("failed to fetch HTML") } defer func() { _ = res.Body.Close() }() if res.StatusCode >= 300 { - log.Errorf("published index: status err: %d", res.StatusCode) + log.Errorfc(c, "published index: status err: %d", res.StatusCode) return "", errors.New("failed to fetch HTML") } str, err := io.ReadAll(res.Body) if err != nil { - log.Errorf("published index: read err: %s", err) + log.Errorfc(c, "published index: read err: %s", err) return "", errors.New("failed to fetch HTML") } return string(str), nil diff --git a/server/internal/usecase/interactor/user.go b/server/internal/usecase/interactor/user.go index f5a337e5d8..f7f3decd7c 100644 --- a/server/internal/usecase/interactor/user.go +++ b/server/internal/usecase/interactor/user.go @@ -195,7 +195,7 @@ func (i *User) StartPasswordReset(ctx context.Context, email string) error { return err } - err = i.mailer.SendMail([]mailer.Contact{ + err = i.mailer.SendMail(ctx, []mailer.Contact{ { Email: u.Email(), Name: u.Name(), @@ -330,7 +330,7 @@ func (i *User) UpdateMe(ctx context.Context, p interfaces.UpdateMeParam, operato if a.Provider != "auth0" { continue } - if _, err := i.authenticator.UpdateUser(gateway.AuthenticatorUpdateUserParam{ + if _, err := i.authenticator.UpdateUser(ctx, gateway.AuthenticatorUpdateUserParam{ ID: a.Sub, Name: p.Name, Email: p.Email, diff --git a/server/internal/usecase/interactor/user_signup.go b/server/internal/usecase/interactor/user_signup.go index 9397ef1646..2630a6de91 100644 --- a/server/internal/usecase/interactor/user_signup.go +++ b/server/internal/usecase/interactor/user_signup.go @@ -377,6 +377,7 @@ func (i *User) createVerification(ctx context.Context, u *user.User) error { } if err := i.mailer.SendMail( + ctx, []mailer.Contact{ { Email: u.Email(), diff --git a/server/pkg/layer/decoding/shp_test.go b/server/pkg/layer/decoding/shp_test.go index 9b26ba247e..6e3a6b7298 100644 --- a/server/pkg/layer/decoding/shp_test.go +++ b/server/pkg/layer/decoding/shp_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/reearth/reearth/server/pkg/shp" + "github.com/samber/lo" "github.com/stretchr/testify/assert" ) @@ -61,6 +62,7 @@ var dataForReadTests = map[string]testCaseData{ } func testPoint(t *testing.T, points [][]float64, shapes []shp.Shape) { + t.Helper() for n, s := range shapes { p, ok := s.(*shp.Point) if !ok { @@ -71,6 +73,7 @@ func testPoint(t *testing.T, points [][]float64, shapes []shp.Shape) { } func testPolyLine(t *testing.T, points [][]float64, shapes []shp.Shape) { + t.Helper() for n, s := range shapes { p, ok := s.(*shp.PolyLine) if !ok { @@ -83,6 +86,7 @@ func testPolyLine(t *testing.T, points [][]float64, shapes []shp.Shape) { } func testPolygon(t *testing.T, points [][]float64, shapes []shp.Shape) { + t.Helper() for n, s := range shapes { p, ok := s.(*shp.Polygon) if !ok { @@ -95,48 +99,48 @@ func testPolygon(t *testing.T, points [][]float64, shapes []shp.Shape) { } func TestSHPReadZip(t *testing.T) { + t.Helper() testshapeIdentity(t, "shapetest/shapes.zip", getShapesFromFile) } func TestSHPReadPoint(t *testing.T) { + t.Helper() testshapeIdentity(t, "shapetest/point.shp", getShapesFromFile) } func TestSHPReadPolyLine(t *testing.T) { + t.Helper() testshapeIdentity(t, "shapetest/polyline.shp", getShapesFromFile) } func TestSHPReadPolygon(t *testing.T) { + t.Helper() testshapeIdentity(t, "shapetest/polygon.shp", getShapesFromFile) } func testshapeIdentity(t *testing.T, prefix string, getter shapeGetterFunc) { + t.Helper() shapes := getter(prefix, t) d := dataForReadTests[prefix] d.tester(t, d.points, shapes) } func getShapesFromFile(filename string, t *testing.T) (shapes []shp.Shape) { + t.Helper() + var reader ShapeReader - var err error - osr, err := os.Open(filename) - assert.NoError(t, err) + osr := lo.Must(os.Open(filename)) if strings.HasSuffix(filename, ".shp") { - reader, err = shp.ReadFrom(osr) + reader = lo.Must(shp.ReadFrom(osr)) } else { - reader, err = shp.ReadZipFrom(osr) - } - if err != nil { - t.Fatal("failed to open shapefile: " + filename + " (" + err.Error() + ")") + reader = lo.Must(shp.ReadZipFrom(osr)) } for reader.Next() { _, shape := reader.Shape() shapes = append(shapes, shape) } - if reader.Err() != nil { - t.Errorf("error while getting shapes for %s: %v", filename, reader.Err()) - } + assert.NoError(t, reader.Err()) return shapes } diff --git a/server/pkg/log/gceformatter.go b/server/pkg/log/gceformatter.go deleted file mode 100644 index 0df77cc26a..0000000000 --- a/server/pkg/log/gceformatter.go +++ /dev/null @@ -1,123 +0,0 @@ -package log - -// https://github.com/znly/logrus-gce with some modifications -// Apache License 2.0 - -import ( - "encoding/json" - "errors" - "fmt" - "runtime" - "strings" - "sync" - "time" - - "github.com/sirupsen/logrus" -) - -type severity string - -const ( - severityDEBUG severity = "DEBUG" - severityINFO severity = "INFO" - severityWARNING severity = "WARNING" - severityERROR severity = "ERROR" - severityCRITICAL severity = "CRITICAL" - severityALERT severity = "ALERT" -) - -var ( - levelsLogrusToGCE = map[logrus.Level]severity{ - logrus.DebugLevel: severityDEBUG, - logrus.InfoLevel: severityINFO, - logrus.WarnLevel: severityWARNING, - logrus.ErrorLevel: severityERROR, - logrus.FatalLevel: severityCRITICAL, - logrus.PanicLevel: severityALERT, - } -) - -var ( - stackSkips = map[logrus.Level]int{} - stackSkipsMu = sync.RWMutex{} -) - -var ( - ErrSkipNotFound = errors.New("could not find skips for log level") -) - -func getSkipLevel(level logrus.Level) (int, error) { - stackSkipsMu.RLock() - if skip, ok := stackSkips[level]; ok { - defer stackSkipsMu.RUnlock() - return skip, nil - } - stackSkipsMu.RUnlock() - - stackSkipsMu.Lock() - defer stackSkipsMu.Unlock() - if skip, ok := stackSkips[level]; ok { - return skip, nil - } - - // detect until we escape logrus back to the client package - // skip out of runtime and logrusgce package, hence 3 - stackSkipsCallers := make([]uintptr, 20) - runtime.Callers(3, stackSkipsCallers) - for i, pc := range stackSkipsCallers { - f := runtime.FuncForPC(pc) - if strings.HasPrefix(f.Name(), "github.com/sirupsen/logrus") { - continue - } - stackSkips[level] = i + 1 - return i + 1, nil - } - return 0, ErrSkipNotFound -} - -type GCEFormatter struct { - withSourceInfo bool -} - -func NewGCEFormatter(withSourceInfo bool) *GCEFormatter { - return &GCEFormatter{withSourceInfo: withSourceInfo} -} - -func (f *GCEFormatter) Format(entry *logrus.Entry) ([]byte, error) { - data := make(logrus.Fields, len(entry.Data)+3) - for k, v := range entry.Data { - switch v := v.(type) { - case error: - // Otherwise errors are ignored by `encoding/json` - // https://github.com/Sirupsen/logrus/issues/137 - data[k] = v.Error() - default: - data[k] = v - } - } - - data["time"] = entry.Time.Format(time.RFC3339Nano) - data["severity"] = levelsLogrusToGCE[entry.Level] - data["logMessage"] = entry.Message - - if f.withSourceInfo { - skip, err := getSkipLevel(entry.Level) - if err != nil { - return nil, err - } - if pc, file, line, ok := runtime.Caller(skip); ok { - f := runtime.FuncForPC(pc) - data["sourceLocation"] = map[string]interface{}{ - "file": file, - "line": line, - "functionName": f.Name(), - } - } - } - - serialized, err := json.Marshal(data) - if err != nil { - return nil, fmt.Errorf("Failed to marshal fields to JSON, %v", err) - } - return append(serialized, '\n'), nil -} diff --git a/server/pkg/log/log.go b/server/pkg/log/log.go deleted file mode 100644 index a42a6f0021..0000000000 --- a/server/pkg/log/log.go +++ /dev/null @@ -1,108 +0,0 @@ -package log - -import ( - "os" - - "github.com/sirupsen/logrus" -) - -func init() { - gcp, _ := os.LookupEnv("GOOGLE_CLOUD_PROJECT") - logrus.SetLevel(logrus.InfoLevel) - if gcp != "" { - logrus.SetFormatter(NewGCEFormatter(false)) - } else { - logrus.SetFormatter(&logrus.TextFormatter{ - DisableColors: false, - FullTimestamp: true, - }) - } -} - -func Tracef(format string, args ...interface{}) { - logrus.Tracef(format, args...) -} - -func Debugf(format string, args ...interface{}) { - logrus.Debugf(format, args...) -} - -func Infof(format string, args ...interface{}) { - logrus.Infof(format, args...) -} - -func Printf(format string, args ...interface{}) { - logrus.Printf(format, args...) -} - -func Warnf(format string, args ...interface{}) { - logrus.Warnf(format, args...) -} - -func Errorf(format string, args ...interface{}) { - logrus.Errorf(format, args...) -} - -func Fatalf(format string, args ...interface{}) { - logrus.Fatalf(format, args...) -} - -func Trace(args ...interface{}) { - logrus.Trace(args...) -} - -func Debug(args ...interface{}) { - logrus.Debug(args...) -} - -func Info(args ...interface{}) { - logrus.Info(args...) -} - -func Print(args ...interface{}) { - logrus.Print(args...) -} - -func Warn(args ...interface{}) { - logrus.Warn(args...) -} - -func Error(args ...interface{}) { - logrus.Error(args...) -} - -func Fatal(args ...interface{}) { - logrus.Fatal(args...) -} - -func Traceln(args ...interface{}) { - logrus.Traceln(args...) -} - -func Debugln(args ...interface{}) { - logrus.Debugln(args...) -} - -func Infoln(args ...interface{}) { - logrus.Infoln(args...) -} - -func Println(args ...interface{}) { - logrus.Println(args...) -} - -func Warnln(args ...interface{}) { - logrus.Warnln(args...) -} - -func Errorln(args ...interface{}) { - logrus.Errorln(args...) -} - -func Fatalln(args ...interface{}) { - logrus.Fatalln(args...) -} - -func Panicf(format string, args ...interface{}) { - logrus.Panicf(format, args...) -} diff --git a/server/pkg/plugin/pluginpack/package.go b/server/pkg/plugin/pluginpack/package.go index 054c63da98..dd549b33b0 100644 --- a/server/pkg/plugin/pluginpack/package.go +++ b/server/pkg/plugin/pluginpack/package.go @@ -3,6 +3,7 @@ package pluginpack import ( "archive/zip" "bytes" + "fmt" "io" "path" "path/filepath" @@ -79,7 +80,7 @@ func readTranslation(fs *zip.Reader, base string) (manifest.TranslationMap, erro } langfile, err := f.Open() if err != nil { - return nil, rerror.ErrInternalBy(err) + return nil, fmt.Errorf("failed to open translation file: %w", err) } defer func() { _ = langfile.Close() diff --git a/server/pkg/scene/sceneops/plugin_installer.go b/server/pkg/scene/sceneops/plugin_installer.go index a881cdc079..9b5279b58a 100644 --- a/server/pkg/scene/sceneops/plugin_installer.go +++ b/server/pkg/scene/sceneops/plugin_installer.go @@ -31,27 +31,27 @@ func (s PluginInstaller) InstallPluginFromRepository(pluginID scene.PluginID) er // for _, s := range manifest.ExtensionSchema { // err = i.propertySchemaRepo.Save(&s) // if err != nil { - // i.output.Upload(nil, rerror.ErrInternalBy(err)) + // i.output.Upload(nil, rerror.ErrInternalByWithContext(ctx, err)) // return // } // } // err = i.pluginRepo.Save(plugin) // if err != nil { - // i.output.Upload(nil, rerror.ErrInternalBy(err)) + // i.output.Upload(nil, rerror.ErrInternalByWithContext(ctx, err)) // return // } // // Download and extract plugin files to storage // data, err := i.pluginRepositoryRepo.Data(inp.Name, inp.Version) // if err != nil { - // i.output.Upload(nil, rerror.ErrInternalBy(err)) + // i.output.Upload(nil, rerror.ErrInternalByWithContext(ctx, err)) // return // } // _, err = i.fileRepo.UploadAndExtractPluginFiles(data, plugin) // if err != nil { - // i.output.Upload(nil, rerror.ErrInternalBy(err)) + // i.output.Upload(nil, rerror.ErrInternalByWithContext(ctx, err)) // return // } @@ -78,7 +78,7 @@ func (s PluginInstaller) InstallPluginFromRepository(pluginID scene.PluginID) er // Public(inp.Public). // Build() // if err != nil { - // i.output.Upload(nil, rerror.ErrInternalBy(err)) + // i.output.Upload(nil, rerror.ErrInternalByWithContext(ctx, err)) // return // } @@ -86,7 +86,7 @@ func (s PluginInstaller) InstallPluginFromRepository(pluginID scene.PluginID) er // if manifest.Schema != nil { // err = i.propertySchemaRepo.Save(manifest.Schema) // if err != nil { - // i.output.Upload(nil, rerror.ErrInternalBy(err)) + // i.output.Upload(nil, rerror.ErrInternalByWithContext(ctx, err)) // return // } // } @@ -94,27 +94,27 @@ func (s PluginInstaller) InstallPluginFromRepository(pluginID scene.PluginID) er // for _, s := range manifest.ExtensionSchema { // err = i.propertySchemaRepo.Save(&s) // if err != nil { - // i.output.Upload(nil, rerror.ErrInternalBy(err)) + // i.output.Upload(nil, rerror.ErrInternalByWithContext(ctx, err)) // return // } // } // err = i.pluginRepo.Save(plugin) // if err != nil { - // i.output.Upload(nil, rerror.ErrInternalBy(err)) + // i.output.Upload(nil, rerror.ErrInternalByWithContext(ctx, err)) // return // } // // Download and extract plugin files to storage // data, err := i.pluginRepositoryRepo.Data(inp.Name, inp.Version) // if err != nil { - // i.output.Upload(nil, rerror.ErrInternalBy(err)) + // i.output.Upload(nil, rerror.ErrInternalByWithContext(ctx, err)) // return // } // _, err = i.fileRepo.UploadAndExtractPluginFiles(data, plugin) // if err != nil { - // i.output.Upload(nil, rerror.ErrInternalBy(err)) + // i.output.Upload(nil, rerror.ErrInternalByWithContext(ctx, err)) // return // } diff --git a/server/pkg/scene/sceneops/plugin_migrator.go b/server/pkg/scene/sceneops/plugin_migrator.go index b78def084c..a3ada84e8a 100644 --- a/server/pkg/scene/sceneops/plugin_migrator.go +++ b/server/pkg/scene/sceneops/plugin_migrator.go @@ -34,7 +34,7 @@ var ( func (s *PluginMigrator) MigratePlugins(ctx context.Context, sc *scene.Scene, oldPluginID, newPluginID plugin.ID) (MigratePluginsResult, error) { if s == nil { - return MigratePluginsResult{}, rerror.ErrInternalBy(errors.New("scene is nil")) + return MigratePluginsResult{}, rerror.ErrInternalByWithContext(ctx, errors.New("scene is nil")) } // should be same plugin but different version