chore: update metrics

This commit is contained in:
Steven
2023-10-17 23:44:16 +08:00
parent e30d0c2dd0
commit a5df36eff2
8 changed files with 72 additions and 4 deletions

View File

@ -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))
}

View File

@ -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
}

View File

@ -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,
})
}