-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' of github.com:vocdoni/vocdoni-node into dev
- Loading branch information
Showing
10 changed files
with
95 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,31 @@ | ||
package ipfs | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
|
||
"go.vocdoni.io/dvote/metrics" | ||
) | ||
|
||
// File collectors | ||
var ( | ||
// FilePeers ... | ||
FilePeers = prometheus.NewGauge(prometheus.GaugeOpts{ | ||
// registerMetrics registers prometheus metrics | ||
func (i *Handler) registerMetrics() { | ||
metrics.Register(prometheus.NewGaugeFunc(prometheus.GaugeOpts{ | ||
Namespace: "file", | ||
Name: "peers", | ||
Help: "The number of connected peers", | ||
}) | ||
// FileAddresses ... | ||
FileAddresses = prometheus.NewGauge(prometheus.GaugeOpts{ | ||
}, | ||
i.countPeers)) | ||
|
||
metrics.Register(prometheus.NewGaugeFunc(prometheus.GaugeOpts{ | ||
Namespace: "file", | ||
Name: "addresses", | ||
Help: "The number of registered addresses", | ||
}) | ||
// FilePins ... | ||
FilePins = prometheus.NewGauge(prometheus.GaugeOpts{ | ||
}, | ||
i.countKnownAddrs)) | ||
|
||
metrics.Register(prometheus.NewGaugeFunc(prometheus.GaugeOpts{ | ||
Namespace: "file", | ||
Name: "pins", | ||
Help: "The number of pinned files", | ||
}) | ||
) | ||
|
||
// RegisterMetrics to initialize the metrics to the agent | ||
func (*Handler) registerMetrics(ma *metrics.Agent) { | ||
ma.Register(FilePeers) | ||
ma.Register(FileAddresses) | ||
ma.Register(FilePins) | ||
} | ||
|
||
// setMetrics to be called as a loop and grab metrics | ||
func (i *Handler) setMetrics(ctx context.Context) error { | ||
peers, err := i.CoreAPI.Swarm().Peers(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
FilePeers.Set(float64(len(peers))) | ||
addresses, err := i.CoreAPI.Swarm().KnownAddrs(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
FileAddresses.Set(float64(len(addresses))) | ||
pins, err := i.countPins(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
FilePins.Set(float64(pins)) | ||
return nil | ||
} | ||
|
||
// CollectMetrics constantly updates the metric values for prometheus | ||
// The function is blocking, should be called in a go routine | ||
// If the metrics Agent is nil, do nothing | ||
func (i *Handler) CollectMetrics(ctx context.Context, ma *metrics.Agent) error { | ||
if ma != nil { | ||
i.registerMetrics(ma) | ||
for { | ||
time.Sleep(ma.RefreshInterval) | ||
tctx, cancel := context.WithTimeout(ctx, time.Minute) | ||
err := i.setMetrics(tctx) | ||
cancel() | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
}, | ||
i.countPins)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.