Debian Codegraph @

exporter/exporterhelper/xexporterhelper/profiles_batch.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package xexporterhelper // import "go.opentelemetry.io/collector/exporter/exporterhelper/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 // MergeSplit splits and/or merges the profiles into multiple requests based on the MaxSizeConfig.
17 //
18 // Following the OTLP 1.7.0 upgrade, this is currently a noop.
19 // See https://github.com/open-telemetry/opentelemetry-collector/issues/13106
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 // TODO(13106): handle merging of profiles (and change the indice tables with their new indices)
37 // req2.mergeTo(req, sz)
38
39 // If no limit we can simply merge the new request into the current and return.
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 // If no limit we can simply merge the new request into the current and return.
51 if maxSize == 0 {
52 return []Request{req}, nil
53 }
54 return req.split(maxSize, sz)
55 }
56
57 // TODO(13106): handle merging of profiles (and change the indice tables with their new indices)
58 /*func (req *profilesRequest) mergeTo(dst *profilesRequest, sz sizer.ProfilesSizer) {
59 if sz != nil {
60 dst.setCachedSize(dst.size(sz) + req.size(sz))
61 req.setCachedSize(0)
62 }
63 req.pd.ResourceProfiles().MoveAndAppendTo(dst.pd.ResourceProfiles())
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 // extractProfiles extracts a new profiles with a maximum number of samples.
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 // If the no more capacity left just return.
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 // This cannot make it to exactly 0 for the bytes,
97 // force it to be 0 since that is the stopping condition.
98 capacityLeft = 0
99 removedSize += extRpSize
100 // There represents the delta between the delta sizes.
101 removedSize += rpSize - rawRpSize - (sz.DeltaSize(rawRpSize-extRpSize) - (rawRpSize - extRpSize))
102 // It is possible that for the bytes scenario, the extracted field contains no profiles.
103 // Do not add it to the destination if that is the case.
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 // extractResourceProfiles extracts profiles and returns a new resource profiles with the specified number of profiles.
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 // Take into account that this can have max "capacity", so when added to the parent will need space for the extra delta size.
123 capacityLeft := capacity - (sz.DeltaSize(capacity) - capacity) - sz.ResourceProfilesSize(destRP)
124 removedSize := 0
125
126 srcRP.ScopeProfiles().RemoveIf(func(srcSS pprofile.ScopeProfiles) bool {
127 // If the no more capacity left just return.
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 // This cannot make it to exactly 0 for the bytes,
137 // force it to be 0 since that is the stopping condition.
138 capacityLeft = 0
139 removedSize += extSsSize
140 // There represents the delta between the delta sizes.
141 removedSize += ssSize - rawSlSize - (sz.DeltaSize(rawSlSize-extSsSize) - (rawSlSize - extSsSize))
142 // It is possible that for the bytes scenario, the extracted field contains no profiles.
143 // Do not add it to the destination if that is the case.
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 // extractScopeProfiles extracts profiles and returns a new scope profiles with the specified number of profiles.
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 // Take into account that this can have max "capacity", so when added to the parent will need space for the extra delta size.
164 capacityLeft := capacity - (sz.DeltaSize(capacity) - capacity) - sz.ScopeProfilesSize(destSS)
165 removedSize := 0
166 srcSS.Profiles().RemoveIf(func(srcProfile pprofile.Profile) bool {
167 // If the no more capacity left just return.
168 if capacityLeft == 0 {
169 return false
170 }
171 rsSize := sz.DeltaSize(sz.ProfileSize(srcProfile))
172 if rsSize > capacityLeft {
173 // This cannot make it to exactly 0 for the bytes,
174 // force it to be 0 since that is the stopping condition.
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 }