Add db metrics
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user