From b0f52ade7ae303daf0b7681fdf5b2634e8706b5b Mon Sep 17 00:00:00 2001 From: Steven Date: Sat, 24 Feb 2024 23:32:39 +0800 Subject: [PATCH] chore: remove metric service --- api/v1/memo.go | 4 --- api/v1/resource.go | 3 -- api/v1/user.go | 2 -- api/v2/auth_service.go | 3 -- api/v2/memo_service.go | 4 --- api/v2/resource_service.go | 2 -- bin/memos/main.go | 16 +--------- go.mod | 1 - go.sum | 3 -- server/integration/telegram.go | 2 -- server/profile/profile.go | 2 -- server/server.go | 3 -- server/service/metric/metric.go | 52 --------------------------------- 13 files changed, 1 insertion(+), 96 deletions(-) delete mode 100644 server/service/metric/metric.go diff --git a/api/v1/memo.go b/api/v1/memo.go index 5be7462a..13ce19ad 100644 --- a/api/v1/memo.go +++ b/api/v1/memo.go @@ -17,7 +17,6 @@ import ( "github.com/usememos/memos/internal/util" "github.com/usememos/memos/plugin/webhook" storepb "github.com/usememos/memos/proto/gen/store" - "github.com/usememos/memos/server/service/metric" "github.com/usememos/memos/store" ) @@ -351,7 +350,6 @@ func (s *APIV1Service) CreateMemo(c echo.Context) error { if err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create activity").SetInternal(err) } - metric.Enqueue("memo comment create") if _, err := s.Store.CreateInbox(ctx, &store.Inbox{ SenderID: memo.CreatorID, ReceiverID: relatedMemo.CreatorID, @@ -410,7 +408,6 @@ func (s *APIV1Service) CreateMemo(c echo.Context) error { log.Warn("Failed to dispatch memo created webhook", zap.Error(err)) } - metric.Enqueue("memo create") return c.JSON(http.StatusOK, memoResponse) } @@ -1011,7 +1008,6 @@ func (s *APIV1Service) dispatchMemoRelatedWebhook(ctx context.Context, memo *Mem if err != nil { return err } - metric.Enqueue("webhook dispatch") for _, hook := range webhooks { payload := convertMemoToWebhookPayload(memo) payload.ActivityType = activityType diff --git a/api/v1/resource.go b/api/v1/resource.go index 0b587de4..ab7c6f68 100644 --- a/api/v1/resource.go +++ b/api/v1/resource.go @@ -22,7 +22,6 @@ import ( "github.com/usememos/memos/internal/log" "github.com/usememos/memos/internal/util" "github.com/usememos/memos/plugin/storage/s3" - "github.com/usememos/memos/server/service/metric" "github.com/usememos/memos/store" ) @@ -162,7 +161,6 @@ func (s *APIV1Service) CreateResource(c echo.Context) error { if err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create resource").SetInternal(err) } - metric.Enqueue("resource create") return c.JSON(http.StatusOK, convertResourceFromStore(resource)) } @@ -240,7 +238,6 @@ func (s *APIV1Service) UploadResource(c echo.Context) error { if err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create resource").SetInternal(err) } - metric.Enqueue("resource create") return c.JSON(http.StatusOK, convertResourceFromStore(resource)) } diff --git a/api/v1/user.go b/api/v1/user.go index 4dc5be15..1f56fc2b 100644 --- a/api/v1/user.go +++ b/api/v1/user.go @@ -12,7 +12,6 @@ import ( "golang.org/x/crypto/bcrypt" "github.com/usememos/memos/internal/util" - "github.com/usememos/memos/server/service/metric" "github.com/usememos/memos/store" ) @@ -183,7 +182,6 @@ func (s *APIV1Service) CreateUser(c echo.Context) error { } userMessage := convertUserFromStore(user) - metric.Enqueue("user create") return c.JSON(http.StatusOK, userMessage) } diff --git a/api/v2/auth_service.go b/api/v2/auth_service.go index ec2d4052..24fb48b3 100644 --- a/api/v2/auth_service.go +++ b/api/v2/auth_service.go @@ -19,7 +19,6 @@ import ( "github.com/usememos/memos/plugin/idp" "github.com/usememos/memos/plugin/idp/oauth2" apiv2pb "github.com/usememos/memos/proto/gen/api/v2" - "github.com/usememos/memos/server/service/metric" "github.com/usememos/memos/store" ) @@ -168,7 +167,6 @@ func (s *APIV2Service) doSignIn(ctx context.Context, user *store.User, expireTim return status.Errorf(codes.Internal, "failed to set grpc header, error: %v", err) } - metric.Enqueue("user sign in") return nil } @@ -214,7 +212,6 @@ func (s *APIV2Service) SignUp(ctx context.Context, request *apiv2pb.SignUpReques if err := s.doSignIn(ctx, user, time.Now().Add(auth.AccessTokenDuration)); err != nil { return nil, status.Errorf(codes.Internal, fmt.Sprintf("failed to sign in, err: %s", err)) } - metric.Enqueue("user sign up") return &apiv2pb.SignUpResponse{ User: convertUserFromStore(user), }, nil diff --git a/api/v2/memo_service.go b/api/v2/memo_service.go index 13ac8679..ae8c4e55 100644 --- a/api/v2/memo_service.go +++ b/api/v2/memo_service.go @@ -23,7 +23,6 @@ import ( "github.com/usememos/memos/plugin/webhook" apiv2pb "github.com/usememos/memos/proto/gen/api/v2" storepb "github.com/usememos/memos/proto/gen/store" - "github.com/usememos/memos/server/service/metric" "github.com/usememos/memos/store" ) @@ -64,7 +63,6 @@ func (s *APIV2Service) CreateMemo(ctx context.Context, request *apiv2pb.CreateMe if err != nil { return nil, err } - metric.Enqueue("memo create") memoMessage, err := s.convertMemoFromStore(ctx, memo) if err != nil { @@ -371,7 +369,6 @@ func (s *APIV2Service) CreateMemoComment(ctx context.Context, request *apiv2pb.C return nil, status.Errorf(codes.Internal, "failed to create inbox") } } - metric.Enqueue("memo comment create") response := &apiv2pb.CreateMemoCommentResponse{ Memo: memo, @@ -806,7 +803,6 @@ func (s *APIV2Service) dispatchMemoRelatedWebhook(ctx context.Context, memo *api if err != nil { return err } - metric.Enqueue("webhook dispatch") for _, hook := range webhooks { payload := convertMemoToWebhookPayload(memo) payload.ActivityType = activityType diff --git a/api/v2/resource_service.go b/api/v2/resource_service.go index dd5a67d2..88c25690 100644 --- a/api/v2/resource_service.go +++ b/api/v2/resource_service.go @@ -11,7 +11,6 @@ import ( "google.golang.org/protobuf/types/known/timestamppb" apiv2pb "github.com/usememos/memos/proto/gen/api/v2" - "github.com/usememos/memos/server/service/metric" "github.com/usememos/memos/store" ) @@ -46,7 +45,6 @@ func (s *APIV2Service) CreateResource(ctx context.Context, request *apiv2pb.Crea return nil, status.Errorf(codes.Internal, "failed to create resource: %v", err) } - metric.Enqueue("resource create") return &apiv2pb.CreateResourceResponse{ Resource: s.convertResourceFromStore(ctx, resource), }, nil diff --git a/bin/memos/main.go b/bin/memos/main.go index 1af88838..5110e323 100644 --- a/bin/memos/main.go +++ b/bin/memos/main.go @@ -16,7 +16,6 @@ import ( "github.com/usememos/memos/internal/log" "github.com/usememos/memos/server" _profile "github.com/usememos/memos/server/profile" - "github.com/usememos/memos/server/service/metric" "github.com/usememos/memos/store" "github.com/usememos/memos/store/db" ) @@ -41,7 +40,6 @@ var ( driver string dsn string serveFrontend bool - enableMetric bool rootCmd = &cobra.Command{ Use: "memos", @@ -74,11 +72,6 @@ var ( return } - if profile.Metric { - // nolint - metric.NewMetricClient(s.ID, *profile) - } - c := make(chan os.Signal, 1) // Trigger graceful shutdown on SIGINT or SIGTERM. // The default signal sent by the `kill` command is SIGTERM, @@ -124,7 +117,6 @@ func init() { rootCmd.PersistentFlags().StringVarP(&driver, "driver", "", "", "database driver") rootCmd.PersistentFlags().StringVarP(&dsn, "dsn", "", "", "database source name(aka. DSN)") rootCmd.PersistentFlags().BoolVarP(&serveFrontend, "frontend", "", true, "serve frontend files") - rootCmd.PersistentFlags().BoolVarP(&enableMetric, "metric", "", true, "allow metric collection") err := viper.BindPFlag("mode", rootCmd.PersistentFlags().Lookup("mode")) if err != nil { @@ -154,17 +146,12 @@ func init() { if err != nil { panic(err) } - err = viper.BindPFlag("metric", rootCmd.PersistentFlags().Lookup("metric")) - if err != nil { - panic(err) - } viper.SetDefault("mode", "demo") viper.SetDefault("driver", "sqlite") viper.SetDefault("addr", "") viper.SetDefault("port", 8081) viper.SetDefault("frontend", true) - viper.SetDefault("metric", true) viper.SetEnvPrefix("memos") } @@ -187,9 +174,8 @@ port: %d mode: %s driver: %s frontend: %t -metric: %t --- -`, profile.Version, profile.Data, profile.DSN, profile.Addr, profile.Port, profile.Mode, profile.Driver, profile.Frontend, profile.Metric) +`, profile.Version, profile.Data, profile.DSN, profile.Addr, profile.Port, profile.Mode, profile.Driver, profile.Frontend) } func printGreetings() { diff --git a/go.mod b/go.mod index 4e1b748b..5f3a2e19 100644 --- a/go.mod +++ b/go.mod @@ -102,7 +102,6 @@ require ( github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/pelletier/go-toml/v2 v2.1.1 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect - github.com/posthog/posthog-go v0.0.0-20240208111551-abe6962c309e github.com/spf13/afero v1.11.0 // indirect github.com/spf13/cast v1.6.0 // indirect github.com/spf13/pflag v1.0.5 // indirect diff --git a/go.sum b/go.sum index 101ba0d2..e147d053 100644 --- a/go.sum +++ b/go.sum @@ -367,8 +367,6 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= -github.com/posthog/posthog-go v0.0.0-20240208111551-abe6962c309e h1:Cks+ATHSvyId+c4XOTdvVomE2Srb6WFlkXljYZddOEw= -github.com/posthog/posthog-go v0.0.0-20240208111551-abe6962c309e/go.mod h1:migYMxlAqcnQy+3eN8mcL0b2tpKy6R+8Zc0lxwk4dKM= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v0.9.3-0.20190127221311-3c4408c8b829/go.mod h1:p2iRAGwDERtqlqzRXnrOVns+ignqQo//hLXqYxZYVNs= github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= @@ -460,7 +458,6 @@ github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVM github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= -github.com/urfave/cli v1.22.5/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo= diff --git a/server/integration/telegram.go b/server/integration/telegram.go index f69cff14..2584418a 100644 --- a/server/integration/telegram.go +++ b/server/integration/telegram.go @@ -16,7 +16,6 @@ import ( "github.com/usememos/memos/plugin/telegram" "github.com/usememos/memos/plugin/webhook" storepb "github.com/usememos/memos/proto/gen/store" - "github.com/usememos/memos/server/service/metric" "github.com/usememos/memos/store" ) @@ -231,7 +230,6 @@ func (t *TelegramHandler) dispatchMemoRelatedWebhook(ctx context.Context, memo s if err != nil { return err } - metric.Enqueue("webhook dispatch") for _, hook := range webhooks { payload := t.convertMemoToWebhookPayload(ctx, memo) payload.ActivityType = activityType diff --git a/server/profile/profile.go b/server/profile/profile.go index 4547ea66..bd22193f 100644 --- a/server/profile/profile.go +++ b/server/profile/profile.go @@ -32,8 +32,6 @@ type Profile struct { Version string `json:"version"` // Frontend indicate the frontend is enabled or not Frontend bool `json:"-"` - // Metric indicate the metric collection is enabled or not - Metric bool `json:"-"` } func (p *Profile) IsDev() bool { diff --git a/server/server.go b/server/server.go index 3cc073f5..2f614d6e 100644 --- a/server/server.go +++ b/server/server.go @@ -18,7 +18,6 @@ import ( "github.com/usememos/memos/server/frontend" "github.com/usememos/memos/server/integration" "github.com/usememos/memos/server/profile" - "github.com/usememos/memos/server/service/metric" versionchecker "github.com/usememos/memos/server/service/version_checker" "github.com/usememos/memos/store" ) @@ -106,8 +105,6 @@ func NewServer(ctx context.Context, profile *profile.Profile, store *store.Store func (s *Server) Start(ctx context.Context) error { go versionchecker.NewVersionChecker(s.Store, s.Profile).Start(ctx) go s.telegramBot.Start(ctx) - - metric.Enqueue("server start") return s.e.Start(fmt.Sprintf("%s:%d", s.Profile.Addr, s.Profile.Port)) } diff --git a/server/service/metric/metric.go b/server/service/metric/metric.go deleted file mode 100644 index 504dc1e3..00000000 --- a/server/service/metric/metric.go +++ /dev/null @@ -1,52 +0,0 @@ -package metric - -import ( - "github.com/posthog/posthog-go" - - "github.com/usememos/memos/server/profile" -) - -const ( - PostHogAPIKey = "phc_YFEi1aqUBW9sX2KDzdvMtK43DNu0mkeoKMKc0EQum2t" -) - -var ( - client *MetricClient -) - -// nolint -type MetricClient struct { - workspaceID string - profile *profile.Profile - phClient *posthog.Client -} - -func NewMetricClient(workspaceID string, profile profile.Profile) (*MetricClient, error) { - phClient, err := posthog.NewWithConfig(PostHogAPIKey, posthog.Config{ - Endpoint: "https://app.posthog.com", - }) - if err != nil { - return nil, err - } - client = &MetricClient{ - workspaceID: workspaceID, - profile: &profile, - phClient: &phClient, - } - return client, nil -} - -func Enqueue(event string) { - if client == nil { - return - } - if client.profile.Mode != "prod" { - return - } - - // nolint - (*client.phClient).Enqueue(posthog.Capture{ - DistinctId: `memos-` + client.workspaceID, - Event: event, - }) -}