Skip to content

Commit 56f2cf7

Browse files
committed
Add a metric for when a tenant has structured metadata sanitized at
ingestion time. Signed-off-by: Callum Styan <callumstyan@gmail.com>
1 parent 339ba1a commit 56f2cf7

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

‎pkg/distributor/distributor.go‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,11 @@ type Distributor struct {
167167
RequestParserWrapper push.RequestParserWrapper
168168

169169
// metrics
170-
ingesterAppends *prometheus.CounterVec
171-
ingesterAppendTimeouts *prometheus.CounterVec
172-
replicationFactor prometheus.Gauge
173-
streamShardCount prometheus.Counter
170+
ingesterAppends *prometheus.CounterVec
171+
ingesterAppendTimeouts *prometheus.CounterVec
172+
replicationFactor prometheus.Gauge
173+
streamShardCount prometheus.Counter
174+
tenantPushSanitizedStructuredMetadata *prometheus.CounterVec
174175

175176
usageTracker push.UsageTracker
176177
ingesterTasks chan pushIngesterTask
@@ -284,6 +285,11 @@ func New(
284285
Name: "stream_sharding_count",
285286
Help: "Total number of times the distributor has sharded streams",
286287
}),
288+
tenantPushSanitizedStructuredMetadata: promauto.With(registerer).NewCounterVec(prometheus.CounterOpts{
289+
Namespace: constants.Loki,
290+
Name: "distributor_push_structured_metadata_sanitized_total",
291+
Help: "The total number of times we've had to sanitize structured metadata (names or values) at ingestion time per tenant.",
292+
}, []string{"tenant"}),
287293
kafkaAppends: promauto.With(registerer).NewCounterVec(prometheus.CounterOpts{
288294
Namespace: constants.Loki,
289295
Name: "distributor_kafka_appends_total",
@@ -527,9 +533,14 @@ func (d *Distributor) Push(ctx context.Context, req *logproto.PushRequest) (*log
527533
continue
528534
}
529535

536+
var normalized string
530537
structuredMetadata := logproto.FromLabelAdaptersToLabels(entry.StructuredMetadata)
531538
for i := range entry.StructuredMetadata {
532-
structuredMetadata[i].Name = otlptranslate.NormalizeLabel(structuredMetadata[i].Name)
539+
normalized = otlptranslate.NormalizeLabel(structuredMetadata[i].Name)
540+
if normalized != structuredMetadata[i].Name {
541+
structuredMetadata[i].Name = normalized
542+
d.tenantPushSanitizedStructuredMetadata.WithLabelValues(tenantID).Inc()
543+
}
533544
if strings.ContainsRune(structuredMetadata[i].Value, utf8.RuneError) {
534545
structuredMetadata[i].Value = strings.Map(removeInvalidUtf, structuredMetadata[i].Value)
535546
}

‎pkg/distributor/distributor_test.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package distributor
33
import (
44
"context"
55
"fmt"
6+
"github.com/prometheus/client_golang/prometheus/testutil"
67
"math"
78
"math/rand"
89
"net/http"
@@ -1961,22 +1962,27 @@ func TestDistributor_StructuredMetadataSanitization(t *testing.T) {
19611962
for _, tc := range []struct {
19621963
req *logproto.PushRequest
19631964
expectedResponse *logproto.PushResponse
1965+
numSanitizations float64
19641966
}{
19651967
{
19661968
makeWriteRequestWithLabels(10, 10, []string{`{foo="bar"}`}, true, false, false),
19671969
success,
1970+
0,
19681971
},
19691972
{
19701973
makeWriteRequestWithLabels(10, 10, []string{`{foo="bar"}`}, true, true, false),
19711974
success,
1975+
10,
19721976
},
19731977
{
19741978
makeWriteRequestWithLabels(10, 10, []string{`{foo="bar"}`}, true, false, true),
19751979
success,
1980+
10,
19761981
},
19771982
{
19781983
makeWriteRequestWithLabels(10, 10, []string{`{foo="bar"}`}, true, true, true),
19791984
success,
1985+
20,
19801986
},
19811987
} {
19821988
distributors, _ := prepare(t, 1, 5, limits, nil)
@@ -1988,5 +1994,7 @@ func TestDistributor_StructuredMetadataSanitization(t *testing.T) {
19881994
response, err := distributors[0].Push(ctx, &request)
19891995
require.NoError(t, err)
19901996
assert.Equal(t, tc.expectedResponse, response)
1997+
//fmt.Println("counter: ", distributors[0].tenantPushSanitizedStructuredMetadata.WithLabelValues())
1998+
assert.Equal(t, tc.numSanitizations, testutil.ToFloat64(distributors[0].tenantPushSanitizedStructuredMetadata.WithLabelValues("test")))
19911999
}
19922000
}

0 commit comments

Comments
 (0)