Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[exporter/loki] Add flags field to lokiEntry #21733

Merged
merged 1 commit into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .chloggen/lokiexporter-add-flags.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: lokiexporter

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Added `flags` field from plog.LogRecord into Loki entry

# One or more tracking issues related to the change
issues: [21650]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
7 changes: 7 additions & 0 deletions pkg/translator/loki/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type lokiEntry struct {
TraceID string `json:"traceid,omitempty"`
SpanID string `json:"spanid,omitempty"`
Severity string `json:"severity,omitempty"`
Flags uint32 `json:"flags,omitempty"`
Attributes map[string]interface{} `json:"attributes,omitempty"`
Resources map[string]interface{} `json:"resources,omitempty"`
InstrumentationScope *instrumentationScope `json:"instrumentation_scope,omitempty"`
Expand Down Expand Up @@ -64,6 +65,7 @@ func Encode(lr plog.LogRecord, res pcommon.Resource, scope pcommon.Instrumentati
Severity: lr.SeverityText(),
Attributes: lr.Attributes().AsRaw(),
Resources: res.Attributes().AsRaw(),
Flags: uint32(lr.Flags()),
}

scopeName := scope.Name()
Expand Down Expand Up @@ -103,6 +105,11 @@ func EncodeLogfmt(lr plog.LogRecord, res pcommon.Resource, scope pcommon.Instrum
keyvals = keyvalsReplaceOrAppend(keyvals, "severity", severity)
}

flags := lr.Flags()
if flags != 0 {
keyvals = keyvalsReplaceOrAppend(keyvals, "flags", lr.Flags())
}

lr.Attributes().Range(func(k string, v pcommon.Value) bool {
keyvals = append(keyvals, valueToKeyvals(fmt.Sprintf("attribute_%s", k), v)...)
return true
Expand Down
20 changes: 20 additions & 0 deletions pkg/translator/loki/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,16 @@ func TestSerializeComplexBody(t *testing.T) {
}
}

func TestEncodeWithFlags(t *testing.T) {
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"}}`
log, resource, scope := exampleLog()
log.SetFlags(plog.DefaultLogRecordFlags.WithIsSampled(true))

out, err := Encode(log, resource, scope)
assert.NoError(t, err)
assert.Equal(t, in, out)
}

func TestEncodeLogfmtWithStringBody(t *testing.T) {
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`
log, resource, scope := exampleLog()
Expand Down Expand Up @@ -206,3 +216,13 @@ func TestEncodeLogfmtWithComplexAttributes(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, in, out)
}

func TestEncodeLogfmtWithFlags(t *testing.T) {
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`
log, resource, scope := exampleLog()
log.Body().SetStr("msg=\"hello world\"")
log.SetFlags(plog.DefaultLogRecordFlags.WithIsSampled(true))
out, err := EncodeLogfmt(log, resource, scope)
assert.NoError(t, err)
assert.Equal(t, in, out)
}