Skip to content

Commit

Permalink
feat(event): update ua to ga4 analytics
Browse files Browse the repository at this point in the history
Signed-off-by: Abhinandan Purkait <[email protected]>
  • Loading branch information
Abhinandan-Purkait committed Nov 10, 2023
1 parent 7a445cd commit 96704c2
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 201 deletions.
2 changes: 1 addition & 1 deletion buildscripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# limitations under the License.

# This script builds the application from source for multiple platforms.
set -e
set -ex

# Get the parent directory of where this script is.
SOURCE="${BASH_SOURCE[0]}"
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ require (
k8s.io/apimachinery v0.27.2
k8s.io/client-go v11.0.1-0.20190409021438-1a26190bd76a+incompatible
k8s.io/code-generator v0.27.2
k8s.io/klog v0.3.0
k8s.io/klog/v2 v2.100.1
k8s.io/utils v0.0.0-20230505201702-9f6742963106
sigs.k8s.io/controller-runtime v0.2.0
Expand Down
5 changes: 2 additions & 3 deletions pkg/driver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,9 @@ var SupportedVolumeCapabilityAccessModes = []*csi.VolumeCapability_AccessMode{
// sendEventOrIgnore sends anonymous local-pv provision/delete events
func sendEventOrIgnore(pvcName, pvName, capacity, method string) {
if lvm.GoogleAnalyticsEnabled == "true" {
fmt.Println("--------> SENDING ONE METRIC")
analytics.New().CommonBuild().ApplicationBuilder().
SetResourceId(pvName).
// SetCampaignName(pvcName). ??? NEW FIELD NEEDED
SetVolumeName(pvName).
SetVolumeClaimName(pvcName).
SetLabel(analytics.EventLabelCapacity).
SetReplicaCount(analytics.LocalPVReplicaCount, method).
SetCategory(method).
Expand Down
33 changes: 1 addition & 32 deletions pkg/usage/googleanalytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ limitations under the License.
package usage

import (
"fmt"

"k8s.io/klog"
"k8s.io/klog/v2"
)

// Send sends a single usage metric to Google Analytics with some
Expand All @@ -29,40 +27,11 @@ func (u *Usage) Send() {
// Instantiate a Gclient with the tracking ID
go func() {
client := u.AnalyticsClient

fmt.Println("----->", client)

event := u.OpenebsEventBuilder.Build()

fmt.Println("------>", event)

if err := client.Send(event); err != nil {
klog.Errorf(err.Error())
return
}
}()
}

// // Un-wrap the gaClient struct back here
// gaClient, err := analytics.NewClient(u.Gclient.trackID)
// if err != nil {
// return
// }
// gaClient.ClientID(u.clientID).
// CampaignSource(u.campaignSource).
// CampaignContent(u.clientID).
// CampaignName(u.campaignName).
// ApplicationID(u.appID).
// ApplicationVersion(u.appVersion).
// DataSource(u.dataSource).
// ApplicationName(u.appName).
// ApplicationInstallerID(u.appInstallerID).
// DocumentTitle(u.documentTitle)
// // Un-wrap the Event struct back here
// event := analytics.NewEvent(u.category, u.action)
// event.Label(u.label)
// event.Value(u.value)
// if err := gaClient.Send(event); err != nil {
// klog.Errorf(err.Error())
// return
// }
198 changes: 39 additions & 159 deletions pkg/usage/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ import (
// Usage struct represents all information about a usage metric sent to
// Google Analytics with respect to the application
type Usage struct {
//
// OpenebsEventBuilder to build the OpenEBSEvent
OpenebsEventBuilder *ga4Event.OpenebsEventBuilder

//
// GA4 Analytics Client
AnalyticsClient *ga4Client.MeasurementClient
}

Expand All @@ -47,12 +47,13 @@ func New() *Usage {
return &Usage{AnalyticsClient: client, OpenebsEventBuilder: openebsEventBuilder}
}

// SetDocumentTitle : usecase(anonymous-id)
// SetVolumeName i.e pv name
func (u *Usage) SetVolumeName(name string) *Usage {
u.OpenebsEventBuilder.VolumeName(name)
return u
}

// SetVolumeName i.e pvc name
func (u *Usage) SetVolumeClaimName(name string) *Usage {
u.OpenebsEventBuilder.VolumeClaimName(name)
return u
Expand Down Expand Up @@ -82,31 +83,6 @@ func (u *Usage) SetValue(v string) *Usage {
return u
}

// Build is a builder method for Usage struct
func (u *Usage) CommonBuild() *Usage {
v := NewVersion()
_ = v.getVersion(false)

u.OpenebsEventBuilder.
Project(AppName).
EngineInstaller(v.installerType)

return u
}

// ApplicationBuilder Application builder is used for adding k8s&openebs environment detail
// for non install events
func (u *Usage) ApplicationBuilder() *Usage {
v := NewVersion()
_ = v.getVersion(false)

u.AnalyticsClient.SetClientId(v.id)

u.OpenebsEventBuilder.EngineVersion(v.openebsVersion).EngineName(DefaultCASType).EngineInstaller(v.installerType).NodeArch(v.nodeArch).NodeOs(v.nodeOs).NodeKernelVersion(v.nodeKernelVersion)

return u
}

// SetVolumeCapacity sets the storage capacity of the volume for a volume event
func (u *Usage) SetVolumeCapacity(volCapG string) *Usage {
s, _ := toGigaUnits(volCapG)
Expand All @@ -130,6 +106,38 @@ func (u *Usage) SetReplicaCount(count, method string) *Usage {
return u
}

// CommonBuild is a common builder method for Usage struct
func (u *Usage) CommonBuild() *Usage {
v := NewVersion()
_ = v.getVersion(false)

u.OpenebsEventBuilder.
Project(AppName).
EngineInstaller(v.installerType).K8sVersion(v.k8sVersion)

return u
}

// ApplicationBuilder Application builder is used for adding k8s&openebs environment detail
// for non install events
func (u *Usage) ApplicationBuilder() *Usage {
v := NewVersion()
_ = v.getVersion(false)

u.AnalyticsClient.SetClientId(v.id)

u.OpenebsEventBuilder.
EngineVersion(v.openebsVersion).
EngineName(DefaultCASType).
EngineInstaller(v.installerType).
NodeArch(v.nodeArch).
NodeOs(v.nodeOs).
NodeKernelVersion(v.nodeKernelVersion).
K8sDefaultNsUid(v.id)

return u
}

// InstallBuilder is a concrete builder for install events
func (u *Usage) InstallBuilder(override bool) *Usage {
v := NewVersion()
Expand All @@ -145,141 +153,13 @@ func (u *Usage) InstallBuilder(override bool) *Usage {
NodeArch(v.nodeArch).
NodeOs(v.nodeOs).
NodeKernelVersion(v.nodeKernelVersion).
ResourceId(v.id).
VolumeName("").
VolumeClaimName("").
Project(AppName).
Category(InstallEvent).
Action(RunningStatus).
Label(EventLabelNode).
Value(strconv.Itoa(clusterSize))
Value(strconv.Itoa(clusterSize)).K8sDefaultNsUid(v.id)

return u
}

// // NewEvent returns an Event struct with eventCategory, eventAction,
// // eventLabel, eventValue fields
// func (u *Usage) NewEvent(c, a, l string, v int64) *Usage {
// u.category = c
// u.action = a
// u.label = l
// u.value = v
// return u
// }

// // Application struct holds details about the Application
// type Application struct {
// // eg. project version
// appVersion string

// // eg. kubernetes version
// appInstallerID string

// // Name of the application, usage(OpenEBS/NDM)
// appID string

// // eg. usage(os-type/architecture) of system or volume's CASType
// appName string
// }

// // Gclient struct represents a Google Analytics hit
// type Gclient struct {
// // constant tracking-id used to send a hit
// trackID string

// // anonymous client-id
// clientID string

// // anonymous campaign source
// campaignSource string

// // anonymous campaign name
// campaignName string

// // https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#ds
// // (usecase) node-detail
// dataSource string

// // Document-title property in Google Analytics
// // https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#dt
// // use-case: uuid of the volume objects or a uuid to anonymously tell objects apart
// documentTitle string
// }

// // SetDataSource : usage(os-type, kernel)
// func (u *Usage) SetDataSource(dataSource string) *Usage {
// u.dataSource = dataSource
// return u
// }

// // SetTrackingID Sets the GA-code for the project
// func (u *Usage) SetTrackingID(track string) *Usage {
// u.trackID = track
// return u
// }

// // SetCampaignSource : source of openebs installater like:
// // helm or operator etc. This will have to be configured
// // via ENV variable OPENEBS_IO_INSTALLER_TYPE
// func (u *Usage) SetCampaignSource(campaignSrc string) *Usage {
// u.campaignSource = campaignSrc
// return u
// }

// // SetApplicationName : usecase(os-type/arch, volume CASType)
// func (u *Usage) SetApplicationName(appName string) *Usage {
// u.appName = appName
// return u
// }

// SetCampaignName : set the name of the PVC or will be empty.
// func (u *Usage) SetCampaignName(campaignName string) *Usage {
// u.campaignName = campaignName
// return u
// }

// // SetApplicationID : usecase(OpenEBS/NDM)
// func (u *Usage) SetApplicationID(appID string) *Usage {
// u.appID = appID
// return u
// }

// // SetApplicationVersion : usecase(project-version)
// func (u *Usage) SetApplicationVersion(appVersion string) *Usage {
// u.appVersion = appVersion
// return u
// }

// // SetApplicationInstallerID : usecase(k8s-version)
// func (u *Usage) SetApplicationInstallerID(appInstallerID string) *Usage {
// u.appInstallerID = appInstallerID
// return u
// }

// // SetClientID sets the anonymous user id
// func (u *Usage) SetClientID(userID string) *Usage {
// u.clientID = userID
// return u
// }

// u.SetApplicationVersion(v.openebsVersion).
// SetApplicationName(v.k8sArch).
// SetApplicationInstallerID(v.k8sVersion).
// SetDataSource(v.nodeType)

// u.SetApplicationVersion(v.openebsVersion).
// SetApplicationName(v.k8sArch).
// SetApplicationInstallerID(v.k8sVersion).
// SetDataSource(v.nodeType).
// SetDocumentTitle(v.id).
// SetApplicationID(AppName).
// NewEvent(InstallEvent, RunningStatus, EventLabelNode, int64(clusterSize))

// // SetVolumeType Wrapper for setting the default storage-engine for volume-provision event
// func (u *Usage) SetVolumeType(volType, method string) *Usage {
// if method == VolumeProvision && volType == "" {
// // Set the default storage engine, if not specified in the request
// u.SetApplicationName(DefaultCASType)
// } else {
// u.SetApplicationName(volType)
// }
// return u
// }
6 changes: 1 addition & 5 deletions pkg/usage/versionset.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ limitations under the License.
package usage

import (
"strings"

k8sapi "github.com/openebs/lib-csi/pkg/client/k8s"
"github.com/openebs/lib-csi/pkg/common/env"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -74,9 +72,7 @@ func (v *VersionSet) fetchAndSetVersion() error {
// Explicitly informing linters that we intended to avoid errors(errcheck)
_ = env.Set(nodeArch, v.nodeArch)
_ = env.Set(k8sVersion, v.k8sVersion)
info, err := k8sapi.GetOSAndKernelVersion()
v.nodeOs = strings.Split(info, " ")[0]
v.nodeKernelVersion = strings.Split(info, " ")[1]
v.nodeOs, v.nodeKernelVersion, err = k8sapi.GetOSAndKernelVersion()

Check failure on line 75 in pkg/usage/versionset.go

View workflow job for this annotation

GitHub Actions / lint

assignment mismatch: 3 variables but k8sapi.GetOSAndKernelVersion returns 2 values (typecheck)
_ = env.Set(nodeOs, v.nodeOs)
_ = env.Set(nodeKernelVersion, v.nodeKernelVersion)
if err != nil {
Expand Down

0 comments on commit 96704c2

Please sign in to comment.