From 81a2596fb881ee670c608c8dc730891f227905cf Mon Sep 17 00:00:00 2001 From: Takeshi NAMAO Date: Wed, 25 Sep 2024 15:52:51 +0900 Subject: [PATCH 1/6] =?UTF-8?q?Go=201.23=20=E3=81=AB=E4=B8=8A=E3=81=92?= =?UTF-8?q?=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 68bf4a5..33f172d 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/shiguredo/sora_exporter -go 1.22 +go 1.23 require ( github.com/alecthomas/kingpin/v2 v2.4.0 From 5fd8ba760fb41908bc3155b2603a221b3de3c2d2 Mon Sep 17 00:00:00 2001 From: Takeshi NAMAO Date: Wed, 25 Sep 2024 15:53:16 +0900 Subject: [PATCH 2/6] =?UTF-8?q?exporter=20=E3=83=A9=E3=82=A4=E3=83=96?= =?UTF-8?q?=E3=83=A9=E3=83=AA=E3=81=AE=E6=9B=B4=E6=96=B0=E3=81=A8=20logger?= =?UTF-8?q?=20=E3=83=A9=E3=82=A4=E3=83=96=E3=83=A9=E3=83=AA=E5=A4=89?= =?UTF-8?q?=E6=9B=B4=E3=81=AE=E8=BF=BD=E5=BE=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- collector/collector.go | 27 ++++++++--------- go.mod | 28 ++++++++++-------- go.sum | 67 ++++++++++++++++++++++++------------------ main.go | 30 +++++++++---------- main_test.go | 27 +++++++++++------ 5 files changed, 99 insertions(+), 80 deletions(-) diff --git a/collector/collector.go b/collector/collector.go index a92a227..6fe9189 100644 --- a/collector/collector.go +++ b/collector/collector.go @@ -5,12 +5,11 @@ import ( "context" "crypto/tls" "encoding/json" + "log/slog" "net/http" "sync" "time" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/prometheus/client_golang/prometheus" ) @@ -21,7 +20,7 @@ var ( type Collector struct { mutex sync.RWMutex - logger log.Logger + logger *slog.Logger timeout time.Duration URI string skipSslVerify bool @@ -49,7 +48,7 @@ type CollectorOptions struct { SkipSslVerify bool Timeout time.Duration FreezeTimeSeconds bool - Logger log.Logger + Logger *slog.Logger EnableSoraClientMetrics bool EnableSoraConnectionErrorMetrics bool EnableErlangVMMetrics bool @@ -103,7 +102,7 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) { req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.URI, nil) if err != nil { - level.Error(c.logger).Log("msg", "failed to create request to sora", "err", err) + c.logger.Error("failed to create request to sora", "err", err) ch <- newGauge(c.soraUp, 0) return } @@ -117,7 +116,7 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) { resp, err := client.Do(req) if err != nil { - level.Error(c.logger).Log("msg", "failed to request to Sora GetStatsReport API", "err", err) + c.logger.Error("failed to request to Sora GetStatsReport API", "err", err) ch <- newGauge(c.soraUp, 0) return } @@ -125,7 +124,7 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) { var report soraGetStatsReport if err := json.NewDecoder(resp.Body).Decode(&report); err != nil { - level.Error(c.logger).Log("msg", "failed to decode response body from Sora GetStatsReport API", "err", err) + c.logger.Error("failed to decode response body from Sora GetStatsReport API", "err", err) ch <- newGauge(c.soraUp, 0) return } @@ -137,14 +136,14 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) { } encodedParams, err := json.Marshal(requestParams) if err != nil { - level.Error(c.logger).Log("msg", "failed to encode Sora ListClusterNodes API request parameters", "err", err) + c.logger.Error("failed to encode Sora ListClusterNodes API request parameters", "err", err) ch <- newGauge(c.soraUp, 0) return } req, err = http.NewRequestWithContext(ctx, http.MethodPost, c.URI, bytes.NewBuffer(encodedParams)) if err != nil { - level.Error(c.logger).Log("msg", "failed to create request to sora", "err", err) + c.logger.Error("failed to create request to sora", "err", err.Error()) ch <- newGauge(c.soraUp, 0) return } @@ -152,14 +151,14 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) { nodeResp, err := client.Do(req) if err != nil { - level.Error(c.logger).Log("msg", "failed to request to Sora ListClusterNodes API", "err", err) + c.logger.Error("failed to request to Sora ListClusterNodes API", "err", err) ch <- newGauge(c.soraUp, 0) return } defer nodeResp.Body.Close() if err := json.NewDecoder(nodeResp.Body).Decode(&nodeList); err != nil { - level.Error(c.logger).Log("msg", "failed to decode response body from Sora ListClusterNodes API", "err", err) + c.logger.Error("failed to decode response body from Sora ListClusterNodes API", "err", err) ch <- newGauge(c.soraUp, 0) return } @@ -167,7 +166,7 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) { req, err = http.NewRequestWithContext(ctx, http.MethodPost, c.URI, nil) if err != nil { - level.Error(c.logger).Log("msg", "failed to create request to sora", "err", err) + c.logger.Error("failed to create request to sora", "err", err) ch <- newGauge(c.soraUp, 0) return } @@ -175,7 +174,7 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) { licenseResp, err := client.Do(req) if err != nil { - level.Error(c.logger).Log("msg", "failed to request to Sora GetLicense API", "err", err) + c.logger.Error("failed to request to Sora GetLicense API", "err", err) ch <- newGauge(c.soraUp, 0) return } @@ -183,7 +182,7 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) { var licenseInfo soraLicenseInfo if err := json.NewDecoder(licenseResp.Body).Decode(&licenseInfo); err != nil { - level.Error(c.logger).Log("msg", "failed to decode response body from Sora GetLicense API", "err", err) + c.logger.Error("failed to decode response body from Sora GetLicense API", "err", err) ch <- newGauge(c.soraUp, 0) return } diff --git a/go.mod b/go.mod index 33f172d..150d97d 100644 --- a/go.mod +++ b/go.mod @@ -4,30 +4,32 @@ go 1.23 require ( github.com/alecthomas/kingpin/v2 v2.4.0 - github.com/go-kit/log v0.2.1 - github.com/prometheus/client_golang v1.19.1 - github.com/prometheus/common v0.54.0 - github.com/prometheus/exporter-toolkit v0.11.0 + github.com/prometheus/client_golang v1.20.4 + github.com/prometheus/common v0.59.1 + github.com/prometheus/exporter-toolkit v0.13.0 ) require ( - github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9 // indirect + github.com/alecthomas/units v0.0.0-20240626203959-61d1e3462e30 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/coreos/go-systemd/v22 v22.5.0 // indirect - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/go-logfmt/logfmt v0.6.0 // indirect github.com/jpillora/backoff v1.0.0 // indirect + github.com/klauspost/compress v1.17.10 // indirect + github.com/kylelemons/godebug v1.1.0 // indirect + github.com/mdlayher/socket v0.5.1 // indirect + github.com/mdlayher/vsock v1.2.1 // indirect + github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect github.com/prometheus/client_model v0.6.1 // indirect github.com/prometheus/procfs v0.15.1 // indirect github.com/xhit/go-str2duration/v2 v2.1.0 // indirect - golang.org/x/crypto v0.24.0 // indirect - golang.org/x/net v0.26.0 // indirect - golang.org/x/oauth2 v0.21.0 // indirect - golang.org/x/sync v0.7.0 // indirect - golang.org/x/sys v0.21.0 // indirect - golang.org/x/text v0.16.0 // indirect + golang.org/x/crypto v0.27.0 // indirect + golang.org/x/net v0.29.0 // indirect + golang.org/x/oauth2 v0.23.0 // indirect + golang.org/x/sync v0.8.0 // indirect + golang.org/x/sys v0.25.0 // indirect + golang.org/x/text v0.18.0 // indirect google.golang.org/protobuf v1.34.2 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect ) diff --git a/go.sum b/go.sum index 698baed..84cabf4 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ github.com/alecthomas/kingpin/v2 v2.4.0 h1:f48lwail6p8zpO1bC4TxtqACaGqHYA22qkHjHpqDjYY= github.com/alecthomas/kingpin/v2 v2.4.0/go.mod h1:0gyi0zQnjuFk8xrkNKamJoyUo382HRL7ATRpFZCw6tE= -github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9 h1:ez/4by2iGztzR4L0zgAOR8lTQK9VlyBVVd7G4omaOQs= -github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE= +github.com/alecthomas/units v0.0.0-20240626203959-61d1e3462e30 h1:t3eaIm0rUkzbrIewtiFmMK5RXHej2XnoXNhxVsAYUfg= +github.com/alecthomas/units v0.0.0-20240626203959-61d1e3462e30/go.mod h1:fvzegU4vN3H1qMT+8wDmzjAcDONcgo2/SZ/TyfdUOFs= 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= @@ -11,60 +11,71 @@ github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSV github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 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/go-kit/log v0.2.1 h1:MRVx0/zhvdseW+Gza6N9rVzU/IVzaeE1SFI4raAhmBU= -github.com/go-kit/log v0.2.1/go.mod h1:NwTd00d/i8cPZ3xOwwiv2PO5MOcx78fFErGNcVmBjv0= -github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4= -github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= 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/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA= github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= +github.com/klauspost/compress v1.17.10 h1:oXAz+Vh0PMUvJczoi+flxpnBEPxoER1IaAnU/NMPtT0= +github.com/klauspost/compress v1.17.10/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= +github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= +github.com/mdlayher/socket v0.5.1 h1:VZaqt6RkGkt2OE9l3GcC6nZkqD3xKeQLyfleW/uBcos= +github.com/mdlayher/socket v0.5.1/go.mod h1:TjPLHI1UgwEv5J1B5q0zTZq12A/6H7nKmtTanQE37IQ= +github.com/mdlayher/vsock v1.2.1 h1:pC1mTJTvjo1r9n9fbm7S1j04rCgCzhCOS5DY0zqHlnQ= +github.com/mdlayher/vsock v1.2.1/go.mod h1:NRfCibel++DgeMD8z/hP+PPTjlNJsdPOmxcnENvE+SE= +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/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f h1:KUppIJq7/+SVif2QVs3tOP0zanoHgBEVAwHxUSIzRqU= github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= 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.19.1 h1:wZWJDwK+NameRJuPGDhlnFgx8e8HN3XHQeLaYJFJBOE= -github.com/prometheus/client_golang v1.19.1/go.mod h1:mP78NwGzrVks5S2H6ab8+ZZGJLZUq1hoULYBAYBw1Ho= +github.com/prometheus/client_golang v1.20.4 h1:Tgh3Yr67PaOv/uTqloMsCEdeuFTatm5zIq5+qNN23vI= +github.com/prometheus/client_golang v1.20.4/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE= github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E= github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY= -github.com/prometheus/common v0.54.0 h1:ZlZy0BgJhTwVZUn7dLOkwCZHUkrAqd3WYtcFCWnM1D8= -github.com/prometheus/common v0.54.0/go.mod h1:/TQgMJP5CuVYveyT7n/0Ix8yLNNXy9yRSkhnLTHPDIQ= -github.com/prometheus/exporter-toolkit v0.11.0 h1:yNTsuZ0aNCNFQ3aFTD2uhPOvr4iD7fdBvKPAEGkNf+g= -github.com/prometheus/exporter-toolkit v0.11.0/go.mod h1:BVnENhnNecpwoTLiABx7mrPB/OLRIgN74qlQbV+FK1Q= +github.com/prometheus/common v0.59.1 h1:LXb1quJHWm1P6wq/U824uxYi4Sg0oGvNeUm1z5dJoX0= +github.com/prometheus/common v0.59.1/go.mod h1:GpWM7dewqmVYcd7SmRaiWVe9SSqjf0UrwnYnpEZNuT0= +github.com/prometheus/exporter-toolkit v0.13.0 h1:lmA0Q+8IaXgmFRKw09RldZmZdnvu9wwcDLIXGmTPw1c= +github.com/prometheus/exporter-toolkit v0.13.0/go.mod h1:2uop99EZl80KdXhv/MxVI2181fMcwlsumFOqBecGkG0= 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/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= -github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +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= github.com/xhit/go-str2duration/v2 v2.1.0 h1:lxklc02Drh6ynqX+DdPyp5pCKLUQpRT8bp8Ydu2Bstc= github.com/xhit/go-str2duration/v2 v2.1.0/go.mod h1:ohY8p+0f07DiV6Em5LKB0s2YpLtXVyJfNt1+BlmyAsU= -golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI= -golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM= -golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ= -golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE= -golang.org/x/oauth2 v0.21.0 h1:tsimM75w1tF/uws5rbeHzIWxEqElMehnc+iW793zsZs= -golang.org/x/oauth2 v0.21.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= -golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= -golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= -golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= -golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= -golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= +golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A= +golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70= +golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo= +golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0= +golang.org/x/oauth2 v0.23.0 h1:PbgcYx2W7i4LvjJWEbf0ngHV6qJYr86PkAV3bXdLEbs= +golang.org/x/oauth2 v0.23.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= +golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= +golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= +golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224= +golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= -gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index 2110c7f..3268ceb 100644 --- a/main.go +++ b/main.go @@ -1,22 +1,20 @@ package main import ( - stdlog "log" + "log/slog" "net/http" "os" "os/user" "time" - "github.com/go-kit/log" - "github.com/go-kit/log/level" "github.com/shiguredo/sora_exporter/collector" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/collectors/version" "github.com/prometheus/client_golang/prometheus/promhttp" - "github.com/prometheus/common/promlog" - "github.com/prometheus/common/promlog/flag" + "github.com/prometheus/common/promslog" + "github.com/prometheus/common/promslog/flag" commonVersion "github.com/prometheus/common/version" promcollectors "github.com/prometheus/client_golang/prometheus/collectors" @@ -83,7 +81,7 @@ type handler struct { exporterMetricsRegistry *prometheus.Registry includeExporterMetrics bool maxRequests int - logger log.Logger + logger *slog.Logger soraAPIURL string soraSkipSslVeirfy bool soraTimeout time.Duration @@ -95,7 +93,7 @@ type handler struct { } func newHandler( - includeExporterMetrics bool, maxRequests int, logger log.Logger, + includeExporterMetrics bool, maxRequests int, logger *slog.Logger, soraAPIURL string, soraSkipSslVeirfy bool, soraTimeout time.Duration, soraFreezeTimeSeconds bool, enableSoraClientMetrics bool, enableSoraConnectionErrorMetrics bool, enableErlangVMMetrics bool, enableSoraClusterMetrics bool) *handler { @@ -145,7 +143,7 @@ func (h *handler) innerHandler() http.Handler { handler := promhttp.HandlerFor( prometheus.Gatherers{h.exporterMetricsRegistry, r}, promhttp.HandlerOpts{ - ErrorLog: stdlog.New(log.NewStdlibAdapter(level.Error(h.logger)), "", 0), + ErrorLog: slog.NewLogLogger(h.logger.Handler(), slog.LevelError), ErrorHandling: promhttp.ContinueOnError, MaxRequestsInFlight: h.maxRequests, Registry: h.exporterMetricsRegistry, @@ -162,20 +160,20 @@ func (h *handler) innerHandler() http.Handler { } func main() { - promlogConfig := &promlog.Config{} - flag.AddFlags(kingpin.CommandLine, promlogConfig) + promslogConfig := &promslog.Config{} + flag.AddFlags(kingpin.CommandLine, promslogConfig) kingpin.Version(commonVersion.Print("sora_exporter")) kingpin.CommandLine.UsageWriter(os.Stdout) kingpin.HelpFlag.Short('h') kingpin.Parse() - logger := promlog.New(promlogConfig) - level.Info(logger).Log("msg", "Starting sora_exporter", "version", commonVersion.Info()) - level.Info(logger).Log("msg", "Build context", "build_context", commonVersion.BuildContext()) + logger := promslog.New(promslogConfig) + logger.Info("Starting sora_exporter", "version", commonVersion.Info()) + logger.Info("Build context", "build_context", commonVersion.BuildContext()) // root 権限で起動してたら warning を出す if user, err := user.Current(); err == nil && user.Uid == "0" { - level.Warn(logger).Log("msg", "Sora Exporter is running as root user. This exporter is designed to run as unpriviledged user, root is not required.") + logger.Warn("Sora Exporter は root ユーザーで実行されています。このエクスポーターは特権を必要としません。root で実行する必要はありません。") } http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { @@ -193,7 +191,7 @@ func main() { *enableSoraClientMetrics, *enableSoraConnectionErrorMetrics, *enableErlangVMMetrics, *enableSoraClusterMetrics) http.Handle(*metricsPath, soraHandler) - level.Info(logger).Log("msg", "Listening on", "address", *listenAddress) + logger.Info("Listening on", "address", *listenAddress) server := &http.Server{} webSystemdSocket := false webConfigFile := "" @@ -203,7 +201,7 @@ func main() { WebConfigFile: &webConfigFile, } if err := web.ListenAndServe(server, webFlagConfig, logger); err != nil { - level.Error(logger).Log("err", err) + logger.Error("Error starting HTTP server", "err", err) os.Exit(1) } } diff --git a/main_test.go b/main_test.go index 2c3756f..9b43180 100644 --- a/main_test.go +++ b/main_test.go @@ -2,6 +2,8 @@ package main import ( "fmt" + "io" + "log/slog" "net/http" "net/http/httptest" "os" @@ -9,7 +11,6 @@ import ( "testing" "time" - "github.com/go-kit/log" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/testutil" "github.com/shiguredo/sora_exporter/collector" @@ -299,13 +300,14 @@ func TestInvalidConfig(t *testing.T) { s := newSora([]byte("invalid config parameter"), []byte(listClusterNodesJSONData), []byte(getLicenseJSONDATA)) defer s.Close() + nopLogger := slog.New(slog.NewTextHandler(io.Discard, nil)) timeout, _ := time.ParseDuration("5s") h := collector.NewCollector(&collector.CollectorOptions{ URI: s.URL, SkipSslVerify: true, Timeout: timeout, FreezeTimeSeconds: true, - Logger: log.NewNopLogger(), + Logger: nopLogger, EnableSoraClientMetrics: true, EnableSoraConnectionErrorMetrics: true, EnableErlangVMMetrics: true, @@ -318,13 +320,14 @@ func TestMaximumMetrics(t *testing.T) { s := newSora([]byte(testJSONData), []byte(listClusterNodesJSONData), []byte(getLicenseJSONDATA)) defer s.Close() + nopLogger := slog.New(slog.NewTextHandler(io.Discard, nil)) timeout, _ := time.ParseDuration("5s") h := collector.NewCollector(&collector.CollectorOptions{ URI: s.URL, SkipSslVerify: true, Timeout: timeout, FreezeTimeSeconds: true, - Logger: log.NewNopLogger(), + Logger: nopLogger, EnableSoraClientMetrics: true, EnableSoraConnectionErrorMetrics: true, EnableErlangVMMetrics: true, @@ -337,13 +340,14 @@ func TestSoraErlangVMEnabledMetrics(t *testing.T) { s := newSora([]byte(testJSONData), []byte(listClusterNodesJSONData), []byte(getLicenseJSONDATA)) defer s.Close() + nopLogger := slog.New(slog.NewTextHandler(io.Discard, nil)) timeout, _ := time.ParseDuration("5s") h := collector.NewCollector(&collector.CollectorOptions{ URI: s.URL, SkipSslVerify: true, Timeout: timeout, FreezeTimeSeconds: true, - Logger: log.NewNopLogger(), + Logger: nopLogger, EnableSoraClientMetrics: false, EnableSoraConnectionErrorMetrics: false, EnableErlangVMMetrics: true, @@ -356,13 +360,14 @@ func TestSoraClientEnabledMetrics(t *testing.T) { s := newSora([]byte(testJSONData), []byte(listClusterNodesJSONData), []byte(getLicenseJSONDATA)) defer s.Close() + nopLogger := slog.New(slog.NewTextHandler(io.Discard, nil)) timeout, _ := time.ParseDuration("5s") h := collector.NewCollector(&collector.CollectorOptions{ URI: s.URL, SkipSslVerify: true, Timeout: timeout, FreezeTimeSeconds: true, - Logger: log.NewNopLogger(), + Logger: nopLogger, EnableSoraClientMetrics: true, EnableSoraConnectionErrorMetrics: false, EnableErlangVMMetrics: false, @@ -375,13 +380,14 @@ func TestSoraConnectionErrorEnabledMetrics(t *testing.T) { s := newSora([]byte(testJSONData), []byte(listClusterNodesJSONData), []byte(getLicenseJSONDATA)) defer s.Close() + nopLogger := slog.New(slog.NewTextHandler(io.Discard, nil)) timeout, _ := time.ParseDuration("5s") h := collector.NewCollector(&collector.CollectorOptions{ URI: s.URL, SkipSslVerify: true, Timeout: timeout, FreezeTimeSeconds: true, - Logger: log.NewNopLogger(), + Logger: nopLogger, EnableSoraClientMetrics: false, EnableSoraConnectionErrorMetrics: true, EnableErlangVMMetrics: false, @@ -421,13 +427,14 @@ func TestMinimumMetrics(t *testing.T) { s := newSora([]byte(resp), []byte(listClusterNodesJSONData), []byte(getLicenseWithoutMaxNodesJSONDATA)) defer s.Close() + nopLogger := slog.New(slog.NewTextHandler(io.Discard, nil)) timeout, _ := time.ParseDuration("5s") h := collector.NewCollector(&collector.CollectorOptions{ URI: s.URL, SkipSslVerify: true, Timeout: timeout, FreezeTimeSeconds: true, - Logger: log.NewNopLogger(), + Logger: nopLogger, EnableSoraClientMetrics: false, EnableSoraConnectionErrorMetrics: false, EnableErlangVMMetrics: false, @@ -440,13 +447,14 @@ func TestSoraClusterEnabledMetrics(t *testing.T) { s := newSora([]byte(testJSONData), []byte(listClusterNodesJSONData), []byte(getLicenseJSONDATA)) defer s.Close() + nopLogger := slog.New(slog.NewTextHandler(io.Discard, nil)) timeout, _ := time.ParseDuration("5s") h := collector.NewCollector(&collector.CollectorOptions{ URI: s.URL, SkipSslVerify: true, Timeout: timeout, FreezeTimeSeconds: true, - Logger: log.NewNopLogger(), + Logger: nopLogger, EnableSoraClientMetrics: false, EnableSoraConnectionErrorMetrics: false, EnableErlangVMMetrics: false, @@ -460,13 +468,14 @@ func TestSoraClusterEnabledMetricsCurrentJsonData(t *testing.T) { s := newSora([]byte(testJSONData), []byte(listClusterNodesCurrentJSONData), []byte(getLicenseJSONDATA)) defer s.Close() + nopLogger := slog.New(slog.NewTextHandler(io.Discard, nil)) timeout, _ := time.ParseDuration("5s") h := collector.NewCollector(&collector.CollectorOptions{ URI: s.URL, SkipSslVerify: true, Timeout: timeout, FreezeTimeSeconds: true, - Logger: log.NewNopLogger(), + Logger: nopLogger, EnableSoraClientMetrics: false, EnableSoraConnectionErrorMetrics: false, EnableErlangVMMetrics: false, From 953f5d3490ea6aa90a688db90aa04e0cdb41659d Mon Sep 17 00:00:00 2001 From: Takeshi NAMAO Date: Wed, 25 Sep 2024 16:15:26 +0900 Subject: [PATCH 3/6] =?UTF-8?q?github=20actions=20=E3=81=AE=E3=82=A4?= =?UTF-8?q?=E3=83=A1=E3=83=BC=E3=82=B8=E3=81=A8=20staticchck=20=E3=81=AE?= =?UTF-8?q?=E3=83=90=E3=83=BC=E3=82=B8=E3=83=A7=E3=83=B3=E3=82=92=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/ci.yml | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 84199cf..96f6cd1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,30 +3,29 @@ name: Go Static Check on: push: branches-ignore: - - 'main' + - "main" tags-ignore: - - '*' + - "*" paths: - "**.go" jobs: - build: name: static-check runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v4 - - name: Set up Go - uses: actions/setup-go@v4 - with: - go-version-file: "./go.mod" + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version-file: "./go.mod" - - run: go version + - run: go version - - run: go fmt . + - run: go fmt . - - uses: dominikh/staticcheck-action@v1.3.0 - with: - version: "2023.1.6" - install-go: false + - uses: dominikh/staticcheck-action@v1.3.1 + with: + version: "2024.1.1" + install-go: false From 6c59776ce03ec7492b641dfcfbdd45064254d11a Mon Sep 17 00:00:00 2001 From: Takeshi NAMAO Date: Wed, 25 Sep 2024 16:56:41 +0900 Subject: [PATCH 4/6] =?UTF-8?q?=E5=A4=89=E6=9B=B4=E5=B1=A5=E6=AD=B4?= =?UTF-8?q?=E3=81=AB=E8=BF=BD=E8=A8=98=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGES.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 23ab40c..ef06e5c 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -11,8 +11,24 @@ ## develop +- [CHANGE] ログライブラリの変更 + - `prometheus/exporter-toolkit` の依存ログライブラリが `go-kit/log` から Go 言語標準ライブラリの `log/slog` に変更されたため、Sora expoter 内で使用しているロガーも `log/slog` に切り替える + - @tnamao +- [UPDATE] 依存パッケージを更新する + - prometheus/client_golang 1.19.1 => 1.20.4 + - prometheus/common 0.54.0 => 0.59.1 + - prometheus/exporter-toolkit 0.11.0 => 0.13.0 + - `prometheus/exporter-toolkit` のログライブラリ切り替えにより `go-kit/log` への依存はなくなりました + - @tnamao + ### misc +- [UPDATE] Github Actions のイメージを更新する + - actions/setup-go v4 => v5 + - dominikh/staticcheck-action v1.3.0 => v1.3.1 +- [UPDATE] CI で実行する staticcheck のバージョンを更新する + - 2023.1.6 => 2024.1.1 + ## 2024.6.0 **リリース日**: 2024-06-20 From 5d33de9c49ef01efa392a5bc52343786ff7976fa Mon Sep 17 00:00:00 2001 From: Takeshi NAMAO Date: Wed, 25 Sep 2024 17:38:06 +0900 Subject: [PATCH 5/6] =?UTF-8?q?=E5=A4=89=E6=9B=B4=E5=B1=A5=E6=AD=B4?= =?UTF-8?q?=E3=81=AB=E8=BF=BD=E8=A8=98=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index ef06e5c..68c93ff 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -13,6 +13,7 @@ - [CHANGE] ログライブラリの変更 - `prometheus/exporter-toolkit` の依存ログライブラリが `go-kit/log` から Go 言語標準ライブラリの `log/slog` に変更されたため、Sora expoter 内で使用しているロガーも `log/slog` に切り替える + - 同様にテストコードで使用していた `NewNopLogger` は代替として `slog.New(slog.NewTextHandler(io.Discard, nil))` を使用する形に変更する - @tnamao - [UPDATE] 依存パッケージを更新する - prometheus/client_golang 1.19.1 => 1.20.4 From 28aebca3858229f504ca78aa9654cdad1a2f15b7 Mon Sep 17 00:00:00 2001 From: Takeshi NAMAO Date: Wed, 25 Sep 2024 17:39:53 +0900 Subject: [PATCH 6/6] =?UTF-8?q?=E5=A4=89=E6=9B=B4=E5=B1=A5=E6=AD=B4?= =?UTF-8?q?=E3=81=AB=E8=BF=BD=E8=A8=98=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 68c93ff..1454809 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -21,6 +21,8 @@ - prometheus/exporter-toolkit 0.11.0 => 0.13.0 - `prometheus/exporter-toolkit` のログライブラリ切り替えにより `go-kit/log` への依存はなくなりました - @tnamao +- [UPDATE] Go を 1.23 に上げる + - @tnamao ### misc