diff --git a/storage/memory/peer_store.go b/storage/memory/peer_store.go index 19a6c20..ecbf95a 100644 --- a/storage/memory/peer_store.go +++ b/storage/memory/peer_store.go @@ -12,7 +12,6 @@ import ( "time" log "github.com/Sirupsen/logrus" - "github.com/prometheus/client_golang/prometheus" "gopkg.in/yaml.v2" "github.com/chihaya/chihaya/bittorrent" @@ -23,44 +22,10 @@ import ( const Name = "memory" func init() { - // Register Prometheus metrics. - prometheus.MustRegister( - promGCDurationMilliseconds, - promInfohashesCount, - promSeedersCount, - promLeechersCount, - ) - // Register the storage driver. storage.RegisterDriver(Name, driver{}) } -var promGCDurationMilliseconds = prometheus.NewHistogram(prometheus.HistogramOpts{ - Name: "chihaya_storage_gc_duration_milliseconds", - Help: "The time it takes to perform storage garbage collection", - Buckets: prometheus.ExponentialBuckets(9.375, 2, 10), -}) - -var promInfohashesCount = prometheus.NewGauge(prometheus.GaugeOpts{ - Name: "chihaya_storage_infohashes_count", - Help: "The number of Infohashes tracked", -}) - -var promSeedersCount = prometheus.NewGauge(prometheus.GaugeOpts{ - Name: "chihaya_storage_seeders_count", - Help: "The number of seeders tracked", -}) - -var promLeechersCount = prometheus.NewGauge(prometheus.GaugeOpts{ - Name: "chihaya_storage_leechers_count", - Help: "The number of leechers tracked", -}) - -// recordGCDuration records the duration of a GC sweep. -func recordGCDuration(duration time.Duration) { - promGCDurationMilliseconds.Observe(float64(duration.Nanoseconds()) / float64(time.Millisecond)) -} - type driver struct{} func (d driver) NewPeerStore(icfg interface{}) (storage.PeerStore, error) { @@ -272,9 +237,14 @@ func (ps *peerStore) populateProm() { s.RUnlock() } - promInfohashesCount.Set(float64(numInfohashes)) - promSeedersCount.Set(float64(numSeeders)) - promLeechersCount.Set(float64(numLeechers)) + storage.PromInfohashesCount.Set(float64(numInfohashes)) + storage.PromSeedersCount.Set(float64(numSeeders)) + storage.PromLeechersCount.Set(float64(numLeechers)) +} + +// recordGCDuration records the duration of a GC sweep. +func recordGCDuration(duration time.Duration) { + storage.PromGCDurationMilliseconds.Observe(float64(duration.Nanoseconds()) / float64(time.Millisecond)) } func (ps *peerStore) getClock() int64 { diff --git a/storage/memorybysubnet/peer_store.go b/storage/memorybysubnet/peer_store.go index 08921b8..5234297 100644 --- a/storage/memorybysubnet/peer_store.go +++ b/storage/memorybysubnet/peer_store.go @@ -13,7 +13,6 @@ import ( "time" log "github.com/Sirupsen/logrus" - "github.com/prometheus/client_golang/prometheus" "gopkg.in/yaml.v2" "github.com/chihaya/chihaya/bittorrent" @@ -24,44 +23,10 @@ import ( const Name = "memorybysubnet" func init() { - // Register Prometheus metrics. - prometheus.MustRegister( - promGCDurationMilliseconds, - promInfohashesCount, - promSeedersCount, - promLeechersCount, - ) - // Register the storage driver. storage.RegisterDriver(Name, driver{}) } -var promGCDurationMilliseconds = prometheus.NewHistogram(prometheus.HistogramOpts{ - Name: "chihaya_storage_gc_duration_milliseconds", - Help: "The time it takes to perform storage garbage collection", - Buckets: prometheus.ExponentialBuckets(9.375, 2, 10), -}) - -var promInfohashesCount = prometheus.NewGauge(prometheus.GaugeOpts{ - Name: "chihaya_storage_infohashes_count", - Help: "The number of Infohashes tracked", -}) - -var promSeedersCount = prometheus.NewGauge(prometheus.GaugeOpts{ - Name: "chihaya_storage_seeders_count", - Help: "The number of seeders tracked", -}) - -var promLeechersCount = prometheus.NewGauge(prometheus.GaugeOpts{ - Name: "chihaya_storage_leechers_count", - Help: "The number of leechers tracked", -}) - -// recordGCDuration records the duration of a GC sweep. -func recordGCDuration(duration time.Duration) { - promGCDurationMilliseconds.Observe(float64(duration.Nanoseconds()) / float64(time.Millisecond)) -} - type driver struct{} func (d driver) NewPeerStore(icfg interface{}) (storage.PeerStore, error) { @@ -311,9 +276,14 @@ func (ps *peerStore) populateProm() { s.RUnlock() } - promInfohashesCount.Set(float64(numInfohashes)) - promSeedersCount.Set(float64(numSeeders)) - promLeechersCount.Set(float64(numLeechers)) + storage.PromInfohashesCount.Set(float64(numInfohashes)) + storage.PromSeedersCount.Set(float64(numSeeders)) + storage.PromLeechersCount.Set(float64(numLeechers)) +} + +// recordGCDuration records the duration of a GC sweep. +func recordGCDuration(duration time.Duration) { + storage.PromGCDurationMilliseconds.Observe(float64(duration.Nanoseconds()) / float64(time.Millisecond)) } func (ps *peerStore) getClock() int64 { diff --git a/storage/prometheus.go b/storage/prometheus.go new file mode 100644 index 0000000..3eb8899 --- /dev/null +++ b/storage/prometheus.go @@ -0,0 +1,44 @@ +package storage + +import "github.com/prometheus/client_golang/prometheus" + +func init() { + // Register the metrics. + prometheus.MustRegister( + PromGCDurationMilliseconds, + PromInfohashesCount, + PromSeedersCount, + PromLeechersCount, + ) +} + +var ( + // PromGCDurationMilliseconds is a histogram used by storage to record the + // durations of execution time required for removing expired peers. + PromGCDurationMilliseconds = prometheus.NewHistogram(prometheus.HistogramOpts{ + Name: "chihaya_storage_gc_duration_milliseconds", + Help: "The time it takes to perform storage garbage collection", + Buckets: prometheus.ExponentialBuckets(9.375, 2, 10), + }) + + // PromInfohashesCount is a gauge used to hold the current total amount of + // unique swarms being tracked by a storage. + PromInfohashesCount = prometheus.NewGauge(prometheus.GaugeOpts{ + Name: "chihaya_storage_infohashes_count", + Help: "The number of Infohashes tracked", + }) + + // PromSeedersCount is a gauge used to hold the current total amount of + // unique seeders per swarm. + PromSeedersCount = prometheus.NewGauge(prometheus.GaugeOpts{ + Name: "chihaya_storage_seeders_count", + Help: "The number of seeders tracked", + }) + + // PromLeechersCount is a gauge used to hold the current total amount of + // unique leechers per swarm. + PromLeechersCount = prometheus.NewGauge(prometheus.GaugeOpts{ + Name: "chihaya_storage_leechers_count", + Help: "The number of leechers tracked", + }) +)