-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a new grpc package under prober which implements the gRPC prober, delegating most of its functionality to the BBE `ProbeGRPC` method.
- Loading branch information
Showing
2 changed files
with
217 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package grpc | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"time" | ||
|
||
"github.com/grafana/synthetic-monitoring-agent/internal/prober/logger" | ||
"github.com/grafana/synthetic-monitoring-agent/internal/tls" | ||
sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring" | ||
"github.com/prometheus/blackbox_exporter/config" | ||
bbeprober "github.com/prometheus/blackbox_exporter/prober" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/rs/zerolog" | ||
) | ||
|
||
var errUnsupportedCheck = errors.New("unsupported check") | ||
|
||
type Prober struct { | ||
config config.Module | ||
} | ||
|
||
func NewProber(ctx context.Context, check sm.Check, logger zerolog.Logger) (Prober, error) { | ||
if check.Settings.Grpc == nil { | ||
return Prober{}, errUnsupportedCheck | ||
} | ||
|
||
cfg, err := settingsToModule(ctx, check.Settings.Grpc, logger) | ||
if err != nil { | ||
return Prober{}, err | ||
} | ||
|
||
cfg.Timeout = time.Duration(check.Timeout) * time.Millisecond | ||
|
||
return Prober{ | ||
config: cfg, | ||
}, nil | ||
} | ||
|
||
func (p Prober) Name() string { | ||
return "grpc" | ||
} | ||
|
||
func (p Prober) Probe(ctx context.Context, target string, registry *prometheus.Registry, logger logger.Logger) bool { | ||
return bbeprober.ProbeGRPC(ctx, target, p.config, registry, logger) | ||
} | ||
|
||
func settingsToModule(ctx context.Context, settings *sm.GrpcSettings, logger zerolog.Logger) (config.Module, error) { | ||
var m config.Module | ||
|
||
m.Prober = sm.CheckTypeGrpc.String() | ||
|
||
m.GRPC.PreferredIPProtocol, m.GRPC.IPProtocolFallback = settings.IpVersion.ToIpProtocol() | ||
|
||
m.GRPC.Service = settings.Service | ||
|
||
m.GRPC.TLS = settings.Tls | ||
|
||
if settings.TlsConfig != nil { | ||
var err error | ||
m.GRPC.TLSConfig, err = tls.SMtoProm(ctx, logger.With().Str("prober", m.Prober).Logger(), settings.TlsConfig) | ||
if err != nil { | ||
return m, err | ||
} | ||
} | ||
|
||
return m, nil | ||
} |
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,149 @@ | ||
package grpc | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"testing" | ||
|
||
sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring" | ||
"github.com/prometheus/blackbox_exporter/config" | ||
promcfg "github.com/prometheus/common/config" | ||
"github.com/rs/zerolog" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestName(t *testing.T) { | ||
name := Prober.Name(Prober{}) | ||
require.Equal(t, name, "grpc") | ||
} | ||
|
||
func TestNewProber(t *testing.T) { | ||
testcases := map[string]struct { | ||
input sm.Check | ||
expected Prober | ||
ExpectError bool | ||
}{ | ||
"default": { | ||
input: sm.Check{ | ||
Target: "www.grafana.com", | ||
Settings: sm.CheckSettings{ | ||
Grpc: &sm.GrpcSettings{}, | ||
}, | ||
}, | ||
expected: Prober{ | ||
config.Module{ | ||
Prober: "grpc", | ||
Timeout: 0, | ||
GRPC: config.GRPCProbe{ | ||
PreferredIPProtocol: "ip6", | ||
IPProtocolFallback: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"no-settings": { | ||
input: sm.Check{ | ||
Target: "www.grafana.com", | ||
Settings: sm.CheckSettings{ | ||
Grpc: nil, | ||
}, | ||
}, | ||
ExpectError: true, | ||
}, | ||
} | ||
|
||
ctx := context.Background() | ||
logger := zerolog.New(io.Discard) | ||
|
||
for name, tc := range testcases { | ||
t.Run(name, func(t *testing.T) { | ||
actual, err := NewProber(ctx, tc.input, logger) | ||
|
||
require.Equal(t, &tc.expected, &actual) | ||
if tc.ExpectError { | ||
require.Error(t, err, "unsupported check") | ||
} else { | ||
require.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestSettingsToModule(t *testing.T) { | ||
testcases := map[string]struct { | ||
input sm.GrpcSettings | ||
expected config.Module | ||
}{ | ||
"default": { | ||
input: sm.GrpcSettings{}, | ||
expected: config.Module{ | ||
Prober: "grpc", | ||
Timeout: 0, | ||
GRPC: config.GRPCProbe{ | ||
PreferredIPProtocol: "ip6", | ||
IPProtocolFallback: true, | ||
}, | ||
}, | ||
}, | ||
"custom svc": { | ||
input: sm.GrpcSettings{ | ||
Service: "customSvc", | ||
}, | ||
expected: config.Module{ | ||
Prober: "grpc", | ||
Timeout: 0, | ||
GRPC: config.GRPCProbe{ | ||
Service: "customSvc", | ||
PreferredIPProtocol: "ip6", | ||
IPProtocolFallback: true, | ||
}, | ||
}, | ||
}, | ||
"ipv4": { | ||
input: sm.GrpcSettings{ | ||
IpVersion: sm.IpVersion_V4, | ||
}, | ||
expected: config.Module{ | ||
Prober: "grpc", | ||
Timeout: 0, | ||
GRPC: config.GRPCProbe{ | ||
PreferredIPProtocol: "ip4", | ||
IPProtocolFallback: false, | ||
}, | ||
}, | ||
}, | ||
"tls": { | ||
input: sm.GrpcSettings{ | ||
Tls: true, | ||
TlsConfig: &sm.TLSConfig{ | ||
InsecureSkipVerify: true, | ||
ServerName: "grafana.com", | ||
}, | ||
}, | ||
expected: config.Module{ | ||
Prober: "grpc", | ||
Timeout: 0, | ||
GRPC: config.GRPCProbe{ | ||
PreferredIPProtocol: "ip6", | ||
IPProtocolFallback: true, | ||
TLS: true, | ||
TLSConfig: promcfg.TLSConfig{ | ||
InsecureSkipVerify: true, | ||
ServerName: "grafana.com", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
ctx := context.Background() | ||
logger := zerolog.New(io.Discard) | ||
|
||
for name, tc := range testcases { | ||
t.Run(name, func(t *testing.T) { | ||
actual, err := settingsToModule(ctx, &tc.input, logger) | ||
require.NoError(t, err) | ||
require.Equal(t, &tc.expected, &actual) | ||
}) | ||
} | ||
} |