Skip to content

Commit

Permalink
Add gRPC test cases to scraper's TestValidateMetrics
Browse files Browse the repository at this point in the history
This also adds the test data corresponding to the gRPC probe metrics and
updates the active series count data.
  • Loading branch information
ka3de committed Nov 29, 2023
1 parent 9e56c40 commit 5a61853
Show file tree
Hide file tree
Showing 6 changed files with 455 additions and 2 deletions.
121 changes: 119 additions & 2 deletions internal/scraper/scraper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/grafana/synthetic-monitoring-agent/internal/pkg/logproto"
"github.com/grafana/synthetic-monitoring-agent/internal/prober"
dnsProber "github.com/grafana/synthetic-monitoring-agent/internal/prober/dns"
grpcProber "github.com/grafana/synthetic-monitoring-agent/internal/prober/grpc"
httpProber "github.com/grafana/synthetic-monitoring-agent/internal/prober/http"
"github.com/grafana/synthetic-monitoring-agent/internal/prober/icmp"
"github.com/grafana/synthetic-monitoring-agent/internal/prober/k6"
Expand All @@ -46,6 +47,9 @@ import (
"github.com/prometheus/prometheus/promql/parser"
"github.com/rs/zerolog"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
grpchealth "google.golang.org/grpc/health/grpc_health_v1"
"kernel.org/pub/linux/libs/security/libcap/cap"
)

Expand Down Expand Up @@ -344,6 +348,58 @@ func TestValidateMetrics(t *testing.T) {
return prober, check, httpSrv.Close
},
},

"grpc": {
setup: func(ctx context.Context, t *testing.T) (prober.Prober, sm.Check, func()) {
srv, clean := setupGRPCServer(t)
check := sm.Check{
Target: srv,
Timeout: 2000,
Settings: sm.CheckSettings{
Grpc: &sm.GrpcSettings{
IpVersion: sm.IpVersion_V4,
},
},
}
prober, err := grpcProber.NewProber(
ctx,
check,
zerolog.New(io.Discard))
if err != nil {
clean()
t.Fatalf("cannot create gRPC prober: %s", err)
}
return prober, check, clean
},
},

"grpc_ssl": {
setup: func(ctx context.Context, t *testing.T) (prober.Prober, sm.Check, func()) {
srv, clean := setupGRPCServerWithSSL(t)
check := sm.Check{
Target: srv,
Timeout: 2000,
Settings: sm.CheckSettings{
Grpc: &sm.GrpcSettings{
IpVersion: sm.IpVersion_V4,
Tls: true,
TlsConfig: &sm.TLSConfig{
InsecureSkipVerify: true,
},
},
},
}
prober, err := grpcProber.NewProber(
ctx,
check,
zerolog.New(io.Discard))
if err != nil {
clean()
t.Fatalf("cannot create gRPC prober: %s", err)
}
return prober, check, clean
},
},
}

for name, testcase := range testcases {
Expand Down Expand Up @@ -630,6 +686,68 @@ func setupTCPServerWithSSL(t *testing.T) (string, func()) {
}
}

type gRPCSrv struct {
grpchealth.HealthServer
}

func (s *gRPCSrv) Check(
context.Context, *grpchealth.HealthCheckRequest,
) (*grpchealth.HealthCheckResponse, error) {
return &grpchealth.HealthCheckResponse{
Status: grpchealth.HealthCheckResponse_SERVING,
}, nil
}

func setupGRPCServer(t *testing.T) (string, func()) {
lis, err := net.Listen("tcp4", ":0")
if err != nil {
t.Fatalf("Error listening on socket: %v", err)
}

srv := grpc.NewServer()
grpchealth.RegisterHealthServer(srv, &gRPCSrv{})

go func() {
_ = srv.Serve(lis)
}()

return lis.Addr().String(), func() {
srv.Stop()
lis.Close()
}
}

