/
usage.go
171 lines (142 loc) · 4.57 KB
/
usage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
Copyright 2023 The OpenEBS Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package usage
import (
"strconv"
k8sapi "github.com/openebs/lib-csi/pkg/client/k8s"
"k8s.io/klog/v2"
ga4Client "github.com/openebs/google-analytics-4/pkg/client"
ga4Event "github.com/openebs/google-analytics-4/pkg/event"
)
// 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
}
// New returns an instance of Usage
func New() *Usage {
client, err := ga4Client.NewMeasurementClient(
ga4Client.WithApiSecret(ApiSecret),
ga4Client.WithMeasurementId(MeasurementId),
)
if err != nil {
return nil
}
openebsEventBuilder := ga4Event.NewOpenebsEventBuilder()
return &Usage{AnalyticsClient: client, OpenebsEventBuilder: openebsEventBuilder}
}
// SetVolumeName i.e pv name
func (u *Usage) SetVolumeName(name string) *Usage {
u.OpenebsEventBuilder.VolumeName(name)
return u
}
// SetVolumeClaimName i.e pvc name
func (u *Usage) SetVolumeClaimName(name string) *Usage {
u.OpenebsEventBuilder.VolumeClaimName(name)
return u
}
// SetCategory sets the category of an event
func (u *Usage) SetCategory(c string) *Usage {
u.OpenebsEventBuilder.Category(c)
return u
}
// SetAction sets the action of an event
func (u *Usage) SetAction(a string) *Usage {
u.OpenebsEventBuilder.Action(a)
return u
}
// SetLabel sets the label for an event
func (u *Usage) SetLabel(l string) *Usage {
u.OpenebsEventBuilder.Label(l)
return u
}
// SetValue sets the value for an event's label
func (u *Usage) SetValue(v string) *Usage {
u.OpenebsEventBuilder.Value(v)
return u
}
// SetVolumeCapacity sets the storage capacity of the volume for a volume event
func (u *Usage) SetVolumeCapacity(volCapG string) *Usage {
s, _ := toGigaUnits(volCapG)
u.SetValue(strconv.FormatInt(s, 10))
return u
}
// SetReplicaCount Wrapper for setting replica count for volume events
// NOTE: This doesn't get the replica count in a volume de-provision event.
// TODO: Pick the current value of replica-count from the CAS-engine
func (u *Usage) SetReplicaCount(count, method string) *Usage {
if method == VolumeProvision && count == "" {
// Case: When volume-provision the replica count isn't specified
// it is set to three by default by the m-apiserver
u.OpenebsEventBuilder.Action(DefaultReplicaCount)
} else {
// Catch all case for volume-deprovision event and
// volume-provision event with an overridden replica-count
u.OpenebsEventBuilder.Action(Replica + count)
}
return u
}
// CommonBuild is a common builder method for Usage struct
func (u *Usage) CommonBuild(engineName string) *Usage {
v := NewVersion()
_ = v.getVersion(false)
u.OpenebsEventBuilder.
Project(AppName).
EngineInstaller(v.installerType).
K8sVersion(v.k8sVersion).
EngineVersion(v.openebsVersion).
EngineInstaller(v.installerType).
EngineName(engineName).
NodeArch(v.nodeArch).
NodeOs(v.nodeOs).
NodeKernelVersion(v.nodeKernelVersion)
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.K8sDefaultNsUid(v.id)
return u
}
// InstallBuilder is a concrete builder for install events
func (u *Usage) InstallBuilder(override bool) *Usage {
v := NewVersion()
clusterSize, _ := k8sapi.NumberOfNodes()
_ = v.getVersion(override)
u.AnalyticsClient.SetClientId(v.id)
u.OpenebsEventBuilder.
K8sDefaultNsUid(v.id).
Category(InstallEvent).
Action(RunningStatus).
Label(EventLabelNode).
Value(strconv.Itoa(clusterSize))
return u
}
// Send POSTS an event over to the GA4 API
func (u *Usage) Send() {
// Instantiate an analytics client
go func() {
client := u.AnalyticsClient
event := u.OpenebsEventBuilder.Build()
if err := client.Send(event); err != nil {
klog.Errorf(err.Error())
return
}
}()
}