diff --git a/core/account.go b/core/account.go index e707f0090..857322863 100644 --- a/core/account.go +++ b/core/account.go @@ -154,7 +154,12 @@ func (mw *Middleware) AccountCreate(cctx context.Context, req *pb.RpcAccountCrea mw.EventSender, } - if mw.app, err = anytype.StartNewApp(context.WithValue(context.Background(), metrics.CtxKeyRequest, "account_create"), mw.clientVersion, comps...); err != nil { + mw.requireClientWithVersion() + if mw.app, err = anytype.StartNewApp( + context.WithValue(context.Background(), metrics.CtxKeyRequest, "account_create"), + mw.clientWithVersion, + comps..., + ); err != nil { return response(newAcc, pb.RpcAccountCreateResponseError_ACCOUNT_CREATED_BUT_FAILED_TO_START_NODE, err) } @@ -303,7 +308,12 @@ func (mw *Middleware) AccountSelect(cctx context.Context, req *pb.RpcAccountSele // if we have created the repo, we need to highlight that we are recovering the account request = request + "_recover" } - if mw.app, err = anytype.StartNewApp(context.WithValue(context.Background(), metrics.CtxKeyRequest, request), mw.clientVersion, comps...); err != nil { + mw.requireClientWithVersion() + if mw.app, err = anytype.StartNewApp( + context.WithValue(context.Background(), metrics.CtxKeyRequest, request), + mw.clientWithVersion, + comps..., + ); err != nil { if errors.Is(err, spacesyncproto.ErrSpaceMissing) { return response(nil, pb.RpcAccountSelectResponseError_FAILED_TO_FIND_ACCOUNT_INFO, err) } @@ -655,7 +665,7 @@ func (mw *Middleware) startApp(cfg *config.Config, derivationResult crypto.Deriv ctxWithValue := context.WithValue(context.Background(), metrics.CtxKeyRequest, "account_create") var err error - if mw.app, err = anytype.StartNewApp(ctxWithValue, mw.clientVersion, comps...); err != nil { + if mw.app, err = anytype.StartNewApp(ctxWithValue, mw.clientWithVersion, comps...); err != nil { return err } return nil diff --git a/core/anytype/bootstrap.go b/core/anytype/bootstrap.go index 794596464..9451bff14 100644 --- a/core/anytype/bootstrap.go +++ b/core/anytype/bootstrap.go @@ -3,6 +3,7 @@ package anytype import ( "context" "os" + "regexp" "time" "github.com/anyproto/any-sync/app" @@ -87,9 +88,9 @@ func BootstrapWallet(rootPath string, derivationResult crypto.DerivationResult) return wallet.NewWithAccountRepo(rootPath, derivationResult) } -func StartNewApp(ctx context.Context, clientVersion string, components ...app.Component) (a *app.App, err error) { +func StartNewApp(ctx context.Context, clientWithVersion string, components ...app.Component) (a *app.App, err error) { a = new(app.App) - a.SetVersionName(appVersion(a, clientVersion)) + a.SetVersionName(appVersion(a, clientWithVersion)) Bootstrap(a, components...) metrics.SharedClient.SetAppVersion(a.Version()) metrics.SharedClient.Run() @@ -102,10 +103,11 @@ func StartNewApp(ctx context.Context, clientVersion string, components ...app.Co return } -func appVersion(a *app.App, clientVersion string) string { - middleVersion := vcs.GetVCSInfo().Version() +func appVersion(a *app.App, clientWithVersion string) string { + clientWithVersion = regexp.MustCompile(`(@|\/)+`).ReplaceAllString(clientWithVersion, "_") + middleVersion := MiddlewareVersion() anySyncVersion := a.AnySyncVersion() - return "client:" + clientVersion + "/middle:" + middleVersion + "/any-sync:" + anySyncVersion + return clientWithVersion + "/middle:" + middleVersion + "/any-sync:" + anySyncVersion } func Bootstrap(a *app.App, components ...app.Component) { @@ -213,3 +215,7 @@ func Bootstrap(a *app.App, components ...app.Component) { Register(graphRenderer) return } + +func MiddlewareVersion() string { + return vcs.GetVCSInfo().Version() +} diff --git a/core/core.go b/core/core.go index de83d07a4..66d8b1de1 100644 --- a/core/core.go +++ b/core/core.go @@ -35,9 +35,9 @@ type Middleware struct { accountSearchCancel context.CancelFunc EventSender event.Sender - sessions session.Service - clientVersion string - app *app.App + sessions session.Service + clientWithVersion string + app *app.App m sync.RWMutex } @@ -180,3 +180,9 @@ func (mw *Middleware) OnPanic(v interface{}) { os.Stderr.Write(stack) log.With("stack", stack).Errorf("panic recovered: %v", v) } + +func (mw *Middleware) requireClientWithVersion() { + if mw.clientWithVersion == "" { + panic(errors.New("client platform with the version must be set using the MetricsSetParameters method")) + } +} diff --git a/core/metrics.go b/core/metrics.go index 70795e3e7..87e028b49 100644 --- a/core/metrics.go +++ b/core/metrics.go @@ -2,6 +2,7 @@ package core import ( "context" + "errors" "github.com/anyproto/anytype-heart/metrics" "github.com/anyproto/anytype-heart/pb" @@ -16,7 +17,11 @@ func (mw *Middleware) MetricsSetParameters(cctx context.Context, req *pb.RpcMetr return m } - mw.clientVersion = req.Platform + "-" + req.Version + if req.Version == "" { + return response(pb.RpcMetricsSetParametersResponseError_BAD_INPUT, + errors.New("version is empty. Version must be in format: 1.0.0-optional-commit-hash-for-dev-builds")) + } + mw.clientWithVersion = req.Platform + ":" + req.Version metrics.SharedClient.SetPlatform(req.Platform) return response(pb.RpcMetricsSetParametersResponseError_NULL, nil)