Skip to content

Commit

Permalink
public metrics (#4723)
Browse files Browse the repository at this point in the history
subset of metrics to record:
- connections
- version
- init size started / completed
- post proof time

they will be recorded with 5 first characters from p2p public key, we probably don't want to gather information about smesher key.

example config:
```json
{
    "main": {
        "metrics-push": "https://public-metrics-gateway.spacemesh.dev/",
        "metrics-push-period": "1s",
        "metrics-push-user": "XXX",
        "metrics-push-pass": "XXXXXX",
        "metrics-push-header": {
            "X-Scope-OrgId": "XXXXXXX"
        }
    }
}
```
  • Loading branch information
dshulyak committed Jul 19, 2023
1 parent 3889e3a commit 0a9b323
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 25 deletions.
2 changes: 2 additions & 0 deletions activation/activation.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/spacemeshos/go-spacemesh/datastore"
"github.com/spacemeshos/go-spacemesh/events"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/metrics/public"
"github.com/spacemeshos/go-spacemesh/p2p/pubsub"
"github.com/spacemeshos/go-spacemesh/signing"
"github.com/spacemeshos/go-spacemesh/sql"
Expand Down Expand Up @@ -301,6 +302,7 @@ func (b *Builder) generateInitialPost(ctx context.Context) error {
}
events.EmitPostComplete(shared.ZeroChallenge)
metrics.PostDuration.Set(float64(time.Since(startTime).Nanoseconds()))
public.PostSeconds.Set(float64(time.Since(startTime)))
b.log.Info("created the initial post")
if b.verifyInitialPost(ctx, post, metadata) != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion activation/nipost.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/events"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/metrics/public"
"github.com/spacemeshos/go-spacemesh/signing"
)

Expand Down Expand Up @@ -253,7 +254,7 @@ func (nb *NIPostBuilder) BuildNIPost(ctx context.Context, challenge *types.NIPos
events.EmitPostComplete(nb.state.PoetProofRef[:])
postGenDuration = time.Since(startTime)
nb.log.With().Info("finished post execution", log.Duration("duration", postGenDuration))

public.PostSeconds.Set(postGenDuration.Seconds())
nb.state.NIPost.Post = proof
nb.state.NIPost.PostMetadata = proofMetadata

Expand Down
3 changes: 3 additions & 0 deletions activation/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/spacemeshos/go-spacemesh/datastore"
"github.com/spacemeshos/go-spacemesh/events"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/metrics/public"
"github.com/spacemeshos/go-spacemesh/sql"
"github.com/spacemeshos/go-spacemesh/sql/atxs"
)
Expand Down Expand Up @@ -315,6 +316,7 @@ func (mgr *PostSetupManager) StartSession(ctx context.Context) error {
log.String("labels_per_unit", fmt.Sprintf("%d", mgr.cfg.LabelsPerUnit)),
log.String("provider", fmt.Sprintf("%d", mgr.lastOpts.ProviderID)),
)
public.InitStart.Set(float64(mgr.lastOpts.NumUnits))
events.EmitInitStart(mgr.id, mgr.commitmentAtxId)
err = mgr.init.Initialize(ctx)

Expand All @@ -337,6 +339,7 @@ func (mgr *PostSetupManager) StartSession(ctx context.Context) error {
events.EmitInitFailure(mgr.id, mgr.commitmentAtxId, err)
return err
}
public.InitEnd.Set(float64(mgr.lastOpts.NumUnits))
events.EmitInitComplete()

