Skip to content

Commit 0b8630d

Browse files
authored
Adds Prometheus metrics (#98)
* Added status metrics in integration test * Check for status after waiting time * added electionID labels to metrics * added unit tests to election status metrics * added ballots and shuffling metrics * fix metrics naming * init metrics in integration tests * cannot run integration tests in parallel * cannot test metrics in parallel integration tests * tested shuffling instances * added PubShares metrics * use gauge for ballots counting * remove conflict * dont use dkg status for now * refactor after review * use a gauge for PromElectionPubShares
1 parent 8fbed8b commit 0b8630d

File tree

3 files changed

+92
-11
lines changed

3 files changed

+92
-11
lines changed

contracts/evoting/evoting.go

+22-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

contracts/evoting/mod.go

+40
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package evoting
22

33
import (
4+
dvoting "github.com/dedis/d-voting"
45
"github.com/dedis/d-voting/contracts/evoting/types"
56
"github.com/dedis/d-voting/services/dkg"
7+
"github.com/prometheus/client_golang/prometheus"
68
"go.dedis.ch/dela/core/access"
79
"go.dedis.ch/dela/core/execution"
810
"go.dedis.ch/dela/core/execution/native"
@@ -19,6 +21,36 @@ import (
1921
_ "github.com/dedis/d-voting/contracts/evoting/json"
2022
)
2123

24+
var (
25+
PromElectionStatus = prometheus.NewGaugeVec(prometheus.GaugeOpts{
26+
Name: "dvoting_status",
27+
Help: "status of election",
28+
},
29+
[]string{"election"},
30+
)
31+
32+
PromElectionBallots = prometheus.NewGaugeVec(prometheus.GaugeOpts{
33+
Name: "dvoting_ballots_total",
34+
Help: "number of cast ballots",
35+
},
36+
[]string{"election"},
37+
)
38+
39+
PromElectionShufflingInstances = prometheus.NewGaugeVec(prometheus.GaugeOpts{
40+
Name: "dvoting_shufflings_total",
41+
Help: "number of shuffling instances",
42+
},
43+
[]string{"election"},
44+
)
45+
46+
PromElectionPubShares = prometheus.NewGaugeVec(prometheus.GaugeOpts{
47+
Name: "dvoting_pubshares_total",
48+
Help: "published public shares",
49+
},
50+
[]string{"election"},
51+
)
52+
)
53+
2254
const (
2355
// ElectionsMetadataKey is the key at which election metadata are saved in
2456
// the storage.
@@ -216,3 +248,11 @@ func (c Contract) Execute(snap store.Snapshot, step execution.Step) error {
216248

217249
return nil
218250
}
251+
252+
func init() {
253+
dvoting.PromCollectors = append(dvoting.PromCollectors,
254+
PromElectionStatus,
255+
PromElectionBallots,
256+
PromElectionShufflingInstances,
257+
PromElectionPubShares)
258+
}

contracts/evoting/mod_test.go

+30-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/dedis/d-voting/contracts/evoting/types"
1111
"github.com/dedis/d-voting/internal/testing/fake"
1212
"github.com/dedis/d-voting/services/dkg"
13+
"github.com/prometheus/client_golang/prometheus/testutil"
1314
"github.com/stretchr/testify/require"
1415
"go.dedis.ch/dela/core/access"
1516
"go.dedis.ch/dela/core/execution"
@@ -109,6 +110,8 @@ func TestExecute(t *testing.T) {
109110
}
110111

111112
func TestCommand_CreateElection(t *testing.T) {
113+
initMetrics()
114+
112115
fakeActor := fakeDkgActor{
113116
publicKey: suite.Point(),
114117
err: nil,
@@ -167,13 +170,16 @@ func TestCommand_CreateElection(t *testing.T) {
167170
require.True(t, ok)
168171

169172
require.Equal(t, types.Initial, election.Status)
173+
require.Equal(t, float64(types.Initial), testutil.ToFloat64(PromElectionStatus))
170174
}
171175

172176
func TestCommand_OpenElection(t *testing.T) {
173177
// TODO
174178
}
175179

176180
func TestCommand_CastVote(t *testing.T) {
181+
initMetrics()
182+
177183
castVote := types.CastVote{
178184
ElectionID: fakeElectionID,
179185
UserID: "dummyUserId",
@@ -298,11 +304,13 @@ func TestCommand_CastVote(t *testing.T) {
298304
require.Len(t, election.Suffragia.Ciphervotes, 1)
299305
require.True(t, castVote.Ballot.Equal(election.Suffragia.Ciphervotes[0]))
300306

301-
require.Equal(t, castVote.UserID,
302-
election.Suffragia.UserIDs[0])
307+
require.Equal(t, castVote.UserID, election.Suffragia.UserIDs[0])
308+
require.Equal(t, float64(len(election.Suffragia.Ciphervotes)), testutil.ToFloat64(PromElectionBallots))
303309
}
304310

305311
func TestCommand_CloseElection(t *testing.T) {
312+
initMetrics()
313+
306314
closeElection := types.CloseElection{
307315
ElectionID: fakeElectionID,
308316
UserID: "dummyUserId",
@@ -349,6 +357,7 @@ func TestCommand_CloseElection(t *testing.T) {
349357
err = cmd.closeElection(snap, makeStep(t, ElectionArg, string(data)))
350358
require.EqualError(t, err, fmt.Sprintf("the election is not open, "+
351359
"current status: %d", types.Initial))
360+
require.Equal(t, 0, testutil.CollectAndCount(PromElectionStatus))
352361

353362
dummyElection.Status = types.Open
354363

@@ -372,6 +381,7 @@ func TestCommand_CloseElection(t *testing.T) {
372381

373382
err = cmd.closeElection(snap, makeStep(t, ElectionArg, string(data)))
374383
require.NoError(t, err)
384+
require.Equal(t, float64(types.Closed), testutil.ToFloat64(PromElectionStatus))
375385

376386
res, err := snap.Get(dummyElectionIDBuff)
377387
require.NoError(t, err)
@@ -383,6 +393,7 @@ func TestCommand_CloseElection(t *testing.T) {
383393
require.True(t, ok)
384394

385395
require.Equal(t, types.Closed, election.Status)
396+
require.Equal(t, float64(types.Closed), testutil.ToFloat64(PromElectionStatus))
386397
}
387398

388399
func TestCommand_ShuffleBallotsCannotShuffleTwice(t *testing.T) {
@@ -429,6 +440,8 @@ func TestCommand_ShuffleBallotsCannotShuffleTwice(t *testing.T) {
429440
}
430441

431442
func TestCommand_ShuffleBallotsValidScenarios(t *testing.T) {
443+
initMetrics()
444+
432445
k := 3
433446

434447
// Simple Shuffle from round 0 :
@@ -454,6 +467,7 @@ func TestCommand_ShuffleBallotsValidScenarios(t *testing.T) {
454467

455468
err = cmd.shuffleBallots(snap, step)
456469
require.NoError(t, err)
470+
require.Equal(t, float64(1), testutil.ToFloat64(PromElectionShufflingInstances))
457471

458472
// Valid Shuffle is over :
459473
shuffleBallots.Round = k
@@ -483,6 +497,7 @@ func TestCommand_ShuffleBallotsValidScenarios(t *testing.T) {
483497

484498
err = cmd.shuffleBallots(snap, makeStep(t, ElectionArg, string(data)))
485499
require.NoError(t, err)
500+
require.Equal(t, float64(k+1), testutil.ToFloat64(PromElectionShufflingInstances))
486501

487502
// Check the shuffle is over:
488503
electionTxIDBuff, err := hex.DecodeString(election.ElectionID)
@@ -497,7 +512,8 @@ func TestCommand_ShuffleBallotsValidScenarios(t *testing.T) {
497512
election, ok := message.(types.Election)
498513
require.True(t, ok)
499514

500-
require.Equal(t, election.Status, types.ShuffledBallots)
515+
require.Equal(t, types.ShuffledBallots, election.Status)
516+
require.Equal(t, float64(types.ShuffledBallots), testutil.ToFloat64(PromElectionStatus))
501517
}
502518

503519
func TestCommand_ShuffleBallotsFormatErrors(t *testing.T) {
@@ -867,6 +883,7 @@ func TestCommand_RegisterPubShares(t *testing.T) {
867883

868884
err = cmd.registerPubshares(snap, makeStep(t, ElectionArg, string(data)))
869885
require.NoError(t, err)
886+
require.Equal(t, float64(1), testutil.ToFloat64(PromElectionPubShares))
870887

871888
// With the public key already used:
872889
election.PubsharesUnits.PubKeys = append(election.PubsharesUnits.PubKeys,
@@ -907,6 +924,7 @@ func TestCommand_RegisterPubShares(t *testing.T) {
907924

908925
err = cmd.registerPubshares(snap, makeStep(t, ElectionArg, string(data)))
909926
require.NoError(t, err)
927+
require.Equal(t, float64(1), testutil.ToFloat64(PromElectionPubShares))
910928

911929
res, err := snap.Get(dummyElectionIDBuff)
912930
require.NoError(t, err)
@@ -1019,7 +1037,7 @@ func TestCommand_DecryptBallots(t *testing.T) {
10191037

10201038
require.Equal(t, types.Ballot{}, election.DecryptedBallots[0])
10211039
require.Equal(t, types.ResultAvailable, election.Status)
1022-
1040+
require.Equal(t, float64(types.ResultAvailable), testutil.ToFloat64(PromElectionStatus))
10231041
}
10241042

10251043
func TestCommand_CancelElection(t *testing.T) {
@@ -1079,7 +1097,7 @@ func TestCommand_CancelElection(t *testing.T) {
10791097
require.True(t, ok)
10801098

10811099
require.Equal(t, types.Canceled, election.Status)
1082-
1100+
require.Equal(t, float64(types.Canceled), testutil.ToFloat64(PromElectionStatus))
10831101
}
10841102

10851103
func TestRegisterContract(t *testing.T) {
@@ -1089,6 +1107,13 @@ func TestRegisterContract(t *testing.T) {
10891107
// -----------------------------------------------------------------------------
10901108
// Utility functions
10911109

1110+
func initMetrics() {
1111+
PromElectionStatus.Reset()
1112+
PromElectionBallots.Reset()
1113+
PromElectionShufflingInstances.Reset()
1114+
PromElectionPubShares.Reset()
1115+
}
1116+
10921117
func initElectionAndContract() (types.Election, Contract) {
10931118
fakeDkg := fakeDKG{
10941119
actor: fakeDkgActor{},

0 commit comments

Comments
 (0)