-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[exporterhelper] Add default batching for OTLP data type
Make the exporter batching capability to be available for the regular exporter helper without using custom requests.
- Loading branch information
Showing
5 changed files
with
340 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package exporterhelper | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"go.opentelemetry.io/collector/exporter/exporterbatcher" | ||
"go.opentelemetry.io/collector/pdata/plog" | ||
) | ||
|
||
// mergeLogs merges two logs requests into one. | ||
func mergeLogs(_ context.Context, r1 Request, r2 Request) (Request, error) { | ||
lr1, ok1 := r1.(*logsRequest) | ||
lr2, ok2 := r2.(*logsRequest) | ||
if !ok1 || !ok2 { | ||
return nil, errors.New("invalid input type") | ||
} | ||
lr2.ld.ResourceLogs().MoveAndAppendTo(lr1.ld.ResourceLogs()) | ||
return lr1, nil | ||
} | ||
|
||
// mergeSplitLogs splits and/or merges the logs into multiple requests based on the MaxSizeConfig. | ||
func mergeSplitLogs(_ context.Context, cfg exporterbatcher.MaxSizeConfig, r1 Request, r2 Request) ([]Request, error) { | ||
var ( | ||
res []Request | ||
destReq *logsRequest | ||
capacityLeft = cfg.MaxSizeItems | ||
) | ||
for _, req := range []Request{r1, r2} { | ||
if req == nil { | ||
continue | ||
} | ||
srcReq, ok := req.(*logsRequest) | ||
if !ok { | ||
return nil, errors.New("invalid input type") | ||
} | ||
if srcReq.ld.LogRecordCount() <= capacityLeft { | ||
if destReq == nil { | ||
destReq = srcReq | ||
} else { | ||
srcReq.ld.ResourceLogs().MoveAndAppendTo(destReq.ld.ResourceLogs()) | ||
} | ||
capacityLeft = cfg.MaxSizeItems - destReq.ld.LogRecordCount() | ||
continue | ||
} | ||
|
||
for { | ||
extractedLogs := extractLogs(srcReq.ld, capacityLeft) | ||
if extractedLogs.LogRecordCount() == 0 { | ||
break | ||
} | ||
capacityLeft -= extractedLogs.LogRecordCount() | ||
if destReq == nil { | ||
destReq = &logsRequest{ld: extractedLogs, pusher: srcReq.pusher} | ||
} else { | ||
extractedLogs.ResourceLogs().MoveAndAppendTo(destReq.ld.ResourceLogs()) | ||
} | ||
if capacityLeft == 0 { | ||
res = append(res, destReq) | ||
destReq = nil | ||
capacityLeft = cfg.MaxSizeItems | ||
} | ||
} | ||
} | ||
|
||
if destReq != nil { | ||
res = append(res, destReq) | ||
} | ||
return res, nil | ||
} | ||
|
||
// extractLogs extracts logs from the input logs and returns a new logs with the specified number of log records. | ||
func extractLogs(srcLogs plog.Logs, count int) plog.Logs { | ||
destLogs := plog.NewLogs() | ||
srcLogs.ResourceLogs().RemoveIf(func(srcRL plog.ResourceLogs) bool { | ||
if resourceLogsCount(srcRL) <= count { | ||
count -= resourceLogsCount(srcRL) | ||
srcRL.MoveTo(destLogs.ResourceLogs().AppendEmpty()) | ||
return true | ||
} | ||
extractedRL := extractResourceLogs(srcRL, count) | ||
if resourceLogsCount(extractedRL) > 0 { | ||
extractedRL.MoveTo(destLogs.ResourceLogs().AppendEmpty()) | ||
} | ||
return resourceLogsCount(srcRL) == 0 | ||
}) | ||
return destLogs | ||
} | ||
|
||
// extractResourceLogs extracts resource logs and returns a new resource logs with the specified number of log records. | ||
func extractResourceLogs(srcRL plog.ResourceLogs, count int) plog.ResourceLogs { | ||
destRL := plog.NewResourceLogs() | ||
srcRL.Resource().CopyTo(destRL.Resource()) | ||
destRL.SetSchemaUrl(srcRL.SchemaUrl()) | ||
srcRL.ScopeLogs().RemoveIf(func(srcSL plog.ScopeLogs) bool { | ||
if srcSL.LogRecords().Len() <= count { | ||
count -= srcSL.LogRecords().Len() | ||
srcSL.MoveTo(destRL.ScopeLogs().AppendEmpty()) | ||
return true | ||
} | ||
extractedSL := extractScopeLogs(srcSL, count) | ||
if extractedSL.LogRecords().Len() > 0 { | ||
extractedSL.MoveTo(destRL.ScopeLogs().AppendEmpty()) | ||
} | ||
return srcSL.LogRecords().Len() == 0 | ||
}) | ||
return destRL | ||
} | ||
|
||
// extractScopeLogs extracts scope logs and returns a new scope logs with the specified number of log records. | ||
func extractScopeLogs(srcSL plog.ScopeLogs, count int) plog.ScopeLogs { | ||
destSL := plog.NewScopeLogs() | ||
destSL.SetSchemaUrl(srcSL.SchemaUrl()) | ||
srcSL.LogRecords().RemoveIf(func(srcLR plog.LogRecord) bool { | ||
if count > 0 { | ||
srcLR.MoveTo(destSL.LogRecords().AppendEmpty()) | ||
count-- | ||
return true | ||
} | ||
return false | ||
}) | ||
return destSL | ||
} | ||
|
||
// resourceLogsCount calculates the total number of log records in the plog.ResourceLogs. | ||
func resourceLogsCount(rl plog.ResourceLogs) int { | ||
count := 0 | ||
for k := 0; k < rl.ScopeLogs().Len(); k++ { | ||
count += rl.ScopeLogs().At(k).LogRecords().Len() | ||
} | ||
return count | ||
} |
Oops, something went wrong.