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"
|
||||
)
|
||||
|
||||
func init() {
|
||||
prometheus.MustRegister(promResponseDurationMilliseconds)
|
||||
recordResponseDuration("action", nil, time.Second)
|
||||
}
|
||||
|
||||
var promResponseDurationMilliseconds = prometheus.NewHistogramVec(
|
||||
prometheus.HistogramOpts{
|
||||
Name: "trakr_http_response_duration_milliseconds",
|
||||
|
|
|
@ -31,6 +31,11 @@ import (
|
|||
"github.com/jzelinskie/trakr/bittorrent/udp/bytepool"
|
||||
)
|
||||
|
||||
func init() {
|
||||
prometheus.MustRegister(promResponseDurationMilliseconds)
|
||||
recordResponseDuration("action", nil, time.Second)
|
||||
}
|
||||
|
||||
var promResponseDurationMilliseconds = prometheus.NewHistogramVec(
|
||||
prometheus.HistogramOpts{
|
||||
Name: "trakr_udp_response_duration_milliseconds",
|
||||
|
|
|
@ -2,17 +2,57 @@ package main
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"runtime/pprof"
|
||||
"syscall"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/spf13/cobra"
|
||||
"gopkg.in/yaml.v2"
|
||||
|
||||
"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() {
|
||||
var configFilePath string
|
||||
var cpuProfilePath string
|
||||
|
@ -33,19 +73,30 @@ func main() {
|
|||
defer pprof.StopCPUProfile()
|
||||
}
|
||||
|
||||
mt, err := trakr.MultiTrackerFromFile(configFilePath)
|
||||
configFile, err := ParseConfigFile(configFilePath)
|
||||
if err != nil {
|
||||
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() {
|
||||
shutdown := make(chan os.Signal)
|
||||
signal.Notify(shutdown, syscall.SIGINT, syscall.SIGTERM)
|
||||
<-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())
|
||||
}
|
||||
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
trakr:
|
||||
announce_interval: 15m
|
||||
gc_interval: 15m
|
||||
gc_expiration: 15m
|
||||
allow_ip_spoofing: true
|
||||
default_num_want: 50
|
||||
prometheus_addr: localhost:6880
|
||||
|
||||
http:
|
||||
addr: 0.0.0.0:6881
|
||||
|
|
54
tracker.go
54
tracker.go
|
@ -18,15 +18,10 @@
|
|||
package trakr
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/jzelinskie/trakr/bittorrent/http"
|
||||
"github.com/jzelinskie/trakr/bittorrent/udp"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
// GenericConfig is a block of configuration who's structure is unknown.
|
||||
|
@ -49,61 +44,28 @@ type MultiTracker struct {
|
|||
peerStore PeerStore
|
||||
httpTracker http.Tracker
|
||||
udpTracker udp.Tracker
|
||||
}
|
||||
|
||||
// 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
|
||||
closing chan struct{}
|
||||
}
|
||||
|
||||
// Stop provides a thread-safe way to shutdown a currently running
|
||||
// MultiTracker.
|
||||
func (t *MultiTracker) Stop() {
|
||||
close(t.closing)
|
||||
}
|
||||
|
||||
// ListenAndServe listens on the protocols and addresses specified in the
|
||||
// HTTPConfig and UDPConfig then blocks serving BitTorrent requests until
|
||||
// t.Stop() is called or an error is returned.
|
||||
func (t *MultiTracker) ListenAndServe() error {
|
||||
t.closing = make(chan struct{})
|
||||
// Build an TrackerFuncs from the PreHooks and PostHooks.
|
||||
// Create a PeerStore instance.
|
||||
// Create a HTTP Tracker instance.
|
||||
// Create a UDP Tracker instance.
|
||||
select {
|
||||
case <-t.closing:
|
||||
return nil
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue