Skip to content

Commit c987149

Browse files
mar4ukatoulme
authored andcommitted
[receiver/loki] Add structured metadata support (open-telemetry#40096)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description Added support for [Structured Metadata](https://grafana.com/docs/loki/latest/get-started/labels/structured-metadata/). If Structured Metadata is present in the loki log entry it will be converted to Log Record Attributes <!-- Issue number (e.g. open-telemetry#1234) or full URL to issue, if applicable. --> #### Link to tracking issue Fixes open-telemetry#40095 <!--Describe what testing was performed and which tests were added.--> #### Testing Added a unit test to verify the translation of Structured metadata to log record attributes Co-authored-by: Antoine Toulme <[email protected]>
1 parent afa4f52 commit c987149

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
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: lokireceiver
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Add support for structured metadata in lokireceiver
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: [40095]
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: [user]

pkg/translator/loki/loki_to_otlp.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,7 @@ func ConvertEntryToLogRecord(entry *push.Entry, lr *plog.LogRecord, labelSet mod
7878
for key, value := range labelSet {
7979
lr.Attributes().PutStr(string(key), string(value))
8080
}
81+
for _, metadata := range entry.StructuredMetadata {
82+
lr.Attributes().PutStr(metadata.Name, metadata.Value)
83+
}
8184
}

pkg/translator/loki/loki_to_otlp_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,40 @@ func TestPushRequestToLogs(t *testing.T) {
7777
},
7878
}),
7979
},
80+
{
81+
name: "Should add structured metadata to log record attributes",
82+
pushRequest: &push.PushRequest{
83+
Streams: []push.Stream{
84+
{
85+
Labels: "{foo=\"bar\", label1=\"value1\"}",
86+
Entries: []push.Entry{
87+
{
88+
Timestamp: time.Unix(0, 1676888496000000000),
89+
Line: "logline 1",
90+
StructuredMetadata: push.LabelsAdapter{
91+
{
92+
Name: "sm-key1",
93+
Value: "sm-value1",
94+
},
95+
},
96+
},
97+
},
98+
},
99+
},
100+
},
101+
keepTimestamp: true,
102+
expected: generateLogs([]Log{
103+
{
104+
Timestamp: 1676888496000000000,
105+
Body: pcommon.NewValueStr("logline 1"),
106+
Attributes: map[string]any{
107+
"foo": "bar",
108+
"label1": "value1",
109+
"sm-key1": "sm-value1",
110+
},
111+
},
112+
}),
113+
},
80114
}
81115

82116
for _, tt := range testCases {

0 commit comments

Comments
 (0)