From b8c3d30b46cf0d1fd678d8b3919f04a19dc47901 Mon Sep 17 00:00:00 2001 From: Abhinandan Purkait Date: Fri, 10 Nov 2023 06:45:17 +0000 Subject: [PATCH] feat(event): update ua to ga4 analytics Signed-off-by: Abhinandan Purkait --- README.md | 66 +++++++++++--------- client/build.go | 4 ++ event/build.go | 152 ++++++++++++++++++++++++++++++++--------------- event/event.go | 4 +- example/main.go | 36 +++++++---- payload/build.go | 5 +- payload/utils.go | 10 ++++ 7 files changed, 184 insertions(+), 93 deletions(-) create mode 100644 payload/utils.go diff --git a/README.md b/README.md index 4c90fc4..b960f11 100644 --- a/README.md +++ b/README.md @@ -23,39 +23,49 @@ Create a new `client` and `Send()` an 'event'. ``` go package main - import ( - gaClient "github.com/openebs/go-ogle-analytics/client" - gaEvent "github.com/openebs/go-ogle-analytics/event" - ) - - func main() { - client, err := gaClient.NewMeasurementClient( - gaClient.WithApiSecret("yourApiSecret"), - gaClient.WithMeasurementId("G-yourMeasurementClient"), - gaClient.WithClientId("uniqueUserId-000000001"), - ) - if err != nil { - panic(err) - } + import ( + "fmt" - event, err := gaEvent.NewOpenebsEvent( - gaEvent.WithCategory("Foo"), - gaEvent.WithAction("Bar"), - gaEvent.WithLabel("Baz"), - gaEvent.WithValue(19072023), + gaClient "github.com/openebs/go-ogle-analytics/client" + gaEvent "github.com/openebs/go-ogle-analytics/event" ) - if err != nil { - panic(err) - } - err = client.Send(event) - if err != nil { - panic(err) + func main() { + client, err := gaClient.NewMeasurementClient( + gaClient.WithApiSecret(""), + gaClient.WithMeasurementId(""), + gaClient.WithClientId(""), + ) + 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("linux/amd64"). + NodeKernelVersion("5.4.0-165-generic"). + VolumeName("pvc-b3968e30-9020-4011-943a-7ab338d5f19f"). + VolumeClaimName("openebs-lvmpv"). + Category("volume-deprovision"). + Action("replica:2"). + Label("Capacity"). + Value("2Gi"). + Build() + + err = client.Send(event) + if err != nil { + panic(err) + } + + fmt.Println("Event fired!") } - println("Event fired!") - } - ``` 3. In GA, go to Report > Realtime 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/event/build.go b/event/build.go index e890264..b961c16 100644 --- a/event/build.go +++ b/event/build.go @@ -1,69 +1,127 @@ 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"` + // Specify Project name, ex OpenEBS + Project string `json:"project"` + // K8s Version, ex v1.25.15 + K8sVersion string `json:"k8s_version"` + // Name of the engine, ex lvm-localpv + EngineName string `json:"engine_name"` + // Version of the engine, ex lvm-v1.3.0-e927123:11-08-2023-e927123 + EngineVersion string `json:"engine_version"` + // Uid of the default k8s ns, ex f5d2a546-19ce-407d-99d4-0655d67e2f76 + K8sDefaultNsUid string `json:"k8s_default_ns_uid"` + // Installer of the app, ex lvm-localpv-helm + EngineInstaller string `json:"engine_installer"` + // Machine's os, ex Ubuntu 20.04.6 LTS + NodeOs string `json:"node_os"` + // Machine's kernel version, ex 5.4.0-165-generic + NodeKernelVersion string `json:"node_kernel_version"` + // Machine's arch, ex linux/amd64 + NodeArch string `json:"node_arch"` + // Name of the pv object, example `pvc-b3968e30-9020-4011-943a-7ab338d5f19f` + VolumeName string `json:"vol_name"` + // Name of the pvc object, example `openebs-lvmpv` + 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_label"` + // Value for the label, i.e 4, 2 + Value string `json:"event_value"` +} + +// OpenebsEventBuilder is builder for OpenebsEvent +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 NewOpenebsEvent(opts ...OpenebsEventOption) (*OpenebsEvent, error) { - e := &OpenebsEvent{} +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 +} - 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) EngineInstaller(engineInstaller string) *OpenebsEventBuilder { + b.openebsEvent.EngineInstaller = engineInstaller + return b +} - return e, nil +func (b *OpenebsEventBuilder) NodeOs(nodeOs string) *OpenebsEventBuilder { + b.openebsEvent.NodeOs = nodeOs + 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) NodeKernelVersion(nodeKernelVersion string) *OpenebsEventBuilder { + b.openebsEvent.NodeKernelVersion = nodeKernelVersion + return b +} - e.Category = category - return nil - } +func (b *OpenebsEventBuilder) NodeArch(nodeArch string) *OpenebsEventBuilder { + b.openebsEvent.NodeArch = nodeArch + 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) VolumeName(volumeName string) *OpenebsEventBuilder { + b.openebsEvent.VolumeName = volumeName + return b +} - e.Action = action - return nil - } +func (b *OpenebsEventBuilder) VolumeClaimName(volumeClaimName string) *OpenebsEventBuilder { + b.openebsEvent.VolumeClaimName = volumeClaimName + 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) Category(category string) *OpenebsEventBuilder { + b.openebsEvent.Category = category + return b +} + +func (b *OpenebsEventBuilder) Action(action string) *OpenebsEventBuilder { + b.openebsEvent.Action = action + return b +} + +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..4bf3a91 100644 --- a/event/event.go +++ b/event/event.go @@ -4,6 +4,6 @@ func (e *OpenebsEvent) CategoryStr() string { return e.Category } -func (e *OpenebsEvent) ActionStr() string { - return e.Action +func (e *OpenebsEvent) EngineNameStr() string { + return e.EngineName } diff --git a/example/main.go b/example/main.go index 507179f..86beb2f 100644 --- a/example/main.go +++ b/example/main.go @@ -1,34 +1,44 @@ 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("linux/amd64"). + NodeKernelVersion("5.4.0-165-generic"). + VolumeName("pvc-b3968e30-9020-4011-943a-7ab338d5f19f"). + VolumeClaimName("openebs-lvmpv"). + 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..eabf87f 100644 --- a/payload/build.go +++ b/payload/build.go @@ -1,9 +1,8 @@ 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 @@ -46,7 +45,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: NormalizedEventName(event.EngineNameStr() + "-" + event.CategoryStr()), Params: *event, }) return nil diff --git a/payload/utils.go b/payload/utils.go new file mode 100644 index 0000000..0eaec83 --- /dev/null +++ b/payload/utils.go @@ -0,0 +1,10 @@ +package payload + +import ( + "strings" +) + +// Replace the `-` with `_` from the name +func NormalizedEventName(name string) string { + return strings.ReplaceAll(name, "-", "_") +}