Skip to content

Commit

Permalink
feat: Account for policy when reporting seen resource and metadata la…
Browse files Browse the repository at this point in the history
…bels (#16189)
  • Loading branch information
DylanGuedes authored Feb 11, 2025
1 parent 9fa4a20 commit 9a356a1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 26 deletions.
8 changes: 6 additions & 2 deletions pkg/loghttp/push/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,14 @@ func otlpToLokiPushRequest(ctx context.Context, ld plog.Logs, userID string, ten
stats.StructuredMetadataBytes[policy] = make(map[time.Duration]int64)
}

if _, ok := stats.ResourceAndSourceMetadataLabels[policy]; !ok {
stats.ResourceAndSourceMetadataLabels[policy] = make(map[time.Duration]push.LabelsAdapter)
}

stats.StructuredMetadataBytes[policy][retentionPeriodForUser] += int64(resourceAttributesAsStructuredMetadataSize)
totalBytesReceived += int64(resourceAttributesAsStructuredMetadataSize)

stats.ResourceAndSourceMetadataLabels[retentionPeriodForUser] = append(stats.ResourceAndSourceMetadataLabels[retentionPeriodForUser], resourceAttributesAsStructuredMetadata...)
stats.ResourceAndSourceMetadataLabels[policy][retentionPeriodForUser] = append(stats.ResourceAndSourceMetadataLabels[policy][retentionPeriodForUser], resourceAttributesAsStructuredMetadata...)

for j := 0; j < sls.Len(); j++ {
scope := sls.At(j).Scope()
Expand Down Expand Up @@ -250,7 +254,7 @@ func otlpToLokiPushRequest(ctx context.Context, ld plog.Logs, userID string, ten
stats.StructuredMetadataBytes[policy][retentionPeriodForUser] += int64(scopeAttributesAsStructuredMetadataSize)
totalBytesReceived += int64(scopeAttributesAsStructuredMetadataSize)

stats.ResourceAndSourceMetadataLabels[retentionPeriodForUser] = append(stats.ResourceAndSourceMetadataLabels[retentionPeriodForUser], scopeAttributesAsStructuredMetadata...)
stats.ResourceAndSourceMetadataLabels[policy][retentionPeriodForUser] = append(stats.ResourceAndSourceMetadataLabels[policy][retentionPeriodForUser], scopeAttributesAsStructuredMetadata...)
for k := 0; k < logs.Len(); k++ {
log := logs.At(k)

Expand Down
56 changes: 34 additions & 22 deletions pkg/loghttp/push/otlp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,10 @@ func TestOTLPToLokiPushRequest(t *testing.T) {
time.Hour: 0,
},
},
ResourceAndSourceMetadataLabels: map[time.Duration]push.LabelsAdapter{
time.Hour: nil,
ResourceAndSourceMetadataLabels: map[string]map[time.Duration]push.LabelsAdapter{
"service-1-policy": {
time.Hour: nil,
},
},
StreamLabelsSize: 21,
MostRecentEntryTimestamp: now,
Expand Down Expand Up @@ -150,8 +152,10 @@ func TestOTLPToLokiPushRequest(t *testing.T) {
time.Hour: 0,
},
},
ResourceAndSourceMetadataLabels: map[time.Duration]push.LabelsAdapter{
time.Hour: nil,
ResourceAndSourceMetadataLabels: map[string]map[time.Duration]push.LabelsAdapter{
"others": {
time.Hour: nil,
},
},
StreamLabelsSize: 27,
MostRecentEntryTimestamp: now,
Expand Down Expand Up @@ -195,8 +199,10 @@ func TestOTLPToLokiPushRequest(t *testing.T) {
time.Hour: 0,
},
},
ResourceAndSourceMetadataLabels: map[time.Duration]push.LabelsAdapter{
time.Hour: nil,
ResourceAndSourceMetadataLabels: map[string]map[time.Duration]push.LabelsAdapter{
"others": {
time.Hour: nil,
},
},
StreamLabelsSize: 47,
MostRecentEntryTimestamp: now,
Expand Down Expand Up @@ -279,11 +285,13 @@ func TestOTLPToLokiPushRequest(t *testing.T) {
time.Hour: 37,
},
},
ResourceAndSourceMetadataLabels: map[time.Duration]push.LabelsAdapter{
time.Hour: []push.LabelAdapter{
{Name: "service_image", Value: "loki"},
{Name: "op", Value: "buzz"},
{Name: "scope_name", Value: "fizz"},
ResourceAndSourceMetadataLabels: map[string]map[time.Duration]push.LabelsAdapter{
"service-1-policy": {
time.Hour: []push.LabelAdapter{
{Name: "service_image", Value: "loki"},
{Name: "op", Value: "buzz"},
{Name: "scope_name", Value: "fizz"},
},
},
},
StreamLabelsSize: 21,
Expand Down Expand Up @@ -376,11 +384,13 @@ func TestOTLPToLokiPushRequest(t *testing.T) {
time.Hour: 97,
},
},
ResourceAndSourceMetadataLabels: map[time.Duration]push.LabelsAdapter{
time.Hour: []push.LabelAdapter{
{Name: "resource_nested_foo", Value: "bar"},
{Name: "scope_nested_foo", Value: "bar"},
{Name: "scope_name", Value: "fizz"},
ResourceAndSourceMetadataLabels: map[string]map[time.Duration]push.LabelsAdapter{
"service-1-policy": {
time.Hour: []push.LabelAdapter{
{Name: "resource_nested_foo", Value: "bar"},
{Name: "scope_nested_foo", Value: "bar"},
{Name: "scope_name", Value: "fizz"},
},
},
},
StreamLabelsSize: 21,
Expand Down Expand Up @@ -533,12 +543,14 @@ func TestOTLPToLokiPushRequest(t *testing.T) {
time.Hour: 113,
},
},
ResourceAndSourceMetadataLabels: map[time.Duration]push.LabelsAdapter{
time.Hour: []push.LabelAdapter{
{Name: "pod_ip", Value: "10.200.200.200"},
{Name: "resource_nested_foo", Value: "bar"},
{Name: "scope_nested_foo", Value: "bar"},
{Name: "scope_name", Value: "fizz"},
ResourceAndSourceMetadataLabels: map[string]map[time.Duration]push.LabelsAdapter{
"service-1-policy": {
time.Hour: []push.LabelAdapter{
{Name: "pod_ip", Value: "10.200.200.200"},
{Name: "resource_nested_foo", Value: "bar"},
{Name: "scope_nested_foo", Value: "bar"},
{Name: "scope_name", Value: "fizz"},
},
},
},
StreamLabelsSize: 42,
Expand Down
4 changes: 2 additions & 2 deletions pkg/loghttp/push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func NewPushStats() *Stats {
LogLinesBytes: map[string]map[time.Duration]int64{},
StructuredMetadataBytes: map[string]map[time.Duration]int64{},
PolicyNumLines: map[string]int64{},
ResourceAndSourceMetadataLabels: map[time.Duration]push.LabelsAdapter{},
ResourceAndSourceMetadataLabels: map[string]map[time.Duration]push.LabelsAdapter{},
}
}

Expand All @@ -116,7 +116,7 @@ type Stats struct {
PolicyNumLines map[string]int64
LogLinesBytes PolicyWithRetentionWithBytes
StructuredMetadataBytes PolicyWithRetentionWithBytes
ResourceAndSourceMetadataLabels map[time.Duration]push.LabelsAdapter
ResourceAndSourceMetadataLabels map[string]map[time.Duration]push.LabelsAdapter
StreamLabelsSize int64
MostRecentEntryTimestamp time.Time
ContentType string
Expand Down

0 comments on commit 9a356a1

Please sign in to comment.