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

chore: update usage tracker with received bytes on stream level instead of each log line #15812

Merged
merged 2 commits into from
Jan 22, 2025
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
20 changes: 9 additions & 11 deletions pkg/loghttp/push/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ func otlpToLokiPushRequest(ctx context.Context, ld plog.Logs, userID string, ten
labelsStr := streamLabels.String()

lbs := modelLabelsSetToLabelsList(streamLabels)
totalBytesReceived := int64(0)

if _, ok := pushRequestsByStream[labelsStr]; !ok {
pushRequestsByStream[labelsStr] = logproto.Stream{
Expand All @@ -197,9 +198,7 @@ func otlpToLokiPushRequest(ctx context.Context, ld plog.Logs, userID string, ten
retentionPeriodForUser := tenantsRetention.RetentionPeriodFor(userID, lbs)

stats.StructuredMetadataBytes[retentionPeriodForUser] += int64(resourceAttributesAsStructuredMetadataSize)
if tracker != nil {
tracker.ReceivedBytesAdd(ctx, userID, retentionPeriodForUser, lbs, float64(resourceAttributesAsStructuredMetadataSize))
}
totalBytesReceived += int64(resourceAttributesAsStructuredMetadataSize)

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

Expand Down Expand Up @@ -252,9 +251,7 @@ func otlpToLokiPushRequest(ctx context.Context, ld plog.Logs, userID string, ten

scopeAttributesAsStructuredMetadataSize := loki_util.StructuredMetadataSize(scopeAttributesAsStructuredMetadata)
stats.StructuredMetadataBytes[retentionPeriodForUser] += int64(scopeAttributesAsStructuredMetadataSize)
if tracker != nil {
tracker.ReceivedBytesAdd(ctx, userID, retentionPeriodForUser, lbs, float64(scopeAttributesAsStructuredMetadataSize))
}
totalBytesReceived += int64(scopeAttributesAsStructuredMetadataSize)

stats.ResourceAndSourceMetadataLabels[retentionPeriodForUser] = append(stats.ResourceAndSourceMetadataLabels[retentionPeriodForUser], scopeAttributesAsStructuredMetadata...)
for k := 0; k < logs.Len(); k++ {
Expand All @@ -279,17 +276,18 @@ func otlpToLokiPushRequest(ctx context.Context, ld plog.Logs, userID string, ten
metadataSize := int64(loki_util.StructuredMetadataSize(entry.StructuredMetadata) - resourceAttributesAsStructuredMetadataSize - scopeAttributesAsStructuredMetadataSize)
stats.StructuredMetadataBytes[retentionPeriodForUser] += metadataSize
stats.LogLinesBytes[retentionPeriodForUser] += int64(len(entry.Line))

if tracker != nil {
tracker.ReceivedBytesAdd(ctx, userID, retentionPeriodForUser, lbs, float64(len(entry.Line)))
tracker.ReceivedBytesAdd(ctx, userID, retentionPeriodForUser, lbs, float64(metadataSize))
}
totalBytesReceived += metadataSize
totalBytesReceived += int64(len(entry.Line))

stats.NumLines++
if entry.Timestamp.After(stats.MostRecentEntryTimestamp) {
stats.MostRecentEntryTimestamp = entry.Timestamp
}
}

if tracker != nil {
tracker.ReceivedBytesAdd(ctx, userID, retentionPeriodForUser, lbs, float64(totalBytesReceived))
}
}
}

Expand Down
13 changes: 8 additions & 5 deletions pkg/loghttp/push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,22 +281,25 @@ func ParseLokiRequest(userID string, r *http.Request, tenantsRetention TenantsRe
if tenantsRetention != nil {
retentionPeriod = tenantsRetention.RetentionPeriodFor(userID, lbs)
}
totalBytesReceived := int64(0)

for _, e := range s.Entries {
pushStats.NumLines++
entryLabelsSize := int64(util.StructuredMetadataSize(e.StructuredMetadata))
pushStats.LogLinesBytes[retentionPeriod] += int64(len(e.Line))
pushStats.StructuredMetadataBytes[retentionPeriod] += entryLabelsSize

if tracker != nil {
tracker.ReceivedBytesAdd(r.Context(), userID, retentionPeriod, lbs, float64(len(e.Line)))
tracker.ReceivedBytesAdd(r.Context(), userID, retentionPeriod, lbs, float64(entryLabelsSize))
}
totalBytesReceived += int64(len(e.Line))
totalBytesReceived += entryLabelsSize

if e.Timestamp.After(pushStats.MostRecentEntryTimestamp) {
pushStats.MostRecentEntryTimestamp = e.Timestamp
}
}

if tracker != nil {
tracker.ReceivedBytesAdd(r.Context(), userID, retentionPeriod, lbs, float64(totalBytesReceived))
}

req.Streams[i] = s
}

Expand Down
Loading