Add support for logger and yml config
This commit is contained in:
parent
9e5450475e
commit
fa8c65f302
4 changed files with 127 additions and 2 deletions
|
@ -2,9 +2,12 @@ package main
|
|||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"git.faercol.me/monitoring/sys-exporter/config"
|
||||
"git.faercol.me/monitoring/sys-exporter/registry"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
|
||||
|
@ -15,6 +18,8 @@ import (
|
|||
_ "git.faercol.me/monitoring/sys-exporter/collector/uptime"
|
||||
)
|
||||
|
||||
var confFileLocation string
|
||||
|
||||
// type gatherer struct {
|
||||
// reg *prometheus.Registry
|
||||
// }
|
||||
|
@ -24,10 +29,27 @@ import (
|
|||
// }
|
||||
|
||||
func main() {
|
||||
fmt.Println("starting server")
|
||||
flag.Parse()
|
||||
|
||||
conf, err := config.New(confFileLocation)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to open configuration: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
logger, err := conf.ZapConfig().Build()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to init logger: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
logger.Info("Starting server")
|
||||
registry.R.Run(context.Background())
|
||||
|
||||
http.Handle("/metrics", promhttp.HandlerFor(registry.R.PromRegistry(), promhttp.HandlerOpts{}))
|
||||
http.ListenAndServe(":2112", nil)
|
||||
}
|
||||
|
||||
func init() {
|
||||
flag.StringVar(&confFileLocation, "config", config.DefaultConfigFileLocation, "Location of the config file")
|
||||
}
|
||||
|
|
82
config/config.go
Normal file
82
config/config.go
Normal file
|
@ -0,0 +1,82 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/goccy/go-yaml"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
|
||||
const DefaultConfigFileLocation = "/etc/sys-exporter/config.yml"
|
||||
|
||||
type logConfigJson struct {
|
||||
Level string `yaml:"level"`
|
||||
}
|
||||
|
||||
func (c logConfigJson) logConfig() (LogConfig, error) {
|
||||
lvl, err := zap.ParseAtomicLevel(c.Level)
|
||||
if err != nil {
|
||||
return LogConfig{}, err
|
||||
}
|
||||
return LogConfig{
|
||||
Level: lvl,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type appConfigJson struct {
|
||||
LogConfig logConfigJson `yaml:"logging"`
|
||||
}
|
||||
|
||||
func (c appConfigJson) appConfig() (AppConfig, error) {
|
||||
lc, err := c.LogConfig.logConfig()
|
||||
if err != nil {
|
||||
return AppConfig{}, err
|
||||
}
|
||||
|
||||
return AppConfig{
|
||||
LogConfig: lc,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type LogConfig struct {
|
||||
Level zap.AtomicLevel `yaml:"level"`
|
||||
}
|
||||
|
||||
func (c *LogConfig) ZapConfig() zap.Config {
|
||||
fmt.Println(c.Level)
|
||||
zapC := zap.Config{
|
||||
Level: c.Level,
|
||||
Development: true, // TODO: remove later
|
||||
Encoding: "console",
|
||||
EncoderConfig: zap.NewDevelopmentEncoderConfig(),
|
||||
OutputPaths: []string{"stdout"},
|
||||
ErrorOutputPaths: []string{"stderr"},
|
||||
}
|
||||
|
||||
zapC.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
|
||||
|
||||
return zapC
|
||||
}
|
||||
|
||||
type AppConfig struct {
|
||||
LogConfig `yaml:"logging"`
|
||||
}
|
||||
|
||||
func New(filepath string) (*AppConfig, error) {
|
||||
fileContent, err := os.ReadFile(filepath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to open config file: %w", err)
|
||||
}
|
||||
|
||||
var v appConfigJson
|
||||
if err := yaml.Unmarshal(fileContent, &v); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse config file: %w", err)
|
||||
}
|
||||
trueConf, err := v.appConfig()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to validate config: %w", err)
|
||||
}
|
||||
return &trueConf, nil
|
||||
}
|
7
go.mod
7
go.mod
|
@ -2,7 +2,11 @@ module git.faercol.me/monitoring/sys-exporter
|
|||
|
||||
go 1.23.3
|
||||
|
||||
require github.com/prometheus/client_golang v1.20.5
|
||||
require (
|
||||
github.com/goccy/go-yaml v1.15.6
|
||||
github.com/prometheus/client_golang v1.20.5
|
||||
go.uber.org/zap v1.27.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/beorn7/perks v1.0.1 // indirect
|
||||
|
@ -12,6 +16,7 @@ require (
|
|||
github.com/prometheus/client_model v0.6.1 // indirect
|
||||
github.com/prometheus/common v0.55.0 // indirect
|
||||
github.com/prometheus/procfs v0.15.1 // indirect
|
||||
go.uber.org/multierr v1.10.0 // indirect
|
||||
golang.org/x/sys v0.22.0 // indirect
|
||||
google.golang.org/protobuf v1.34.2 // indirect
|
||||
)
|
||||
|
|
16
go.sum
16
go.sum
|
@ -2,6 +2,10 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
|
|||
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
|
||||
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/goccy/go-yaml v1.15.6 h1:gy5kf1yjMia3/c3wWD+u1z3lU5XlhpT8FZGaLJU9cOA=
|
||||
github.com/goccy/go-yaml v1.15.6/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
|
||||
|
@ -10,6 +14,8 @@ github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0
|
|||
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/prometheus/client_golang v1.20.5 h1:cxppBPuYhUnsO6yo/aoRol4L7q7UFfdm+bR9r+8l63Y=
|
||||
github.com/prometheus/client_golang v1.20.5/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
|
||||
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
|
||||
|
@ -18,7 +24,17 @@ github.com/prometheus/common v0.55.0 h1:KEi6DK7lXW/m7Ig5i47x0vRzuBsHuvJdi5ee6Y3G
|
|||
github.com/prometheus/common v0.55.0/go.mod h1:2SECS4xJG1kd8XF9IcM1gMX6510RAEL65zxzNImwdc8=
|
||||
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
|
||||
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
|
||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
||||
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
|
||||
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
|
||||
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
|
||||
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
|
||||
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
|
||||
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
|
||||
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
|
||||
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
|
Loading…
Reference in a new issue