Skip to content

Commit 4ef7da6

Browse files
committed
test dkg status metrics
1 parent d49a531 commit 4ef7da6

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-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

+26
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"
@@ -46,12 +48,15 @@ func init() {
4648
// If you get the persistent data from an actor and then recreate an actor
4749
// from that data, the persistent data should be the same in both actors.
4850
func TestActor_MarshalJSON(t *testing.T) {
51+
initMetrics()
52+
4953
p := NewPedersen(fake.Mino{}, &fake.Service{}, &fake.Pool{}, fake.Factory{}, fake.Signer{})
5054

5155
// Create new actor
5256
actor1, err := p.NewActor([]byte("deadbeef"), &fake.Pool{},
5357
fake.Manager{}, NewHandlerData())
5458
require.NoError(t, err)
59+
require.Equal(t, float64(dkg.Initialized), testutil.ToFloat64(evoting.PromElectionDkgStatus))
5560

5661
// Serialize its persistent data
5762
actor1Buf, err := actor1.MarshalJSON()
@@ -62,8 +67,11 @@ func TestActor_MarshalJSON(t *testing.T) {
6267
err = handlerData.UnmarshalJSON(actor1Buf)
6368
require.NoError(t, err)
6469

70+
initMetrics()
71+
6572
actor2, err := p.NewActor([]byte("beefdead"), &fake.Pool{}, fake.Manager{}, handlerData)
6673
require.NoError(t, err)
74+
require.Equal(t, float64(dkg.Initialized), testutil.ToFloat64(evoting.PromElectionDkgStatus))
6775

6876
// Check that the persistent data is the same for both actors
6977
requireActorsEqual(t, actor1, actor2)
@@ -72,6 +80,8 @@ func TestActor_MarshalJSON(t *testing.T) {
7280
// After initializing a Pedersen when dkgMap is not empty, the actors map should
7381
// contain the same information as dkgMap
7482
func TestPedersen_InitNonEmptyMap(t *testing.T) {
83+
initMetrics()
84+
7585
// Create a new DKG map and fill it with data
7686
dkgMap := fake.NewInMemoryDB()
7787

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

142152
_, err = p.NewActor(electionIDBuf, &fake.Pool{}, fake.Manager{}, handlerData)
143153
if err != nil {
154+
require.Equal(t, float64(dkg.Failed), testutil.ToFloat64(evoting.PromElectionDkgStatus))
144155
return err
156+
} else {
157+
require.Equal(t, float64(dkg.Initialized), testutil.ToFloat64(evoting.PromElectionDkgStatus))
145158
}
146159

160+
initMetrics()
161+
147162
return nil
148163
})
149164
})
@@ -301,6 +316,8 @@ func TestPedersen_TwoListens(t *testing.T) {
301316
}
302317

303318
func TestPedersen_Setup(t *testing.T) {
319+
initMetrics()
320+
304321
electionID := "d3adbeef"
305322

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

326343
_, err := actor.Setup()
327344
require.EqualError(t, err, "failed to get election: election does not exist: <nil>")
345+
require.Equal(t, float64(dkg.Failed), testutil.ToFloat64(evoting.PromElectionDkgStatus))
346+
347+
initMetrics()
328348

329349
actor.electionID = electionID
330350

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

334354
_, err = actor.Setup()
335355
require.EqualError(t, err, fake.Err("failed to stream"))
356+
require.Equal(t, float64(dkg.Failed), testutil.ToFloat64(evoting.PromElectionDkgStatus))
336357

337358
// RPC is bogus 2
338359
actor.rpc = fake.NewRPC()
@@ -396,6 +417,7 @@ func TestPedersen_Setup(t *testing.T) {
396417
// We test that particular behaviour later.
397418
_, err = actor.Setup()
398419
require.NoError(t, err)
420+
require.Equal(t, float64(dkg.Setup), testutil.ToFloat64(evoting.PromElectionDkgStatus))
399421
}
400422

401423
func TestPedersen_GetPublicKey(t *testing.T) {
@@ -623,6 +645,10 @@ func TestPedersen_ComputePubshares_OK(t *testing.T) {
623645
// -----------------------------------------------------------------------------
624646
// Utility functions
625647

648+
func initMetrics() {
649+
evoting.PromElectionDkgStatus.Reset()
650+
}
651+
626652
// actorsEqual checks that two actors hold the same data
627653
func requireActorsEqual(t require.TestingT, actor1, actor2 dkg.Actor) {
628654
actor1Data, err := actor1.MarshalJSON()

0 commit comments

Comments
 (0)