|
5 | 5 | "time"
|
6 | 6 |
|
7 | 7 | "github.com/prometheus/common/model"
|
| 8 | + "github.com/stretchr/testify/assert" |
8 | 9 | "github.com/stretchr/testify/require"
|
9 | 10 |
|
10 | 11 | googlev1 "github.com/grafana/pyroscope/api/gen/proto/go/google/v1"
|
@@ -50,7 +51,7 @@ func TestValidateLabels(t *testing.T) {
|
50 | 51 | {Name: "foo3", Value: "bar"},
|
51 | 52 | {Name: "foo4", Value: "bar"},
|
52 | 53 | },
|
53 |
| - expectedErr: `profile series '{foo1="bar", foo2="bar", foo3="bar", foo4="bar", service_name="svc"}' has 5 label names; limit 3`, |
| 54 | + expectedErr: `profile series '{foo1="bar", foo2="bar", foo3="bar", foo4="bar", service_name="svc"}' has 5 label names; limit 4`, |
54 | 55 | expectedReason: MaxLabelNamesPerSeries,
|
55 | 56 | },
|
56 | 57 | {
|
@@ -112,10 +113,22 @@ func TestValidateLabels(t *testing.T) {
|
112 | 113 | expectedReason: DuplicateLabelNames,
|
113 | 114 | expectedErr: "profile with labels '{__name__=\"qux\", service_name=\"svc\", service_name=\"svc\"}' has duplicate label name: 'service_name'",
|
114 | 115 | },
|
| 116 | + |
| 117 | + { |
| 118 | + name: "dupe sanitized", |
| 119 | + lbs: []*typesv1.LabelPair{ |
| 120 | + {Name: model.MetricNameLabel, Value: "qux"}, |
| 121 | + {Name: "label.name", Value: "foo"}, |
| 122 | + {Name: "label.name", Value: "bar"}, |
| 123 | + {Name: phlaremodel.LabelNameServiceName, Value: "svc"}, |
| 124 | + }, |
| 125 | + expectedReason: DuplicateLabelNames, |
| 126 | + expectedErr: "profile with labels '{__name__=\"qux\", label_name=\"foo\", label_name=\"bar\", service_name=\"svc\"}' has duplicate label name: 'label.name'", |
| 127 | + }, |
115 | 128 | } {
|
116 | 129 | t.Run(tt.name, func(t *testing.T) {
|
117 | 130 | err := ValidateLabels(MockLimits{
|
118 |
| - MaxLabelNamesPerSeriesValue: 3, |
| 131 | + MaxLabelNamesPerSeriesValue: 4, |
119 | 132 | MaxLabelNameLengthValue: 12,
|
120 | 133 | MaxLabelValueLengthValue: 10,
|
121 | 134 | }, "foo", tt.lbs)
|
@@ -400,3 +413,41 @@ func TestValidateFlamegraphMaxNodes(t *testing.T) {
|
400 | 413 | })
|
401 | 414 | }
|
402 | 415 | }
|
| 416 | + |
| 417 | +func Test_SanitizeLabelName(t *testing.T) { |
| 418 | + for _, tc := range []struct { |
| 419 | + input string |
| 420 | + expected string |
| 421 | + valid bool |
| 422 | + }{ |
| 423 | + {"", "", false}, |
| 424 | + {".", "_", true}, |
| 425 | + {".a", "_a", true}, |
| 426 | + {"a.", "a_", true}, |
| 427 | + {"..", "__", true}, |
| 428 | + {"..a", "__a", true}, |
| 429 | + {"a..", "a__", true}, |
| 430 | + {"a.a", "a_a", true}, |
| 431 | + {".a.", "_a_", true}, |
| 432 | + {"..a..", "__a__", true}, |
| 433 | + {"世界", "世界", false}, |
| 434 | + {"界世_a", "界世_a", false}, |
| 435 | + {"界世__a", "界世__a", false}, |
| 436 | + {"a_世界", "a_世界", false}, |
| 437 | + {"0.a", "0.a", false}, |
| 438 | + {"0a", "0a", false}, |
| 439 | + {"a.0", "a_0", true}, |
| 440 | + {"a0", "a0", true}, |
| 441 | + {"_", "_", true}, |
| 442 | + {"__a", "__a", true}, |
| 443 | + {"__a__", "__a__", true}, |
| 444 | + } { |
| 445 | + tc := tc |
| 446 | + t.Run("", func(t *testing.T) { |
| 447 | + origName, actual, valid := SanitizeLabelName(tc.input) |
| 448 | + assert.Equal(t, tc.input, origName) |
| 449 | + assert.Equal(t, tc.expected, actual) |
| 450 | + assert.Equal(t, tc.valid, valid) |
| 451 | + }) |
| 452 | + } |
| 453 | +} |
0 commit comments