func setupGRPCServerWithSSL(t *testing.T) (string, func()) {
lis, err := net.Listen("tcp4", ":0")
if err != nil {
t.Fatalf("Error listening on socket: %v", err)
}

cert, err := tls.X509KeyPair(localhostCert, localhostKey)
if err != nil {
t.Fatalf("Error creating X509 key pair: %v", err)
}

tlsCfg := tls.Config{
Certificates: []tls.Certificate{cert},
NextProtos: []string{"foo"},
}

srv := grpc.NewServer(
grpc.Creds(credentials.NewTLS(&tlsCfg)),
)
grpchealth.RegisterHealthServer(srv, &gRPCSrv{})

go func() {
_ = srv.Serve(lis)
}()

return lis.Addr().String(), func() {
srv.Stop()
lis.Close()
}
}

func TestMakeTimeseries(t *testing.T) {
testTime := time.Now()
testValue := 42.0
Expand Down Expand Up @@ -1403,8 +1521,7 @@ func (f testProbeFactory) New(ctx context.Context, logger zerolog.Logger, check
return f.builder(), check.Target, nil
}

type testPublisher struct {
}
type testPublisher struct{}

func (testPublisher) Publish(pusher.Payload) {
}
Expand Down
103 changes: 103 additions & 0 deletions internal/scraper/testdata/grpc.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# HELP probe_dns_lookup_time_seconds Returns the time taken for probe dns lookup in seconds
# TYPE probe_dns_lookup_time_seconds gauge
probe_dns_lookup_time_seconds 2.431e-06
# HELP probe_duration_seconds Returns how long the probe took to complete in seconds
# TYPE probe_duration_seconds gauge
probe_duration_seconds 0.00072303
# HELP probe_grpc_duration_seconds Duration of gRPC request by phase
# TYPE probe_grpc_duration_seconds gauge
probe_grpc_duration_seconds{phase="check"} 0.000652168
probe_grpc_duration_seconds{phase="resolve"} 2.431e-06
# HELP probe_grpc_healthcheck_response Response HealthCheck response
# TYPE probe_grpc_healthcheck_response gauge
probe_grpc_healthcheck_response{serving_status="NOT_SERVING"} 0
probe_grpc_healthcheck_response{serving_status="SERVICE_UNKNOWN"} 0
probe_grpc_healthcheck_response{serving_status="SERVING"} 1
probe_grpc_healthcheck_response{serving_status="UNKNOWN"} 0
# HELP probe_grpc_ssl Indicates if SSL was used for the connection
# TYPE probe_grpc_ssl gauge
probe_grpc_ssl 0
# HELP probe_grpc_status_code Response gRPC status code
# TYPE probe_grpc_status_code gauge
probe_grpc_status_code 0
# HELP probe_ip_addr_hash Specifies the hash of IP address. It's useful to detect if the IP address changes.
# TYPE probe_ip_addr_hash gauge
probe_ip_addr_hash 1.268118805e+09
# HELP probe_ip_protocol Specifies whether probe ip protocol is IP4 or IP6
# TYPE probe_ip_protocol gauge
probe_ip_protocol 4
# HELP probe_ssl_earliest_cert_expiry Returns last SSL chain expiry in unixtime
# TYPE probe_ssl_earliest_cert_expiry gauge
probe_ssl_earliest_cert_expiry 0
# HELP probe_success Displays whether or not the probe was a success
# TYPE probe_success gauge
probe_success 1
# HELP sm_check_info Provides information about a single check configuration
# TYPE sm_check_info gauge
sm_check_info 1
# HELP probe_all_duration_seconds Returns how long the probe took to complete in seconds (histogram)
# TYPE probe_all_duration_seconds histogram
probe_all_duration_seconds_bucket{le="0.005"} 1
probe_all_duration_seconds_bucket{le="0.01"} 1
probe_all_duration_seconds_bucket{le="0.025"} 1
probe_all_duration_seconds_bucket{le="0.05"} 1
probe_all_duration_seconds_bucket{le="0.1"} 1
probe_all_duration_seconds_bucket{le="0.25"} 1
probe_all_duration_seconds_bucket{le="0.5"} 1
probe_all_duration_seconds_bucket{le="1"} 1
probe_all_duration_seconds_bucket{le="2.5"} 1
probe_all_duration_seconds_bucket{le="5"} 1
probe_all_duration_seconds_bucket{le="10"} 1
probe_all_duration_seconds_bucket{le="+Inf"} 1
probe_all_duration_seconds_sum 0.00072303
probe_all_duration_seconds_count 1
# HELP probe_all_success Displays whether or not the probe was a success (summary)
# TYPE probe_all_success summary
probe_all_success_sum 1
probe_all_success_count 1
# HELP probe_dns_lookup_all_time_seconds Returns the time taken for probe dns lookup in seconds (histogram)
# TYPE probe_dns_lookup_all_time_seconds histogram
probe_dns_lookup_all_time_seconds_bucket{le="0.005"} 1
probe_dns_lookup_all_time_seconds_bucket{le="0.01"} 1
probe_dns_lookup_all_time_seconds_bucket{le="0.025"} 1
probe_dns_lookup_all_time_seconds_bucket{le="0.05"} 1
probe_dns_lookup_all_time_seconds_bucket{le="0.1"} 1
probe_dns_lookup_all_time_seconds_bucket{le="0.25"} 1
probe_dns_lookup_all_time_seconds_bucket{le="0.5"} 1
probe_dns_lookup_all_time_seconds_bucket{le="1"} 1
probe_dns_lookup_all_time_seconds_bucket{le="2.5"} 1
probe_dns_lookup_all_time_seconds_bucket{le="5"} 1
probe_dns_lookup_all_time_seconds_bucket{le="10"} 1
probe_dns_lookup_all_time_seconds_bucket{le="+Inf"} 1
probe_dns_lookup_all_time_seconds_sum 2.431e-06
probe_dns_lookup_all_time_seconds_count 1
# HELP probe_grpc_all_duration_seconds Duration of gRPC request by phase (histogram)
# TYPE probe_grpc_all_duration_seconds histogram
probe_grpc_all_duration_seconds_bucket{phase="check",le="0.005"} 1
probe_grpc_all_duration_seconds_bucket{phase="check",le="0.01"} 1
probe_grpc_all_duration_seconds_bucket{phase="check",le="0.025"} 1
probe_grpc_all_duration_seconds_bucket{phase="check",le="0.05"} 1
probe_grpc_all_duration_seconds_bucket{phase="check",le="0.1"} 1
probe_grpc_all_duration_seconds_bucket{phase="check",le="0.25"} 1
probe_grpc_all_duration_seconds_bucket{phase="check",le="0.5"} 1
probe_grpc_all_duration_seconds_bucket{phase="check",le="1"} 1
probe_grpc_all_duration_seconds_bucket{phase="check",le="2.5"} 1
probe_grpc_all_duration_seconds_bucket{phase="check",le="5"} 1
probe_grpc_all_duration_seconds_bucket{phase="check",le="10"} 1
probe_grpc_all_duration_seconds_bucket{phase="check",le="+Inf"} 1
probe_grpc_all_duration_seconds_sum{phase="check"} 0.000652168
probe_grpc_all_duration_seconds_count{phase="check"} 1
probe_grpc_all_duration_seconds_bucket{phase="resolve",le="0.005"} 1
probe_grpc_all_duration_seconds_bucket{phase="resolve",le="0.01"} 1
probe_grpc_all_duration_seconds_bucket{phase="resolve",le="0.025"} 1
probe_grpc_all_duration_seconds_bucket{phase="resolve",le="0.05"} 1
probe_grpc_all_duration_seconds_bucket{phase="resolve",le="0.1"} 1
probe_grpc_all_duration_seconds_bucket{phase="resolve",le="0.25"} 1
probe_grpc_all_duration_seconds_bucket{phase="resolve",le="0.5"} 1
probe_grpc_all_duration_seconds_bucket{phase="resolve",le="1"} 1
probe_grpc_all_duration_seconds_bucket{phase="resolve",le="2.5"} 1
probe_grpc_all_duration_seconds_bucket{phase="resolve",le="5"} 1
probe_grpc_all_duration_seconds_bucket{phase="resolve",le="10"} 1
probe_grpc_all_duration_seconds_bucket{phase="resolve",le="+Inf"} 1
probe_grpc_all_duration_seconds_sum{phase="resolve"} 2.431e-06
probe_grpc_all_duration_seconds_count{phase="resolve"} 1
57 changes: 57 additions & 0 deletions internal/scraper/testdata/grpc_basic.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# HELP probe_dns_lookup_time_seconds Returns the time taken for probe dns lookup in seconds
# TYPE probe_dns_lookup_time_seconds gauge
probe_dns_lookup_time_seconds 2.765e-06
# HELP probe_duration_seconds Returns how long the probe took to complete in seconds
# TYPE probe_duration_seconds gauge
probe_duration_seconds 0.00047477
# HELP probe_grpc_duration_seconds Duration of gRPC request by phase
# TYPE probe_grpc_duration_seconds gauge
probe_grpc_duration_seconds{phase="check"} 0.000359518
probe_grpc_duration_seconds{phase="resolve"} 2.765e-06
# HELP probe_grpc_healthcheck_response Response HealthCheck response
# TYPE probe_grpc_healthcheck_response gauge
probe_grpc_healthcheck_response{serving_status="NOT_SERVING"} 0
probe_grpc_healthcheck_response{serving_status="SERVICE_UNKNOWN"} 0
probe_grpc_healthcheck_response{serving_status="SERVING"} 1
probe_grpc_healthcheck_response{serving_status="UNKNOWN"} 0
# HELP probe_grpc_ssl Indicates if SSL was used for the connection
# TYPE probe_grpc_ssl gauge
probe_grpc_ssl 0
# HELP probe_grpc_status_code Response gRPC status code
# TYPE probe_grpc_status_code gauge
probe_grpc_status_code 0
# HELP probe_ip_addr_hash Specifies the hash of IP address. It's useful to detect if the IP address changes.
# TYPE probe_ip_addr_hash gauge
probe_ip_addr_hash 1.268118805e+09
# HELP probe_ip_protocol Specifies whether probe ip protocol is IP4 or IP6
# TYPE probe_ip_protocol gauge
probe_ip_protocol 4
# HELP probe_ssl_earliest_cert_expiry Returns last SSL chain expiry in unixtime
# TYPE probe_ssl_earliest_cert_expiry gauge
probe_ssl_earliest_cert_expiry 0
# HELP probe_success Displays whether or not the probe was a success
# TYPE probe_success gauge
probe_success 1
# HELP sm_check_info Provides information about a single check configuration
# TYPE sm_check_info gauge
sm_check_info 1
# HELP probe_all_duration_seconds Returns how long the probe took to complete in seconds (histogram)
# TYPE probe_all_duration_seconds histogram
probe_all_duration_seconds_bucket{le="0.005"} 1
probe_all_duration_seconds_bucket{le="0.01"} 1
probe_all_duration_seconds_bucket{le="0.025"} 1
probe_all_duration_seconds_bucket{le="0.05"} 1
probe_all_duration_seconds_bucket{le="0.1"} 1
probe_all_duration_seconds_bucket{le="0.25"} 1
probe_all_duration_seconds_bucket{le="0.5"} 1
probe_all_duration_seconds_bucket{le="1"} 1
probe_all_duration_seconds_bucket{le="2.5"} 1
probe_all_duration_seconds_bucket{le="5"} 1
probe_all_duration_seconds_bucket{le="10"} 1
probe_all_duration_seconds_bucket{le="+Inf"} 1
probe_all_duration_seconds_sum 0.00047477
probe_all_duration_seconds_count 1
# HELP probe_all_success Displays whether or not the probe was a success (summary)
# TYPE probe_all_success summary
probe_all_success_sum 1
probe_all_success_count 1
Loading

0 comments on commit 5a61853

Please sign in to comment.