Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/apiserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (as *apiServer) Serve(ctx context.Context, o orchestrator.Orchestrator) (er
}

if as.metricsEnabled {
metricsHTTPServer, err := newHTTPServer(ctx, "metrics", as.createMetricsMuxRouter(ctx), metricsErrChan, metricsConfigPrefix)
metricsHTTPServer, err := newHTTPServer(ctx, "metrics", as.createMetricsMuxRouter(), metricsErrChan, metricsConfigPrefix)
if err != nil {
return err
}
Expand Down Expand Up @@ -494,7 +494,7 @@ func (as *apiServer) createAdminMuxRouter(o orchestrator.Orchestrator) *mux.Rout
return r
}

func (as *apiServer) createMetricsMuxRouter(_ context.Context) *mux.Router {
func (as *apiServer) createMetricsMuxRouter() *mux.Router {
r := mux.NewRouter()

r.Path(config.GetString(config.MetricsPath)).Handler(promhttp.InstrumentMetricHandler(metrics.Registry(),
Expand Down
3 changes: 1 addition & 2 deletions internal/apiserver/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@ import (
"testing"
"time"

"github.com/hyperledger/firefly/internal/metrics"

"github.com/getkin/kin-openapi/openapi3"
"github.com/gorilla/mux"
"github.com/hyperledger/firefly/internal/config"
"github.com/hyperledger/firefly/internal/i18n"
"github.com/hyperledger/firefly/internal/metrics"
"github.com/hyperledger/firefly/internal/oapispec"
"github.com/hyperledger/firefly/mocks/orchestratormocks"
"github.com/stretchr/testify/assert"
Expand Down
20 changes: 13 additions & 7 deletions internal/batchpin/batchpin.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ package batchpin
import (
"context"

"github.com/hyperledger/firefly/internal/config"
"github.com/hyperledger/firefly/internal/identity"
"github.com/hyperledger/firefly/internal/metrics"
"github.com/hyperledger/firefly/pkg/blockchain"
"github.com/hyperledger/firefly/pkg/database"
"github.com/hyperledger/firefly/pkg/fftypes"
Expand All @@ -30,21 +32,22 @@ type Submitter interface {
}

type batchPinSubmitter struct {
database database.Plugin
identity identity.Manager
blockchain blockchain.Plugin
database database.Plugin
identity identity.Manager
blockchain blockchain.Plugin
metricsEnabled bool
}

func NewBatchPinSubmitter(di database.Plugin, im identity.Manager, bi blockchain.Plugin) Submitter {
return &batchPinSubmitter{
database: di,
identity: im,
blockchain: bi,
database: di,
identity: im,
blockchain: bi,
metricsEnabled: config.GetBool(config.MetricsEnabled),
}
}

func (bp *batchPinSubmitter) SubmitPinnedBatch(ctx context.Context, batch *fftypes.Batch, contexts []*fftypes.Bytes32) error {

tx := &fftypes.Transaction{
ID: batch.Payload.TX.ID,
Subject: fftypes.TransactionSubject{
Expand Down Expand Up @@ -75,6 +78,9 @@ func (bp *batchPinSubmitter) SubmitPinnedBatch(ctx context.Context, batch *fftyp
return err
}

if bp.metricsEnabled {
metrics.BatchPinCounter.Inc()
}
// Write the batch pin to the blockchain
return bp.blockchain.SubmitBatchPin(ctx, op.ID, nil /* TODO: ledger selection */, batch.Key, &blockchain.BatchPin{
Namespace: batch.Namespace,
Expand Down
40 changes: 39 additions & 1 deletion internal/batchpin/batchpin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"fmt"
"testing"

"github.com/hyperledger/firefly/internal/config"
"github.com/hyperledger/firefly/internal/metrics"
"github.com/hyperledger/firefly/mocks/blockchainmocks"
"github.com/hyperledger/firefly/mocks/databasemocks"
"github.com/hyperledger/firefly/mocks/identitymanagermocks"
Expand All @@ -29,6 +31,8 @@ import (
"github.com/stretchr/testify/mock"
)

var utConfPrefix = config.NewPluginConfig("metrics")

func newTestBatchPinSubmitter(t *testing.T) *batchPinSubmitter {
mdi := &databasemocks.Plugin{}
mim := &identitymanagermocks.Manager{}
Expand All @@ -38,7 +42,6 @@ func newTestBatchPinSubmitter(t *testing.T) *batchPinSubmitter {
}

func TestSubmitPinnedBatchOk(t *testing.T) {

bp := newTestBatchPinSubmitter(t)
ctx := context.Background()

Expand Down Expand Up @@ -70,7 +73,42 @@ func TestSubmitPinnedBatchOk(t *testing.T) {

err := bp.SubmitPinnedBatch(ctx, batch, contexts)
assert.NoError(t, err)
}

func TestSubmitPinnedBatchWithMetricsOk(t *testing.T) {
metrics.Registry()
config.Set(config.MetricsEnabled, true)
bp := newTestBatchPinSubmitter(t)
ctx := context.Background()

mbi := bp.blockchain.(*blockchainmocks.Plugin)
mdi := bp.database.(*databasemocks.Plugin)

batch := &fftypes.Batch{
ID: fftypes.NewUUID(),
Identity: fftypes.Identity{
Author: "id1",
Key: "0x12345",
},
Payload: fftypes.BatchPayload{
TX: fftypes.TransactionRef{
ID: fftypes.NewUUID(),
},
},
}
contexts := []*fftypes.Bytes32{}

mdi.On("UpsertTransaction", ctx, mock.Anything, false).Return(nil)
mdi.On("UpsertOperation", ctx, mock.MatchedBy(func(op *fftypes.Operation) bool {
assert.Equal(t, fftypes.OpTypeBlockchainBatchPin, op.Type)
assert.Equal(t, "ut", op.Plugin)
assert.Equal(t, *batch.Payload.TX.ID, *op.Transaction)
return true
}), false).Return(nil)
mbi.On("SubmitBatchPin", ctx, mock.Anything, (*fftypes.UUID)(nil), "0x12345", mock.Anything).Return(nil)

err := bp.SubmitPinnedBatch(ctx, batch, contexts)
assert.NoError(t, err)
}

func TestSubmitPinnedBatchOpFail(t *testing.T) {
Expand Down
25 changes: 22 additions & 3 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,40 @@

package metrics

import "github.com/prometheus/client_golang/prometheus"
import (
"github.com/prometheus/client_golang/prometheus"
)

var registry *prometheus.Registry
var BatchPinCounter prometheus.Counter

// MetricsBatchPin is the prometheus metric for total number of batch pins submitted
var MetricsBatchPin = "ff_batchpin_total"

// Registry returns FireFly's customized Prometheus registry
func Registry() *prometheus.Registry {
if registry == nil {
initMetricsCollectors()
registry = prometheus.NewRegistry()
registry.MustRegister(prometheus.NewGoCollector())
registry.MustRegister(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}))
registerMetricsCollectors()
}

return registry
}

func initMetricsCollectors() {
BatchPinCounter = prometheus.NewCounter(prometheus.CounterOpts{
Name: MetricsBatchPin,
Help: "Number of batch pins submitted",
})
}

func registerMetricsCollectors() {
registry.MustRegister(prometheus.NewGoCollector())
registry.MustRegister(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}))
registry.MustRegister(BatchPinCounter)
}

// Clear will reset the Prometheus metrics registry, useful for testing
func Clear() {
registry = nil
Expand Down