From a5df36eff229f948e60da917aef16c65f8488d16 Mon Sep 17 00:00:00 2001 From: Steven Date: Tue, 17 Oct 2023 23:44:16 +0800 Subject: [PATCH] chore: update metrics --- api/v1/memo.go | 2 ++ api/v1/user.go | 2 ++ cmd/memos.go | 4 +++ go.mod | 1 + go.sum | 4 +++ server/server.go | 8 +++-- server/service/{ => backup}/backup.go | 3 +- server/service/metric/metric.go | 52 +++++++++++++++++++++++++++ 8 files changed, 72 insertions(+), 4 deletions(-) rename server/service/{ => backup}/backup.go (98%) create mode 100644 server/service/metric/metric.go diff --git a/api/v1/memo.go b/api/v1/memo.go index ddd43411..06138977 100644 --- a/api/v1/memo.go +++ b/api/v1/memo.go @@ -14,6 +14,7 @@ import ( "github.com/usememos/memos/common/log" "github.com/usememos/memos/common/util" + "github.com/usememos/memos/server/service/metric" "github.com/usememos/memos/store" ) @@ -387,6 +388,7 @@ func (s *APIV1Service) CreateMemo(c echo.Context) error { } } } + metric.Enqueue("memo create") return c.JSON(http.StatusOK, memoResponse) } diff --git a/api/v1/user.go b/api/v1/user.go index 0b20c7ca..17d31437 100644 --- a/api/v1/user.go +++ b/api/v1/user.go @@ -12,6 +12,7 @@ import ( "golang.org/x/crypto/bcrypt" "github.com/usememos/memos/common/util" + "github.com/usememos/memos/server/service/metric" "github.com/usememos/memos/store" ) @@ -169,6 +170,7 @@ func (s *APIV1Service) CreateUser(c echo.Context) error { if err := s.createUserCreateActivity(c, userMessage); err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create activity").SetInternal(err) } + metric.Enqueue("user create") return c.JSON(http.StatusOK, userMessage) } diff --git a/cmd/memos.go b/cmd/memos.go index 1717005e..f972415c 100644 --- a/cmd/memos.go +++ b/cmd/memos.go @@ -15,6 +15,7 @@ import ( "github.com/usememos/memos/common/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" ) @@ -64,6 +65,9 @@ var ( return } + // 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, diff --git a/go.mod b/go.mod index e78657c6..458df8fc 100644 --- a/go.mod +++ b/go.mod @@ -106,6 +106,7 @@ require ( github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/pelletier/go-toml/v2 v2.1.0 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect + github.com/posthog/posthog-go v0.0.0-20230801140217-d607812dee69 github.com/spf13/afero v1.10.0 // indirect github.com/spf13/cast v1.5.1 // indirect github.com/spf13/pflag v1.0.5 // indirect diff --git a/go.sum b/go.sum index a9413987..89e42768 100644 --- a/go.sum +++ b/go.sum @@ -284,6 +284,7 @@ github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26/go.mod h1:dDKJzRmX4S3 github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4= github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= @@ -474,6 +475,8 @@ 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-20230801140217-d607812dee69 h1:01dHVodha5BzrMtVmcpPeA4VYbZEsTXQ6m4123zQXJk= +github.com/posthog/posthog-go v0.0.0-20230801140217-d607812dee69/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= @@ -573,6 +576,7 @@ github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs 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.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ= diff --git a/server/server.go b/server/server.go index 326c2770..fc2aff1e 100644 --- a/server/server.go +++ b/server/server.go @@ -22,7 +22,8 @@ import ( "github.com/usememos/memos/plugin/telegram" "github.com/usememos/memos/server/integration" "github.com/usememos/memos/server/profile" - "github.com/usememos/memos/server/service" + "github.com/usememos/memos/server/service/backup" + "github.com/usememos/memos/server/service/metric" "github.com/usememos/memos/store" ) @@ -38,7 +39,7 @@ type Server struct { apiV2Service *apiv2.APIV2Service // Asynchronous runners. - backupRunner *service.BackupRunner + backupRunner *backup.BackupRunner telegramBot *telegram.Bot } @@ -54,7 +55,7 @@ func NewServer(ctx context.Context, profile *profile.Profile, store *store.Store Profile: profile, // Asynchronous runners. - backupRunner: service.NewBackupRunner(store), + backupRunner: backup.NewBackupRunner(store), telegramBot: telegram.NewBotWithHandler(integration.NewTelegramHandler(store)), } @@ -132,6 +133,7 @@ func (s *Server) Start(ctx context.Context) error { } }() + metric.Enqueue("server start") return s.e.Start(fmt.Sprintf("%s:%d", s.Profile.Addr, s.Profile.Port)) } diff --git a/server/service/backup.go b/server/service/backup/backup.go similarity index 98% rename from server/service/backup.go rename to server/service/backup/backup.go index ade63e86..9f073916 100644 --- a/server/service/backup.go +++ b/server/service/backup/backup.go @@ -1,4 +1,4 @@ -package service +package backup import ( "context" @@ -13,6 +13,7 @@ import ( "github.com/usememos/memos/store" ) +// nolint type BackupRunner struct { Store *store.Store } diff --git a/server/service/metric/metric.go b/server/service/metric/metric.go new file mode 100644 index 00000000..504dc1e3 --- /dev/null +++ b/server/service/metric/metric.go @@ -0,0 +1,52 @@ +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, + }) +}