Skip to content

Commit

Permalink
Refactor to be more go-idiomatic
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsillydog authored and djaglowski committed Dec 11, 2024
1 parent e05d511 commit 2e035a2
Showing 1 changed file with 37 additions and 37 deletions.
74 changes: 37 additions & 37 deletions exporter/chronicleexporter/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,31 +423,31 @@ func (m *protoMarshaler) constructPayloads(rawLogs map[string][]*api.LogEntry, n

func (m *protoMarshaler) enforceMaximumsGRPCRequest(request *api.BatchCreateLogsRequest) []*api.BatchCreateLogsRequest {
size := proto.Size(request)
if size > m.cfg.BatchRequestSizeLimitGRPC || len(request.Batch.Entries) > m.cfg.BatchLogCountLimitGRPC {
if len(request.Batch.Entries) < 2 {
m.teleSettings.Logger.Error("Single entry exceeds max request size. Dropping entry", zap.Int("size", size))
return []*api.BatchCreateLogsRequest{}
entries := request.Batch.Entries
if size <= m.cfg.BatchRequestSizeLimitGRPC && len(entries) <= m.cfg.BatchLogCountLimitGRPC {
return []*api.BatchCreateLogsRequest{
request,
}
}

// split request into two
entries := request.Batch.Entries
mid := len(entries) / 2
leftHalf := entries[:mid]
rightHalf := entries[mid:]
if len(entries) < 2 {
m.teleSettings.Logger.Error("Single entry exceeds max request size. Dropping entry", zap.Int("size", size))
return []*api.BatchCreateLogsRequest{}
}

request.Batch.Entries = leftHalf
otherHalfRequest := m.buildGRPCRequest(rightHalf, request.Batch.LogType, request.Batch.Source.Namespace, request.Batch.Source.Labels)
// split request into two
mid := len(entries) / 2
leftHalf := entries[:mid]
rightHalf := entries[mid:]

// re-enforce max size restriction on each half
enforcedRequest := m.enforceMaximumsGRPCRequest(request)
enforcedOtherHalfRequest := m.enforceMaximumsGRPCRequest(otherHalfRequest)
request.Batch.Entries = leftHalf
otherHalfRequest := m.buildGRPCRequest(rightHalf, request.Batch.LogType, request.Batch.Source.Namespace, request.Batch.Source.Labels)

return append(enforcedRequest, enforcedOtherHalfRequest...)
}
// re-enforce max size restriction on each half
enforcedRequest := m.enforceMaximumsGRPCRequest(request)
enforcedOtherHalfRequest := m.enforceMaximumsGRPCRequest(otherHalfRequest)

return []*api.BatchCreateLogsRequest{
request,
}
return append(enforcedRequest, enforcedOtherHalfRequest...)
}

func (m *protoMarshaler) buildGRPCRequest(entries []*api.LogEntry, logType, namespace string, ingestionLabels []*api.Label) *api.BatchCreateLogsRequest {
Expand Down Expand Up @@ -535,30 +535,30 @@ func (m *protoMarshaler) constructHTTPPayloads(rawLogs map[string][]*api.Log) ma
func (m *protoMarshaler) enforceMaximumsHTTPRequest(request *api.ImportLogsRequest) []*api.ImportLogsRequest {
size := proto.Size(request)
logs := request.GetInlineSource().Logs
if size > m.cfg.BatchRequestSizeLimitHTTP || len(logs) > m.cfg.BatchLogCountLimitHTTP {
if len(logs) < 2 {
m.teleSettings.Logger.Error("Single entry exceeds max request size. Dropping entry", zap.Int("size", size))
return []*api.ImportLogsRequest{}
if size <= m.cfg.BatchRequestSizeLimitHTTP && len(logs) <= m.cfg.BatchLogCountLimitHTTP {
return []*api.ImportLogsRequest{
request,
}
}

// split request into two
mid := len(logs) / 2
leftHalf := logs[:mid]
rightHalf := logs[mid:]
if len(logs) < 2 {
m.teleSettings.Logger.Error("Single entry exceeds max request size. Dropping entry", zap.Int("size", size))
return []*api.ImportLogsRequest{}
}

request.GetInlineSource().Logs = leftHalf
otherHalfRequest := m.buildHTTPRequest(rightHalf)
// split request into two
mid := len(logs) / 2
leftHalf := logs[:mid]
rightHalf := logs[mid:]

// re-enforce max size restriction on each half
enforcedRequest := m.enforceMaximumsHTTPRequest(request)
enforcedOtherHalfRequest := m.enforceMaximumsHTTPRequest(otherHalfRequest)
request.GetInlineSource().Logs = leftHalf
otherHalfRequest := m.buildHTTPRequest(rightHalf)

return append(enforcedRequest, enforcedOtherHalfRequest...)
}
// re-enforce max size restriction on each half
enforcedRequest := m.enforceMaximumsHTTPRequest(request)
enforcedOtherHalfRequest := m.enforceMaximumsHTTPRequest(otherHalfRequest)

return []*api.ImportLogsRequest{
request,
}
return append(enforcedRequest, enforcedOtherHalfRequest...)
}

func (m *protoMarshaler) buildHTTPRequest(entries []*api.Log) *api.ImportLogsRequest {
Expand Down

0 comments on commit 2e035a2

Please sign in to comment.