get prom working
This commit is contained in:
parent
5c99738b7f
commit
11d135ce49
5 changed files with 75 additions and 49 deletions
|
@ -29,6 +29,11 @@ import (
|
||||||
"github.com/jzelinskie/trakr/bittorrent"
|
"github.com/jzelinskie/trakr/bittorrent"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
prometheus.MustRegister(promResponseDurationMilliseconds)
|
||||||
|
recordResponseDuration("action", nil, time.Second)
|
||||||
|
}
|
||||||
|
|
||||||
var promResponseDurationMilliseconds = prometheus.NewHistogramVec(
|
var promResponseDurationMilliseconds = prometheus.NewHistogramVec(
|
||||||
prometheus.HistogramOpts{
|
prometheus.HistogramOpts{
|
||||||
Name: "trakr_http_response_duration_milliseconds",
|
Name: "trakr_http_response_duration_milliseconds",
|
||||||
|
|
|
@ -31,6 +31,11 @@ import (
|
||||||
"github.com/jzelinskie/trakr/bittorrent/udp/bytepool"
|
"github.com/jzelinskie/trakr/bittorrent/udp/bytepool"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
prometheus.MustRegister(promResponseDurationMilliseconds)
|
||||||
|
recordResponseDuration("action", nil, time.Second)
|
||||||
|
}
|
||||||
|
|
||||||
var promResponseDurationMilliseconds = prometheus.NewHistogramVec(
|
var promResponseDurationMilliseconds = prometheus.NewHistogramVec(
|
||||||
prometheus.HistogramOpts{
|
prometheus.HistogramOpts{
|
||||||
Name: "trakr_udp_response_duration_milliseconds",
|
Name: "trakr_udp_response_duration_milliseconds",
|
||||||
|
|
|
@ -2,17 +2,57 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"runtime/pprof"
|
"runtime/pprof"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"gopkg.in/yaml.v2"
|
||||||
|
|
||||||
"github.com/jzelinskie/trakr"
|
"github.com/jzelinskie/trakr"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ConfigFile struct {
|
||||||
|
Config struct {
|
||||||
|
PrometheusAddr string `yaml:"prometheus_addr"`
|
||||||
|
trakr.MultiTracker
|
||||||
|
} `yaml:"trakr"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseConfigFile returns a new ConfigFile given the path to a YAML
|
||||||
|
// configuration file.
|
||||||
|
//
|
||||||
|
// It supports relative and absolute paths and environment variables.
|
||||||
|
func ParseConfigFile(path string) (*ConfigFile, error) {
|
||||||
|
if path == "" {
|
||||||
|
return nil, errors.New("no config path specified")
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err := os.Open(os.ExpandEnv(path))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
contents, err := ioutil.ReadAll(f)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var cfgFile ConfigFile
|
||||||
|
err = yaml.Unmarshal(contents, &cfgFile)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &cfgFile, nil
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var configFilePath string
|
var configFilePath string
|
||||||
var cpuProfilePath string
|
var cpuProfilePath string
|
||||||
|
@ -33,19 +73,30 @@ func main() {
|
||||||
defer pprof.StopCPUProfile()
|
defer pprof.StopCPUProfile()
|
||||||
}
|
}
|
||||||
|
|
||||||
mt, err := trakr.MultiTrackerFromFile(configFilePath)
|
configFile, err := ParseConfigFile(configFilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("failed to read config: " + err.Error())
|
return errors.New("failed to read config: " + err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
promServer := http.Server{
|
||||||
|
Addr: configFile.Config.PrometheusAddr,
|
||||||
|
Handler: prometheus.Handler(),
|
||||||
|
}
|
||||||
|
log.Println("started serving prometheus stats on", configFile.Config.PrometheusAddr)
|
||||||
|
if err := promServer.ListenAndServe(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
shutdown := make(chan os.Signal)
|
shutdown := make(chan os.Signal)
|
||||||
signal.Notify(shutdown, syscall.SIGINT, syscall.SIGTERM)
|
signal.Notify(shutdown, syscall.SIGINT, syscall.SIGTERM)
|
||||||
<-shutdown
|
<-shutdown
|
||||||
mt.Stop()
|
configFile.Config.MultiTracker.Stop()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if err := mt.ListenAndServe(); err != nil {
|
if err := configFile.Config.MultiTracker.ListenAndServe(); err != nil {
|
||||||
return errors.New("failed to cleanly shutdown: " + err.Error())
|
return errors.New("failed to cleanly shutdown: " + err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
trakr:
|
trakr:
|
||||||
announce_interval: 15m
|
announce_interval: 15m
|
||||||
|
gc_interval: 15m
|
||||||
|
gc_expiration: 15m
|
||||||
allow_ip_spoofing: true
|
allow_ip_spoofing: true
|
||||||
default_num_want: 50
|
default_num_want: 50
|
||||||
|
prometheus_addr: localhost:6880
|
||||||
|
|
||||||
http:
|
http:
|
||||||
addr: 0.0.0.0:6881
|
addr: 0.0.0.0:6881
|
||||||
|
|
54
tracker.go
54
tracker.go
|
@ -18,15 +18,10 @@
|
||||||
package trakr
|
package trakr
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jzelinskie/trakr/bittorrent/http"
|
"github.com/jzelinskie/trakr/bittorrent/http"
|
||||||
"github.com/jzelinskie/trakr/bittorrent/udp"
|
"github.com/jzelinskie/trakr/bittorrent/udp"
|
||||||
"gopkg.in/yaml.v2"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// GenericConfig is a block of configuration who's structure is unknown.
|
// GenericConfig is a block of configuration who's structure is unknown.
|
||||||
|
@ -49,61 +44,28 @@ type MultiTracker struct {
|
||||||
peerStore PeerStore
|
peerStore PeerStore
|
||||||
httpTracker http.Tracker
|
httpTracker http.Tracker
|
||||||
udpTracker udp.Tracker
|
udpTracker udp.Tracker
|
||||||
}
|
closing chan struct{}
|
||||||
|
|
||||||
// decodeConfigFile unmarshals an io.Reader into a new MultiTracker.
|
|
||||||
func decodeConfigFile(r io.Reader) (*MultiTracker, error) {
|
|
||||||
contents, err := ioutil.ReadAll(r)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
cfgFile := struct {
|
|
||||||
mt MultiTracker `yaml:"trakr"`
|
|
||||||
}{}
|
|
||||||
err = yaml.Unmarshal(contents, cfgFile)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return &cfgFile.mt, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// MultiTrackerFromFile returns a new MultiTracker given the path to a YAML
|
|
||||||
// configuration file.
|
|
||||||
//
|
|
||||||
// It supports relative and absolute paths and environment variables.
|
|
||||||
func MultiTrackerFromFile(path string) (*MultiTracker, error) {
|
|
||||||
if path == "" {
|
|
||||||
return nil, errors.New("no config path specified")
|
|
||||||
}
|
|
||||||
|
|
||||||
f, err := os.Open(os.ExpandEnv(path))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
cfg, err := decodeConfigFile(f)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return cfg, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop provides a thread-safe way to shutdown a currently running
|
// Stop provides a thread-safe way to shutdown a currently running
|
||||||
// MultiTracker.
|
// MultiTracker.
|
||||||
func (t *MultiTracker) Stop() {
|
func (t *MultiTracker) Stop() {
|
||||||
|
close(t.closing)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListenAndServe listens on the protocols and addresses specified in the
|
// ListenAndServe listens on the protocols and addresses specified in the
|
||||||
// HTTPConfig and UDPConfig then blocks serving BitTorrent requests until
|
// HTTPConfig and UDPConfig then blocks serving BitTorrent requests until
|
||||||
// t.Stop() is called or an error is returned.
|
// t.Stop() is called or an error is returned.
|
||||||
func (t *MultiTracker) ListenAndServe() error {
|
func (t *MultiTracker) ListenAndServe() error {
|
||||||
|
t.closing = make(chan struct{})
|
||||||
// Build an TrackerFuncs from the PreHooks and PostHooks.
|
// Build an TrackerFuncs from the PreHooks and PostHooks.
|
||||||
// Create a PeerStore instance.
|
// Create a PeerStore instance.
|
||||||
// Create a HTTP Tracker instance.
|
// Create a HTTP Tracker instance.
|
||||||
// Create a UDP Tracker instance.
|
// Create a UDP Tracker instance.
|
||||||
|
select {
|
||||||
|
case <-t.closing:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue