Skip to content

Commit

Permalink
Fix Prometheus metrics (#773)
Browse files Browse the repository at this point in the history
* chore: replace dots in ipv4 addresses with slashes

Signed-off-by: Rolf Ahrenberg <rolf.ahrenberg@saunalahti.fi>

* chore: tweak default mapper configuration for Prometheus

Signed-off-by: Rolf Ahrenberg <rolf.ahrenberg@saunalahti.fi>

---------

Signed-off-by: Rolf Ahrenberg <rolf.ahrenberg@saunalahti.fi>
  • Loading branch information
rofafor authored Jan 15, 2025
1 parent fccc165 commit 60d8e81
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
36 changes: 22 additions & 14 deletions src/stats/prom/default_mapper.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,53 @@ mappings:
labels:
domain: "$1"
key1: "$2"
- match: "ratelimit.service.rate_limit.*.*.shadow_mode"
name: "ratelimit_service_rate_limit_shadow_mode"
timer_type: "histogram"
labels:
domain: "$1"
key1: "$2"

- match: "ratelimit.service.rate_limit.*.*.*.near_limit"
- match: "ratelimit\\.service\\.rate_limit\\.([^\\.]*)\\.([^\\.]*)\\.([^\\.]*)(\\..*)?\\.near_limit"
match_type: regex
name: "ratelimit_service_rate_limit_near_limit"
timer_type: "histogram"
labels:
domain: "$1"
key1: "$2"
key2: "$3"
- match: "ratelimit.service.rate_limit.*.*.*.over_limit"
- match: "ratelimit\\.service\\.rate_limit\\.([^\\.]*)\\.([^\\.]*)\\.([^\\.]*)(\\..*)?\\.over_limit"
match_type: regex
name: "ratelimit_service_rate_limit_over_limit"
timer_type: "histogram"
labels:
domain: "$1"
key1: "$2"
key2: "$3"
- match: "ratelimit.service.rate_limit.*.*.*.total_hits"
- match: "ratelimit\\.service\\.rate_limit\\.([^\\.]*)\\.([^\\.]*)\\.([^\\.]*)(\\..*)?\\.total_hits"
match_type: regex
name: "ratelimit_service_rate_limit_total_hits"
timer_type: "histogram"
labels:
domain: "$1"
key1: "$2"
key2: "$3"
- match: "ratelimit.service.rate_limit.*.*.*.within_limit"
- match: "ratelimit\\.service\\.rate_limit\\.([^\\.]*)\\.([^\\.]*)\\.([^\\.]*)(\\..*)?\\.within_limit"
match_type: regex
name: "ratelimit_service_rate_limit_within_limit"
timer_type: "histogram"
labels:
domain: "$1"
key1: "$2"
key2: "$3"
- match: "ratelimit\\.service\\.rate_limit\\.([^\\.]*)\\.([^\\.]*)\\.([^\\.]*)(\\..*)?\\.shadow_mode"
match_type: regex
name: "ratelimit_service_rate_limit_shadow_mode"
timer_type: "histogram"
labels:
domain: "$1"
key1: "$2"
key2: "$3"

- match: "ratelimit.service.call.should_rate_limit.*"
name: "ratelimit_service_should_rate_limit_error"
Expand All @@ -65,7 +83,6 @@ mappings:
match_metric_type: counter
labels:
grpc_method: "$1"

- match: "ratelimit_server.*.response_time"
name: "ratelimit_service_response_time_seconds"
timer_type: histogram
Expand All @@ -75,15 +92,6 @@ mappings:
- match: "ratelimit.service.config_load_success"
name: "ratelimit_service_config_load_success"
match_metric_type: counter

- match: "ratelimit.service.config_load_error"
name: "ratelimit_service_config_load_error"
match_metric_type: counter

- match: "ratelimit.service.rate_limit.*.*.*.shadow_mode"
name: "ratelimit_service_rate_limit_shadow_mode"
timer_type: "histogram"
labels:
domain: "$1"
key1: "$2"
key2: "$3"
10 changes: 9 additions & 1 deletion src/stats/prom/prometheus_sink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ func toMap(labels []*dto.LabelPair) map[string]string {
func TestFlushCounterWithDifferentLabels(t *testing.T) {
s.FlushCounter("ratelimit.service.rate_limit.domain1.key1_val1.over_limit", 1)
s.FlushCounter("ratelimit.service.rate_limit.domain1.key1_val1.key2_val2.over_limit", 2)
s.FlushCounter("ratelimit.service.rate_limit.domain1.key3_val3.key4_val4.over_limit", 1)
s.FlushCounter("ratelimit.service.rate_limit.domain1.key3_val3.key4_val4.key5_val5.over_limit", 2)
assert.Eventually(t, func() bool {
metricFamilies, err := prometheus.DefaultGatherer.Gather()
require.NoError(t, err)
Expand All @@ -56,7 +58,7 @@ func TestFlushCounterWithDifferentLabels(t *testing.T) {

m, ok := metrics["ratelimit_service_rate_limit_over_limit"]
require.True(t, ok)
require.Len(t, m.Metric, 2)
require.Len(t, m.Metric, 3)
require.Equal(t, 1.0, *m.Metric[0].Counter.Value)
require.Equal(t, map[string]string{
"domain": "domain1",
Expand All @@ -68,6 +70,12 @@ func TestFlushCounterWithDifferentLabels(t *testing.T) {
"key1": "key1_val1",
"key2": "key2_val2",
}, toMap(m.Metric[1].Label))
require.Equal(t, 3.0, *m.Metric[2].Counter.Value)
require.Equal(t, map[string]string{
"domain": "domain1",
"key1": "key3_val3",
"key2": "key4_val4",
}, toMap(m.Metric[2].Label))
return true
}, time.Second, time.Millisecond)
}
Expand Down
7 changes: 6 additions & 1 deletion src/utils/utilities.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

import (
"regexp"
"strings"

pb "github.com/envoyproxy/go-control-plane/envoy/service/ratelimit/v3"
Expand Down Expand Up @@ -61,10 +62,14 @@ func MaskCredentialsInUrl(url string) string {
return strings.Join(urls, ",")
}

var ipv4Regex = regexp.MustCompile(`\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}`)

// Remove invalid characters from the stat name.
func SanitizeStatName(s string) string {
r := strings.NewReplacer(":", "_", "|", "_")
return r.Replace(s)
return ipv4Regex.ReplaceAllStringFunc(r.Replace(s), func(ip string) string {
return strings.ReplaceAll(ip, ".", "_")
})
}

func GetHitsAddends(request *pb.RateLimitRequest) []uint64 {
Expand Down

0 comments on commit 60d8e81

Please sign in to comment.