Skip to content

Commit cf0a565

Browse files
authored
Merge pull request #8 from openebs/name-changes
feat(event): add NodeCount, ReplicaCount, VolumeCapacity event fields Signed-off-by: Niladri Halder <[email protected]>
2 parents ed2bb54 + 5554773 commit cf0a565

File tree

9 files changed

+61
-86
lines changed

9 files changed

+61
-86
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ Create a new `client` and `Send()` an 'event'.
5353
VolumeName("pvc-b3968e30-9020-4011-943a-7ab338d5f19f").
5454
VolumeClaimName("openebs-lvmpv").
5555
Category("volume-deprovision").
56-
Action("replica:2").
57-
Label("Capacity").
58-
Value("2Gi").
56+
NodeCount("3").
5957
Build()
6058

6159
err = client.Send(event)

example/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ func main() {
3030
VolumeName("pvc-b3968e30-9020-4011-943a-7ab338d5f19f").
3131
VolumeClaimName("openebs-lvmpv").
3232
Category("volume_deprovision").
33-
Action("replica").
34-
Label("Capacity").
35-
Value("2").
33+
NodeCount("2").
34+
VolumeCapacity("19238457924875977657").
35+
ReplicaCount("1000").
3636
Build()
3737

3838
err = client.Send(event)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/openebs/google-analytics-4
33
go 1.19
44

55
require (
6-
github.com/docker/go-units v0.5.0
6+
github.com/dustin/go-humanize v1.0.1
77
github.com/openebs/lib-csi v0.8.2
88
k8s.io/apimachinery v0.27.2
99
k8s.io/klog/v2 v2.100.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
1414
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1515
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1616
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
17-
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
18-
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
1917
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
18+
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
19+
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
2020
github.com/emicklei/go-restful/v3 v3.10.2 h1:hIovbnmBTLjHXkqEBUz3HGpXZdM7ZrE9fJIZIqlJLqE=
2121
github.com/emicklei/go-restful/v3 v3.10.2/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
2222
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=

pkg/event/build.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ type OpenebsEvent struct {
4444
VolumeClaimName string `json:"vol_claim_name"`
4545
// Category of event, i.e install, volume-provision
4646
Category string `json:"event_category"`
47-
// Action of the event, i.e running, replica:1
48-
Action string `json:"event_action"`
49-
// Label for the event, i.e nodes, capacity
50-
Label string `json:"event_label"`
51-
// Value for the label, i.e 4, 2
52-
Value string `json:"event_value"`
47+
// NodeCount is the number of kubernetes nodes in the cluster.
48+
NodeCount string `json:"node_count"`
49+
// VolumeCapacity is the size of a volume.
50+
VolumeCapacity string `json:"vol_capacity,omitempty"`
51+
// ReplicaCount is the number of replicas attached to a volume.
52+
ReplicaCount string `json:"vol_replica_count,omitempty"`
5353
}
5454

5555
// OpenebsEventBuilder is builder for OpenebsEvent
@@ -123,18 +123,18 @@ func (b *OpenebsEventBuilder) Category(category string) *OpenebsEventBuilder {
123123
return b
124124
}
125125

126-
func (b *OpenebsEventBuilder) Action(action string) *OpenebsEventBuilder {
127-
b.openebsEvent.Action = action
126+
func (b *OpenebsEventBuilder) NodeCount(nodeCount string) *OpenebsEventBuilder {
127+
b.openebsEvent.NodeCount = nodeCount
128128
return b
129129
}
130130

131-
func (b *OpenebsEventBuilder) Label(label string) *OpenebsEventBuilder {
132-
b.openebsEvent.Label = label
131+
func (b *OpenebsEventBuilder) ReplicaCount(replicaCount string) *OpenebsEventBuilder {
132+
b.openebsEvent.ReplicaCount = replicaCount
133133
return b
134134
}
135135

136-
func (b *OpenebsEventBuilder) Value(value string) *OpenebsEventBuilder {
137-
b.openebsEvent.Value = value
136+
func (b *OpenebsEventBuilder) VolumeCapacity(capacity string) *OpenebsEventBuilder {
137+
b.openebsEvent.VolumeCapacity = capacity
138138
return b
139139
}
140140

usage/ping.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,23 @@ const (
3636
)
3737

3838
// PingCheck sends ping events to Google Analytics
39-
func PingCheck(engineName, category string) {
39+
func PingCheck(engineName, category string, pingImmediately bool) {
4040
// Create a new usage field
4141
u := New()
42+
pingSender := u.CommonBuild(engineName).
43+
InstallBuilder(true).
44+
SetCategory(category)
45+
46+
if pingImmediately {
47+
// Ping immediately.
48+
pingSender.Send()
49+
}
50+
4251
duration := getPingPeriod()
4352
ticker := time.NewTicker(duration)
4453
for range ticker.C {
45-
u.CommonBuild(engineName).
46-
InstallBuilder(true).
47-
SetCategory(category).
48-
Send()
54+
// Ping periodically, starting at 'duration'.
55+
pingSender.Send()
4956
}
5057
}
5158

usage/usage.go

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ package usage
1919
import (
2020
"strconv"
2121

22-
k8sapi "github.com/openebs/lib-csi/pkg/client/k8s"
2322
"k8s.io/klog/v2"
2423

2524
ga4Client "github.com/openebs/google-analytics-4/pkg/client"
2625
ga4Event "github.com/openebs/google-analytics-4/pkg/event"
26+
k8sapi "github.com/openebs/lib-csi/pkg/client/k8s"
2727
)
2828

2929
// Usage struct represents all information about a usage metric sent to
@@ -67,44 +67,22 @@ func (u *Usage) SetCategory(c string) *Usage {
6767
return u
6868
}
6969

70-
// SetAction sets the action of an event
71-
func (u *Usage) SetAction(a string) *Usage {
72-
u.OpenebsEventBuilder.Action(a)
73-
return u
74-
}
75-
76-
// SetLabel sets the label for an event
77-
func (u *Usage) SetLabel(l string) *Usage {
78-
u.OpenebsEventBuilder.Label(l)
70+
// SetNodeCount sets the node count for a k8s cluster.
71+
func (u *Usage) SetNodeCount(n string) *Usage {
72+
u.OpenebsEventBuilder.NodeCount(n)
7973
return u
8074
}
8175

82-
// SetValue sets the value for an event's label
83-
func (u *Usage) SetValue(v string) *Usage {
84-
u.OpenebsEventBuilder.Value(v)
85-
return u
86-
}
87-
88-
// SetVolumeCapacity sets the storage capacity of the volume for a volume event
76+
// SetVolumeCapacity sets the size of a volume.
8977
func (u *Usage) SetVolumeCapacity(volCapG string) *Usage {
90-
s, _ := toGigaUnits(volCapG)
91-
u.SetValue(strconv.FormatInt(s, 10))
78+
s, _ := toHumanSize(volCapG)
79+
u.OpenebsEventBuilder.VolumeCapacity(s)
9280
return u
9381
}
9482

95-
// SetReplicaCount Wrapper for setting replica count for volume events
96-
// NOTE: This doesn't get the replica count in a volume de-provision event.
97-
// TODO: Pick the current value of replica-count from the CAS-engine
98-
func (u *Usage) SetReplicaCount(count, method string) *Usage {
99-
if method == VolumeProvision && count == "" {
100-
// Case: When volume-provision the replica count isn't specified
101-
// it is set to three by default by the m-apiserver
102-
u.OpenebsEventBuilder.Action(DefaultReplicaCount)
103-
} else {
104-
// Catch all case for volume-deprovision event and
105-
// volume-provision event with an overridden replica-count
106-
u.OpenebsEventBuilder.Action(Replica + count)
107-
}
83+
// SetReplicaCount sets the number of replicas for a volume.
84+
func (u *Usage) SetReplicaCount(replicaCount string) *Usage {
85+
u.OpenebsEventBuilder.ReplicaCount(replicaCount)
10886
return u
10987
}
11088

@@ -149,9 +127,7 @@ func (u *Usage) InstallBuilder(override bool) *Usage {
149127
u.OpenebsEventBuilder.
150128
K8sDefaultNsUid(v.id).
151129
Category(InstallEvent).
152-
Action(RunningStatus).
153-
Label(EventLabelNode).
154-
Value(strconv.Itoa(clusterSize))
130+
NodeCount(strconv.Itoa(clusterSize))
155131

156132
return u
157133
}

usage/utils.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ limitations under the License.
1616

1717
package usage
1818

19-
import "github.com/docker/go-units"
19+
import (
20+
"github.com/dustin/go-humanize"
21+
)
2022

21-
// toGigaUnits converts a size from xB to bytes where x={k,m,g,t,p...}
22-
// and return the number of Gigabytes as an integer
23-
// 1 gigabyte=1000 megabyte
24-
func toGigaUnits(size string) (int64, error) {
25-
sizeInBytes, err := units.FromHumanSize(size)
26-
return sizeInBytes / units.GB, err
23+
// toHumanSize converts sizes to legible human sizes in IEC units.
24+
func toHumanSize(size string) (string, error) {
25+
sizeInBytes, err := humanize.ParseBigBytes(size)
26+
return humanize.BigIBytes(sizeInBytes), err
2727
}

usage/utils_test.go

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,44 +18,38 @@ package usage
1818

1919
import "testing"
2020

21-
func TestToGigaUnits(t *testing.T) {
21+
func TestToHumanSize(t *testing.T) {
2222
tests := map[string]struct {
23-
stringSize string
24-
expectedGsize int64
25-
positiveTest bool
23+
stringSize string
24+
expectedSize string
25+
positiveTest bool
2626
}{
27-
"One Hundred Twenty Three thousand Four Hundred Fifty Six Teribytes": {
27+
"One Hundred Twenty Three thousand Four Hundred Fifty Six Tebibytes": {
2828
"123456 TiB",
29-
123456000,
29+
"121 PiB",
3030
true,
3131
},
3232
"One Gibibyte": {
3333
"1 GiB",
34-
1,
34+
"1.0 GiB",
3535
true,
3636
},
3737
"One Megabyte": {
3838
"1 MB",
39-
0, // One cannot express <1GB in integer
39+
"977 KiB",
4040
true,
4141
},
42-
"One Megabyte negative-case": {
43-
"1 MB",
44-
1,
45-
false,
46-
// 1 MB isn't 1 GB
47-
},
4842
"One hundred four point five gigabyte": {
4943
"104.5 GB",
50-
104,
44+
"97 GiB",
5145
true,
5246
},
5347
}
5448

5549
for testKey, testSuite := range tests {
56-
gotValue, err := toGigaUnits(testSuite.stringSize)
57-
if (gotValue != testSuite.expectedGsize || err != nil) && testSuite.positiveTest {
58-
t.Fatalf("Tests failed for %s, expected=%d, got=%d", testKey, testSuite.expectedGsize, gotValue)
50+
gotValue, err := toHumanSize(testSuite.stringSize)
51+
if (gotValue != testSuite.expectedSize || err != nil) && testSuite.positiveTest {
52+
t.Fatalf("Tests failed for %s, expected=%s, got=%s", testKey, testSuite.expectedSize, gotValue)
5953
}
6054
}
6155
}

0 commit comments

Comments
 (0)