Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions alerting/alert/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"crypto/sha256"
"encoding/hex"
"errors"
"log/slog"
"strconv"
"strings"
"time"

"github.com/TwiN/logr"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -131,7 +131,7 @@ func (alert *Alert) Checksum() string {
func (alert *Alert) ProviderOverrideAsBytes() []byte {
yamlBytes, err := yaml.Marshal(alert.ProviderOverride)
if err != nil {
logr.Warnf("[alert.ProviderOverrideAsBytes] Failed to marshal alert override of type=%s as bytes: %v", alert.Type, err)
slog.Warn("Failed to marshal alert override as bytes", "type", alert.Type, "error", err)
}
return yamlBytes
}
4 changes: 2 additions & 2 deletions alerting/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package alerting

import (
"log/slog"
"reflect"
"strings"

Expand Down Expand Up @@ -46,7 +47,6 @@ import (
"github.com/TwiN/gatus/v5/alerting/provider/webex"
"github.com/TwiN/gatus/v5/alerting/provider/zapier"
"github.com/TwiN/gatus/v5/alerting/provider/zulip"
"github.com/TwiN/logr"
)

// Config is the configuration for alerting providers
Expand Down Expand Up @@ -186,7 +186,7 @@ func (config *Config) GetAlertingProviderByAlertType(alertType alert.Type) provi
return fieldValue.Interface().(provider.AlertProvider)
}
}
logr.Infof("[alerting.GetAlertingProviderByAlertType] No alerting provider found for alert type %s", alertType)
slog.Info("No alerting provider found for alert type", "type", alertType)
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions alerting/provider/incidentio/incidentio.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"errors"
"fmt"
"io"
"log/slog"
"net/http"
"strconv"
"strings"

