-
Notifications
You must be signed in to change notification settings - Fork 111
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
feat(lib/transactions) ready transactions metrics #1656
Merged
Merged
Changes from 18 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
681cfaf
pool ready transaction metrics
EclesioMeloJunior 5f62d09
chore: add priority queue metrics
EclesioMeloJunior 365b8d7
Merge branch 'development' into eclesio/ready-txs-metrics
EclesioMeloJunior 34b7c33
chore: fix lint
EclesioMeloJunior cc20c9f
chore: remove println
EclesioMeloJunior f864c3e
Merge branch 'development' into eclesio/ready-txs-metrics
EclesioMeloJunior 4f15bd1
chore: add copyright comments
EclesioMeloJunior 8227b2f
chore: resolve comments
EclesioMeloJunior 38a7ef7
Merge branch 'eclesio/ready-txs-metrics' of github.com:ChainSafe/goss…
EclesioMeloJunior f390373
chore: remove stop function and add comments to exported funcs
EclesioMeloJunior 232ed98
chore: adjust tests
EclesioMeloJunior c7e9fb4
Merge branch 'development' into eclesio/ready-txs-metrics
EclesioMeloJunior b394fc2
chore: make metrics.Start blocking and using wg to inner gorout
EclesioMeloJunior 533bc6b
chore: fix typo
EclesioMeloJunior 2f695b1
Merge branch 'eclesio/ready-txs-metrics' of github.com:ChainSafe/goss…
EclesioMeloJunior 8faa274
Merge branch 'development' into eclesio/ready-txs-metrics
EclesioMeloJunior 63bb7ab
chore: fix metrics test
EclesioMeloJunior 6930c63
fix: solving data race condition on metrics tests
EclesioMeloJunior e9bb260
Merge branch 'development' into eclesio/ready-txs-metrics
EclesioMeloJunior File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
// Copyright 2019 ChainSafe Systems (ON) Corp. | ||
// This file is part of gossamer. | ||
// | ||
// The gossamer library is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Lesser General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// The gossamer library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with the gossamer library. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package metrics | ||
|
||
import ( | ||
"context" | ||
"runtime" | ||
"sync" | ||
"time" | ||
|
||
ethmetrics "github.com/ethereum/go-ethereum/metrics" | ||
) | ||
|
||
// GaugeMetrics interface allows the exportation of many gauge metrics | ||
// the implementer could exports | ||
type GaugeMetrics interface { | ||
CollectGauge() map[string]int64 | ||
} | ||
|
||
// Collector struct controls the metrics and executes polling to extract the values | ||
type Collector struct { | ||
ctx context.Context | ||
gauges []GaugeMetrics | ||
wg sync.WaitGroup | ||
} | ||
|
||
// NewCollector creates a new Collector | ||
func NewCollector(ctx context.Context) *Collector { | ||
return &Collector{ | ||
ctx: ctx, | ||
wg: sync.WaitGroup{}, | ||
gauges: make([]GaugeMetrics, 0), | ||
} | ||
} | ||
|
||
// Start will start one goroutine to collect all the gauges registered and | ||
// a separate goroutine to collect process metrics | ||
func (c *Collector) Start() { | ||
ethmetrics.Enabled = true | ||
c.wg.Add(2) | ||
|
||
go c.startCollectProccessMetrics() | ||
go c.startCollectGauges() | ||
|
||
c.wg.Wait() | ||
} | ||
|
||
// AddGauge adds a GaugeMetrics implementer on gauges list | ||
func (c *Collector) AddGauge(g GaugeMetrics) { | ||
c.gauges = append(c.gauges, g) | ||
} | ||
|
||
func (c *Collector) startCollectGauges() { | ||
t := time.NewTicker(Refresh) | ||
defer func() { | ||
t.Stop() | ||
c.wg.Done() | ||
}() | ||
|
||
for { | ||
select { | ||
case <-c.ctx.Done(): | ||
return | ||
case <-t.C: | ||
for _, g := range c.gauges { | ||
m := g.CollectGauge() | ||
|
||
for label, value := range m { | ||
pooltx := ethmetrics.GetOrRegisterGauge(label, nil) | ||
pooltx.Update(value) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (c *Collector) startCollectProccessMetrics() { | ||
cpuStats := make([]*ethmetrics.CPUStats, 2) | ||
memStats := make([]*runtime.MemStats, 2) | ||
for i := 0; i < len(memStats); i++ { | ||
cpuStats[i] = new(ethmetrics.CPUStats) | ||
memStats[i] = new(runtime.MemStats) | ||
} | ||
|
||
// Define the various metrics to collect | ||
var ( | ||
cpuSysLoad = ethmetrics.GetOrRegisterGauge("system/cpu/sysload", ethmetrics.DefaultRegistry) | ||
cpuSysWait = ethmetrics.GetOrRegisterGauge("system/cpu/syswait", ethmetrics.DefaultRegistry) | ||
cpuProcLoad = ethmetrics.GetOrRegisterGauge("system/cpu/procload", ethmetrics.DefaultRegistry) | ||
cpuGoroutines = ethmetrics.GetOrRegisterGauge("system/cpu/goroutines", ethmetrics.DefaultRegistry) | ||
|
||
memPauses = ethmetrics.GetOrRegisterMeter("system/memory/pauses", ethmetrics.DefaultRegistry) | ||
memAlloc = ethmetrics.GetOrRegisterMeter("system/memory/allocs", ethmetrics.DefaultRegistry) | ||
memFrees = ethmetrics.GetOrRegisterMeter("system/memory/frees", ethmetrics.DefaultRegistry) | ||
memHeld = ethmetrics.GetOrRegisterGauge("system/memory/held", ethmetrics.DefaultRegistry) | ||
memUsed = ethmetrics.GetOrRegisterGauge("system/memory/used", ethmetrics.DefaultRegistry) | ||
) | ||
|
||
t := time.NewTicker(Refresh) | ||
defer func() { | ||
t.Stop() | ||
c.wg.Done() | ||
}() | ||
|
||
for i := 1; ; i++ { | ||
select { | ||
case <-c.ctx.Done(): | ||
return | ||
case <-t.C: | ||
location1 := i % 2 | ||
location2 := (i - 1) % 2 | ||
|
||
ethmetrics.ReadCPUStats(cpuStats[location1]) | ||
cpuSysLoad.Update((cpuStats[location1].GlobalTime - cpuStats[location2].GlobalTime) / refreshFreq) | ||
cpuSysWait.Update((cpuStats[location1].GlobalWait - cpuStats[location2].GlobalWait) / refreshFreq) | ||
cpuProcLoad.Update((cpuStats[location1].LocalTime - cpuStats[location2].LocalTime) / refreshFreq) | ||
cpuGoroutines.Update(int64(runtime.NumGoroutine())) | ||
|
||
runtime.ReadMemStats(memStats[location1]) | ||
memPauses.Mark(int64(memStats[location1].PauseTotalNs - memStats[location2].PauseTotalNs)) | ||
memAlloc.Mark(int64(memStats[location1].Mallocs - memStats[location2].Mallocs)) | ||
memFrees.Mark(int64(memStats[location1].Frees - memStats[location2].Frees)) | ||
memHeld.Update(int64(memStats[location1].HeapSys - memStats[location1].HeapReleased)) | ||
memUsed.Update(int64(memStats[location1].Alloc)) | ||
} | ||
} | ||
} |
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,60 +1,53 @@ | ||
// Copyright 2019 ChainSafe Systems (ON) Corp. | ||
// This file is part of gossamer. | ||
// | ||
// The gossamer library is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Lesser General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// The gossamer library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with the gossamer library. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package metrics | ||
|
||
import ( | ||
"runtime" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/ethereum/go-ethereum/metrics" | ||
log "github.com/ChainSafe/log15" | ||
ethmetrics "github.com/ethereum/go-ethereum/metrics" | ||
"github.com/ethereum/go-ethereum/metrics/prometheus" | ||
) | ||
|
||
var logger = log.New("pkg", "metrics") | ||
|
||
const ( | ||
// Refresh is the refresh time for publishing metrics. | ||
Refresh = time.Second * 10 | ||
refreshFreq = int64(Refresh / time.Second) | ||
) | ||
|
||
// CollectProcessMetrics periodically collects various metrics about the running process. | ||
func CollectProcessMetrics() { | ||
// Create the various data collectors | ||
cpuStats := make([]*metrics.CPUStats, 2) | ||
memStats := make([]*runtime.MemStats, 2) | ||
for i := 0; i < len(memStats); i++ { | ||
cpuStats[i] = new(metrics.CPUStats) | ||
memStats[i] = new(runtime.MemStats) | ||
} | ||
|
||
// Define the various metrics to collect | ||
var ( | ||
cpuSysLoad = metrics.GetOrRegisterGauge("system/cpu/sysload", metrics.DefaultRegistry) | ||
cpuSysWait = metrics.GetOrRegisterGauge("system/cpu/syswait", metrics.DefaultRegistry) | ||
cpuProcLoad = metrics.GetOrRegisterGauge("system/cpu/procload", metrics.DefaultRegistry) | ||
cpuGoroutines = metrics.GetOrRegisterGauge("system/cpu/goroutines", metrics.DefaultRegistry) | ||
|
||
memPauses = metrics.GetOrRegisterMeter("system/memory/pauses", metrics.DefaultRegistry) | ||
memAlloc = metrics.GetOrRegisterMeter("system/memory/allocs", metrics.DefaultRegistry) | ||
memFrees = metrics.GetOrRegisterMeter("system/memory/frees", metrics.DefaultRegistry) | ||
memHeld = metrics.GetOrRegisterGauge("system/memory/held", metrics.DefaultRegistry) | ||
memUsed = metrics.GetOrRegisterGauge("system/memory/used", metrics.DefaultRegistry) | ||
) | ||
|
||
// Iterate loading the different stats and updating the meters | ||
for i := 1; ; i++ { | ||
location1 := i % 2 | ||
location2 := (i - 1) % 2 | ||
|
||
metrics.ReadCPUStats(cpuStats[location1]) | ||
cpuSysLoad.Update((cpuStats[location1].GlobalTime - cpuStats[location2].GlobalTime) / refreshFreq) | ||
cpuSysWait.Update((cpuStats[location1].GlobalWait - cpuStats[location2].GlobalWait) / refreshFreq) | ||
cpuProcLoad.Update((cpuStats[location1].LocalTime - cpuStats[location2].LocalTime) / refreshFreq) | ||
cpuGoroutines.Update(int64(runtime.NumGoroutine())) | ||
|
||
runtime.ReadMemStats(memStats[location1]) | ||
memPauses.Mark(int64(memStats[location1].PauseTotalNs - memStats[location2].PauseTotalNs)) | ||
memAlloc.Mark(int64(memStats[location1].Mallocs - memStats[location2].Mallocs)) | ||
memFrees.Mark(int64(memStats[location1].Frees - memStats[location2].Frees)) | ||
memHeld.Update(int64(memStats[location1].HeapSys - memStats[location1].HeapReleased)) | ||
memUsed.Update(int64(memStats[location1].Alloc)) | ||
// PublishMetrics function will export the /metrics endpoint to prometheus process | ||
func PublishMetrics(address string) { | ||
ethmetrics.Enabled = true | ||
setupMetricsServer(address) | ||
} | ||
|
||
time.Sleep(Refresh) | ||
} | ||
// setupMetricsServer starts a dedicated metrics server at the given address. | ||
func setupMetricsServer(address string) { | ||
m := http.NewServeMux() | ||
m.Handle("/metrics", prometheus.Handler(ethmetrics.DefaultRegistry)) | ||
logger.Info("Starting metrics server", "addr", fmt.Sprintf("http://%s/metrics", address)) | ||
go func() { | ||
if err := http.ListenAndServe(address, m); err != nil { | ||
log.Error("Failure in running metrics server", "err", err) | ||
} | ||
}() | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like making
Start
functions blocking. It is the callers responsibility to callgo c.Start()
. You could use async.WaitGroup
to wait for these child goroutines to finish.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!