-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
243 additions
and
2 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
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,54 @@ | ||
// Package metrics implements Prometheus metrics for email sender service | ||
package metrics | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
const ( | ||
prometheusNamespace = "acs" | ||
prometheusSubsystem = "emailsender" | ||
clusterIDLabelName = "cluster_id" | ||
) | ||
|
||
var ( | ||
metrics *Metrics | ||
once sync.Once | ||
) | ||
|
||
// Metrics holds the prometheus.Collector instances | ||
type Metrics struct { | ||
emailsSent *prometheus.CounterVec | ||
} | ||
|
||
// Register registers the metrics with the given prometheus.Registerer. | ||
func (m *Metrics) Register(r prometheus.Registerer) { | ||
r.MustRegister(m.emailsSent) | ||
} | ||
|
||
// IncEmailsSent increments the metric counter for started probe runs. | ||
func (m *Metrics) IncEmailsSent(clusterID string) { | ||
m.emailsSent.With(prometheus.Labels{clusterIDLabelName: clusterID}).Inc() | ||
} | ||
|
||
// NewInstance returns the global Singleton instance for Metrics. | ||
func NewInstance() *Metrics { | ||
once.Do(func() { | ||
metrics = newMetrics() | ||
}) | ||
return metrics | ||
} | ||
|
||
func newMetrics() *Metrics { | ||
return &Metrics{ | ||
emailsSent: prometheus.NewCounterVec(prometheus.CounterOpts{ | ||
Namespace: prometheusNamespace, | ||
Subsystem: prometheusSubsystem, | ||
Name: "email_sent_total", | ||
Help: "The number of sent emails.", | ||
}, []string{clusterIDLabelName}, | ||
), | ||
} | ||
} |
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 metrics | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/testutil" | ||
io_prometheus_client "github.com/prometheus/client_model/go" | ||
"github.com/stackrox/acs-fleet-manager/emailsender/config" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var ( | ||
clusterID = "test-1" | ||
cfg = &config.Config{ | ||
ClusterID: clusterID, | ||
MetricsAddress: ":9999", | ||
} | ||
) | ||
|
||
func getMetricSeries(t *testing.T, registry *prometheus.Registry, name string) *io_prometheus_client.Metric { | ||
metrics := serveMetrics(t, registry) | ||
require.Contains(t, metrics, name) | ||
targetMetric := metrics[name] | ||
require.NotEmpty(t, targetMetric.Metric) | ||
return targetMetric.Metric[0] | ||
} | ||
|
||
func TestCounterIncrements(t *testing.T) { | ||
const expectedIncrement = 1.0 | ||
|
||
tt := []struct { | ||
metricName string | ||
callIncrementFunc func(m *Metrics) | ||
}{ | ||
{ | ||
metricName: "acs_emailsender_email_sent_total", | ||
callIncrementFunc: func(m *Metrics) { | ||
m.IncEmailsSent(cfg.ClusterID) | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range tt { | ||
tc := tc | ||
t.Run(tc.metricName, func(t *testing.T) { | ||
m := newMetrics() | ||
registry := initPrometheus(m) | ||
tc.callIncrementFunc(m) | ||
|
||
targetSeries := getMetricSeries(t, registry, tc.metricName) | ||
|
||
// Test that the metrics value is 1 after calling the incrementFunc. | ||
value := targetSeries.GetCounter().GetValue() | ||
assert.Equalf(t, expectedIncrement, value, "metric %s has unexpected value", tc.metricName) | ||
label := targetSeries.GetLabel()[0] | ||
assert.Containsf(t, label.GetName(), clusterIDLabelName, "metric %s has unexpected label", tc.metricName) | ||
assert.Containsf(t, label.GetValue(), clusterID, "metric %s has unexpected label", tc.metricName) | ||
}) | ||
} | ||
} | ||
|
||
func TestMetricsConformity(t *testing.T) { | ||
metrics := newMetrics() | ||
|
||
for _, metric := range []prometheus.Collector{ | ||
metrics.emailsSent, | ||
} { | ||
problems, err := testutil.CollectAndLint(metric) | ||
assert.NoError(t, err) | ||
assert.Empty(t, problems) | ||
} | ||
} |
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,49 @@ | ||
package metrics | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/collectors" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
"github.com/stackrox/acs-fleet-manager/emailsender/config" | ||
"github.com/stackrox/rox/pkg/utils" | ||
) | ||
|
||
// NewMetricsServer returns the metrics server. | ||
func NewMetricsServer(config *config.Config) *http.Server { | ||
registry := initPrometheus(NewInstance()) | ||
return newMetricsServer(config.MetricsAddress, registry) | ||
} | ||
|
||
// ListenAndServe listens for incoming requests and serves the metrics. | ||
func ListenAndServe(server *http.Server) { | ||
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { | ||
utils.Should(errors.Wrap(err, "failed to serve metrics")) | ||
} | ||
} | ||
|
||
// CloseMetricsServer closes the metrics server. | ||
func CloseMetricsServer(server *http.Server) { | ||
if err := server.Close(); err != nil { | ||
utils.Should(errors.Wrap(err, "failed to close metrics server")) | ||
} | ||
} | ||
|
||
func initPrometheus(customMetrics *Metrics) *prometheus.Registry { | ||
registry := prometheus.NewRegistry() | ||
// Register default metrics to use a dedicated registry instead of prometheus.DefaultRegistry. | ||
// This makes it easier to isolate metric state when unit testing this package. | ||
registry.MustRegister(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{})) | ||
registry.MustRegister(collectors.NewGoCollector()) | ||
customMetrics.Register(registry) | ||
return registry | ||
} | ||
|
||
func newMetricsServer(address string, registry *prometheus.Registry) *http.Server { | ||
mux := http.NewServeMux() | ||
mux.Handle("/metrics", promhttp.HandlerFor(registry, promhttp.HandlerOpts{})) | ||
|
||
return &http.Server{Addr: address, Handler: mux} | ||
} |
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,49 @@ | ||
package metrics | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
io_prometheus_client "github.com/prometheus/client_model/go" | ||
"github.com/prometheus/common/expfmt" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type metricResponse map[string]*io_prometheus_client.MetricFamily | ||
|
||
func TestMetricsServerCorrectAddress(t *testing.T) { | ||
server := NewMetricsServer(cfg) | ||
defer func(server *http.Server) { | ||
err := server.Close() | ||
require.NoError(t, err) | ||
}(server) | ||
assert.Equal(t, ":9999", server.Addr) | ||
} | ||
|
||
func TestMetricsServerServesDefaultMetrics(t *testing.T) { | ||
registry := initPrometheus(NewInstance()) | ||
metrics := serveMetrics(t, registry) | ||
assert.Contains(t, metrics, "go_memstats_alloc_bytes", "not found default metrics") | ||
} | ||
|
||
func serveMetrics(t *testing.T, registry *prometheus.Registry) metricResponse { | ||
rec := httptest.NewRecorder() | ||
req, err := http.NewRequest(http.MethodGet, "/metrics", nil) | ||
require.NoError(t, err, "failed creating metrics requests") | ||
|
||
server := newMetricsServer(":9999", registry) | ||
defer func(server *http.Server) { | ||
err := server.Close() | ||
require.NoError(t, err) | ||
}(server) | ||
server.Handler.ServeHTTP(rec, req) | ||
require.Equal(t, http.StatusOK, rec.Code, "status code should be OK") | ||
|
||
promParser := expfmt.TextParser{} | ||
metrics, err := promParser.TextToMetricFamilies(rec.Body) | ||
require.NoError(t, err, "failed parsing metrics response") | ||
return metrics | ||
} |