Add db metrics

This commit is contained in:
Ben Johnson
2020-12-31 16:09:35 -07:00
parent da5087c14c
commit 9d0e79c2cf
5 changed files with 590 additions and 20 deletions

View File

@@ -93,14 +93,25 @@ The commands are:
`[1:])
}
// Default configuration settings.
const (
DefaultAddr = ":9090"
)
// Config represents a configuration file for the litestream daemon.
type Config struct {
// Bind address for serving metrics.
Addr string `yaml:"addr"`
// List of databases to manage.
DBs []*DBConfig `yaml:"databases"`
}
// DefaultConfig returns a new instance of Config with defaults set.
func DefaultConfig() Config {
return Config{}
return Config{
Addr: DefaultAddr,
}
}
func (c *Config) DBConfig(path string) *DBConfig {

View File

@@ -5,10 +5,13 @@ import (
"errors"
"flag"
"fmt"
"net"
"net/http"
"os"
"os/signal"
"github.com/benbjohnson/litestream"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type ReplicateCommand struct {
@@ -38,7 +41,7 @@ func (c *ReplicateCommand) Run(ctx context.Context, args []string) (err error) {
}
// Setup signal handler.
ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(ctx)
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt)
go func() { <-ch; cancel() }()
@@ -66,6 +69,16 @@ func (c *ReplicateCommand) Run(ctx context.Context, args []string) (err error) {
// Notify user that initialization is done.
fmt.Printf("Initialized with %d databases.\n", len(c.DBs))
// Serve metrics over HTTP if enabled.
if config.Addr != "" {
_, port, _ := net.SplitHostPort(config.Addr)
fmt.Printf("Serving metrics on http://localhost:%s/metrics\n", port)
go func() {
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(config.Addr, nil)
}()
}
// Wait for signal to stop program.
<-ctx.Done()
signal.Reset()