Skip to content

Commit

Permalink
Merge pull request #573 from stakater/enhance-grafana
Browse files Browse the repository at this point in the history
Add Probes and Freq in GrafanaConfig
  • Loading branch information
MuneebAijaz authored Mar 15, 2024
2 parents e9afb8c + 8fc791d commit e03ec0c
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 15 deletions.
8 changes: 8 additions & 0 deletions api/v1alpha1/endpointmonitor_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,14 @@ type GCloudConfig struct {
// GrafnaConfiguration defines the configuration for Grafana Cloud Monitor Provider
type GrafanaConfig struct {
TenantId int64 `json:"tenantId,omitempty"`

// The frequency value specifies how often the check runs in milliseconds
Frequency int64 `json:"frequency,omitempty"`

// Probes are the monitoring agents responsible for simulating user interactions with your web applications
// or services. These agents periodically send requests to predefined URLs and record the responses,
// checking for expected outcomes and measuring performance.
Probes []string `json:"probes,omitempty"`
}

// URLSource represents the set of resources to fetch the URL from
Expand Down
7 changes: 6 additions & 1 deletion api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ spec:
grafanaConfig:
description: Configuration for Grafana Cloud Monitor Provider
properties:
frequency:
description: The frequency value specifies how often the check
runs in milliseconds
format: int64
type: integer
probes:
description: probes are the monitoring agents responsible for
simulating user interactions with your web applications or services.
These agents periodically send requests to predefined URLs and
record the responses, checking for expected outcomes and measuring
performance.
items:
type: string
type: array
tenantId:
format: int64
type: integer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ spec:
grafanaConfig:
description: Configuration for Grafana Cloud Monitor Provider
properties:
frequency:
description: The frequency value specifies how often the check
runs in milliseconds
format: int64
type: integer
probes:
description: Probes are the monitoring agents responsible for
simulating user interactions with your web applications or services.
These agents periodically send requests to predefined URLs and
record the responses, checking for expected outcomes and measuring
performance.
items:
type: string
type: array
tenantId:
format: int64
type: integer
Expand Down
7 changes: 5 additions & 2 deletions docs/grafana-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ metadata:
spec:
forceHttps: true
url: https://stakater.com/
gcloudConfig:
frequency: 5000
grafanaConfig:
frequency: 10000
probes:
- Amsterdam
- Singapore
```
56 changes: 48 additions & 8 deletions pkg/monitors/grafana/grafana-monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package grafana
import (
"context"
"fmt"
"net/http"
"reflect"
"strconv"

"github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring"
smapi "github.com/grafana/synthetic-monitoring-api-go-client"
endpointmonitorv1alpha1 "github.com/stakater/IngressMonitorController/v2/api/v1alpha1"
"github.com/stakater/IngressMonitorController/v2/pkg/config"
"github.com/stakater/IngressMonitorController/v2/pkg/models"
"net/http"
"reflect"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"strconv"
)

var log = logf.Log.WithName("grafana-monitor")
Expand Down Expand Up @@ -71,28 +72,67 @@ func (service *GrafanaMonitorService) GetAll() (monitors []models.Monitor) {
log.Error(err, "Error getting monitors")
return nil
}
availableProbes, err := service.smClient.ListProbes(service.ctx)
if err != nil {
log.Error(err, "Error getting probes")
return nil
}

var probes []string
for _, check := range checks {
for _, probeId := range check.Probes {
for _, availableProbe := range availableProbes {
if probeId == availableProbe.Id {
probes = append(probes, availableProbe.Name)
}
}
}
monitors = append(monitors, models.Monitor{
Name: check.Job,
URL: check.Target,
ID: fmt.Sprintf("%v", check.Id),
Config: &endpointmonitorv1alpha1.GrafanaConfig{
TenantId: check.TenantId,
TenantId: check.TenantId,
Frequency: check.Frequency,
Probes: probes,
},
})
}
return monitors
}

func (service *GrafanaMonitorService) CreateSyntheticCheck(monitor models.Monitor, tenantID int64) (*synthetic_monitoring.Check, error) {
probes, err := service.smClient.ListProbes(service.ctx)

availableProbes, err := service.smClient.ListProbes(service.ctx)
if err != nil {
return nil, fmt.Errorf("Error listing probes %v", err)
}

probeIDs := make([]int64, len(probes))
for i, p := range probes {
var probeToSet []synthetic_monitoring.Probe
var configProbeNames []string
var frequency int64 = service.frequency
providerConfig, _ := monitor.Config.(*endpointmonitorv1alpha1.GrafanaConfig)
if providerConfig != nil {
// load configs from EndpointMonitor CR
if providerConfig.Frequency > 0 {
frequency = providerConfig.Frequency
}
if len(providerConfig.Probes) > 0 {
configProbeNames = providerConfig.Probes
for _, probe := range availableProbes {
for _, configProbe := range configProbeNames {
if probe.Name == configProbe {
probeToSet = append(probeToSet, probe)
}
}
}
availableProbes = probeToSet
}
}

// if probes are set in EndpointMonitor, apply those, otherwise apply all of the available probe options
probeIDs := make([]int64, len(availableProbes))
for i, p := range availableProbes {
probeIDs[i] = p.Id
}

Expand All @@ -105,7 +145,7 @@ func (service *GrafanaMonitorService) CreateSyntheticCheck(monitor models.Monito
Id: checkId,
Target: monitor.URL,
Job: monitor.Name,
Frequency: service.frequency,
Frequency: frequency,
TenantId: tenantID,
Timeout: 2000,
Enabled: true,
Expand Down
17 changes: 13 additions & 4 deletions pkg/monitors/grafana/grafana-monitor_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package grafana

import (
"reflect"
"testing"

endpointmonitorv1alpha1 "github.com/stakater/IngressMonitorController/v2/api/v1alpha1"
"github.com/stakater/IngressMonitorController/v2/pkg/config"
"github.com/stakater/IngressMonitorController/v2/pkg/models"
"github.com/stakater/IngressMonitorController/v2/pkg/util"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"testing"
)

func init() {
Expand All @@ -26,7 +28,11 @@ func TestAddMonitorWithCorrectValues(t *testing.T) {
}
service.Setup(*provider)

m := models.Monitor{Name: "google-test", URL: "https://google.com"}
m := models.Monitor{Name: "google-test", URL: "https://google.com", Config: &endpointmonitorv1alpha1.GrafanaConfig{
Frequency: 20000,
Probes: []string{"Singapore"},
}}

preExistingMonitor, _ := service.GetByName(m.Name)

if preExistingMonitor != nil {
Expand All @@ -50,8 +56,11 @@ func TestAddMonitorWithCorrectValues(t *testing.T) {
if err != nil {
t.Error("Monitor should've been found", monitor, err)
}
if monitor.Name != m.Name || monitor.URL != m.URL {
t.Error("URL and name should be the same", monitor, m)
monitorConfig, _ := monitor.Config.(*endpointmonitorv1alpha1.GrafanaConfig)
providerConfig, _ := m.Config.(*endpointmonitorv1alpha1.GrafanaConfig)

if monitor.Name != m.Name || monitor.URL != m.URL || monitorConfig.Frequency != providerConfig.Frequency || reflect.DeepEqual(monitorConfig.Probes, providerConfig.Probes) {
t.Error("URL, name, frequency and probes should be the same", monitor, m)
}
service.Remove(*monitor)

Expand Down

0 comments on commit e03ec0c

Please sign in to comment.