Skip to content

Commit ca5af6d

Browse files
committed
[pkg/translator/loki] Moved labels names normalization to LogToLokiEntry function
1 parent 2160716 commit ca5af6d

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: lokitranslator
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Public method `LogToLokiEntry` from `pkg/loki/translator` now returns normalized (`.` replaced by `_`) label names
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [26093]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: []

pkg/translator/loki/logs_to_loki.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,8 @@ func LogsToLokiRequests(ld plog.Logs, defaultLabelsEnabled map[string]bool) map[
8080

8181
group.report.NumSubmitted++
8282

83-
processed := model.LabelSet{}
84-
for label := range entry.Labels {
85-
// Loki doesn't support dots in label names
86-
// labelName is normalized label name to follow Prometheus label names standard
87-
labelName := prometheustranslator.NormalizeLabel(string(label))
88-
processed[model.LabelName(labelName)] = entry.Labels[label]
89-
}
90-
9183
// create the stream name based on the labels
92-
labels := processed.String()
84+
labels := entry.Labels.String()
9385
if stream, ok := group.streams[labels]; ok {
9486
stream.Entries = append(stream.Entries, *entry.Entry)
9587
continue
@@ -128,7 +120,7 @@ type PushEntry struct {
128120
Labels model.LabelSet
129121
}
130122

131-
// LogToLokiEntry converts LogRecord into Loki log entry enriched with labels and tenant
123+
// LogToLokiEntry converts LogRecord into Loki log entry enriched with normalized labels
132124
func LogToLokiEntry(lr plog.LogRecord, rl pcommon.Resource, scope pcommon.InstrumentationScope, defaultLabelsEnabled map[string]bool) (*PushEntry, error) {
133125
// we may remove attributes, so change only our version
134126
log := plog.NewLogRecord()
@@ -155,9 +147,17 @@ func LogToLokiEntry(lr plog.LogRecord, rl pcommon.Resource, scope pcommon.Instru
155147
return nil, err
156148
}
157149

150+
labels := model.LabelSet{}
151+
for label := range mergedLabels {
152+
// Loki doesn't support dots in label names
153+
// labelName is normalized label name to follow Prometheus label names standard
154+
labelName := prometheustranslator.NormalizeLabel(string(label))
155+
labels[model.LabelName(labelName)] = mergedLabels[label]
156+
}
157+
158158
return &PushEntry{
159159
Entry: entry,
160-
Labels: mergedLabels,
160+
Labels: labels,
161161
}, nil
162162
}
163163

pkg/translator/loki/logs_to_loki_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ func TestLogToLokiEntry(t *testing.T) {
521521
},
522522
Labels: model.LabelSet{
523523
"exporter": "OTLP",
524-
"host.name": "guarana",
524+
"host_name": "guarana",
525525
},
526526
},
527527
err: nil,
@@ -543,7 +543,7 @@ func TestLogToLokiEntry(t *testing.T) {
543543
},
544544
Labels: model.LabelSet{
545545
"exporter": "OTLP",
546-
"host.name": "guarana",
546+
"host_name": "guarana",
547547
},
548548
},
549549
},
@@ -565,7 +565,7 @@ func TestLogToLokiEntry(t *testing.T) {
565565
},
566566
Labels: model.LabelSet{
567567
"exporter": "OTLP",
568-
"host.name": "guarana",
568+
"host_name": "guarana",
569569
},
570570
},
571571
},

0 commit comments

Comments
 (0)