Skip to content
Open
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
58 changes: 55 additions & 3 deletions pkg/checks/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package checks

import (
"context"
"net/url"
"strconv"
"sync"
"time"

Expand Down Expand Up @@ -56,12 +58,15 @@ type CheckBase struct {
DoneChan chan struct{}
}

// Runtime is the interface that all check configurations must implement
// Runtime is the interface that all check configurations must implement.
type Runtime interface {
// For returns the name of the check being configured
// For returns the name of the check being configured.
For() string
// Validate checks if the configuration is valid
// Validate checks if the configuration is valid.
Validate() error
// Enrich enriches the configuration with the [GlobalTarget]s.
// Each configuration is responsible for adding the targets it needs.
Enrich(ctx context.Context, targets []GlobalTarget)
}

// Result encapsulates the outcome of a check run.
Expand All @@ -84,3 +89,50 @@ type GlobalTarget struct {
Url string `json:"url"`
LastSeen time.Time `json:"lastSeen"`
}

// URL returns the [url.URL] representation of the target.
func (g GlobalTarget) URL() (*url.URL, error) {
return url.Parse(g.Url)
}
Comment on lines +94 to +96
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We're already storing the url on the GlobalTarget as a string. Why not parse that string into a url when we first construct the object? That way getting the url can never fail. Getting the URL as a string is using stringbuilder internally, so it should also be reasonably efficient (atleast faster than parsing the url)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

To add to this, we also call this method everywhere in the interface implementation, so parsing the url is done quite often.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This will require a bigger refactoring, since we use the string everywhere. Should I do it in this PR or shall I open another?


// Hostname returns the host of the target URL, stripping the port if present.
func (g GlobalTarget) Hostname() (string, error) {
u, err := g.URL()
if err != nil {
return "", err
}
return u.Hostname(), nil
}

// Default ports
const (
httpPort = 80
httpsPort = 443
)

// Port returns the port of the target URL.
func (g GlobalTarget) Port() (int, error) {
u, err := g.URL()
if err != nil {
return 0, err
}

// If the port is not specified, the default port for the scheme is returned.
if u.Port() == "" {
switch u.Scheme {
case "http":
return httpPort, nil
case "https":
return httpsPort, nil
default:
return 0, nil
}
}

return strconv.Atoi(u.Port())
}

// String returns the URL of the target.
func (g GlobalTarget) String() string {
return g.Url
}
22 changes: 21 additions & 1 deletion pkg/checks/dns/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
package dns

import (
"context"
"fmt"
"slices"
"strings"
"time"

"github.com/telekom/sparrow/internal/helper"
"github.com/telekom/sparrow/internal/logger"
"github.com/telekom/sparrow/pkg/checks"
)

Expand All @@ -21,9 +24,9 @@ const (
// Config defines the configuration parameters for a DNS check
type Config struct {
Targets []string `json:"targets" yaml:"targets"`
Retry helper.RetryConfig `json:"retry" yaml:"retry"`
Interval time.Duration `json:"interval" yaml:"interval"`
Timeout time.Duration `json:"timeout" yaml:"timeout"`
Retry helper.RetryConfig `json:"retry" yaml:"retry"`
}

// For returns the name of the check
Expand All @@ -49,3 +52,20 @@ func (c *Config) Validate() error {

return nil
}

// Enrich adds the global targets to the configuration
func (c *Config) Enrich(ctx context.Context, targets []checks.GlobalTarget) {
log := logger.FromContext(ctx)
for _, t := range targets {
hostname, err := t.Hostname()
if err != nil {
log.ErrorContext(ctx, "Failed to get hostname from target", "target", t.String(), "error", err)
continue
}

if !slices.Contains(c.Targets, hostname) {
log.DebugContext(ctx, "Adding target to DNS check", "target", hostname)
c.Targets = append(c.Targets, hostname)
}
}
}
23 changes: 22 additions & 1 deletion pkg/checks/health/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
package health

import (
"context"
"fmt"
"net/url"
"slices"
"time"

"github.com/telekom/sparrow/internal/helper"
"github.com/telekom/sparrow/internal/logger"
"github.com/telekom/sparrow/pkg/checks"
)

Expand All @@ -21,9 +24,9 @@ const (
// Config defines the configuration parameters for a health check
type Config struct {
Targets []string `json:"targets,omitempty" yaml:"targets,omitempty"`
Retry helper.RetryConfig `json:"retry" yaml:"retry"`
Interval time.Duration `json:"interval" yaml:"interval"`
Timeout time.Duration `json:"timeout" yaml:"timeout"`
Retry helper.RetryConfig `json:"retry" yaml:"retry"`
}

// For returns the name of the check
Expand Down Expand Up @@ -54,3 +57,21 @@ func (c *Config) Validate() error {

return nil
}

// Enrich adds the global targets to the configuration
func (c *Config) Enrich(ctx context.Context, targets []checks.GlobalTarget) {
log := logger.FromContext(ctx)
for _, t := range targets {
u, err := t.URL()
if err != nil {
log.ErrorContext(ctx, "Failed to get URL from target", "target", t.String(), "error", err)
continue
}

target := u.String()
if !slices.Contains(c.Targets, target) {
log.DebugContext(ctx, "Adding target to health check", "target", target)
c.Targets = append(c.Targets, target)
}
}
}
23 changes: 22 additions & 1 deletion pkg/checks/latency/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
package latency

import (
"context"
"fmt"
"net/url"
"slices"
"time"

"github.com/telekom/sparrow/internal/helper"
"github.com/telekom/sparrow/internal/logger"
"github.com/telekom/sparrow/pkg/checks"
)

Expand All @@ -21,9 +24,9 @@ const (
// Config defines the configuration parameters for a latency check
type Config struct {
Targets []string `json:"targets,omitempty" yaml:"targets,omitempty"`
Retry helper.RetryConfig `json:"retry" yaml:"retry"`
Interval time.Duration `json:"interval" yaml:"interval"`
Timeout time.Duration `json:"timeout" yaml:"timeout"`
Retry helper.RetryConfig `json:"retry" yaml:"retry"`
}

// For returns the name of the check
Expand Down Expand Up @@ -54,3 +57,21 @@ func (c *Config) Validate() error {

return nil
}

// Enrich adds the global targets to the configuration
func (c *Config) Enrich(ctx context.Context, targets []checks.GlobalTarget) {
log := logger.FromContext(ctx)
for _, t := range targets {
u, err := t.URL()
if err != nil {
log.ErrorContext(ctx, "Failed to get URL from target", "target", t.String(), "error", err)
continue
}

target := u.String()
if !slices.Contains(c.Targets, target) {
log.DebugContext(ctx, "Adding target to latency check", "target", target)
c.Targets = append(c.Targets, target)
}
}
}
12 changes: 12 additions & 0 deletions pkg/checks/runtime/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
package runtime

import (
"context"
"errors"

"github.com/telekom/sparrow/internal/logger"
"github.com/telekom/sparrow/pkg/checks"
"github.com/telekom/sparrow/pkg/checks/dns"
"github.com/telekom/sparrow/pkg/checks/health"
Expand Down Expand Up @@ -57,6 +59,16 @@ func (c Config) Iter() []checks.Runtime {
return configs
}

// Enrich enriches the configuration with the provided [GlobalTarget]s.
func (c Config) Enrich(ctx context.Context, targets []checks.GlobalTarget) Config {
l := logger.FromContext(ctx)
for _, cfg := range c.Iter() {
l.DebugContext(ctx, "Enriching check configuration", "check", cfg.For())
cfg.Enrich(ctx, targets)
}
return c
}

// size returns the number of checks configured
func (c Config) size() int {
size := 0
Expand Down
26 changes: 26 additions & 0 deletions pkg/checks/traceroute/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
package traceroute

import (
"context"
"fmt"
"net"
"net/url"
"slices"
"time"

"github.com/telekom/sparrow/internal/helper"
"github.com/telekom/sparrow/internal/logger"
"github.com/telekom/sparrow/pkg/checks"
)

Expand Down Expand Up @@ -53,3 +56,26 @@ func (c *Config) Validate() error {
}
return nil
}

// Enrich adds the global targets to the configuration
func (c *Config) Enrich(ctx context.Context, targets []checks.GlobalTarget) {
log := logger.FromContext(ctx)
for _, t := range targets {
u, err := t.URL()
if err != nil {
log.ErrorContext(ctx, "Failed to get URL from target", "target", t.String(), "error", err)
continue
}

// Error handling is not necessary here, as the URL has been validated before.
port, _ := t.Port()
if !slices.ContainsFunc(c.Targets, func(t Target) bool {
return t.Addr == u.Hostname() && t.Port == port
}) {
c.Targets = append(c.Targets, Target{
Addr: u.Hostname(),
Port: port,
})
}
}
}
33 changes: 9 additions & 24 deletions pkg/sparrow/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ package sparrow
import (
"context"
"fmt"
"net/url"
"slices"
"strings"
"sync"
"time"

"github.com/telekom/sparrow/internal/logger"
"github.com/telekom/sparrow/pkg/api"
"github.com/telekom/sparrow/pkg/checks"
"github.com/telekom/sparrow/pkg/checks/runtime"
"github.com/telekom/sparrow/pkg/config"
"github.com/telekom/sparrow/pkg/db"
Expand Down Expand Up @@ -126,36 +124,23 @@ func (s *Sparrow) Run(ctx context.Context) error {
// enrichTargets updates the targets of the sparrow's checks with the
// global targets. Per default, the two target lists are merged.
func (s *Sparrow) enrichTargets(ctx context.Context, cfg runtime.Config) runtime.Config {
l := logger.FromContext(ctx)
if cfg.Empty() || s.tarMan == nil {
return cfg
}

for _, gt := range s.tarMan.GetTargets() {
u, err := url.Parse(gt.Url)
var gts []checks.GlobalTarget
for _, t := range s.tarMan.GetTargets() {
hostname, err := t.Hostname()
if err != nil {
l.Error("Failed to parse global target URL", "error", err, "url", gt.Url)
logger.FromContext(ctx).ErrorContext(ctx, "Failed to get hostname from target", "target", t, "error", err)
continue
}

// split off hostWithoutPort because it could contain a port
hostWithoutPort := strings.Split(u.Host, ":")[0]
if hostWithoutPort == s.config.SparrowName {
// We don't need to enrich the configs with the own hostname
if s.config.SparrowName == hostname {
continue
}

if cfg.HasHealthCheck() && !slices.Contains(cfg.Health.Targets, u.String()) {
cfg.Health.Targets = append(cfg.Health.Targets, u.String())
}
if cfg.HasLatencyCheck() && !slices.Contains(cfg.Latency.Targets, u.String()) {
cfg.Latency.Targets = append(cfg.Latency.Targets, u.String())
}
if cfg.HasDNSCheck() && !slices.Contains(cfg.Dns.Targets, hostWithoutPort) {
cfg.Dns.Targets = append(cfg.Dns.Targets, hostWithoutPort)
}
gts = append(gts, t)
}

return cfg
return cfg.Enrich(ctx, gts)
}

// shutdown shuts down the sparrow and all managed components gracefully.
Expand Down
Loading
Loading