diff --git a/metrics/http_metrics.go b/metrics/http_metrics.go index ff98b1d..1ba58b7 100644 --- a/metrics/http_metrics.go +++ b/metrics/http_metrics.go @@ -32,8 +32,8 @@ type httpServerMetric struct { func NewHttpServerMetric( namespace string, labelNames []string, - staticLabels prometheus.Labels, reg prometheus.Registerer, + labels ...Label, ) HttpServerMetric { requestStarted := prometheus.NewGaugeVec(prometheus.GaugeOpts{ Namespace: namespace, @@ -65,6 +65,11 @@ func NewHttpServerMetric( Help: "Total number of the 5xx requests.", }, labelNames) + staticLabels := make(map[string]string) + for _, label := range labels { + staticLabels[label.Key] = label.Value + } + Register(staticLabels, reg, requestStarted, requestDurationSeconds, requestSucceededTotal, requestClientErrTotal, requestServerErrTotal) return &httpServerMetric{ diff --git a/metrics/metrics.go b/metrics/metrics.go index e440aae..565c6ae 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -29,11 +29,16 @@ type performanceMetric struct { executionFailedTotal *prometheus.CounterVec } +type Label struct { + Key string + Value string +} + func NewPerformanceMetric( namespace string, labelNames []string, - staticLabels prometheus.Labels, reg prometheus.Registerer, + labels ...Label, ) PerformanceMetric { executionStarted := prometheus.NewGaugeVec(prometheus.GaugeOpts{ Namespace: namespace, @@ -59,6 +64,11 @@ func NewPerformanceMetric( Help: "Total number of the executions which failed.", }, labelNames) + staticLabels := make(map[string]string) + for _, label := range labels { + staticLabels[label.Key] = label.Value + } + Register(staticLabels, reg, executionStarted, executionDurationSeconds, executionSucceededTotal, executionFailedTotal) return &performanceMetric{ diff --git a/middleware/metrics.go b/middleware/metrics.go index 1b6ebe3..f5a2bd6 100644 --- a/middleware/metrics.go +++ b/middleware/metrics.go @@ -10,8 +10,8 @@ import ( const labelPath = "path" const labelMethod = "method" -func MetricsMiddleware(namespace string, labels prometheus.Labels, reg prometheus.Registerer) gin.HandlerFunc { - perfMetric := metrics.NewHttpServerMetric(namespace, []string{labelPath, labelMethod}, labels, reg) +func MetricsMiddleware(namespace string, reg prometheus.Registerer, labels ...metrics.Label) gin.HandlerFunc { + perfMetric := metrics.NewHttpServerMetric(namespace, []string{labelPath, labelMethod}, reg, labels...) return func(c *gin.Context) { path := c.FullPath() method := c.Request.Method diff --git a/middleware/metrics_test.go b/middleware/metrics_test.go index 3a4417c..c3c35a5 100644 --- a/middleware/metrics_test.go +++ b/middleware/metrics_test.go @@ -13,7 +13,7 @@ import ( func TestMetricsMiddleware(t *testing.T) { r := prometheus.NewRegistry() router := gin.New() - router.Use(MetricsMiddleware("", nil, r)) + router.Use(MetricsMiddleware("", r)) successGroup := router.Group("/success") successGroup.GET("/:test", func(c *gin.Context) { diff --git a/testy/tagged.go b/testy/tagged.go index 280f8e1..31b2285 100644 --- a/testy/tagged.go +++ b/testy/tagged.go @@ -15,18 +15,22 @@ const ( // TaggedTestsEnvVar defines the name of the environment variable for the tagged tests. // Example: -// TaggedTestsEnvVar="TEST_TAGS" -// env TEST_TAGS="unit" go test ./... +// +// TaggedTestsEnvVar="TEST_TAGS" +// env TEST_TAGS="unit" go test ./... var TaggedTestsEnvVar = "TEST_TAGS" // RequireTestTag runs the test if the provided tag matches at least one runtime tag. // Example: -// func TestSomething(t *testing.T) { -// RequireTestTag(t, "unit") -// ... -// } +// +// func TestSomething(t *testing.T) { +// RequireTestTag(t, "unit") +// ... +// } +// // Run with: -// env TEST_TAGS="unit,integration" go test ./... +// +// env TEST_TAGS="unit,integration" go test ./... func RequireTestTag(t *testing.T, testTag string) { if !getRuntimeTags().contains(testTag) { t.Skipf("skipping test '%s', requires '%s' tag", t.Name(), testTag)