-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new middleware for monitoring gin http routes (#171)
* modify metrics to send dynamic label values * added path, and http method label
- Loading branch information
Showing
3 changed files
with
146 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package middleware | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/prometheus/client_golang/prometheus" | ||
|
||
"github.com/trustwallet/go-libs/metrics" | ||
) | ||
|
||
const labelPath = "path" | ||
const labelMethod = "method" | ||
|
||
func MetricsMiddleware(namespace string, labels prometheus.Labels, reg prometheus.Registerer) gin.HandlerFunc { | ||
perfMetric := metrics.NewPerformanceMetric(namespace, []string{labelPath, labelMethod}, labels, reg) | ||
return func(c *gin.Context) { | ||
path := c.FullPath() | ||
method := c.Request.Method | ||
|
||
// route not found, call next and immediately return | ||
if path == "" { | ||
c.Next() | ||
return | ||
} | ||
|
||
labelValues := []string{path, method} | ||
|
||
startTime := perfMetric.Start(labelValues...) | ||
c.Next() | ||
perfMetric.Duration(startTime, labelValues...) | ||
|
||
statusCode := c.Writer.Status() | ||
if successfulHttpStatusCode(statusCode) { | ||
perfMetric.Success(labelValues...) | ||
} else { | ||
perfMetric.Failure(labelValues...) | ||
} | ||
} | ||
} | ||
|
||
func successfulHttpStatusCode(statusCode int) bool { | ||
return 200 <= statusCode && statusCode < 300 | ||
} |
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,75 @@ | ||
package middleware | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestMetricsMiddleware(t *testing.T) { | ||
r := prometheus.NewRegistry() | ||
router := gin.New() | ||
router.Use(MetricsMiddleware("", nil, r)) | ||
|
||
successGroup := router.Group("/success") | ||
successGroup.GET("/:test", func(c *gin.Context) { | ||
c.JSON(http.StatusOK, struct{}{}) | ||
}) | ||
|
||
successGroup.GET("", func(c *gin.Context) { | ||
c.JSON(http.StatusOK, struct{}{}) | ||
}) | ||
|
||
router.GET("/error", func(c *gin.Context) { | ||
_ = c.AbortWithError(http.StatusInternalServerError, errors.New("oops error")) | ||
}) | ||
|
||
// 2 successes, 1 errors | ||
_ = performRequest("GET", "/success?haha=1&hoho=2", router) | ||
_ = performRequest("GET", "/error?hehe=1&huhu=3", router) | ||
_ = performRequest("GET", "/success/hihi", router) | ||
|
||
metricFamilies, err := r.Gather() | ||
require.NoError(t, err) | ||
|
||
const executionFailedTotal = "execution_failed_total" | ||
const executionSucceededTotal = "execution_succeeded_total" | ||
|
||
// metricFamily.Name --> label --> counter value | ||
expected := map[string]map[string]int{ | ||
executionSucceededTotal: { | ||
"/success": 1, | ||
"/success/:test": 1, | ||
"/error": 0, | ||
}, | ||
executionFailedTotal: { | ||
"/success": 0, | ||
"/success/:test": 0, | ||
"/error": 1, | ||
}, | ||
} | ||
|
||
for _, metricFamily := range metricFamilies { | ||
expectedLabelCounterMap, ok := expected[*metricFamily.Name] | ||
if !ok { | ||
continue | ||
} | ||
|
||
require.Len(t, metricFamily.Metric, len(expectedLabelCounterMap)) | ||
for _, metric := range metricFamily.Metric { | ||
require.Len(t, metric.Label, 2) | ||
var chosenLabelIdx = -1 | ||
for idx, label := range metric.Label { | ||
if *label.Name == labelPath { | ||
chosenLabelIdx = idx | ||
} | ||
} | ||
require.NotEqual(t, -1, chosenLabelIdx) | ||
require.Equal(t, float64(expectedLabelCounterMap[*metric.Label[chosenLabelIdx].Value]), *metric.Counter.Value) | ||
} | ||
} | ||
} |