Skip to content

Commit

Permalink
[exporter/loki] Add flags field to lokiEntry (open-telemetry#21733)
Browse files Browse the repository at this point in the history
**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
  • Loading branch information
mar4uk committed Jun 14, 2023
1 parent 50bbb9f commit e7608db
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
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 @@ -23,6 +23,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 @@ -53,6 +54,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 @@ -92,6 +94,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 @@ -141,6 +141,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 @@ -195,3 +205,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)
}

0 comments on commit e7608db

Please sign in to comment.