Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| dc8adc1aea | |||
| 35ea262b99 |
@@ -1,4 +1,4 @@
|
||||
FROM alpine:3.20.3
|
||||
FROM alpine:3.21.0
|
||||
|
||||
RUN apk --no-cache upgrade
|
||||
RUN apk --no-cache add ca-certificates tzdata zsh jq tmux curl
|
||||
|
||||
2
go.mod
2
go.mod
@@ -16,7 +16,7 @@ require (
|
||||
github.com/spf13/cobra v1.8.1
|
||||
github.com/stretchr/testify v1.10.0
|
||||
go.ntppool.org/api v0.3.4
|
||||
go.ntppool.org/common v0.3.0
|
||||
go.ntppool.org/common v0.3.1
|
||||
go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho v0.58.0
|
||||
go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace v0.58.0
|
||||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.58.0
|
||||
|
||||
2
go.sum
2
go.sum
@@ -140,6 +140,8 @@ go.ntppool.org/api v0.3.4 h1:KeRyFhIRkjJwZif7hkpqEDEBmukyYGiOi2Fd6j3UzQ0=
|
||||
go.ntppool.org/api v0.3.4/go.mod h1:LFLAwnrc/JyjzKnjgf8tCOJhps6oFIjuledS3PCx7xc=
|
||||
go.ntppool.org/common v0.3.0 h1:IuSmyjEhI1F3tr5kc5MqlR4cy5y0o5f3EKvC7Koc6rs=
|
||||
go.ntppool.org/common v0.3.0/go.mod h1:25pUt3YUusF1MY0nsljjskcMMeTvKZszVvNsubvWhSM=
|
||||
go.ntppool.org/common v0.3.1 h1:JaJpS3m8oAc9jH0yhHYJnjOC+RUzxx/F+EDe0QON4eQ=
|
||||
go.ntppool.org/common v0.3.1/go.mod h1:1SKjFBH9AL7Fj2S0zy41isHzV6dTC+6yIKD5QDtX8aY=
|
||||
go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=
|
||||
go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A=
|
||||
go.opentelemetry.io/contrib/bridges/otelslog v0.8.0 h1:G3sKsNueSdxuACINFxKrQeimAIst0A5ytA2YJH+3e1c=
|
||||
|
||||
@@ -73,7 +73,7 @@ func (srv *Server) getHistoryParameters(ctx context.Context, c echo.Context) (hi
|
||||
|
||||
monitorParam := c.QueryParam("monitor")
|
||||
|
||||
var monitorID uint32 = 0
|
||||
var monitorID uint32
|
||||
switch monitorParam {
|
||||
case "":
|
||||
name := "recentmedian.scores.ntp.dev"
|
||||
@@ -223,7 +223,6 @@ func (srv *Server) history(c echo.Context) error {
|
||||
default:
|
||||
return c.String(http.StatusNotFound, "not implemented")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (srv *Server) historyJSON(ctx context.Context, c echo.Context, server ntpdb.Server, history *logscores.LogScoreHistory) error {
|
||||
@@ -326,12 +325,13 @@ func (srv *Server) historyJSON(ctx context.Context, c echo.Context, server ntpdb
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, res)
|
||||
|
||||
}
|
||||
|
||||
func (srv *Server) historyCSV(ctx context.Context, c echo.Context, history *logscores.LogScoreHistory) error {
|
||||
log := logger.Setup()
|
||||
ctx, span := tracing.Tracer().Start(ctx, "history.csv")
|
||||
defer span.End()
|
||||
|
||||
b := bytes.NewBuffer([]byte{})
|
||||
w := csv.NewWriter(b)
|
||||
|
||||
@@ -342,7 +342,11 @@ func (srv *Server) historyCSV(ctx context.Context, c echo.Context, history *logs
|
||||
return s
|
||||
}
|
||||
|
||||
w.Write([]string{"ts_epoch", "ts", "offset", "step", "score", "monitor_id", "monitor_name", "leap", "error"})
|
||||
err := w.Write([]string{"ts_epoch", "ts", "offset", "step", "score", "monitor_id", "monitor_name", "leap", "error"})
|
||||
if err != nil {
|
||||
log.ErrorContext(ctx, "could not write csv header", "err", err)
|
||||
return err
|
||||
}
|
||||
for _, l := range history.LogScores {
|
||||
// log.Debug("csv line", "id", l.ID, "n", i)
|
||||
|
||||
@@ -381,7 +385,6 @@ func (srv *Server) historyCSV(ctx context.Context, c echo.Context, history *logs
|
||||
w.Flush()
|
||||
if err := w.Error(); err != nil {
|
||||
log.ErrorContext(ctx, "could not flush csv", "err", err)
|
||||
span.End()
|
||||
return c.String(http.StatusInternalServerError, "csv error")
|
||||
}
|
||||
|
||||
@@ -392,5 +395,4 @@ func (srv *Server) historyCSV(ctx context.Context, c echo.Context, history *logs
|
||||
// Chrome and Firefox force-download text/csv files, so use text/plain
|
||||
// https://bugs.chromium.org/p/chromium/issues/detail?id=152911
|
||||
return c.Blob(http.StatusOK, "text/plain", b.Bytes())
|
||||
|
||||
}
|
||||
|
||||
@@ -76,7 +76,7 @@ func NewServer(ctx context.Context, configFile string) (*Server, error) {
|
||||
Environment: conf.DeploymentMode(),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("tracing init: %w", err)
|
||||
}
|
||||
|
||||
srv.tpShutdown = append(srv.tpShutdown, tpShutdown)
|
||||
@@ -274,28 +274,29 @@ func (srv *Server) userCountryData(c echo.Context) error {
|
||||
UserCountry: data,
|
||||
ZoneStats: zoneStats,
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func healthHandler(srv *Server, log *slog.Logger) func(w http.ResponseWriter, req *http.Request) {
|
||||
|
||||
return func(w http.ResponseWriter, req *http.Request) {
|
||||
|
||||
ctx := req.Context()
|
||||
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
g, ctx := errgroup.WithContext(ctx)
|
||||
|
||||
stats := srv.db.Stats()
|
||||
log.InfoContext(ctx, "health requests", "url", req.URL.String(), "stats", stats)
|
||||
if stats.OpenConnections > 5 {
|
||||
log.InfoContext(ctx, "health requests", "url", req.URL.String(), "stats", stats)
|
||||
}
|
||||
|
||||
reset, err := strconv.ParseBool(req.URL.Query().Get("reset"))
|
||||
log.InfoContext(ctx, "db reset request", "err", err, "reset", reset)
|
||||
if resetParam := req.URL.Query().Get("reset"); resetParam != "" {
|
||||
reset, err := strconv.ParseBool(resetParam)
|
||||
log.InfoContext(ctx, "db reset request", "err", err, "reset", reset)
|
||||
|
||||
if err == nil && reset {
|
||||
log.InfoContext(ctx, "setting idle db conns to zero")
|
||||
srv.db.SetMaxIdleConns(0)
|
||||
srv.db.SetConnMaxLifetime(5 * time.Second)
|
||||
if err == nil && reset {
|
||||
log.InfoContext(ctx, "setting idle db conns to zero")
|
||||
srv.db.SetMaxIdleConns(0)
|
||||
srv.db.SetConnMaxLifetime(5 * time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
g.Go(func() error {
|
||||
@@ -316,13 +317,19 @@ func healthHandler(srv *Server, log *slog.Logger) func(w http.ResponseWriter, re
|
||||
return nil
|
||||
})
|
||||
|
||||
err = g.Wait()
|
||||
err := g.Wait()
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
w.Write([]byte("db ping err"))
|
||||
_, err = w.Write([]byte("db ping err"))
|
||||
if err != nil {
|
||||
log.ErrorContext(ctx, "could not write response", "err", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("ok"))
|
||||
_, err = w.Write([]byte("ok"))
|
||||
if err != nil {
|
||||
log.ErrorContext(ctx, "could not write response", "err", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user