Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prometheus Metrics #113

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
9 changes: 9 additions & 0 deletions internal/evmconfig/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package evmconfig

import "github.com/hyperledger/firefly-common/pkg/config"

var ffc = config.AddRootKey

var (
MetricsEnabled = ffc("metrics.enabled")
)
20 changes: 20 additions & 0 deletions internal/metrics/evm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package metrics

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

var EvmTransactionSubmissionCounter prometheus.Counter

var (
MetricsTransactionSubmission = "ff_transaction_submission_total"
)

func InitEvmCustomMetrics() {
EvmTransactionSubmissionCounter = prometheus.NewCounter(prometheus.CounterOpts{
Name: MetricsTransactionSubmission,
Help: "Total number of transactions submitted to the EVM Connect",
})
}

func RegsiterEvmCustomMetrics() {
registry.MustRegister(EvmTransactionSubmissionCounter)
}
49 changes: 49 additions & 0 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package metrics

import (
"context"
"sync"
"time"

"github.com/hyperledger/firefly-common/pkg/config"
"github.com/hyperledger/firefly-evmconnect/internal/evmconfig"
)

var mutex = &sync.Mutex{}

type Manager interface {
IsMetricsEnabled() bool
GetTime(id string) time.Time
AddTime(id string)
}

type metricsManager struct {
ctx context.Context
metricsEnabled bool
timeMap map[string]time.Time
}

func NewMetricsManager(ctx context.Context) Manager {
return &metricsManager{
ctx: ctx,
metricsEnabled: config.GetBool(evmconfig.MetricsEnabled),
timeMap: make(map[string]time.Time),
}
}

func (mm *metricsManager) IsMetricsEnabled() bool {
return mm.metricsEnabled
}

func (mm *metricsManager) AddTime(id string) {
mutex.Lock()
mm.timeMap[id] = time.Now()
mutex.Unlock()
}

func (mm *metricsManager) GetTime(id string) time.Time {
mutex.Lock()
time := mm.timeMap[id]
mutex.Unlock()
return time
}
62 changes: 62 additions & 0 deletions internal/metrics/prometheus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package metrics

import (
"sync"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
muxprom "gitlab.com/hfuss/mux-prometheus/pkg/middleware"
)

var regMux sync.Mutex
var registry *prometheus.Registry
var evmInstrumentation *muxprom.Instrumentation

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

// GetEvmServerInstrumentation returns the transaction server's Prometheus middleware, ensuring its metrics are never
// registered twice
func GetEvmServerInstrumentation() *muxprom.Instrumentation {
regMux.Lock()
defer regMux.Unlock()
if evmInstrumentation == nil {
evmInstrumentation = NewInstrumentation("ffevm")
}
return evmInstrumentation
}

func NewInstrumentation(subsystem string) *muxprom.Instrumentation {
return muxprom.NewCustomInstrumentation(
true,
"ff_evmconnect",
subsystem,
prometheus.DefBuckets,
map[string]string{},
Registry(),
)
}

// Clear will reset the Prometheus metrics registry and instrumentations, useful for testing
func Clear() {
registry = nil
evmInstrumentation = nil

}

func initMetricsCollectors() {
InitEvmCustomMetrics()
}

func registerMetricsCollectors() {
registry.MustRegister(collectors.NewGoCollector())
registry.MustRegister(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}))
RegsiterEvmCustomMetrics()
}