Skip to content

Commit ca31f05

Browse files
committed
Loki Write: Internal Labels
This change is to add logic so that labels that start with `__` are filtered out, unless they are `externalLabels`. Labels that start with `__` are used internally for things such as metadata. If a user want to preserve the contents of one of these internal labels, then can do so using relabelling to remove the `__`. This change also adds tests which check various different scenarios
1 parent 58f068b commit ca31f05

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ Main (unreleased)
6868

6969
- Only log EOF errors for syslog port investigations in `loki.source.syslog` as Debug, not Warn. (@dehaansa)
7070

71+
- Fix `loki.write` no longer includes internal labels `__`. (@matt-gp)
72+
7173
v1.11.3
7274
-----------------
7375

internal/component/common/loki/client/batch.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (b *batch) add(entry loki.Entry) error {
6666
b.totalBytes += entrySize(entry.Entry)
6767

6868
// Append the entry to an already existing stream (if any)
69-
labels := labelsMapToString(entry.Labels, ReservedLabelTenantID)
69+
labels := labelsMapToString(entry.Labels)
7070
if stream, ok := b.streams[labels]; ok {
7171
stream.Entries = append(stream.Entries, entry.Entry)
7272
return nil
@@ -90,7 +90,7 @@ func (b *batch) addFromWAL(lbs model.LabelSet, entry push.Entry, segmentNum int)
9090
b.totalBytes += len(entry.Line)
9191

9292
// Append the entry to an already existing stream (if any)
93-
labels := labelsMapToString(lbs, ReservedLabelTenantID)
93+
labels := labelsMapToString(lbs)
9494
if stream, ok := b.streams[labels]; ok {
9595
stream.Entries = append(stream.Entries, entry)
9696
b.countForSegment(segmentNum)
@@ -112,14 +112,16 @@ func (b *batch) addFromWAL(lbs model.LabelSet, entry push.Entry, segmentNum int)
112112
return nil
113113
}
114114

115-
// labelsMapToString encodes an entry's label set as a string, ignoring the without label.
116-
func labelsMapToString(ls model.LabelSet, without model.LabelName) string {
115+
// labelsMapToString encodes an entry's label set as a string, ignoring internal labels
116+
func labelsMapToString(ls model.LabelSet) string {
117117
var b strings.Builder
118118
totalSize := 2
119119
lstrs := make([]model.LabelName, 0, len(ls))
120120

121121
for l, v := range ls {
122-
if l == without {
122+
123+
// skip internal labels
124+
if strings.HasPrefix(string(l), "__") {
123125
continue
124126
}
125127

internal/component/common/loki/client/batch_test.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,48 @@ func BenchmarkLabelsMapToString(b *testing.B) {
180180
var r string
181181
for i := 0; i < b.N; i++ {
182182
// store in r prevent the compiler eliminating the function call.
183-
r = labelsMapToString(labelSet, ReservedLabelTenantID)
183+
r = labelsMapToString(labelSet)
184184
}
185185
result = r
186186
}
187+
188+
func TestLabelsMapToString(t *testing.T) {
189+
tests := []struct {
190+
name string
191+
input model.LabelSet
192+
expected string
193+
}{
194+
{
195+
name: "empty label set",
196+
input: model.LabelSet{},
197+
expected: "{}",
198+
},
199+
{
200+
name: "single label",
201+
input: model.LabelSet{"app": "my-app"},
202+
expected: `{app="my-app"}`,
203+
},
204+
{
205+
name: "multiple labels",
206+
input: model.LabelSet{"app": "my-app", "env": "prod"},
207+
expected: `{app="my-app", env="prod"}`,
208+
},
209+
{
210+
name: "labels with reserved labels",
211+
input: model.LabelSet{"app": "my-app", "__meta_label_abc": "meta-abc", "__meta_label_def": "meta-def", "abc": "123"},
212+
expected: `{abc="123", app="my-app"}`,
213+
},
214+
{
215+
name: "only reserved labels",
216+
input: model.LabelSet{"__meta_label_abc": "meta-abc", "__meta_label_def": "meta-def"},
217+
expected: "{}",
218+
},
219+
}
220+
221+
for _, tc := range tests {
222+
t.Run(tc.name, func(t *testing.T) {
223+
actual := labelsMapToString(tc.input)
224+
assert.Equal(t, tc.expected, actual)
225+
})
226+
}
227+
}

0 commit comments

Comments
 (0)