Skip to content

Commit

Permalink
CR: register default prometheus opencensus gatherers as Metric to Def…
Browse files Browse the repository at this point in the history
…aultRegistry
  • Loading branch information
algorandskiy committed Mar 22, 2024
1 parent 5b604cb commit d144fc7
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 24 deletions.
1 change: 0 additions & 1 deletion cmd/algod/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,6 @@ func run() int {
sendHeartbeat := func() {
values := make(map[string]float64)
metrics.DefaultRegistry().AddMetrics(values)
metrics.AddOpenCensusMetrics(values)

heartbeatDetails := baseHeartbeatEvent
heartbeatDetails.Metrics = values
Expand Down
1 change: 0 additions & 1 deletion daemon/algod/api/server/common/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ func Metrics(ctx lib.ReqContext, context echo.Context) {

var buf strings.Builder
metrics.DefaultRegistry().WriteMetrics(&buf, "")
metrics.WriteOpenCensusMetrics(&buf, "")
w.Write([]byte(buf.String()))
}

Expand Down
2 changes: 2 additions & 0 deletions network/p2p/dht/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (

"github.com/algorand/go-algorand/config"
algoproto "github.com/algorand/go-algorand/protocol"
"github.com/algorand/go-algorand/util/metrics"
)

const minBackoff = time.Second * 5
Expand All @@ -60,6 +61,7 @@ func MakeDHT(ctx context.Context, h host.Host, networkID algoproto.NetworkID, cf
if err := view.Register(dhtmetrics.DefaultViews...); err != nil {
return nil, err

Check warning on line 62 in network/p2p/dht/dht.go

View check run for this annotation

Codecov / codecov/patch

network/p2p/dht/dht.go#L61-L62

Added lines #L61 - L62 were not covered by tests
}
metrics.DefaultRegistry().Register(&metrics.OpencensusDefaultMetrics)

Check warning on line 64 in network/p2p/dht/dht.go

View check run for this annotation

Codecov / codecov/patch

network/p2p/dht/dht.go#L64

Added line #L64 was not covered by tests
}

return dht.New(ctx, h, dhtCfg...)
Expand Down
3 changes: 3 additions & 0 deletions network/p2p/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/algorand/go-algorand/logging"
pstore "github.com/algorand/go-algorand/network/p2p/peerstore"
"github.com/algorand/go-algorand/network/phonebook"
"github.com/algorand/go-algorand/util/metrics"
"github.com/algorand/go-deadlock"

"github.com/libp2p/go-libp2p"
Expand Down Expand Up @@ -126,6 +127,8 @@ func MakeHost(cfg config.Local, datadir string, pstore *pstore.PeerStore) (host.
var disableMetrics = func(cfg *libp2p.Config) error { return nil }
if !cfg.EnableMetricReporting {
disableMetrics = libp2p.DisableMetrics()
} else {
metrics.DefaultRegistry().Register(&metrics.PrometheusDefaultMetrics)

Check warning on line 131 in network/p2p/p2p.go

View check run for this annotation

Codecov / codecov/patch

network/p2p/p2p.go#L131

Added line #L131 was not covered by tests
}

host, err := libp2p.New(
Expand Down
36 changes: 20 additions & 16 deletions util/metrics/opencensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,26 @@ import (
"golang.org/x/exp/slices"
)

type defaultOpencensusGatherer struct {
names []string
}

// WriteMetric return opencensus data converted to algorand format
func (og *defaultOpencensusGatherer) WriteMetric(buf *strings.Builder, parentLabels string) {
metrics := collectOpenCensusMetrics(og.names)
for _, metric := range metrics {
metric.WriteMetric(buf, parentLabels)

Check warning on line 39 in util/metrics/opencensus.go

View check run for this annotation

Codecov / codecov/patch

util/metrics/opencensus.go#L36-L39

Added lines #L36 - L39 were not covered by tests
}
}

// AddMetric return opencensus data converted to algorand format
func (og *defaultOpencensusGatherer) AddMetric(values map[string]float64) {
metrics := collectOpenCensusMetrics(og.names)
for _, metric := range metrics {
metric.AddMetric(values)

Check warning on line 47 in util/metrics/opencensus.go

View check run for this annotation

Codecov / codecov/patch

util/metrics/opencensus.go#L44-L47

Added lines #L44 - L47 were not covered by tests
}
}

type statExporter struct {
names map[string]struct{}
metrics []Metric
Expand All @@ -47,22 +67,6 @@ func collectOpenCensusMetrics(names []string) []Metric {
return exporter.metrics
}

// WriteOpenCensusMetrics return opencensus data converted to algorand format
func WriteOpenCensusMetrics(buf *strings.Builder, parentLabels string, names ...string) {
metrics := collectOpenCensusMetrics(names)
for _, metric := range metrics {
metric.WriteMetric(buf, parentLabels)
}
}

// AddOpenCensusMetrics return opencensus data converted to algorand format
func AddOpenCensusMetrics(values map[string]float64, names ...string) {
metrics := collectOpenCensusMetrics(names)
for _, metric := range metrics {
metric.AddMetric(values)
}
}

// statCounter stores single int64 value per stat with labels
type statCounter struct {
name string
Expand Down
16 changes: 10 additions & 6 deletions util/metrics/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,23 @@ import (
iopc "github.com/prometheus/client_model/go"
)

// WritePrometheusMetrics return prometheus converted to algorand format.
type defaultPrometheusGatherer struct {
names []string
}

// WriteMetric return prometheus converted to algorand format.
// Supports only counter and gauge types and ignores go_ metrics.
func WritePrometheusMetrics(buf *strings.Builder, parentLabels string, names ...string) {
metrics := collectOpenCensusMetrics(names)
func (pg *defaultPrometheusGatherer) WriteMetric(buf *strings.Builder, parentLabels string) {
metrics := collectOpenCensusMetrics(pg.names)
for _, metric := range metrics {
metric.WriteMetric(buf, parentLabels)

Check warning on line 38 in util/metrics/prometheus.go

View check run for this annotation

Codecov / codecov/patch

util/metrics/prometheus.go#L35-L38

Added lines #L35 - L38 were not covered by tests
}
}

// AddPrometheusMetrics return prometheus data converted to algorand format.
// AddMetric return prometheus data converted to algorand format.
// Supports only counter and gauge types and ignores go_ metrics.
func AddPrometheusMetrics(values map[string]float64, names ...string) {
metrics := collectPrometheusMetrics(names)
func (pg *defaultPrometheusGatherer) AddMetric(values map[string]float64) {
metrics := collectPrometheusMetrics(pg.names)
for _, metric := range metrics {
metric.AddMetric(values)

Check warning on line 47 in util/metrics/prometheus.go

View check run for this annotation

Codecov / codecov/patch

util/metrics/prometheus.go#L44-L47

Added lines #L44 - L47 were not covered by tests
}
Expand Down
6 changes: 6 additions & 0 deletions util/metrics/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ func DefaultRegistry() *Registry {
return defaultRegistry
}

// PrometheusDefaultMetrics is the default prometheus gatherer implementing the Metric interface
var PrometheusDefaultMetrics = defaultPrometheusGatherer{}

// OpencensusDefaultMetrics is the default prometheus gatherer implementing the Metric interface
var OpencensusDefaultMetrics = defaultOpencensusGatherer{}

func init() {
defaultRegistry = MakeRegistry()
}
Expand Down

0 comments on commit d144fc7

Please sign in to comment.