Compare commits
27 commits
Author | SHA1 | Date | |
---|---|---|---|
d9939c8701 | |||
37f297b4dc | |||
15310ee2ff | |||
7d66fe6fb0 | |||
96121f3487 | |||
0887112c44 | |||
127f21e445 | |||
44e84bd8e1 | |||
265cbd5abf | |||
2a574d6f3b | |||
36e9aa49f7 | |||
c713e16ed2 | |||
3cc1653e11 | |||
f34056f241 | |||
dcc11fca09 | |||
a21ebe5af2 | |||
d68724a470 | |||
c71e965481 | |||
f103bf3d94 | |||
47825aa2f6 | |||
730326ede8 | |||
e84f93efca | |||
0ca9b4e06c | |||
bddc702ef3 | |||
046324f098 | |||
2d88d8a13f | |||
775eb974ab |
16 changed files with 293 additions and 29 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -22,6 +22,9 @@ go.work
|
|||
|
||||
# vscode Go debugging files
|
||||
__debug_*
|
||||
ntppool-exporter
|
||||
bin/
|
||||
|
||||
# vscode editor config
|
||||
.vscode/
|
||||
|
||||
|
|
35
.woodpecker/docker-deploy.yml
Normal file
35
.woodpecker/docker-deploy.yml
Normal file
|
@ -0,0 +1,35 @@
|
|||
when:
|
||||
- event: [pull_request, tag]
|
||||
|
||||
|
||||
steps:
|
||||
docker-deploy-pr:
|
||||
when:
|
||||
- event: pull_request
|
||||
image: woodpeckerci/plugin-docker-buildx
|
||||
settings:
|
||||
dockerfile: Dockerfile
|
||||
platforms: linux/arm/v7,linux/arm64/v8,linux/amd64
|
||||
repo: lauralani/ntppool-exporter
|
||||
registry: https://index.docker.io/v1/
|
||||
tag: pr-${CI_COMMIT_PULL_REQUEST}
|
||||
username: lauralani
|
||||
password:
|
||||
from_secret: dockerhub_token
|
||||
|
||||
|
||||
docker-deploy-tag:
|
||||
when:
|
||||
event: tag
|
||||
image: woodpeckerci/plugin-docker-buildx
|
||||
settings:
|
||||
dockerfile: Dockerfile
|
||||
platforms: linux/arm/v7,linux/arm64/v8,linux/amd64
|
||||
repo: lauralani/ntppool-exporter
|
||||
registry: https://index.docker.io/v1/
|
||||
auto_tag: true
|
||||
username: lauralani
|
||||
password:
|
||||
from_secret: dockerhub_token
|
||||
|
||||
|
9
.woodpecker/golang-test.yml
Normal file
9
.woodpecker/golang-test.yml
Normal file
|
@ -0,0 +1,9 @@
|
|||
when:
|
||||
- event: push
|
||||
|
||||
steps:
|
||||
test:
|
||||
image: golang:1.22-bullseye
|
||||
commands:
|
||||
- go mod download
|
||||
- go test -v ./...
|
19
CHANGELOG.md
19
CHANGELOG.md
|
@ -6,22 +6,35 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
## [Unreleased]
|
||||
### Changed
|
||||
- changed docker base image to gcr.io/distroless/static-debian12
|
||||
|
||||
## [0.2.0] - 2024-04-19
|
||||
### ⚠️ Breaking Changes
|
||||
- renamed the score metric from `ntppool_score` to `ntppool_server_score`
|
||||
- removed unneccessary `server` label in metrics
|
||||
|
||||
### Added
|
||||
- added in-memory cache to reduce API strain (#4)
|
||||
|
||||
### Changed
|
||||
- fixed some debug logs
|
||||
- update readme to reflect new repo
|
||||
|
||||
## [0.1.1] - 2024-04-17
|
||||
### Fixed
|
||||
|
||||
- Fix nil references when http request fails (#2)
|
||||
|
||||
## [0.1.0] - 2024-04-17
|
||||
|
||||
### Added
|
||||
|
||||
- This CHANGELOG file to document all significant changes
|
||||
- LICENSE
|
||||
- CONTRIBUTING guidelines
|
||||
- MAINTAINERS file
|
||||
- minimal implementation of the exporter
|
||||
|
||||
[unreleased]: https://gitlab.com/adoralaura/ntppool-exporter/compare/0.1.1...HEAD
|
||||
[unreleased]: https://gitlab.com/adoralaura/ntppool-exporter/compare/0.2.0...HEAD
|
||||
[0.2.0]: https://gitlab.com/adoralaura/ntppool-exporter/compare/0.1.1...0.2.0
|
||||
[0.1.1]: https://gitlab.com/adoralaura/ntppool-exporter/compare/0.1.0...0.1.1
|
||||
[0.1.0]: https://gitlab.com/adoralaura/ntppool-exporter/releases/tag/0.1.0
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
# Contributing
|
||||
|
||||
We use GitLab to manage reviews of pull requests.
|
||||
I use my own [Forgejo Instance](https://code.lila.network) to manage issues and pull requests.
|
||||
|
||||
* If you have a trivial fix or improvement, go ahead and create a pull request,
|
||||
addressing (with `@...`) the maintainer of this repository (see
|
||||
[MAINTAINERS.md](MAINTAINERS.md)) in the description of the pull request.
|
||||
|
||||
* If you plan to do something more involved, first please create [a new issue](https://gitlab.com/adoralaura/ntppool-exporter/-/issues/new).
|
||||
* If you plan to do something more involved, first please [send me a mail]( mailto:adora@lila.network?subject=%5Bntppool-exporter%5D).
|
||||
|
||||
# What to contribute
|
||||
|
||||
|
|
|
@ -20,10 +20,9 @@ RUN go build -o ntppool-exporter main.go
|
|||
RUN chmod +x ntppool-exporter
|
||||
|
||||
|
||||
FROM debian:12 AS prod
|
||||
FROM gcr.io/distroless/static-debian12 AS prod
|
||||
|
||||
WORKDIR /app
|
||||
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
|
||||
COPY --from=build /var/app/ntppool-exporter /bin/
|
||||
|
||||
LABEL maintainer="Adora Laura Kalb <adora@lila.network>"
|
||||
|
|
3
Makefile
Normal file
3
Makefile
Normal file
|
@ -0,0 +1,3 @@
|
|||
|
||||
test:
|
||||
go test ./...
|
|
@ -2,16 +2,13 @@
|
|||
|
||||
This exporter is @adoralaura s try to display server scores from servers in [ntppool.org](https://www.ntppool.org) in a format which [Prometheus](https://prometheus.io/) can ingest.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# Usage
|
||||
|
||||
## Installation
|
||||
|
||||
Binaries can be downloaded soon from the [GitLab releases page](https://gitlab.com/adoralaura/ntppool-exporter/-/releases) and need no
|
||||
special installation
|
||||
Pre-built docker images can be found at [Docker Hub](https://hub.docker.com/r/lauralani/ntppool-exporter). A sample `docker-compose.yml` can be [found here](docker-compose.yml).
|
||||
|
||||
Binaries can be built by cloning this repository and run `go build main.go` with [Golang](https://go.dev/) installed.
|
||||
|
||||
## Running
|
||||
|
||||
|
|
1
VERSION
1
VERSION
|
@ -1 +0,0 @@
|
|||
0.1.1
|
93
cache/cache.go
vendored
Normal file
93
cache/cache.go
vendored
Normal file
|
@ -0,0 +1,93 @@
|
|||
/*
|
||||
Copyright 2024 Adora Laura Kalb <adora@lila.network>
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package cache
|
||||
|
||||
import (
|
||||
"net/netip"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
GlobalScoreCache *ScoreCache
|
||||
cacheExpiredAfter = 5 * time.Minute
|
||||
)
|
||||
|
||||
type ServerScore struct {
|
||||
expiresAt time.Time
|
||||
Score float64
|
||||
}
|
||||
|
||||
type ScoreCache struct {
|
||||
stop chan struct{}
|
||||
|
||||
mu sync.RWMutex
|
||||
|
||||
scores map[netip.Addr]ServerScore
|
||||
}
|
||||
|
||||
type CacheMissError struct{}
|
||||
|
||||
func (m *CacheMissError) Error() string {
|
||||
return "User is not in cache!"
|
||||
}
|
||||
|
||||
func newCacheMissError() *CacheMissError {
|
||||
return &CacheMissError{}
|
||||
}
|
||||
|
||||
func NewScoreCache() *ScoreCache {
|
||||
lc := &ScoreCache{
|
||||
scores: make(map[netip.Addr]ServerScore),
|
||||
stop: make(chan struct{}),
|
||||
}
|
||||
|
||||
return lc
|
||||
}
|
||||
|
||||
func (sc *ScoreCache) Add(score float64, ip netip.Addr, ts time.Time) {
|
||||
ssc := ServerScore{Score: score, expiresAt: ts.Add(cacheExpiredAfter)}
|
||||
sc.mu.Lock()
|
||||
sc.scores[ip] = ssc
|
||||
sc.mu.Unlock()
|
||||
}
|
||||
|
||||
func (lc *ScoreCache) Get(ip netip.Addr) (ServerScore, error) {
|
||||
now := time.Now()
|
||||
lc.mu.RLock()
|
||||
|
||||
cachedScore, ok := lc.scores[ip]
|
||||
if !ok {
|
||||
lc.mu.RUnlock()
|
||||
return ServerScore{}, newCacheMissError()
|
||||
}
|
||||
lc.mu.RUnlock()
|
||||
|
||||
if now.After(cachedScore.expiresAt) {
|
||||
lc.delete(ip)
|
||||
return ServerScore{}, newCacheMissError()
|
||||
}
|
||||
|
||||
return cachedScore, nil
|
||||
}
|
||||
|
||||
func (lc *ScoreCache) delete(ip netip.Addr) {
|
||||
lc.mu.Lock()
|
||||
|
||||
delete(lc.scores, ip)
|
||||
lc.mu.Unlock()
|
||||
}
|
49
cache/cache_test.go
vendored
Normal file
49
cache/cache_test.go
vendored
Normal file
|
@ -0,0 +1,49 @@
|
|||
/*
|
||||
Copyright 2024 Adora Laura Kalb <adora@lila.network>
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package cache
|
||||
|
||||
import (
|
||||
"net/netip"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestCacheGet(t *testing.T) {
|
||||
const testFloat = 1.23456
|
||||
cache := NewScoreCache()
|
||||
cache.Add(testFloat, netip.MustParseAddr("1.2.3.4"), time.Now().Add(5*time.Minute))
|
||||
|
||||
score, _ := cache.Get(netip.MustParseAddr("1.2.3.4"))
|
||||
if score.Score != testFloat {
|
||||
t.Fatalf("cache.Get(\"1.2.3.4\") = %f, want %f", score.Score, testFloat)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCacheMiss(t *testing.T) {
|
||||
now := time.Now()
|
||||
const testFloat = 1.23456
|
||||
|
||||
expiredTime := now.Add(-(time.Minute * 10))
|
||||
|
||||
cache := NewScoreCache()
|
||||
cache.Add(testFloat, netip.MustParseAddr("1.2.3.4"), expiredTime)
|
||||
|
||||
_, err := cache.Get(netip.MustParseAddr("1.2.3.4"))
|
||||
if err == nil {
|
||||
t.Fatalf("cache.Get(\"1.2.3.4\") = got nil, want error")
|
||||
}
|
||||
}
|
|
@ -27,6 +27,7 @@ import (
|
|||
"github.com/go-kit/log"
|
||||
"github.com/go-kit/log/level"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"golang.adora.codes/ntppool-exporter/cache"
|
||||
"golang.adora.codes/ntppool-exporter/models"
|
||||
)
|
||||
|
||||
|
@ -52,7 +53,7 @@ func (c Collector) Describe(ch chan<- *prometheus.Desc) {
|
|||
|
||||
// Collect implements Prometheus.Collector.
|
||||
func (c Collector) Collect(ch chan<- prometheus.Metric) {
|
||||
logger := log.With(c.logger, "scraper")
|
||||
logger := log.With(c.logger, "module", "scraper")
|
||||
level.Debug(logger).Log("msg", "Starting scrape")
|
||||
start := time.Now()
|
||||
c.collect(ch, logger)
|
||||
|
@ -61,7 +62,24 @@ func (c Collector) Collect(ch chan<- prometheus.Metric) {
|
|||
}
|
||||
|
||||
func (c Collector) collect(ch chan<- prometheus.Metric, logger log.Logger) {
|
||||
httpError := false
|
||||
score, err := cache.GlobalScoreCache.Get(c.target)
|
||||
if err == nil {
|
||||
level.Debug(logger).Log("msg", "Serving score from cache",
|
||||
"server", c.target.String(), "score", score.Score)
|
||||
|
||||
serverScoreMetric := prometheus.NewDesc("ntppool_server_score",
|
||||
"Shows the server score currently assigned at ntppool.org",
|
||||
nil, nil)
|
||||
|
||||
m1 := prometheus.MustNewConstMetric(serverScoreMetric, prometheus.GaugeValue, score.Score)
|
||||
m1 = prometheus.NewMetricWithTimestamp(time.Now(), m1)
|
||||
ch <- m1
|
||||
return
|
||||
}
|
||||
|
||||
level.Debug(logger).Log("msg", "Cache miss, querying API",
|
||||
"server", c.target.String())
|
||||
|
||||
var serverApiScore float64 = 0
|
||||
const apiEndpoint = "https://www.ntppool.org/scores/"
|
||||
const apiQuery = "/json?limit=1&monitor=24"
|
||||
|
@ -73,7 +91,7 @@ func (c Collector) collect(ch chan<- prometheus.Metric, logger log.Logger) {
|
|||
|
||||
if err != nil {
|
||||
level.Error(logger).Log("msg", "Error sending HTTP request", "url", url, "message", err)
|
||||
httpError = true
|
||||
return
|
||||
}
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
|
@ -97,14 +115,16 @@ func (c Collector) collect(ch chan<- prometheus.Metric, logger log.Logger) {
|
|||
return
|
||||
}
|
||||
|
||||
if !httpError {
|
||||
serverApiScore = response.History[0].Score
|
||||
}
|
||||
serverApiScore = response.History[0].Score
|
||||
|
||||
cache.GlobalScoreCache.Add(serverApiScore, c.target, time.Now())
|
||||
level.Debug(logger).Log("msg", "Added score to cache",
|
||||
"server", c.target.String(), "score", score.Score)
|
||||
|
||||
// TODO: Test or delete
|
||||
serverScoreMetric := prometheus.NewDesc("ntppool_score",
|
||||
serverScoreMetric := prometheus.NewDesc("ntppool_server_score",
|
||||
"Shows the server score currently assigned at ntppool.org",
|
||||
nil, prometheus.Labels{"server": c.target.String()})
|
||||
nil, nil)
|
||||
|
||||
// TODO: Test or delete
|
||||
//c.metrics.NtpppolServerScore.Add(serverApiScore)
|
||||
|
|
8
docker-compose.yml
Normal file
8
docker-compose.yml
Normal file
|
@ -0,0 +1,8 @@
|
|||
services:
|
||||
ntppool_exporter:
|
||||
image: lauralani/ntppool-exporter:latest
|
||||
restart: always
|
||||
ports:
|
||||
- '127.0.0.1:43609:43609'
|
||||
# Add this to enable debug logging:
|
||||
# command: --log.level=debug
|
25
helpers/helpers.go
Normal file
25
helpers/helpers.go
Normal file
|
@ -0,0 +1,25 @@
|
|||
/*
|
||||
Copyright 2024 Adora Laura Kalb <adora@lila.network>
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func UnixToTime(i int64) time.Time {
|
||||
return time.Unix(i, 0)
|
||||
}
|
17
main.go
17
main.go
|
@ -20,6 +20,7 @@ import (
|
|||
"net/http"
|
||||
"net/netip"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/alecthomas/kingpin/v2"
|
||||
"github.com/go-kit/log"
|
||||
|
@ -32,11 +33,13 @@ import (
|
|||
"github.com/prometheus/common/version"
|
||||
"github.com/prometheus/exporter-toolkit/web"
|
||||
webflag "github.com/prometheus/exporter-toolkit/web/kingpinflag"
|
||||
"golang.adora.codes/ntppool-exporter/cache"
|
||||
"golang.adora.codes/ntppool-exporter/collector"
|
||||
)
|
||||
|
||||
const (
|
||||
namespace = "ntppool"
|
||||
namespace = "ntppool"
|
||||
appVersion = "0.2.0"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -87,6 +90,7 @@ func handler(w http.ResponseWriter, r *http.Request, logger log.Logger, exporter
|
|||
}
|
||||
|
||||
func main() {
|
||||
version.Version = appVersion
|
||||
promlogConfig := &promlog.Config{}
|
||||
flag.AddFlags(kingpin.CommandLine, promlogConfig)
|
||||
kingpin.Version(version.Print("ntppool_exporter"))
|
||||
|
@ -94,7 +98,9 @@ func main() {
|
|||
kingpin.Parse()
|
||||
logger := promlog.New(promlogConfig)
|
||||
|
||||
level.Info(logger).Log("msg", "Starting ntppool_exporter", "version", version.Info())
|
||||
cache.GlobalScoreCache = cache.NewScoreCache()
|
||||
|
||||
level.Info(logger).Log("msg", "Starting ntppool_exporter", "version", appVersion)
|
||||
level.Info(logger).Log("build_context", version.BuildContext())
|
||||
|
||||
exporterMetrics := collector.Metrics{
|
||||
|
@ -146,7 +152,12 @@ func main() {
|
|||
http.Handle("/", landingPage)
|
||||
}
|
||||
|
||||
srv := &http.Server{}
|
||||
srv := &http.Server{
|
||||
ReadHeaderTimeout: 15 * time.Second,
|
||||
ReadTimeout: 15 * time.Second,
|
||||
WriteTimeout: 10 * time.Second,
|
||||
IdleTimeout: 30 * time.Second,
|
||||
}
|
||||
if err := web.ListenAndServe(srv, toolkitFlags, logger); err != nil {
|
||||
level.Error(logger).Log("msg", "Error starting HTTP server", "err", err)
|
||||
os.Exit(1)
|
||||
|
|
|
@ -34,8 +34,8 @@ type ApiResponse struct {
|
|||
}
|
||||
|
||||
type ApiResponseHistory struct {
|
||||
Timestamp int `json:"ts"`
|
||||
Step int `json:"step"`
|
||||
Score float64 `json:"score"`
|
||||
MonitorID int `json:"monitor_id"`
|
||||
TimestampInt int64 `json:"ts"`
|
||||
Step int `json:"step"`
|
||||
Score float64 `json:"score"`
|
||||
MonitorID int `json:"monitor_id"`
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue