Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flag to Ignore unsuccessful assessments due to intermittent ssllabs issue #24

Merged
merged 5 commits into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 20 additions & 1 deletion pkg/exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"github.com/prometheus/client_golang/prometheus"
)

const probeSuccessMetricName = "ssllabs_probe_success"

// Handle runs SSLLabs assessment on the specified target
// and returns a Prometheus Registry with the results
func Handle(ctx context.Context, logger log.Logger, target string) prometheus.Gatherer {
Expand All @@ -34,7 +36,7 @@ func Handle(ctx context.Context, logger log.Logger, target string) prometheus.Ga
Help: "Displays how long the assessment took to complete in seconds",
})
probeSuccessGauge = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "ssllabs_probe_success",
Name: probeSuccessMetricName,
Help: "Displays whether the assessment succeeded or not",
})

Expand Down Expand Up @@ -81,3 +83,20 @@ func Handle(ctx context.Context, logger log.Logger, target string) prometheus.Ga

return registry
}

// Failed checks whether the assessment failed or not
func Failed(registry prometheus.Gatherer) bool {
metrics, err := registry.Gather()
if err != nil {
return false
}

for _, m := range metrics {
if m.GetName() == probeSuccessMetricName {
result := m.GetMetric()[0].GetGauge().Value
return *result == 0
}
}

return false
}
64 changes: 64 additions & 0 deletions pkg/exporter/exporter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2020 Anas Ait Said Oubrahim

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package exporter

import (
"testing"

"github.com/prometheus/client_golang/prometheus"
)

func TestFailed(t *testing.T) {
var cases = []struct {
name string
value float64
expectedResult bool
}{
{
name: "failed_assessment",
value: 0,
expectedResult: true,
},
{
name: "successful_assessment",
value: 1,
expectedResult: false,
},
{
name: "unregistered_metric",
value: -1,
expectedResult: false,
},
}

for _, c := range cases {
// init registry
registry := prometheus.NewRegistry()
probeSuccessGauge := prometheus.NewGauge(prometheus.GaugeOpts{
Name: probeSuccessMetricName,
Help: "Displays whether the assessment succeeded or not",
})

if c.value != -1 {
registry.MustRegister(probeSuccessGauge)
probeSuccessGauge.Set(c.value)
}

result := Failed(registry)
if result != c.expectedResult {
t.Errorf("Test case : %v failed.\nExpected : %v\nGot : %v\n", c.name, c.expectedResult, result)
}
}
}
14 changes: 10 additions & 4 deletions ssllabs_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ const (
)

var (
listenAddress = kingpin.Flag("listen-address", "The address to listen on for HTTP requests.").Default(":19115").String()
probeTimeout = kingpin.Flag("timeout", "Time duration before canceling an ongoing probe such as 30m or 1h5m. This value must be at least 1m. Valid duration units are ns, us (or µs), ms, s, m, h.").Default("10m").String()
logLevel = kingpin.Flag("log-level", "Printed logs level.").Default("debug").Enum("error", "warn", "info", "debug")
cacheRetention = kingpin.Flag("cache-retention", "Time duration to keep entries in cache such as 30m or 1h5m. Valid duration units are ns, us (or µs), ms, s, m, h.").Default("1h").String()
listenAddress = kingpin.Flag("listen-address", "The address to listen on for HTTP requests.").Default(":19115").String()
probeTimeout = kingpin.Flag("timeout", "Time duration before canceling an ongoing probe such as 30m or 1h5m. This value must be at least 1m. Valid duration units are ns, us (or µs), ms, s, m, h.").Default("10m").String()
logLevel = kingpin.Flag("log-level", "Printed logs level.").Default("debug").Enum("error", "warn", "info", "debug")
cacheRetention = kingpin.Flag("cache-retention", "Time duration to keep entries in cache such as 30m or 1h5m. Valid duration units are ns, us (or µs), ms, s, m, h.").Default("1h").String()
cacheIgnoreFailed = kingpin.Flag("cache-ignore-failed", "Do not cache failed results due to intermittent SSLLabs issues.").Default("False").Bool()

// build parameters
branch string
Expand Down Expand Up @@ -78,6 +79,11 @@ func probeHandler(w http.ResponseWriter, r *http.Request, logger log.Logger, tim

registry = exporter.Handle(ctx, logger, target)

// do not cache failed assessments if configured
if *cacheIgnoreFailed && exporter.Failed(registry) {
return
}

// add the assessment results to the cache
resultsCache.add(target, registry)
}
Expand Down