Skip to content

Commit e7608db

Browse files
authored
[exporter/loki] Add flags field to lokiEntry (open-telemetry#21733)
**Description:** There is currently no way to get the [Flags](https://github.com/open-telemetry/opentelemetry-collector/blob/main/pdata/internal/data/protogen/logs/v1/logs.pb.go#L396) field from plog.LogRecord into Loki. Added Flags field to lokiEntry in this PR **Link to tracking Issue:** open-telemetry#21650 **Testing:** Added unit tests
1 parent 50bbb9f commit e7608db

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

.chloggen/lokiexporter-add-flags.yaml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
2+
change_type: enhancement
3+
4+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
5+
component: lokiexporter
6+
7+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
8+
note: Added `flags` field from plog.LogRecord into Loki entry
9+
10+
# One or more tracking issues related to the change
11+
issues: [21650]
12+
13+
# (Optional) One or more lines of additional information to render under the primary note.
14+
# These lines will be padded with 2 spaces and then inserted directly into the document.
15+
# Use pipe (|) for multiline entries.
16+
subtext:

pkg/translator/loki/encode.go

+7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type lokiEntry struct {
2323
TraceID string `json:"traceid,omitempty"`
2424
SpanID string `json:"spanid,omitempty"`
2525
Severity string `json:"severity,omitempty"`
26+
Flags uint32 `json:"flags,omitempty"`
2627
Attributes map[string]interface{} `json:"attributes,omitempty"`
2728
Resources map[string]interface{} `json:"resources,omitempty"`
2829
InstrumentationScope *instrumentationScope `json:"instrumentation_scope,omitempty"`
@@ -53,6 +54,7 @@ func Encode(lr plog.LogRecord, res pcommon.Resource, scope pcommon.Instrumentati
5354
Severity: lr.SeverityText(),
5455
Attributes: lr.Attributes().AsRaw(),
5556
Resources: res.Attributes().AsRaw(),
57+
Flags: uint32(lr.Flags()),
5658
}
5759

5860
scopeName := scope.Name()
@@ -92,6 +94,11 @@ func EncodeLogfmt(lr plog.LogRecord, res pcommon.Resource, scope pcommon.Instrum
9294
keyvals = keyvalsReplaceOrAppend(keyvals, "severity", severity)
9395
}
9496

97+
flags := lr.Flags()
98+
if flags != 0 {
99+
keyvals = keyvalsReplaceOrAppend(keyvals, "flags", lr.Flags())
100+
}
101+
95102
lr.Attributes().Range(func(k string, v pcommon.Value) bool {
96103
keyvals = append(keyvals, valueToKeyvals(fmt.Sprintf("attribute_%s", k), v)...)
97104
return true

pkg/translator/loki/encode_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,16 @@ func TestSerializeComplexBody(t *testing.T) {
141141
}
142142
}
143143

144+
func TestEncodeWithFlags(t *testing.T) {
145+
in := `{"body":"Example log","traceid":"01020304000000000000000000000000","spanid":"0506070800000000","severity":"error","flags":1,"attributes":{"attr1":"1","attr2":"2"},"resources":{"host.name":"something"},"instrumentation_scope":{"name":"example-logger-name","version":"v1"}}`
146+
log, resource, scope := exampleLog()
147+
log.SetFlags(plog.DefaultLogRecordFlags.WithIsSampled(true))
148+
149+
out, err := Encode(log, resource, scope)
150+
assert.NoError(t, err)
151+
assert.Equal(t, in, out)
152+
}
153+
144154
func TestEncodeLogfmtWithStringBody(t *testing.T) {
145155
in := `msg="hello world" traceID=01020304000000000000000000000000 spanID=0506070800000000 severity=error attribute_attr1=1 attribute_attr2=2 resource_host.name=something instrumentation_scope_name=example-logger-name instrumentation_scope_version=v1`
146156
log, resource, scope := exampleLog()
@@ -195,3 +205,13 @@ func TestEncodeLogfmtWithComplexAttributes(t *testing.T) {
195205
assert.NoError(t, err)
196206
assert.Equal(t, in, out)
197207
}
208+
209+
func TestEncodeLogfmtWithFlags(t *testing.T) {
210+
in := `msg="hello world" traceID=01020304000000000000000000000000 spanID=0506070800000000 severity=error flags=1 attribute_attr1=1 attribute_attr2=2 resource_host.name=something instrumentation_scope_name=example-logger-name instrumentation_scope_version=v1`
211+
log, resource, scope := exampleLog()
212+
log.Body().SetStr("msg=\"hello world\"")
213+
log.SetFlags(plog.DefaultLogRecordFlags.WithIsSampled(true))
214+
out, err := EncodeLogfmt(log, resource, scope)
215+
assert.NoError(t, err)
216+
assert.Equal(t, in, out)
217+
}

0 commit comments

Comments
 (0)