Compare commits
No commits in common. "44e84bd8e19df696cc7523863f5abf78e55e6d8f" and "dcc11fca0922f1d8ae25899a5ca21a65364252bf" have entirely different histories.
44e84bd8e1
...
dcc11fca09
8 changed files with 9 additions and 211 deletions
|
@ -1,9 +0,0 @@
|
||||||
when:
|
|
||||||
- event: push
|
|
||||||
|
|
||||||
steps:
|
|
||||||
test:
|
|
||||||
image: golang:1.22-bullseye
|
|
||||||
commands:
|
|
||||||
- go mod download
|
|
||||||
- go test -v ./...
|
|
3
Makefile
3
Makefile
|
@ -1,3 +0,0 @@
|
||||||
|
|
||||||
test:
|
|
||||||
go test ./...
|
|
93
cache/cache.go
vendored
93
cache/cache.go
vendored
|
@ -1,93 +0,0 @@
|
||||||
/*
|
|
||||||
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
49
cache/cache_test.go
vendored
|
@ -1,49 +0,0 @@
|
||||||
/*
|
|
||||||
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,7 +27,6 @@ import (
|
||||||
"github.com/go-kit/log"
|
"github.com/go-kit/log"
|
||||||
"github.com/go-kit/log/level"
|
"github.com/go-kit/log/level"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
"golang.adora.codes/ntppool-exporter/cache"
|
|
||||||
"golang.adora.codes/ntppool-exporter/models"
|
"golang.adora.codes/ntppool-exporter/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -62,24 +61,7 @@ func (c Collector) Collect(ch chan<- prometheus.Metric) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Collector) collect(ch chan<- prometheus.Metric, logger log.Logger) {
|
func (c Collector) collect(ch chan<- prometheus.Metric, logger log.Logger) {
|
||||||
score, err := cache.GlobalScoreCache.Get(c.target)
|
httpError := false
|
||||||
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
|
var serverApiScore float64 = 0
|
||||||
const apiEndpoint = "https://www.ntppool.org/scores/"
|
const apiEndpoint = "https://www.ntppool.org/scores/"
|
||||||
const apiQuery = "/json?limit=1&monitor=24"
|
const apiQuery = "/json?limit=1&monitor=24"
|
||||||
|
@ -91,7 +73,7 @@ func (c Collector) collect(ch chan<- prometheus.Metric, logger log.Logger) {
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
level.Error(logger).Log("msg", "Error sending HTTP request", "url", url, "message", err)
|
level.Error(logger).Log("msg", "Error sending HTTP request", "url", url, "message", err)
|
||||||
return
|
httpError = true
|
||||||
}
|
}
|
||||||
res, err := client.Do(req)
|
res, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -115,11 +97,9 @@ func (c Collector) collect(ch chan<- prometheus.Metric, logger log.Logger) {
|
||||||
return
|
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
|
// TODO: Test or delete
|
||||||
serverScoreMetric := prometheus.NewDesc("ntppool_server_score",
|
serverScoreMetric := prometheus.NewDesc("ntppool_server_score",
|
||||||
|
|
|
@ -1,25 +0,0 @@
|
||||||
/*
|
|
||||||
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)
|
|
||||||
}
|
|
3
main.go
3
main.go
|
@ -33,7 +33,6 @@ import (
|
||||||
"github.com/prometheus/common/version"
|
"github.com/prometheus/common/version"
|
||||||
"github.com/prometheus/exporter-toolkit/web"
|
"github.com/prometheus/exporter-toolkit/web"
|
||||||
webflag "github.com/prometheus/exporter-toolkit/web/kingpinflag"
|
webflag "github.com/prometheus/exporter-toolkit/web/kingpinflag"
|
||||||
"golang.adora.codes/ntppool-exporter/cache"
|
|
||||||
"golang.adora.codes/ntppool-exporter/collector"
|
"golang.adora.codes/ntppool-exporter/collector"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -96,8 +95,6 @@ func main() {
|
||||||
kingpin.Parse()
|
kingpin.Parse()
|
||||||
logger := promlog.New(promlogConfig)
|
logger := promlog.New(promlogConfig)
|
||||||
|
|
||||||
cache.GlobalScoreCache = cache.NewScoreCache()
|
|
||||||
|
|
||||||
level.Info(logger).Log("msg", "Starting ntppool_exporter", "version", version.Info())
|
level.Info(logger).Log("msg", "Starting ntppool_exporter", "version", version.Info())
|
||||||
level.Info(logger).Log("build_context", version.BuildContext())
|
level.Info(logger).Log("build_context", version.BuildContext())
|
||||||
|
|
||||||
|
|
|
@ -34,7 +34,7 @@ type ApiResponse struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type ApiResponseHistory struct {
|
type ApiResponseHistory struct {
|
||||||
TimestampInt int64 `json:"ts"`
|
Timestamp int `json:"ts"`
|
||||||
Step int `json:"step"`
|
Step int `json:"step"`
|
||||||
Score float64 `json:"score"`
|
Score float64 `json:"score"`
|
||||||
MonitorID int `json:"monitor_id"`
|
MonitorID int `json:"monitor_id"`
|
||||||
|
|
Loading…
Reference in a new issue