Skip to content

Commit

Permalink
register taskFactory metrics in agent metrics server if it present
Browse files Browse the repository at this point in the history
  • Loading branch information
gdiazlo committed Apr 11, 2019
1 parent 8f967d0 commit d08fad8
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
5 changes: 5 additions & 0 deletions gossip/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ func (d *BatchProcessor) wasProcessed(b *protocol.BatchSnapshots) bool {

func (d *BatchProcessor) Subscribe(id int, ch <-chan *Message) {
d.id = id

if d.a.metrics != nil {
d.a.metrics.MustRegister(d.metrics...)
}

go func() {
for {
select {
Expand Down
50 changes: 50 additions & 0 deletions gossip/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@
package gossip

import (
"context"
"io/ioutil"
"net/http"
"strings"
"sync"
"testing"
"time"

"github.com/bbva/qed/protocol"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -103,3 +108,48 @@ func TestBatchProcessorWasProcessed(t *testing.T) {
// dropped by the wasProcessed function
require.Equal(t, 1, len(ts.ch), "Output queue must be 1, duplicate event must be dropped by processor")
}

type fakeTaskFactory struct{}

func (f fakeTaskFactory) Metrics() []prometheus.Collector {
return []prometheus.Collector{
prometheus.NewCounter(prometheus.CounterOpts{Name: "fakeCounterMetric"}),
}
}

func (f fakeTaskFactory) New(c context.Context) Task {
return func() error {
return nil
}
}

func TestBatchProcessorRegisterMetrics(t *testing.T) {

conf := DefaultConfig()
conf.NodeName = "testNode"
conf.Role = "auditor"
conf.BindAddr = "127.0.0.1:12345"
conf.MetricsAddr = "127.0.0.1:12346"

a, err := NewAgentFromConfig(conf)
require.NoError(t, err, "Error creating agent!")
a.Start()
defer a.Shutdown()
// wait for agent to start
// all services
time.Sleep(3 * time.Second)

p := NewBatchProcessor(a, []TaskFactory{&fakeTaskFactory{}})
a.In.Subscribe(BatchMessageType, p, 0)
defer p.Stop()

resp, err := http.Get("http://" + conf.MetricsAddr + "/metrics")
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
found := strings.Index(string(body), "fakeCounterMetric")

require.True(t, found > 0, "Metric not found!")
}

0 comments on commit d08fad8

Please sign in to comment.