-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This supports the multihttp check type. It's not yet connected to the API (i.e. it might receive a multihttp check from the API but it won't run it). Signed-off-by: Marcelo E. Magallon <marcelo.magallon@grafana.com>
- Loading branch information
Showing
11 changed files
with
1,233 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package multihttp | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"time" | ||
|
||
"github.com/grafana/synthetic-monitoring-agent/internal/prober/logger" | ||
sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/rs/zerolog" | ||
) | ||
|
||
const proberName = "multi_http" | ||
|
||
var errUnsupportedCheck = errors.New("unsupported check") | ||
|
||
type Module struct { | ||
Prober string | ||
Timeout time.Duration | ||
} | ||
|
||
type Prober struct { | ||
logger zerolog.Logger | ||
config Module | ||
runner *runner | ||
} | ||
|
||
func NewProber(ctx context.Context, check sm.Check, logger zerolog.Logger) (Prober, error) { | ||
var p Prober | ||
|
||
if check.Settings.Multihttp == nil { | ||
return p, errUnsupportedCheck | ||
} | ||
|
||
if err := check.Settings.Multihttp.Validate(); err != nil { | ||
return p, err | ||
} | ||
|
||
p.config = settingsToModule(check.Settings.Multihttp) | ||
p.config.Timeout = time.Duration(check.Timeout) * time.Millisecond | ||
|
||
r, err := newRunner(check.Settings.Multihttp) | ||
if err != nil { | ||
return p, err | ||
} | ||
|
||
p.runner = r | ||
p.logger = logger | ||
|
||
return p, nil | ||
} | ||
|
||
func (p Prober) Name() string { | ||
return proberName | ||
} | ||
|
||
func (p Prober) Probe(ctx context.Context, target string, registry *prometheus.Registry, logger logger.Logger) bool { | ||
err := p.runner.Run(ctx, registry, logger, p.logger) | ||
if err != nil { | ||
p.logger.Warn().Err(err).Msg("running probe") | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
func settingsToModule(settings *sm.MultiHttpSettings) Module { | ||
var m Module | ||
|
||
m.Prober = sm.CheckTypeMultiHttp.String() | ||
|
||
return m | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package multihttp | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring" | ||
"github.com/rs/zerolog" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNewProber(t *testing.T) { | ||
ctx, cancel := testContext(t) | ||
t.Cleanup(cancel) | ||
|
||
logger := zerolog.New(zerolog.NewTestWriter(t)) | ||
|
||
testcases := map[string]struct { | ||
check synthetic_monitoring.Check | ||
expectFailure bool | ||
}{ | ||
"valid": { | ||
expectFailure: false, | ||
check: synthetic_monitoring.Check{ | ||
Target: "http://www.example.org", | ||
Job: "test", | ||
Frequency: 10 * 1000, | ||
Timeout: 10 * 1000, | ||
Probes: []int64{1}, | ||
Settings: synthetic_monitoring.CheckSettings{ | ||
Multihttp: &synthetic_monitoring.MultiHttpSettings{ | ||
Entries: []*synthetic_monitoring.MultiHttpEntry{ | ||
{ | ||
Request: &synthetic_monitoring.MultiHttpEntryRequest{ | ||
Url: "http://www.example.org", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"settings must be valid": { | ||
expectFailure: true, | ||
check: synthetic_monitoring.Check{ | ||
Target: "http://www.example.org", | ||
Job: "test", | ||
Frequency: 10 * 1000, | ||
Timeout: 10 * 1000, | ||
Probes: []int64{1}, | ||
Settings: synthetic_monitoring.CheckSettings{ | ||
Multihttp: &synthetic_monitoring.MultiHttpSettings{ | ||
Entries: []*synthetic_monitoring.MultiHttpEntry{ | ||
// This is invalid because the requsest does not have a URL | ||
{}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"must contain multihttp settings": { | ||
expectFailure: true, | ||
check: synthetic_monitoring.Check{ | ||
Target: "http://www.example.org", | ||
Job: "test", | ||
Frequency: 10 * 1000, | ||
Timeout: 10 * 1000, | ||
Probes: []int64{1}, | ||
Settings: synthetic_monitoring.CheckSettings{ | ||
// The settings are valid for ping, but not for multihttp | ||
Ping: &synthetic_monitoring.PingSettings{}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for name, tc := range testcases { | ||
t.Run(name, func(t *testing.T) { | ||
p, err := NewProber(ctx, tc.check, logger) | ||
if tc.expectFailure { | ||
require.Error(t, err) | ||
return | ||
} | ||
|
||
require.NoError(t, err) | ||
require.Equal(t, proberName, p.config.Prober) | ||
require.Equal(t, 10*time.Second, p.config.Timeout) | ||
// TODO: check script | ||
}) | ||
} | ||
} | ||
|
||
func testContext(t *testing.T) (context.Context, func()) { | ||
if deadline, ok := t.Deadline(); ok { | ||
return context.WithDeadline(context.Background(), deadline) | ||
} | ||
|
||
return context.WithTimeout(context.Background(), 10*time.Second) | ||
} |
Oops, something went wrong.