mgr.logger.With().Info("post setup completed",
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func AddCommands(cmd *cobra.Command) {
cfg.MetricsPort, "metric server port")
cmd.PersistentFlags().StringVar(&cfg.MetricsPush, "metrics-push",
cfg.MetricsPush, "Push metrics to url")
cmd.PersistentFlags().IntVar(&cfg.MetricsPushPeriod, "metrics-push-period",
cmd.PersistentFlags().DurationVar(&cfg.MetricsPushPeriod, "metrics-push-period",
cfg.MetricsPushPeriod, "Push period")
cmd.PersistentFlags().StringArrayVar(&cfg.PoETServers, "poet-server",
cfg.PoETServers, "The poet server url. (temporary) Can be passed multiple times")
Expand Down
9 changes: 5 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,11 @@ type BaseConfig struct {
CollectMetrics bool `mapstructure:"metrics"`
MetricsPort int `mapstructure:"metrics-port"`

MetricsPush string `mapstructure:"metrics-push"`
MetricsPushPeriod int `mapstructure:"metrics-push-period"`
MetricsPushUser string `mapstructure:"metrics-push-user"`
MetricsPushPass string `mapstructure:"metrics-push-pass"`
MetricsPush string `mapstructure:"metrics-push"`
MetricsPushPeriod time.Duration `mapstructure:"metrics-push-period"`
MetricsPushUser string `mapstructure:"metrics-push-user"`
MetricsPushPass string `mapstructure:"metrics-push-pass"`
MetricsPushHeader map[string]string `mapstructure:"metrics-push-header"`

ProfilerName string `mapstructure:"profiler-name"`
ProfilerURL string `mapstructure:"profiler-url"`
Expand Down
12 changes: 10 additions & 2 deletions config/mainnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func MainnetConfig() Config {
if err := postPowDifficulty.UnmarshalText([]byte("00037ec8ec25e6d2c00000000000000000000000000000000000000000000000")); err != nil {
panic(err)
}

p2pconfig := p2p.DefaultConfig()
return Config{
BaseConfig: BaseConfig{
DataDirParent: defaultDataDir,
Expand All @@ -45,9 +45,17 @@ func MainnetConfig() Config {
OptFilterThreshold: 90,

TickSize: 9331200,
PoETServers: []string{
"https://mainnet-poet-0.spacemesh.network",
"https://mainnet-poet-1.spacemesh.network",
"https://mainnet-poet-2.spacemesh.network",
"https://poet-110.spacemesh.network",
"https://poet-111.spacemesh.network",
},
},
Genesis: &GenesisConfig{
GenesisTime: "2023-07-14T08:00:00Z",
ExtraData: "00000000000000000001a6bc150307b5c1998045752b3c87eccf3c013036f3cc",
Accounts: MainnetAccounts(),
},
Tortoise: tortoise.Config{
Expand Down Expand Up @@ -105,7 +113,7 @@ func MainnetConfig() Config {
DataDir: os.TempDir(),
Interval: 30 * time.Second,
},
P2P: p2p.DefaultConfig(),
P2P: p2pconfig,
API: grpcserver.DefaultConfig(),
TIME: timeConfig.DefaultConfig(),
SMESHING: DefaultSmeshingConfig(),
Expand Down
29 changes: 29 additions & 0 deletions metrics/public/public.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package public

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

var Registry = prometheus.NewRegistry()

var (
Connections = promauto.With(Registry).NewGaugeVec(prometheus.GaugeOpts{
Namespace: "smh",
Name: "connections",
}, []string{"dir"})
initSize = promauto.With(Registry).NewGaugeVec(prometheus.GaugeOpts{
Namespace: "smh",
Name: "init_size",
}, []string{"step"})
InitStart = initSize.WithLabelValues("start")
InitEnd = initSize.WithLabelValues("complete")
PostSeconds = promauto.With(Registry).NewGaugeVec(prometheus.GaugeOpts{
Namespace: "smh",
Name: "post_seconds",
}, []string{}).WithLabelValues()
Version = promauto.With(Registry).NewGaugeVec(prometheus.GaugeOpts{
Namespace: "smh",
Name: "version",
}, []string{"version"})
)
24 changes: 13 additions & 11 deletions metrics/push.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
package metrics

import (
"net/http"
"time"

stdprometheus "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/push"

"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/metrics/public"
)

// StartPushingMetrics begins pushing metrics to the url specified by the --metrics-push flag
// with period specified by the --metrics-push-period flag.
func StartPushingMetrics(url, username, password string, periodSec int, nodeID, networkID string) {
period := time.Duration(periodSec) * time.Second
ticker := time.NewTicker(period)

pusher := push.New(url, "go-spacemesh").Gatherer(stdprometheus.DefaultGatherer).
Grouping("node_id", nodeID).
Grouping("network_id", networkID)

func StartPushingMetrics(url, username, password string, headers map[string]string, period time.Duration, nodeID, networkID string) {
header := http.Header{}
for k, v := range headers {
header.Add(k, v)
}
pusher := push.New(url, "go-spacemesh").Gatherer(public.Registry).
Grouping("node", nodeID).
Grouping("network", networkID).
Header(header)
if username != "" && password != "" {
pusher.BasicAuth(username, password)
pusher = pusher.BasicAuth(username, password)
}

go func() {
ticker := time.NewTicker(period)
for range ticker.C {
err := pusher.Push()
if err != nil {
Expand Down
13 changes: 9 additions & 4 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import (
"github.com/spacemeshos/go-spacemesh/malfeasance"
"github.com/spacemeshos/go-spacemesh/mesh"
"github.com/spacemeshos/go-spacemesh/metrics"
"github.com/spacemeshos/go-spacemesh/metrics/public"
"github.com/spacemeshos/go-spacemesh/miner"
"github.com/spacemeshos/go-spacemesh/node/mapstructureutil"
"github.com/spacemeshos/go-spacemesh/p2p"
Expand Down Expand Up @@ -453,9 +454,9 @@ func (app *App) Initialize() error {
timeCfg.TimeConfigValues = app.Config.TIME

app.setupLogging()

app.introduction()

public.Version.WithLabelValues(cmd.Version).Set(1)
return nil
}

Expand Down Expand Up @@ -1374,9 +1375,13 @@ func (app *App) Start(ctx context.Context) error {
}

if app.Config.MetricsPush != "" {
metrics.StartPushingMetrics(app.Config.MetricsPush,
app.Config.MetricsPushUser, app.Config.MetricsPushPass, app.Config.MetricsPushPeriod,
app.host.ID().String(), app.Config.Genesis.GenesisID().ShortString())
metrics.StartPushingMetrics(
app.Config.MetricsPush,
app.Config.MetricsPushUser,
app.Config.MetricsPushPass,
app.Config.MetricsPushHeader,
app.Config.MetricsPushPeriod,
app.host.ID().String()[:5], app.Config.Genesis.GenesisID().ShortString())
}

if err := app.startServices(ctx); err != nil {
Expand Down
8 changes: 6 additions & 2 deletions p2p/metrics/connections.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package metrics

import (
"strings"
"time"

"github.com/libp2p/go-libp2p/core/network"
ma "github.com/multiformats/go-multiaddr"

"github.com/spacemeshos/go-spacemesh/metrics"
"github.com/spacemeshos/go-spacemesh/metrics/public"
)

var (
Expand Down Expand Up @@ -52,12 +54,14 @@ func (c *ConnectionsMeeter) Listen(network.Network, ma.Multiaddr) {}
func (c *ConnectionsMeeter) ListenClose(network.Network, ma.Multiaddr) {}

// Connected called when a connection opened.
func (c *ConnectionsMeeter) Connected(network.Network, network.Conn) {
func (c *ConnectionsMeeter) Connected(_ network.Network, conn network.Conn) {
public.Connections.WithLabelValues(strings.ToLower(conn.Stat().Direction.String())).Inc()
connections.WithLabelValues().Inc()
}

// Disconnected called when a connection closed.
func (c *ConnectionsMeeter) Disconnected(network.Network, network.Conn) {
func (c *ConnectionsMeeter) Disconnected(_ network.Network, conn network.Conn) {
public.Connections.WithLabelValues(strings.ToLower(conn.Stat().Direction.String())).Dec()
connections.WithLabelValues().Dec()
}

Expand Down

0 comments on commit 0a9b323

Please sign in to comment.