Skip to content

Commit

Permalink
use context on logging
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 committed Jun 28, 2023
1 parent 1f9440d commit cb9b005
Show file tree
Hide file tree
Showing 41 changed files with 173 additions and 402 deletions.
6 changes: 3 additions & 3 deletions server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,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-20230628094334-9f836a79dabb
github.com/reearth/reearthx v0.0.0-20230628100740-772ae55edde1
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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
9 changes: 3 additions & 6 deletions server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -492,12 +493,8 @@ 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-20230628083626-a5efdcea5ff3 h1:TWAum5PQ1lNOg+TfBFL9lpw9Z0V7d0TCqxNQTQHOUoQ=
github.com/reearth/reearthx v0.0.0-20230628083626-a5efdcea5ff3/go.mod h1:Rh7MJPKq43f+HZ/PwjZ5vEbGPpllNFvUrxn9sBn2b+s=
github.com/reearth/reearthx v0.0.0-20230628094334-9f836a79dabb h1:tCYnqv0N2kwx90fMjUHEnsYfbiChJGeXdl+NMHPJCxY=
github.com/reearth/reearthx v0.0.0-20230628094334-9f836a79dabb/go.mod h1:O4zFr6ks8jDKfgy9NBpZKp31jpmAPnfOtMQK9B6xX54=
github.com/reearth/reearthx v0.0.0-20230628100740-772ae55edde1 h1:jt73WlNFZQ8m7tvhyOyVcLLmtlEr+a8g3ZzGU1aLqwg=
github.com/reearth/reearthx v0.0.0-20230628100740-772ae55edde1/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=
Expand Down
2 changes: 1 addition & 1 deletion server/internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,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
Expand Down
4 changes: 2 additions & 2 deletions server/internal/app/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,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())
Expand All @@ -80,7 +80,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())
Expand Down
8 changes: 4 additions & 4 deletions server/internal/infrastructure/adapter/property_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
10 changes: 5 additions & 5 deletions server/internal/infrastructure/auth0/authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion server/internal/infrastructure/auth0/authenticator_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package auth0

import (
"context"
"encoding/json"
"net/http"
"strings"
Expand Down Expand Up @@ -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,
})
Expand Down
20 changes: 10 additions & 10 deletions server/internal/infrastructure/fs/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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()
Expand All @@ -144,28 +144,28 @@ 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)
}
}

if err := f.fs.Rename(from, dest); err != nil {
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
}
Expand All @@ -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
}
Expand Down
18 changes: 9 additions & 9 deletions server/internal/infrastructure/fs/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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()
Expand All @@ -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{}
Expand All @@ -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()
Expand Down
10 changes: 5 additions & 5 deletions server/internal/infrastructure/fs/property_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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"))
}
Loading

0 comments on commit cb9b005

Please sign in to comment.