Expose Prometheus metrics on a separate port
Metrics are served on METRICS_PORT (default 2112, "off" to disable), never on the public port. Besides the Go runtime and process metrics this adds: - http_requests_total and http_request_duration_seconds per route pattern, from an Echo middleware. - musicserver_songs_requested_total and musicserver_songs_played_total by pick mode (random, low, classic). The mode is now stored with each queued song (migration 000006, not released yet). - Library size, sync duration, last successful sync, broken songs from the last full sync, and the database connection pool. Refs #35, #36, #37 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c62b65d7cd
commit
f79f51ea4c
28 files changed
+864
-70
No files matched your search
@@ -17,6 +17,9 @@ const DefaultReleasesURL = "https://gitea.sanplex.xyz/api/v1/repos/sansan/MusicP
|
||||
// Config holds all settings read from the environment.
|
||||
type Config struct {
|
||||
Port int
|
||||
// MetricsPort serves Prometheus metrics on a port of its own, so /metrics is never
|
||||
// reachable through the public port. 0 means metrics are not served.
|
||||
MetricsPort int
|
||||
|
||||
DBHost string
|
||||
DBPort string
|
||||
@@ -77,6 +80,11 @@ func load(getenv func(string) string) (Config, error) {
|
||||
}
|
||||
cfg.Port = port
|
||||
|
||||
cfg.MetricsPort, err = parseMetricsPort(getenv("METRICS_PORT"))
|
||||
if err != nil {
|
||||
errs = append(errs, fmt.Errorf("METRICS_PORT: %w", err))
|
||||
}
|
||||
|
||||
for name, value := range map[string]string{
|
||||
"DB_HOST": cfg.DBHost,
|
||||
"DB_PORT": cfg.DBPort,
|
||||
@@ -120,6 +128,24 @@ func parseCIDRs(value string) ([]*net.IPNet, error) {
|
||||
return nets, nil
|
||||
}
|
||||
|
||||
// DefaultMetricsPort is used when METRICS_PORT is not set.
|
||||
const DefaultMetricsPort = 2112
|
||||
|
||||
// parseMetricsPort reads METRICS_PORT: empty for the default, "off" for no metrics, or a port.
|
||||
func parseMetricsPort(value string) (int, error) {
|
||||
switch value {
|
||||
case "":
|
||||
return DefaultMetricsPort, nil
|
||||
case "off":
|
||||
return 0, nil
|
||||
}
|
||||
port, err := strconv.Atoi(value)
|
||||
if err != nil || port < 1 || port > 65535 {
|
||||
return 0, fmt.Errorf("%q is not a port or \"off\"", value)
|
||||
}
|
||||
return port, nil
|
||||
}
|
||||
|
||||
// parseOrigins parses a comma-separated list of origins (scheme://host[:port], nothing else).
|
||||
func parseOrigins(value string) ([]string, error) {
|
||||
var origins []string
|
||||
|
||||
Reference in new issue
Block a user