Skip to content

Commit

Permalink
otlp: fix otlp service.name check when no resource attributes are def…
Browse files Browse the repository at this point in the history
…ined (#11837)

**What this PR does / why we need it**:
When no resource attributes are defined, check for `service.name`
resource attributes fail with `nil pointer dereference`. This PR fixes
the issue by checking the bool returned by the `Get` method on resource
attributes.

**Checklist**
- [x] Tests updated

(cherry picked from commit 7a04da8)
  • Loading branch information
sandeepsukhani committed Jan 31, 2024
1 parent b1e7221 commit 5cf53de
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/loghttp/push/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func otlpToLokiPushRequest(ld plog.Logs, userID string, tenantsRetention Tenants
res := rls.At(i).Resource()
resAttrs := res.Attributes()

if v, _ := resAttrs.Get(attrServiceName); v.AsString() == "" {
if v, ok := resAttrs.Get(attrServiceName); !ok || v.AsString() == "" {
resAttrs.PutStr(attrServiceName, "unknown_service")
}
resourceAttributesAsStructuredMetadata := make(push.LabelsAdapter, 0, resAttrs.Len())
Expand Down
72 changes: 72 additions & 0 deletions pkg/loghttp/push/otlp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,78 @@ func TestOTLPToLokiPushRequest(t *testing.T) {
mostRecentEntryTimestamp: now,
},
},
{
name: "no resource attributes defined",
otlpConfig: DefaultOTLPConfig,
generateLogs: func() plog.Logs {
ld := plog.NewLogs()
ld.ResourceLogs().AppendEmpty()
ld.ResourceLogs().At(0).ScopeLogs().AppendEmpty().LogRecords().AppendEmpty().Body().SetStr("test body")
ld.ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().At(0).SetTimestamp(pcommon.Timestamp(now.UnixNano()))
return ld
},
expectedPushRequest: logproto.PushRequest{
Streams: []logproto.Stream{
{
Labels: `{service_name="unknown_service"}`,
Entries: []logproto.Entry{
{
Timestamp: now,
Line: "test body",
StructuredMetadata: push.LabelsAdapter{},
},
},
},
},
},
expectedStats: Stats{
numLines: 1,
logLinesBytes: map[time.Duration]int64{
time.Hour: 9,
},
structuredMetadataBytes: map[time.Duration]int64{
time.Hour: 0,
},
streamLabelsSize: 27,
mostRecentEntryTimestamp: now,
},
},
{
name: "service.name not defined in resource attributes",
otlpConfig: DefaultOTLPConfig,
generateLogs: func() plog.Logs {
ld := plog.NewLogs()
ld.ResourceLogs().AppendEmpty().Resource().Attributes().PutStr("service.namespace", "foo")
ld.ResourceLogs().At(0).ScopeLogs().AppendEmpty().LogRecords().AppendEmpty().Body().SetStr("test body")
ld.ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().At(0).SetTimestamp(pcommon.Timestamp(now.UnixNano()))
return ld
},
expectedPushRequest: logproto.PushRequest{
Streams: []logproto.Stream{
{
Labels: `{service_name="unknown_service", service_namespace="foo"}`,
Entries: []logproto.Entry{
{
Timestamp: now,
Line: "test body",
StructuredMetadata: push.LabelsAdapter{},
},
},
},
},
},
expectedStats: Stats{
numLines: 1,
logLinesBytes: map[time.Duration]int64{
time.Hour: 9,
},
structuredMetadataBytes: map[time.Duration]int64{
time.Hour: 0,
},
streamLabelsSize: 47,
mostRecentEntryTimestamp: now,
},
},
{
name: "resource attributes and scope attributes stored as structured metadata",
otlpConfig: DefaultOTLPConfig,
Expand Down

0 comments on commit 5cf53de

Please sign in to comment.