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 variadic labels #244

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion metrics/http_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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{
Expand Down
12 changes: 11 additions & 1 deletion metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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{
Expand Down
4 changes: 2 additions & 2 deletions middleware/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion middleware/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
18 changes: 11 additions & 7 deletions testy/tagged.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down