"github.com/TwiN/gatus/v5/alerting/alert"
"github.com/TwiN/gatus/v5/client"
"github.com/TwiN/gatus/v5/config/endpoint"
"github.com/TwiN/logr"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -117,7 +117,7 @@ func (provider *AlertProvider) Send(ep *endpoint.Endpoint, alert *alert.Alert, r
err = json.NewDecoder(response.Body).Decode(&incidentioResponse)
if err != nil {
// Silently fail. We don't want to create tons of alerts just because we failed to parse the body.
logr.Errorf("[incidentio.Send] Ran into error decoding pagerduty response: %s", err.Error())
slog.Error("Error decoding incident.io response", "error", err)
}
alert.ResolveKey = incidentioResponse.DeduplicationKey
return err
Expand Down
4 changes: 2 additions & 2 deletions alerting/provider/pagerduty/pagerduty.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"errors"
"fmt"
"io"
"log/slog"
"net/http"

"github.com/TwiN/gatus/v5/alerting/alert"
"github.com/TwiN/gatus/v5/client"
"github.com/TwiN/gatus/v5/config/endpoint"
"github.com/TwiN/logr"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -105,7 +105,7 @@ func (provider *AlertProvider) Send(ep *endpoint.Endpoint, alert *alert.Alert, r
var payload pagerDutyResponsePayload
if err = json.NewDecoder(response.Body).Decode(&payload); err != nil {
// Silently fail. We don't want to create tons of alerts just because we failed to parse the body.
logr.Errorf("[pagerduty.Send] Ran into error decoding pagerduty response: %s", err.Error())
slog.Error("Ran into error decoding pagerduty response", "error", err.Error())
} else {
alert.ResolveKey = payload.DedupKey
}
Expand Down
8 changes: 4 additions & 4 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"io/fs"
"log/slog"
"net/http"
"os"

Expand All @@ -10,7 +11,6 @@ import (
"github.com/TwiN/gatus/v5/config/web"
static "github.com/TwiN/gatus/v5/web"
"github.com/TwiN/health"
"github.com/TwiN/logr"
fiber "github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/adaptor"
"github.com/gofiber/fiber/v2/middleware/compress"
Expand All @@ -29,11 +29,11 @@ type API struct {
func New(cfg *config.Config) *API {
api := &API{}
if cfg.Web == nil {
logr.Warnf("[api.New] nil web config passed as parameter. This should only happen in tests. Using default web configuration")
slog.Warn("nil web config passed as parameter. This should only happen in tests. Using default web configuration")
cfg.Web = web.GetDefaultConfig()
}
if cfg.UI == nil {
logr.Warnf("[api.New] nil ui config passed as parameter. This should only happen in tests. Using default ui configuration")
slog.Warn("nil ui config passed as parameter. This should only happen in tests. Using default ui configuration")
cfg.UI = ui.GetDefaultConfig()
}
api.router = api.createRouter(cfg)
Expand All @@ -47,7 +47,7 @@ func (a *API) Router() *fiber.App {
func (a *API) createRouter(cfg *config.Config) *fiber.App {
app := fiber.New(fiber.Config{
ErrorHandler: func(c *fiber.Ctx, err error) error {
logr.Errorf("[api.ErrorHandler] %s", err.Error())
slog.Error("API error", slog.String("error", err.Error()))
return fiber.DefaultErrorHandler(c, err)
},
ReadBufferSize: cfg.Web.ReadBufferSize,
Expand Down
4 changes: 2 additions & 2 deletions api/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"errors"
"log/slog"
"math"
"net/http"
"net/url"
Expand All @@ -10,7 +11,6 @@ import (

"github.com/TwiN/gatus/v5/storage/store"
"github.com/TwiN/gatus/v5/storage/store/common"
"github.com/TwiN/logr"
"github.com/gofiber/fiber/v2"
"github.com/wcharczuk/go-chart/v2"
"github.com/wcharczuk/go-chart/v2/drawing"
Expand Down Expand Up @@ -121,7 +121,7 @@ func ResponseTimeChart(c *fiber.Ctx) error {
c.Set("Expires", "0")
c.Status(http.StatusOK)
if err := graph.Render(chart.SVG, c); err != nil {
logr.Errorf("[api.ResponseTimeChart] Failed to render response time chart: %s", err.Error())
slog.Error("Failed to render response time chart", "error", err)
return c.Status(500).SendString(err.Error())
}
return nil
Expand Down
20 changes: 10 additions & 10 deletions api/endpoint_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"log/slog"
"net/url"

"github.com/TwiN/gatus/v5/client"
Expand All @@ -13,7 +14,6 @@ import (
"github.com/TwiN/gatus/v5/storage/store"
"github.com/TwiN/gatus/v5/storage/store/common"
"github.com/TwiN/gatus/v5/storage/store/common/paging"
"github.com/TwiN/logr"
"github.com/gofiber/fiber/v2"
)

Expand All @@ -27,19 +27,19 @@ func EndpointStatuses(cfg *config.Config) fiber.Handler {
if !exists {
endpointStatuses, err := store.Get().GetAllEndpointStatuses(paging.NewEndpointStatusParams().WithResults(page, pageSize))
if err != nil {
logr.Errorf("[api.EndpointStatuses] Failed to retrieve endpoint statuses: %s", err.Error())
slog.Error("Failed to retrieve endpoint statuses", "error", err)
return c.Status(500).SendString(err.Error())
}
// ALPHA: Retrieve endpoint statuses from remote instances
if endpointStatusesFromRemote, err := getEndpointStatusesFromRemoteInstances(cfg.Remote); err != nil {
logr.Errorf("[handler.EndpointStatuses] Silently failed to retrieve endpoint statuses from remote: %s", err.Error())
slog.Error("Silently failed to retrieve endpoint statuses from remote", "error", err)
} else if endpointStatusesFromRemote != nil {
endpointStatuses = append(endpointStatuses, endpointStatusesFromRemote...)
}
// Marshal endpoint statuses to JSON
data, err = json.Marshal(endpointStatuses)
if err != nil {
logr.Errorf("[api.EndpointStatuses] Unable to marshal object to JSON: %s", err.Error())
slog.Error("Unable to marshal endpoint statuses to JSON", "error", err)
return c.Status(500).SendString("unable to marshal object to JSON")
}
cache.SetWithTTL(fmt.Sprintf("endpoint-status-%d-%d", page, pageSize), data, cacheTTL)
Expand All @@ -61,13 +61,13 @@ func getEndpointStatusesFromRemoteInstances(remoteConfig *remote.Config) ([]*end
response, err := httpClient.Get(instance.URL)
if err != nil {
// Log the error but continue with other instances
logr.Errorf("[api.getEndpointStatusesFromRemoteInstances] Failed to retrieve endpoint statuses from %s: %s", instance.URL, err.Error())
slog.Error("Failed to retrieve endpoint statuses from remote instance", "url", instance.URL, "error", err)
continue
}
var endpointStatuses []*endpoint.Status
if err = json.NewDecoder(response.Body).Decode(&endpointStatuses); err != nil {
_ = response.Body.Close()
logr.Errorf("[api.getEndpointStatusesFromRemoteInstances] Failed to decode endpoint statuses from %s: %s", instance.URL, err.Error())
slog.Error("Failed to decode endpoint statuses from remote instance", "url", instance.URL, "error", err)
continue
}
_ = response.Body.Close()
Expand All @@ -89,24 +89,24 @@ func EndpointStatus(cfg *config.Config) fiber.Handler {
page, pageSize := extractPageAndPageSizeFromRequest(c, cfg.Storage.MaximumNumberOfResults)
key, err := url.QueryUnescape(c.Params("key"))
if err != nil {
logr.Errorf("[api.EndpointStatus] Failed to decode key: %s", err.Error())
slog.Error("Failed to percent-decode query key", "error", err)
return c.Status(400).SendString("invalid key encoding")
}
endpointStatus, err := store.Get().GetEndpointStatusByKey(key, paging.NewEndpointStatusParams().WithResults(page, pageSize).WithEvents(1, cfg.Storage.MaximumNumberOfEvents))
if err != nil {
if errors.Is(err, common.ErrEndpointNotFound) {
return c.Status(404).SendString(err.Error())
}
logr.Errorf("[api.EndpointStatus] Failed to retrieve endpoint status: %s", err.Error())
slog.Error("Failed to retrieve endpoint status", "error", err)
return c.Status(500).SendString(err.Error())
}
if endpointStatus == nil { // XXX: is this check necessary?
logr.Errorf("[api.EndpointStatus] Endpoint with key=%s not found", key)
slog.Error("Endpoint status not found", "key", key)
return c.Status(404).SendString("not found")
}
output, err := json.Marshal(endpointStatus)
if err != nil {
logr.Errorf("[api.EndpointStatus] Unable to marshal object to JSON: %s", err.Error())
slog.Error("Unable to marshal endpoint status to JSON", "error", err)
return c.Status(500).SendString("unable to marshal object to JSON")
}
c.Set("Content-Type", "application/json")
Expand Down
16 changes: 8 additions & 8 deletions api/external_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"errors"
"log/slog"
"strings"
"time"

Expand All @@ -11,7 +12,6 @@ import (
"github.com/TwiN/gatus/v5/storage/store"
"github.com/TwiN/gatus/v5/storage/store/common"
"github.com/TwiN/gatus/v5/watchdog"
"github.com/TwiN/logr"
"github.com/gofiber/fiber/v2"
)

Expand All @@ -35,11 +35,11 @@ func CreateExternalEndpointResult(cfg *config.Config) fiber.Handler {
key := c.Params("key")
externalEndpoint := cfg.GetExternalEndpointByKey(key)
if externalEndpoint == nil {
logr.Errorf("[api.CreateExternalEndpointResult] External endpoint with key=%s not found", key)
slog.Error("External endpoint not found", "key", key)
return c.Status(404).SendString("not found")
}
if externalEndpoint.Token != token {
logr.Errorf("[api.CreateExternalEndpointResult] Invalid token for external endpoint with key=%s", key)
slog.Error("Invalid token for external endpoint", "key", key)
return c.Status(401).SendString("invalid token")
}
// Persist the result in the storage
Expand All @@ -51,7 +51,7 @@ func CreateExternalEndpointResult(cfg *config.Config) fiber.Handler {
if len(c.Query("duration")) > 0 {
parsedDuration, err := time.ParseDuration(c.Query("duration"))
if err != nil {
logr.Errorf("[api.CreateExternalEndpointResult] Invalid duration from string=%s with error: %s", c.Query("duration"), err.Error())
slog.Error("Invalid duration", "duration", c.Query("duration"), "error", err.Error())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: More message context needed?

return c.Status(400).SendString("invalid duration: " + err.Error())
}
result.Duration = parsedDuration
Expand All @@ -64,14 +64,14 @@ func CreateExternalEndpointResult(cfg *config.Config) fiber.Handler {
if errors.Is(err, common.ErrEndpointNotFound) {
return c.Status(404).SendString(err.Error())
}
logr.Errorf("[api.CreateExternalEndpointResult] Failed to insert result in storage: %s", err.Error())
slog.Error("Failed to insert endpoint result", "error", err.Error())
return c.Status(500).SendString(err.Error())
}
logr.Infof("[api.CreateExternalEndpointResult] Successfully inserted result for external endpoint with key=%s and success=%s", c.Params("key"), success)
slog.Info("Successfully inserted result for external endpoint", slog.Group("result", "key", c.Params("key"), "success", success))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: Remove group test or apply in other places too?

inEndpointMaintenanceWindow := false
for _, maintenanceWindow := range externalEndpoint.MaintenanceWindows {
if maintenanceWindow.IsUnderMaintenance() {
logr.Debug("[api.CreateExternalEndpointResult] Under endpoint maintenance window")
slog.Debug("External endpoint under maintenance window", "key", externalEndpoint.Key)
inEndpointMaintenanceWindow = true
}
}
Expand All @@ -81,7 +81,7 @@ func CreateExternalEndpointResult(cfg *config.Config) fiber.Handler {
externalEndpoint.NumberOfSuccessesInARow = convertedEndpoint.NumberOfSuccessesInARow
externalEndpoint.NumberOfFailuresInARow = convertedEndpoint.NumberOfFailuresInARow
} else {
logr.Debug("[api.CreateExternalEndpointResult] Not handling alerting because currently in the maintenance window")
slog.Debug("Not handling alerting because currently in the maintenance window", "key", externalEndpoint.Key)
}
if cfg.Metrics {
metrics.PublishMetricsForEndpoint(convertedEndpoint, result, extraLabels)
Expand Down
6 changes: 3 additions & 3 deletions api/spa.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package api
import (
_ "embed"
"html/template"
"log/slog"

"github.com/TwiN/gatus/v5/config/ui"
static "github.com/TwiN/gatus/v5/web"
"github.com/TwiN/logr"
"github.com/gofiber/fiber/v2"
)

Expand All @@ -26,14 +26,14 @@ func SinglePageApplication(uiConfig *ui.Config) fiber.Handler {
t, err := template.ParseFS(static.FileSystem, static.IndexPath)
if err != nil {
// This should never happen, because ui.ValidateAndSetDefaults validates that the template works.
logr.Errorf("[api.SinglePageApplication] Failed to parse template. This should never happen, because the template is validated on start. Error: %s", err.Error())
slog.Error("Failed to parse template. This should never happen, because the template is validated on start.", "error", err)
return c.Status(500).SendString("Failed to parse template. This should never happen, because the template is validated on start.")
}
c.Set("Content-Type", "text/html")
err = t.Execute(c, vd)
if err != nil {
// This should never happen, because ui.ValidateAndSetDefaults validates that the template works.
logr.Errorf("[api.SinglePageApplication] Failed to execute template. This should never happen, because the template is validated on start. Error: %s", err.Error())
slog.Error("Failed to parse template. This should never happen, because the template is validated on start.", "error", err)
return c.Status(500).SendString("Failed to parse template. This should never happen, because the template is validated on start.")
}
return c.SendStatus(200)
Expand Down
6 changes: 3 additions & 3 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"errors"
"fmt"
"io"
"log/slog"
"net"
"net/http"
"net/smtp"
Expand All @@ -19,7 +20,6 @@ import (
"time"

"github.com/TwiN/gocache/v2"
"github.com/TwiN/logr"
"github.com/TwiN/whois"
"github.com/gorilla/websocket"
"github.com/ishidawataru/sctp"
Expand Down Expand Up @@ -163,7 +163,7 @@ func CanPerformStartTLS(address string, config *Config) (connected bool, certifi
if err != nil {
// We're ignoring the error, because it should have been validated on startup ValidateAndSetDefaults.
// It shouldn't happen, but if it does, we'll log it... Better safe than sorry ;)
logr.Errorf("[client.getHTTPClient] THIS SHOULD NOT HAPPEN. Silently ignoring invalid DNS resolver due to error: %s", err.Error())
slog.Error("Should never happen: Silently ignoring invalid DNS resolver", "error", err)
} else {
dialer := &net.Dialer{
Resolver: &net.Resolver{
Expand Down Expand Up @@ -464,7 +464,7 @@ func QueryDNS(queryType, queryName, url string) (connected bool, dnsRcode string
m.SetQuestion(queryName, queryTypeAsUint16)
r, _, err := c.Exchange(m, url)
if err != nil {
logr.Infof("[client.QueryDNS] Error exchanging DNS message: %v", err)
slog.Info("Error exchanging DNS message", "error", err)
return false, "", nil, err
}
connected = true
Expand Down
Loading