| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package xexporterhelper |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"errors" |
| 9 |
"fmt" |
| 10 |
|
| 11 |
"go.opentelemetry.io/collector/exporter/exporterhelper" |
| 12 |
"go.opentelemetry.io/collector/exporter/exporterhelper/internal/sizer" |
| 13 |
"go.opentelemetry.io/collector/pdata/pprofile" |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
func (req *profilesRequest) MergeSplit(_ context.Context, maxSize int, szt exporterhelper.RequestSizerType, r2 Request) ([]Request, error) { |
| 21 |
var sz sizer.ProfilesSizer |
| 22 |
switch szt { |
| 23 |
case exporterhelper.RequestSizerTypeItems: |
| 24 |
sz = &sizer.ProfilesCountSizer{} |
| 25 |
case exporterhelper.RequestSizerTypeBytes: |
| 26 |
sz = &sizer.ProfilesBytesSizer{} |
| 27 |
default: |
| 28 |
return nil, errors.New("unknown sizer type") |
| 29 |
} |
| 30 |
|
| 31 |
if r2 != nil && r2.ItemsCount() > 0 { |
| 32 |
req2, ok := r2.(*profilesRequest) |
| 33 |
if !ok { |
| 34 |
return nil, errors.New("invalid input type") |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
if maxSize == 0 { |
| 41 |
return []Request{req, req2}, nil |
| 42 |
} |
| 43 |
|
| 44 |
sp1, err1 := req.split(maxSize, sz) |
| 45 |
sp2, err2 := req2.split(maxSize, sz) |
| 46 |
|
| 47 |
return append(sp1, sp2...), errors.Join(err1, err2) |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
if maxSize == 0 { |
| 52 |
return []Request{req}, nil |
| 53 |
} |
| 54 |
return req.split(maxSize, sz) |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
func (req *profilesRequest) split(maxSize int, sz sizer.ProfilesSizer) ([]Request, error) { |
| 67 |
var res []Request |
| 68 |
for req.size(sz) > maxSize { |
| 69 |
pd, rmSize := extractProfiles(req.pd, maxSize, sz) |
| 70 |
if pd.SampleCount() == 0 { |
| 71 |
return res, fmt.Errorf("one sample size is greater than max size, dropping items: %d", req.pd.SampleCount()) |
| 72 |
} |
| 73 |
req.setCachedSize(req.size(sz) - rmSize) |
| 74 |
res = append(res, newProfilesRequest(pd)) |
| 75 |
} |
| 76 |
|
| 77 |
res = append(res, req) |
| 78 |
return res, nil |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
func extractProfiles(srcProfiles pprofile.Profiles, capacity int, sz sizer.ProfilesSizer) (pprofile.Profiles, int) { |
| 83 |
destProfiles := pprofile.NewProfiles() |
| 84 |
capacityLeft := capacity - sz.ProfilesSize(destProfiles) |
| 85 |
removedSize := 0 |
| 86 |
srcProfiles.ResourceProfiles().RemoveIf(func(srcRP pprofile.ResourceProfiles) bool { |
| 87 |
|
| 88 |
if capacityLeft == 0 { |
| 89 |
return false |
| 90 |
} |
| 91 |
rawRpSize := sz.ResourceProfilesSize(srcRP) |
| 92 |
rpSize := sz.DeltaSize(rawRpSize) |
| 93 |
|
| 94 |
if rpSize > capacityLeft { |
| 95 |
extSrcRP, extRpSize := extractResourceProfiles(srcRP, capacityLeft, sz) |
| 96 |
|
| 97 |
|
| 98 |
capacityLeft = 0 |
| 99 |
removedSize += extRpSize |
| 100 |
|
| 101 |
removedSize += rpSize - rawRpSize - (sz.DeltaSize(rawRpSize-extRpSize) - (rawRpSize - extRpSize)) |
| 102 |
|
| 103 |
|
| 104 |
if extSrcRP.ScopeProfiles().Len() > 0 { |
| 105 |
extSrcRP.MoveTo(destProfiles.ResourceProfiles().AppendEmpty()) |
| 106 |
} |
| 107 |
return extSrcRP.ScopeProfiles().Len() != 0 |
| 108 |
} |
| 109 |
capacityLeft -= rpSize |
| 110 |
removedSize += rpSize |
| 111 |
srcRP.MoveTo(destProfiles.ResourceProfiles().AppendEmpty()) |
| 112 |
return true |
| 113 |
}) |
| 114 |
return destProfiles, removedSize |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
func extractResourceProfiles(srcRP pprofile.ResourceProfiles, capacity int, sz sizer.ProfilesSizer) (pprofile.ResourceProfiles, int) { |
| 119 |
destRP := pprofile.NewResourceProfiles() |
| 120 |
destRP.SetSchemaUrl(srcRP.SchemaUrl()) |
| 121 |
srcRP.Resource().CopyTo(destRP.Resource()) |
| 122 |
|
| 123 |
capacityLeft := capacity - (sz.DeltaSize(capacity) - capacity) - sz.ResourceProfilesSize(destRP) |
| 124 |
removedSize := 0 |
| 125 |
|
| 126 |
srcRP.ScopeProfiles().RemoveIf(func(srcSS pprofile.ScopeProfiles) bool { |
| 127 |
|
| 128 |
if capacityLeft == 0 { |
| 129 |
return false |
| 130 |
} |
| 131 |
|
| 132 |
rawSlSize := sz.ScopeProfilesSize(srcSS) |
| 133 |
ssSize := sz.DeltaSize(rawSlSize) |
| 134 |
if ssSize > capacityLeft { |
| 135 |
extSrcSS, extSsSize := extractScopeProfiles(srcSS, capacityLeft, sz) |
| 136 |
|
| 137 |
|
| 138 |
capacityLeft = 0 |
| 139 |
removedSize += extSsSize |
| 140 |
|
| 141 |
removedSize += ssSize - rawSlSize - (sz.DeltaSize(rawSlSize-extSsSize) - (rawSlSize - extSsSize)) |
| 142 |
|
| 143 |
|
| 144 |
if extSrcSS.Profiles().Len() > 0 { |
| 145 |
extSrcSS.MoveTo(destRP.ScopeProfiles().AppendEmpty()) |
| 146 |
} |
| 147 |
return extSrcSS.Profiles().Len() != 0 |
| 148 |
} |
| 149 |
capacityLeft -= ssSize |
| 150 |
removedSize += ssSize |
| 151 |
srcSS.MoveTo(destRP.ScopeProfiles().AppendEmpty()) |
| 152 |
return true |
| 153 |
}) |
| 154 |
|
| 155 |
return destRP, removedSize |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
func extractScopeProfiles(srcSS pprofile.ScopeProfiles, capacity int, sz sizer.ProfilesSizer) (pprofile.ScopeProfiles, int) { |
| 160 |
destSS := pprofile.NewScopeProfiles() |
| 161 |
destSS.SetSchemaUrl(srcSS.SchemaUrl()) |
| 162 |
srcSS.Scope().CopyTo(destSS.Scope()) |
| 163 |
|
| 164 |
capacityLeft := capacity - (sz.DeltaSize(capacity) - capacity) - sz.ScopeProfilesSize(destSS) |
| 165 |
removedSize := 0 |
| 166 |
srcSS.Profiles().RemoveIf(func(srcProfile pprofile.Profile) bool { |
| 167 |
|
| 168 |
if capacityLeft == 0 { |
| 169 |
return false |
| 170 |
} |
| 171 |
rsSize := sz.DeltaSize(sz.ProfileSize(srcProfile)) |
| 172 |
if rsSize > capacityLeft { |
| 173 |
|
| 174 |
|
| 175 |
capacityLeft = 0 |
| 176 |
return false |
| 177 |
} |
| 178 |
capacityLeft -= rsSize |
| 179 |
removedSize += rsSize |
| 180 |
srcProfile.MoveTo(destSS.Profiles().AppendEmpty()) |
| 181 |
return true |
| 182 |
}) |
| 183 |
return destSS, removedSize |
| 184 |
} |