Skip to content
Snippets Groups Projects
Commit e09c0f536372 authored by Christophe de Vienne's avatar Christophe de Vienne
Browse files

Enable metrics on /metrics, add '--environment' flag, add 'xxx_info' gauge

parent 2e7fd36e7588
No related branches found
No related tags found
No related merge requests found
Pipeline #7536 passed
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
github.com/go-openapi/swag v0.19.9 github.com/go-openapi/swag v0.19.9
github.com/golang-migrate/migrate/v4 v4.12.2 github.com/golang-migrate/migrate/v4 v4.12.2
github.com/jmoiron/sqlx v1.2.1-0.20200615141059-0794cb1f47ee github.com/jmoiron/sqlx v1.2.1-0.20200615141059-0794cb1f47ee
github.com/json-iterator/go v1.1.6 github.com/json-iterator/go v1.1.10
github.com/justinas/alice v1.2.0 github.com/justinas/alice v1.2.0
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 github.com/lann/builder v0.0.0-20180802200727-47ae307949d0
github.com/orus-io/go-flags v1.4.0 github.com/orus-io/go-flags v1.4.0
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
github.com/justinas/alice v1.2.0 github.com/justinas/alice v1.2.0
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 github.com/lann/builder v0.0.0-20180802200727-47ae307949d0
github.com/orus-io/go-flags v1.4.0 github.com/orus-io/go-flags v1.4.0
github.com/prometheus/client_golang v1.8.0
github.com/rs/zerolog v1.19.0 github.com/rs/zerolog v1.19.0
github.com/stretchr/testify v1.6.1 github.com/stretchr/testify v1.6.1
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a
......
This diff is collapsed.
package orusapi
import (
"fmt"
"net/http"
"strings"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// Prometheus serves the '/metrics' endpoint
func Prometheus(path string) func(next http.Handler) http.Handler {
handler := promhttp.Handler()
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, path) {
handler.ServeHTTP(rw, r)
return
}
if next == nil {
rw.Header().Set("Content-Type", "text/plain")
rw.WriteHeader(http.StatusNotFound)
_, _ = rw.Write([]byte(fmt.Sprintf("%q not found", path)))
return
}
next.ServeHTTP(rw, r)
})
}
}
...@@ -90,6 +90,8 @@ ...@@ -90,6 +90,8 @@
Prometheus bool `long:"prometheus" description:"enable prometheus metrics on /metrics"` Prometheus bool `long:"prometheus" description:"enable prometheus metrics on /metrics"`
Environment string `no-flag:"t" description:"A environment name"`
api API api API
log zerolog.Logger log zerolog.Logger
handler http.Handler handler http.Handler
...@@ -449,6 +451,9 @@ ...@@ -449,6 +451,9 @@
// SetHandler allows for setting a http handler on this server // SetHandler allows for setting a http handler on this server
func (s *Server) SetHandler(handler http.Handler) { func (s *Server) SetHandler(handler http.Handler) {
s.handler = handler s.handler = handler
if s.Prometheus {
s.handler = Prometheus("/metrics")(s.handler)
}
} }
// UnixListener returns the domain socket listener // UnixListener returns the domain socket listener
......
...@@ -8,7 +8,11 @@ ...@@ -8,7 +8,11 @@
"orus.io/orus-io/go-orusapi/database" "orus.io/orus-io/go-orusapi/database"
) )
var (
Version string
)
type ConfigFile struct { type ConfigFile struct {
ConfigFile string `long:"config" short:"c" env:"CONFIG" no-ini:"t" description:"A configuration file"` ConfigFile string `long:"config" short:"c" env:"CONFIG" no-ini:"t" description:"A configuration file"`
} }
...@@ -11,9 +15,13 @@ ...@@ -11,9 +15,13 @@
type ConfigFile struct { type ConfigFile struct {
ConfigFile string `long:"config" short:"c" env:"CONFIG" no-ini:"t" description:"A configuration file"` ConfigFile string `long:"config" short:"c" env:"CONFIG" no-ini:"t" description:"A configuration file"`
} }
type Info struct {
Environment string `long:"environment" env:"ENVIRONMENT" ini-name:"environment" default:"default" description:"A environment name, used in sentry and prometheus"`
}
var ( var (
Logger = orusapi.DefaultLogger(os.Stdout) Logger = orusapi.DefaultLogger(os.Stdout)
LoggingOptions = orusapi.MustLoggingOptions(orusapi.NewLoggingOptions(&Logger, os.Stdout)) LoggingOptions = orusapi.MustLoggingOptions(orusapi.NewLoggingOptions(&Logger, os.Stdout))
DatabaseOptions = &database.Options{} DatabaseOptions = &database.Options{}
ConfigFileOption = &ConfigFile{} ConfigFileOption = &ConfigFile{}
...@@ -15,8 +23,10 @@ ...@@ -15,8 +23,10 @@
var ( var (
Logger = orusapi.DefaultLogger(os.Stdout) Logger = orusapi.DefaultLogger(os.Stdout)
LoggingOptions = orusapi.MustLoggingOptions(orusapi.NewLoggingOptions(&Logger, os.Stdout)) LoggingOptions = orusapi.MustLoggingOptions(orusapi.NewLoggingOptions(&Logger, os.Stdout))
DatabaseOptions = &database.Options{} DatabaseOptions = &database.Options{}
ConfigFileOption = &ConfigFile{} ConfigFileOption = &ConfigFile{}
InfoOptions = &Info{}
SentryOptions = orusapi.NewSentryOptions(LoggingOptions) SentryOptions = orusapi.NewSentryOptions(LoggingOptions)
parser = flags.NewNamedParser("{{ dasherize (pascalize .Name) }}", flags.HelpFlag|flags.PassDoubleDash) parser = flags.NewNamedParser("{{ dasherize (pascalize .Name) }}", flags.HelpFlag|flags.PassDoubleDash)
...@@ -64,7 +74,12 @@ ...@@ -64,7 +74,12 @@
if _, err := parser.AddGroup("Configuration", "Configuration file", ConfigFileOption); err != nil { if _, err := parser.AddGroup("Configuration", "Configuration file", ConfigFileOption); err != nil {
panic(err) panic(err)
} }
g, err := parser.AddGroup("Logging", "Logging options", LoggingOptions) g, err := parser.AddGroup("Info", "Info options", InfoOptions)
if err != nil {
panic(err)
}
g, err = parser.AddGroup("Logging", "Logging options", LoggingOptions)
if err != nil { if err != nil {
panic(err) panic(err)
} }
......
...@@ -65,7 +65,21 @@ ...@@ -65,7 +65,21 @@
} }
if cmd.Server.Prometheus { if cmd.Server.Prometheus {
var infoGauge = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "{{ snakize .Name }}_info",
Help: "Informations about the {{ pascalize .Name }} server",
ConstLabels: map[string]string{
"environment": InfoOptions.Environment,
"version": Version,
},
})
infoGauge.Set(1)
if err := prometheus.Register(infoGauge); err != nil {
return err
}
defer prometheus.Unregister(infoGauge)
collector := sqlstats.NewStatsCollector("db", db) collector := sqlstats.NewStatsCollector("db", db)
if err := prometheus.Register(collector); err != nil { if err := prometheus.Register(collector); err != nil {
return err return err
} }
...@@ -68,7 +82,8 @@ ...@@ -68,7 +82,8 @@
collector := sqlstats.NewStatsCollector("db", db) collector := sqlstats.NewStatsCollector("db", db)
if err := prometheus.Register(collector); err != nil { if err := prometheus.Register(collector); err != nil {
return err return err
} }
defer prometheus.Unregister(collector)
} }
config := restapi.Config{ config := restapi.Config{
...@@ -80,6 +95,7 @@ ...@@ -80,6 +95,7 @@
} }
cmd.Server.SetLog(Logger) cmd.Server.SetLog(Logger)
cmd.Server.Environment = InfoOptions.Environment
configuredAPI, err := restapi.ConfigureAPI(cmd.API, config) configuredAPI, err := restapi.ConfigureAPI(cmd.API, config)
if err != nil { if err != nil {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment