Skip to content

Commit b92f8df

Browse files
committed
🔍 implement telemetry
1 parent 0fe30bd commit b92f8df

File tree

8 files changed

+127
-9
lines changed

8 files changed

+127
-9
lines changed

cyclops-ctrl/.env

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
DISABLE_TELEMETRY=true
2+
PORT=8888

cyclops-ctrl/cmd/main/main.go

+23-4
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ package main
33
import (
44
"flag"
55
"fmt"
6-
"github.com/cyclops-ui/cycops-ctrl/internal/auth"
7-
"github.com/cyclops-ui/cycops-ctrl/internal/template"
8-
"github.com/cyclops-ui/cycops-ctrl/internal/template/cache"
96
"os"
7+
"strconv"
108

9+
_ "github.com/joho/godotenv/autoload"
1110
"k8s.io/apimachinery/pkg/runtime"
1211
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
1312
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
@@ -17,10 +16,14 @@ import (
1716
"sigs.k8s.io/controller-runtime/pkg/log/zap"
1817

1918
cyclopsv1alpha1 "github.com/cyclops-ui/cycops-ctrl/api/v1alpha1"
19+
"github.com/cyclops-ui/cycops-ctrl/internal/auth"
2020
"github.com/cyclops-ui/cycops-ctrl/internal/cluster/k8sclient"
2121
"github.com/cyclops-ui/cycops-ctrl/internal/handler"
2222
"github.com/cyclops-ui/cycops-ctrl/internal/modulecontroller"
2323
"github.com/cyclops-ui/cycops-ctrl/internal/storage/templates"
24+
"github.com/cyclops-ui/cycops-ctrl/internal/telemetry"
25+
"github.com/cyclops-ui/cycops-ctrl/internal/template"
26+
"github.com/cyclops-ui/cycops-ctrl/internal/template/cache"
2427
)
2528

2629
var (
@@ -54,6 +57,9 @@ func main() {
5457

5558
setupLog.Info("starting handler")
5659

60+
telemetryClient, _ := telemetry.NewClient(getEnvBool("DISABLE_TELEMETRY"))
61+
telemetryClient.InstanceStart()
62+
5763
k8sClient, err := k8sclient.New()
5864
if err != nil {
5965
fmt.Println("error bootstrapping Kubernetes client", err)
@@ -71,7 +77,7 @@ func main() {
7177
cache.NewInMemoryTemplatesCache(),
7278
)
7379

74-
handler, err := handler.New(templatesStorage, templatesRepo, k8sClient)
80+
handler, err := handler.New(templatesStorage, templatesRepo, k8sClient, telemetryClient)
7581
if err != nil {
7682
panic(err)
7783
}
@@ -97,6 +103,7 @@ func main() {
97103
templatesRepo,
98104
templatesStorage,
99105
k8sClient,
106+
telemetryClient,
100107
)).SetupWithManager(mgr); err != nil {
101108
setupLog.Error(err, "unable to create controller", "controller", "Module")
102109
os.Exit(1)
@@ -118,3 +125,15 @@ func main() {
118125
os.Exit(1)
119126
}
120127
}
128+
129+
func getEnvBool(key string) bool {
130+
value := os.Getenv(key)
131+
if value == "" {
132+
return false
133+
}
134+
b, err := strconv.ParseBool(value)
135+
if err != nil {
136+
return false
137+
}
138+
return b
139+
}

cyclops-ctrl/go.mod

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ require (
1010
github.com/go-git/go-git/v5 v5.11.0
1111
github.com/go-logr/logr v1.3.0
1212
github.com/go-redis/redis/v8 v8.11.5
13+
github.com/google/uuid v1.6.0
14+
github.com/joho/godotenv v1.5.1
1315
github.com/json-iterator/go v1.1.12
1416
github.com/onsi/ginkgo/v2 v2.13.0
1517
github.com/onsi/gomega v1.29.0
1618
github.com/pkg/errors v0.9.1
19+
github.com/posthog/posthog-go v0.0.0-20240315130956-036dfa9f3555
1720
golang.org/x/net v0.19.0
1821
gopkg.in/yaml.v2 v2.4.0
1922
helm.sh/helm/v3 v3.14.2
@@ -79,7 +82,6 @@ require (
7982
github.com/google/go-cmp v0.6.0 // indirect
8083
github.com/google/gofuzz v1.2.0 // indirect
8184
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect
82-
github.com/google/uuid v1.3.0 // indirect
8385
github.com/gorilla/mux v1.8.0 // indirect
8486
github.com/huandu/xstrings v1.4.0 // indirect
8587
github.com/imdario/mergo v0.3.13 // indirect

cyclops-ctrl/go.sum

+6-2
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/
200200
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 h1:K6RDEckDVWvDI9JAJYCmNdQXq6neHJOYx3V6jnqNEec=
201201
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
202202
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
203-
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
204-
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
203+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
204+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
205205
github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH4=
206206
github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q=
207207
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
@@ -220,6 +220,8 @@ github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLf
220220
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
221221
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
222222
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
223+
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
224+
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
223225
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
224226
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
225227
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
@@ -305,6 +307,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
305307
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
306308
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
307309
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
310+
github.com/posthog/posthog-go v0.0.0-20240315130956-036dfa9f3555 h1:RqJZxk2VAaZYCCk4ZVo7iLqp4a03LWitjE0tNIMyvMU=
311+
github.com/posthog/posthog-go v0.0.0-20240315130956-036dfa9f3555/go.mod h1:QjlpryJtfYLrZF2GUkAhejH4E7WlDbdKkvOi5hLmkdg=
308312
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
309313
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
310314
github.com/prometheus/client_golang v1.1.0/go.mod h1:I1FGZT9+L76gKKOs5djB6ezCbFQP1xR9D75/vuwEF3g=

cyclops-ctrl/internal/controller/modules.go

+6
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,28 @@ import (
1313
"github.com/cyclops-ui/cycops-ctrl/internal/mapper"
1414
"github.com/cyclops-ui/cycops-ctrl/internal/models/dto"
1515
"github.com/cyclops-ui/cycops-ctrl/internal/storage/templates"
16+
"github.com/cyclops-ui/cycops-ctrl/internal/telemetry"
1617
"github.com/cyclops-ui/cycops-ctrl/internal/template"
1718
)
1819

1920
type Modules struct {
2021
kubernetesClient *k8sclient.KubernetesClient
2122
templatesRepo *template.Repo
2223
templates *templates.Storage
24+
telemetryClient telemetry.Client
2325
}
2426

2527
func NewModulesController(
2628
templates *templates.Storage,
2729
templatesRepo *template.Repo,
2830
kubernetes *k8sclient.KubernetesClient,
31+
telemetryClient telemetry.Client,
2932
) *Modules {
3033
return &Modules{
3134
kubernetesClient: kubernetes,
3235
templatesRepo: templatesRepo,
3336
templates: templates,
37+
telemetryClient: telemetryClient,
3438
}
3539
}
3640

@@ -211,6 +215,8 @@ func (m *Modules) CreateModule(ctx *gin.Context) {
211215
return
212216
}
213217

218+
m.telemetryClient.ModuleCreation()
219+
214220
err = m.kubernetesClient.CreateModule(module)
215221
if err != nil {
216222
fmt.Println(err)

cyclops-ctrl/internal/handler/handler.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/cyclops-ui/cycops-ctrl/internal/cluster/k8sclient"
99
"github.com/cyclops-ui/cycops-ctrl/internal/controller"
1010
"github.com/cyclops-ui/cycops-ctrl/internal/storage/templates"
11+
"github.com/cyclops-ui/cycops-ctrl/internal/telemetry"
1112
templaterepo "github.com/cyclops-ui/cycops-ctrl/internal/template"
1213
)
1314

@@ -17,17 +18,21 @@ type Handler struct {
1718
templatesRepo *templaterepo.Repo
1819
templatesStorage *templates.Storage
1920
k8sClient *k8sclient.KubernetesClient
21+
22+
telemetryClient telemetry.Client
2023
}
2124

2225
func New(
2326
templates *templates.Storage,
2427
templatesRepo *templaterepo.Repo,
2528
kubernetesClient *k8sclient.KubernetesClient,
29+
telemetryClient telemetry.Client,
2630
) (*Handler, error) {
2731
return &Handler{
2832
templatesRepo: templatesRepo,
2933
templatesStorage: templates,
3034
k8sClient: kubernetesClient,
35+
telemetryClient: telemetryClient,
3136
router: gin.New(),
3237
}, nil
3338
}
@@ -36,7 +41,7 @@ func (h *Handler) Start() error {
3641
gin.SetMode(gin.DebugMode)
3742

3843
templatesController := controller.NewTemplatesController(h.templatesStorage, h.templatesRepo, h.k8sClient)
39-
modulesController := controller.NewModulesController(h.templatesStorage, h.templatesRepo, h.k8sClient)
44+
modulesController := controller.NewModulesController(h.templatesStorage, h.templatesRepo, h.k8sClient, h.telemetryClient)
4045
clusterController := controller.NewClusterController(h.k8sClient)
4146

4247
h.router = gin.New()

cyclops-ctrl/internal/modulecontroller/module_controller.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"github.com/cyclops-ui/cycops-ctrl/internal/cluster/k8sclient"
3535
"github.com/cyclops-ui/cycops-ctrl/internal/models"
3636
"github.com/cyclops-ui/cycops-ctrl/internal/storage/templates"
37+
"github.com/cyclops-ui/cycops-ctrl/internal/telemetry"
3738
templaterepo "github.com/cyclops-ui/cycops-ctrl/internal/template"
3839
)
3940

@@ -46,7 +47,8 @@ type ModuleReconciler struct {
4647
templates *templates.Storage
4748
kubernetesClient *k8sclient.KubernetesClient
4849

49-
logger logr.Logger
50+
telemetryClient telemetry.Client
51+
logger logr.Logger
5052
}
5153

5254
func NewModuleReconciler(
@@ -55,13 +57,15 @@ func NewModuleReconciler(
5557
templatesRepo *templaterepo.Repo,
5658
templates *templates.Storage,
5759
kubernetesClient *k8sclient.KubernetesClient,
60+
telemetryClient telemetry.Client,
5861
) *ModuleReconciler {
5962
return &ModuleReconciler{
6063
Client: client,
6164
Scheme: scheme,
6265
templatesRepo: templatesRepo,
6366
templates: templates,
6467
kubernetesClient: kubernetesClient,
68+
telemetryClient: telemetryClient,
6569
logger: ctrl.Log.WithName("reconciler"),
6670
}
6771
}
@@ -81,6 +85,7 @@ func NewModuleReconciler(
8185
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
8286
func (r *ModuleReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
8387
_ = log.FromContext(ctx)
88+
r.telemetryClient.ModuleReconciliation()
8489

8590
var module cyclopsv1alpha1.Module
8691
err := r.Get(ctx, req.NamespacedName, &module)
+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package telemetry
2+
3+
import (
4+
"github.com/google/uuid"
5+
"github.com/posthog/posthog-go"
6+
)
7+
8+
type Client interface {
9+
ModuleCreation()
10+
ModuleReconciliation()
11+
InstanceStart()
12+
}
13+
14+
type EnqueueClient struct {
15+
client posthog.Client
16+
distinctID string
17+
}
18+
19+
type MockClient struct{}
20+
21+
func NewClient(disable bool) (Client, error) {
22+
if disable {
23+
return MockClient{}, nil
24+
}
25+
26+
client, err := posthog.NewWithConfig(
27+
"phc_1GSZ1j83eWbXITdpYO3u2Epo6ZZ7IimmRsLue7oDx3p",
28+
posthog.Config{
29+
Endpoint: "https://eu.posthog.com",
30+
},
31+
)
32+
if err != nil {
33+
return nil, err
34+
}
35+
36+
id, err := uuid.NewUUID()
37+
if err != nil {
38+
return nil, err
39+
}
40+
41+
return EnqueueClient{
42+
client: client,
43+
distinctID: id.String(),
44+
}, nil
45+
}
46+
47+
func (c EnqueueClient) InstanceStart() {
48+
_ = c.client.Enqueue(posthog.Capture{
49+
Event: "cyclops-instance-start",
50+
DistinctId: c.distinctID,
51+
})
52+
}
53+
54+
func (c EnqueueClient) ModuleReconciliation() {
55+
_ = c.client.Enqueue(posthog.Capture{
56+
Event: "module-reconciliation",
57+
DistinctId: c.distinctID,
58+
})
59+
}
60+
61+
func (c EnqueueClient) ModuleCreation() {
62+
_ = c.client.Enqueue(posthog.Capture{
63+
Event: "module-creation",
64+
DistinctId: c.distinctID,
65+
})
66+
}
67+
68+
func (c MockClient) InstanceStart() {
69+
}
70+
71+
func (c MockClient) ModuleReconciliation() {
72+
}
73+
74+
func (c MockClient) ModuleCreation() {
75+
}

0 commit comments

Comments
 (0)