Skip to content

Commit e459f37

Browse files
committed
test dkg status metrics
1 parent d49a531 commit e459f37

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

contracts/evoting/mod.go

+7
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ var (
4949
},
5050
[]string{"election"},
5151
)
52+
53+
PromElectionDkgStatus = prometheus.NewGaugeVec(prometheus.GaugeOpts{
54+
Name: "dvoting_dkg_status",
55+
Help: "status of distributed key generator",
56+
},
57+
[]string{"election"},
58+
)
5259
)
5360

5461
const (

services/dkg/pedersen/mod.go

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"go.dedis.ch/dela"
1313
"go.dedis.ch/dela/core/ordering"
1414

15+
"github.com/dedis/d-voting/contracts/evoting"
1516
etypes "github.com/dedis/d-voting/contracts/evoting/types"
1617

1718
"github.com/dedis/d-voting/internal/tracing"
@@ -134,6 +135,8 @@ func (s *Pedersen) NewActor(electionIDBuf []byte, pool pool.Pool, txmngr txn.Man
134135
status: dkg.Status{Status: dkg.Initialized},
135136
}
136137

138+
evoting.PromElectionDkgStatus.WithLabelValues(electionID).Set(float64(dkg.Initialized))
139+
137140
s.Lock()
138141
defer s.Unlock()
139142
s.actors[electionID] = a
@@ -169,6 +172,8 @@ func (a *Actor) setErr(err error, args map[string]interface{}) {
169172
Err: err,
170173
Args: args,
171174
}
175+
176+
evoting.PromElectionDkgStatus.WithLabelValues(a.electionID).Set(float64(dkg.Failed))
172177
}
173178

174179
// Setup implements dkg.Actor. It initializes the DKG protocol across all
@@ -297,6 +302,7 @@ func (a *Actor) Setup() (kyber.Point, error) {
297302
}
298303

299304
a.status = dkg.Status{Status: dkg.Setup}
305+
evoting.PromElectionDkgStatus.WithLabelValues(a.electionID).Set(float64(dkg.Setup))
300306

301307
return dkgPubKeys[0], nil
302308
}

services/dkg/pedersen/mod_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ import (
1414
"go.dedis.ch/dela/core/validation"
1515
"golang.org/x/xerrors"
1616

17+
"github.com/dedis/d-voting/contracts/evoting"
1718
etypes "github.com/dedis/d-voting/contracts/evoting/types"
1819
"github.com/dedis/d-voting/internal/testing/fake"
1920
"github.com/dedis/d-voting/services/dkg"
2021
"github.com/dedis/d-voting/services/dkg/pedersen/types"
22+
"github.com/prometheus/client_golang/prometheus/testutil"
2123
"github.com/stretchr/testify/require"
2224
"go.dedis.ch/dela/core/ordering/cosipbft/authority"
2325
"go.dedis.ch/dela/core/store/kv"
@@ -52,6 +54,7 @@ func TestActor_MarshalJSON(t *testing.T) {
5254
actor1, err := p.NewActor([]byte("deadbeef"), &fake.Pool{},
5355
fake.Manager{}, NewHandlerData())
5456
require.NoError(t, err)
57+
require.Equal(t, float64(dkg.Initialized), testutil.ToFloat64(evoting.PromElectionDkgStatus))
5558

5659
// Serialize its persistent data
5760
actor1Buf, err := actor1.MarshalJSON()
@@ -62,8 +65,11 @@ func TestActor_MarshalJSON(t *testing.T) {
6265
err = handlerData.UnmarshalJSON(actor1Buf)
6366
require.NoError(t, err)
6467

68+
initMetrics()
69+
6570
actor2, err := p.NewActor([]byte("beefdead"), &fake.Pool{}, fake.Manager{}, handlerData)
6671
require.NoError(t, err)
72+
require.Equal(t, float64(dkg.Initialized), testutil.ToFloat64(evoting.PromElectionDkgStatus))
6773

6874
// Check that the persistent data is the same for both actors
6975
requireActorsEqual(t, actor1, actor2)
@@ -72,6 +78,8 @@ func TestActor_MarshalJSON(t *testing.T) {
7278
// After initializing a Pedersen when dkgMap is not empty, the actors map should
7379
// contain the same information as dkgMap
7480
func TestPedersen_InitNonEmptyMap(t *testing.T) {
81+
initMetrics()
82+
7583
// Create a new DKG map and fill it with data
7684
dkgMap := fake.NewInMemoryDB()
7785

@@ -141,9 +149,14 @@ func TestPedersen_InitNonEmptyMap(t *testing.T) {
141149

142150
_, err = p.NewActor(electionIDBuf, &fake.Pool{}, fake.Manager{}, handlerData)
143151
if err != nil {
152+
require.Equal(t, float64(dkg.Failed), testutil.ToFloat64(evoting.PromElectionDkgStatus))
144153
return err
154+
} else {
155+
require.Equal(t, float64(dkg.Initialized), testutil.ToFloat64(evoting.PromElectionDkgStatus))
145156
}
146157

158+
initMetrics()
159+
147160
return nil
148161
})
149162
})
@@ -301,6 +314,8 @@ func TestPedersen_TwoListens(t *testing.T) {
301314
}
302315

303316
func TestPedersen_Setup(t *testing.T) {
317+
initMetrics()
318+
304319
electionID := "d3adbeef"
305320

306321
service := fake.NewService(electionID, etypes.Election{
@@ -325,6 +340,9 @@ func TestPedersen_Setup(t *testing.T) {
325340

326341
_, err := actor.Setup()
327342
require.EqualError(t, err, "failed to get election: election does not exist: <nil>")
343+
require.Equal(t, float64(dkg.Failed), testutil.ToFloat64(evoting.PromElectionDkgStatus))
344+
345+
initMetrics()
328346

329347
actor.electionID = electionID
330348

@@ -333,6 +351,7 @@ func TestPedersen_Setup(t *testing.T) {
333351

334352
_, err = actor.Setup()
335353
require.EqualError(t, err, fake.Err("failed to stream"))
354+
require.Equal(t, float64(dkg.Failed), testutil.ToFloat64(evoting.PromElectionDkgStatus))
336355

337356
// RPC is bogus 2
338357
actor.rpc = fake.NewRPC()
@@ -396,6 +415,7 @@ func TestPedersen_Setup(t *testing.T) {
396415
// We test that particular behaviour later.
397416
_, err = actor.Setup()
398417
require.NoError(t, err)
418+
require.Equal(t, float64(dkg.Setup), testutil.ToFloat64(evoting.PromElectionDkgStatus))
399419
}
400420

401421
func TestPedersen_GetPublicKey(t *testing.T) {
@@ -623,6 +643,10 @@ func TestPedersen_ComputePubshares_OK(t *testing.T) {
623643
// -----------------------------------------------------------------------------
624644
// Utility functions
625645

646+
func initMetrics() {
647+
evoting.PromElectionDkgStatus.Reset()
648+
}
649+
626650
// actorsEqual checks that two actors hold the same data
627651
func requireActorsEqual(t require.TestingT, actor1, actor2 dkg.Actor) {
628652
actor1Data, err := actor1.MarshalJSON()

0 commit comments

Comments
 (0)