From 8c1ad479780734d8c0c7a2973d6fc2cd71c93afa Mon Sep 17 00:00:00 2001 From: Abhinandan Purkait Date: Thu, 9 Nov 2023 08:53:19 +0000 Subject: [PATCH] feat(event): update ua to ga4 analytics Signed-off-by: Abhinandan Purkait --- client/build.go | 4 ++ client/client.go | 5 ++ event/build.go | 147 ++++++++++++++++++++++++++++++++--------------- event/event.go | 7 +++ example/main.go | 34 ++++++----- payload/build.go | 10 ++-- 6 files changed, 142 insertions(+), 65 deletions(-) diff --git a/client/build.go b/client/build.go index c7c31eb..a7723c0 100644 --- a/client/build.go +++ b/client/build.go @@ -85,3 +85,7 @@ func WithClientId(clientId string) MeasurementClientOption { return nil } } + +func (client *MeasurementClient) SetClientId(clientId string) { + client.clientId = clientId +} diff --git a/client/client.go b/client/client.go index b670bd1..718b096 100644 --- a/client/client.go +++ b/client/client.go @@ -3,6 +3,7 @@ package client import ( "bytes" "encoding/json" + "fmt" "net/http" "net/url" @@ -39,6 +40,8 @@ func (c *MeasurementClient) Send(event *event.OpenebsEvent) error { return err } + fmt.Println(string(jsonData)) + gaUrl := "https://www.google-analytics.com/mp/collect" req, err := http.NewRequest("POST", gaUrl, bytes.NewReader(jsonData)) @@ -48,6 +51,8 @@ func (c *MeasurementClient) Send(event *event.OpenebsEvent) error { req.Header.Set("Content-Type", "application/json") + fmt.Println(req.URL) + resp, err := client.HttpClient.Do(req) if err != nil { return err diff --git a/event/build.go b/event/build.go index e890264..4b03e54 100644 --- a/event/build.go +++ b/event/build.go @@ -1,69 +1,122 @@ package event -import ( - "github.com/openebs/lib-csi/pkg/common/errors" -) - type OpenebsEventOption func(*OpenebsEvent) error // OpenebsEvent Hit Type type OpenebsEvent struct { - Category string `json:"category"` - Action string `json:"action"` - Label string `json:"label"` - Value int64 `json:"value"` + // Our Application Org, example OpenEBS + Project string `json:"project"` + // K8s Version, example v1.25.15 + K8sVersion string `json:"k8s_version"` + // Name of the engine,example lvm-localpv + EngineName string `json:"engine_name"` + // Version of the engine, example lvm-v1.3.x-e927123:11-08-2023-e927123 + EngineVersion string `json:"engine_version"` + // Uid of the default k8s ns, to uniquely identify, example f5d2a546-19ce-407d-99d4-0655d67e2f76 + K8sDefaultNsUid string `json:"k8s_default_ns_uid"` + // Installer of the app, example lvm-localpv-helm + EngineInstaller string `json:"engine_installer"` + // Machine's os example Ubuntu 20.04.6 LTS, 5.4.0-165-generic, x86_64 + NodeOs string `json:"node_os"` + // Machine's kernel version, example 5.4.0-165-generic + NodeKernelVersion string `json:"node_kernel_version"` + // Machine's arch, example x86_64 + NodeArch string `json:"node_arch"` + // Id of the involved object, example `pvc-b3968e30-9020-4011-943a-7ab338d5f19f` + VolumeName string `json:"vol_name"` + // Extra detail of the involved object, example `lvm-vol-new` + VolumeClaimName string `json:"vol_claim_name"` + // Category of event, i.e install, volume-provision + Category string `json:"event_category"` + // Action of the event, i.e running, replica:1 + Action string `json:"event_action"` + // Label for the event, i.e nodes, capacity + Label string `json:"event_category"` + // Value for the event, i.e 4, 2 + Value string `json:"event_action"` +} + +// OpenebsEventBuilder builder pattern code +type OpenebsEventBuilder struct { + openebsEvent *OpenebsEvent +} + +func NewOpenebsEventBuilder() *OpenebsEventBuilder { + openebsEvent := &OpenebsEvent{} + b := &OpenebsEventBuilder{openebsEvent: openebsEvent} + return b +} + +func (b *OpenebsEventBuilder) Project(project string) *OpenebsEventBuilder { + b.openebsEvent.Project = project + return b +} + +func (b *OpenebsEventBuilder) K8sVersion(k8sVersion string) *OpenebsEventBuilder { + b.openebsEvent.K8sVersion = k8sVersion + return b +} + +func (b *OpenebsEventBuilder) EngineName(engineName string) *OpenebsEventBuilder { + b.openebsEvent.EngineName = engineName + return b +} + +func (b *OpenebsEventBuilder) EngineVersion(engineVersion string) *OpenebsEventBuilder { + b.openebsEvent.EngineVersion = engineVersion + return b +} + +func (b *OpenebsEventBuilder) K8sDefaultNsUid(k8sDefaultNsUid string) *OpenebsEventBuilder { + b.openebsEvent.K8sDefaultNsUid = k8sDefaultNsUid + return b } -func NewOpenebsEvent(opts ...OpenebsEventOption) (*OpenebsEvent, error) { - e := &OpenebsEvent{} +func (b *OpenebsEventBuilder) EngineInstaller(engineInstaller string) *OpenebsEventBuilder { + b.openebsEvent.EngineInstaller = engineInstaller + return b +} - var err error - for _, opt := range opts { - err = opt(e) - if err != nil { - return nil, errors.Wrap(err, "failed to build OpenebsEvent") - } - } +func (b *OpenebsEventBuilder) NodeOs(nodeOs string) *OpenebsEventBuilder { + b.openebsEvent.NodeOs = nodeOs + return b +} - return e, nil +func (b *OpenebsEventBuilder) NodeKernelVersion(nodeKernelVersion string) *OpenebsEventBuilder { + b.openebsEvent.NodeKernelVersion = nodeKernelVersion + return b } -func WithCategory(category string) OpenebsEventOption { - return func(e *OpenebsEvent) error { - if len(category) == 0 { - return errors.Errorf("failed to set OpenebsEvent category: category is an empty string") - } +func (b *OpenebsEventBuilder) NodeArch(nodeArch string) *OpenebsEventBuilder { + b.openebsEvent.NodeArch = nodeArch + return b +} - e.Category = category - return nil - } +func (b *OpenebsEventBuilder) ResourceId(resourceId string) *OpenebsEventBuilder { + b.openebsEvent.ResourceId = resourceId + return b } -func WithAction(action string) OpenebsEventOption { - return func(e *OpenebsEvent) error { - if len(action) == 0 { - return errors.Errorf("failed to set OpenebsEvent action: action is an empty string") - } +func (b *OpenebsEventBuilder) Category(category string) *OpenebsEventBuilder { + b.openebsEvent.Category = category + return b +} - e.Action = action - return nil - } +func (b *OpenebsEventBuilder) Action(action string) *OpenebsEventBuilder { + b.openebsEvent.Action = action + return b } -func WithLabel(label string) OpenebsEventOption { - return func(e *OpenebsEvent) error { - if len(label) == 0 { - return errors.Errorf("failed to set OpenebsEvent label: label is an empty string") - } +func (b *OpenebsEventBuilder) Label(label string) *OpenebsEventBuilder { + b.openebsEvent.Label = label + return b +} - e.Label = label - return nil - } +func (b *OpenebsEventBuilder) Value(value string) *OpenebsEventBuilder { + b.openebsEvent.Value = value + return b } -func WithValue(value int64) OpenebsEventOption { - return func(e *OpenebsEvent) error { - e.Value = value - return nil - } +func (b *OpenebsEventBuilder) Build() *OpenebsEvent { + return b.openebsEvent } diff --git a/event/event.go b/event/event.go index 5fc4f51..94b35dc 100644 --- a/event/event.go +++ b/event/event.go @@ -1,9 +1,16 @@ package event +import "strings" + func (e *OpenebsEvent) CategoryStr() string { return e.Category } +// Replace the `-` with `_` in the Category +func (e *OpenebsEvent) NormalizedCategoryStr() string { + return strings.ReplaceAll(e.CategoryStr(), "-", "_") +} + func (e *OpenebsEvent) ActionStr() string { return e.Action } diff --git a/example/main.go b/example/main.go index 507179f..8351e44 100644 --- a/example/main.go +++ b/example/main.go @@ -1,34 +1,42 @@ package main import ( + "fmt" gaClient "github.com/openebs/go-ogle-analytics/client" gaEvent "github.com/openebs/go-ogle-analytics/event" ) func main() { client, err := gaClient.NewMeasurementClient( - gaClient.WithApiSecret("NguBiGh6QeOdeG3zJswggQ"), - gaClient.WithMeasurementId("G-TZGP46618W"), - gaClient.WithClientId("uniqueUserId-000000001"), + gaClient.WithApiSecret(""), + gaClient.WithMeasurementId(""), + gaClient.WithClientId(""), ) if err != nil { panic(err) } - event, err := gaEvent.NewOpenebsEvent( - gaEvent.WithCategory("Foo"), - gaEvent.WithAction("Bar"), - gaEvent.WithLabel("Baz"), - gaEvent.WithValue(19072023), - ) - if err != nil { - panic(err) - } + event := gaEvent.NewOpenebsEventBuilder(). + Project("OpenEBS"). + K8sVersion("v1.25.15"). + EngineName("test-engine"). + EngineVersion("v1.0.0"). + K8sDefaultNsUid("f5d2a546-19ce-407d-99d4-0655d67e2f76"). + EngineInstaller("helm"). + NodeOs("Ubuntu 20.04.6 LTS"). + NodeArch("x86_64"). + NodeKernelVersion(" 5.4.0-165-generic"). + ResourceId("pvc-b3968e30-9020-4011-943a-7ab338d5f19f"). + Category("volume-deprovision"). + Action("replica:2"). + Label("Capacity"). + Value("2Gi"). + Build() err = client.Send(event) if err != nil { panic(err) } - println("Event fired!") + fmt.Println("Event fired!") } diff --git a/payload/build.go b/payload/build.go index 16afff8..07ca732 100644 --- a/payload/build.go +++ b/payload/build.go @@ -1,16 +1,16 @@ package payload import ( - "github.com/openebs/lib-csi/pkg/common/errors" - "github.com/openebs/go-ogle-analytics/event" + "github.com/openebs/lib-csi/pkg/common/errors" ) type PayloadOption func(*Payload) error type Payload struct { - ClientId string `json:"client_id"` - Events []ApiEvent `json:"events"` + ClientId string `json:"client_id"` + + Events []ApiEvent `json:"events"` } type ApiEvent struct { @@ -46,7 +46,7 @@ func WithClientId(clientId string) PayloadOption { func WithOpenebsEvent(event *event.OpenebsEvent) PayloadOption { return func(p *Payload) error { p.Events = append(p.Events, ApiEvent{ - Name: event.CategoryStr() + "_" + event.ActionStr(), + Name: event.NormalizedCategoryStr(), Params: *event, }) return nil