@@ -89,6 +89,7 @@ var profileTypes = map[ProfileType]profileType{
8989 Filename : "cpu.pprof" ,
9090 Collect : func (p * profiler ) ([]byte , error ) {
9191 var buf bytes.Buffer
92+ var outBuf bytes.Buffer
9293 // Start the CPU profiler at the end of the profiling
9394 // period so that we're sure to capture the CPU usage of
9495 // this library, which mostly happens at the end
@@ -101,9 +102,7 @@ var profileTypes = map[ProfileType]profileType{
101102 runtime .SetCPUProfileRate (p .cfg .cpuProfileRate )
102103 }
103104
104- compressor := p .compressors [CPUProfile ]
105- compressor .Reset (& buf )
106- if err := p .startCPUProfile (compressor ); err != nil {
105+ if err := p .startCPUProfile (& outBuf ); err != nil {
107106 return nil , err
108107 }
109108 p .interruptibleSleep (p .cfg .cpuDuration )
@@ -113,10 +112,12 @@ var profileTypes = map[ProfileType]profileType{
113112 // the other profile types
114113 p .pendingProfiles .Wait ()
115114 p .stopCPUProfile ()
116- if err := compressor .Close (); err != nil {
117- return nil , err
118- }
119- return buf .Bytes (), nil
115+
116+ c := p .compressors [CPUProfile ]
117+ c .Reset (& buf )
118+ _ , writeErr := outBuf .WriteTo (c )
119+ closeErr := c .Close ()
120+ return buf .Bytes (), cmp .Or (writeErr , closeErr )
120121 },
121122 },
122123 // HeapProfile is complex due to how the Go runtime exposes it. It contains 4
@@ -175,10 +176,10 @@ var profileTypes = map[ProfileType]profileType{
175176 return nil , err
176177 }
177178
178- compressor := p .compressors [expGoroutineWaitProfile ]
179- compressor .Reset (pprof )
180- err := goroutineDebug2ToPprof (text , compressor , now )
181- err = cmp .Or (err , compressor .Close ())
179+ c := p .compressors [expGoroutineWaitProfile ]
180+ c .Reset (pprof )
181+ err := goroutineDebug2ToPprof (text , c , now )
182+ err = cmp .Or (err , c .Close ())
182183 return pprof .Bytes (), err
183184 },
184185 },
@@ -187,11 +188,11 @@ var profileTypes = map[ProfileType]profileType{
187188 Filename : "metrics.json" ,
188189 Collect : func (p * profiler ) ([]byte , error ) {
189190 var buf bytes.Buffer
190- compressor := p .compressors [MetricsProfile ]
191- compressor .Reset (& buf )
191+ c := p .compressors [MetricsProfile ]
192+ c .Reset (& buf )
192193 interrupted := p .interruptibleSleep (p .cfg .period )
193- err := p .met .report (now (), compressor )
194- err = cmp .Or (err , compressor .Close ())
194+ err := p .met .report (now (), c )
195+ err = cmp .Or (err , c .Close ())
195196 if err != nil && interrupted {
196197 err = errProfilerStopped
197198 }
@@ -204,9 +205,8 @@ var profileTypes = map[ProfileType]profileType{
204205 Collect : func (p * profiler ) ([]byte , error ) {
205206 p .lastTrace = time .Now ()
206207 buf := new (bytes.Buffer )
207- compressor := p .compressors [executionTrace ]
208- compressor .Reset (buf )
209- lt := newLimitedTraceCollector (compressor , int64 (p .cfg .traceConfig .Limit ))
208+ outBuf := new (bytes.Buffer )
209+ lt := newLimitedTraceCollector (outBuf , int64 (p .cfg .traceConfig .Limit ))
210210 if err := trace .Start (lt ); err != nil {
211211 return nil , err
212212 }
@@ -217,10 +217,12 @@ var profileTypes = map[ProfileType]profileType{
217217 case <- lt .done : // The trace size limit was exceeded
218218 }
219219 trace .Stop ()
220- if err := compressor .Close (); err != nil {
221- return nil , err
222- }
223- return buf .Bytes (), nil
220+
221+ c := p .compressors [executionTrace ]
222+ c .Reset (buf )
223+ _ , writeErr := outBuf .WriteTo (c )
224+ closeErr := c .Close ()
225+ return buf .Bytes (), cmp .Or (writeErr , closeErr )
224226 },
225227 },
226228}
@@ -284,10 +286,10 @@ func collectGenericProfile(name string, pt ProfileType) func(p *profiler) ([]byt
284286 var buf bytes.Buffer
285287 dp , ok := p .deltas [pt ]
286288 if ! ok || ! p .cfg .deltaProfiles {
287- compressor := p .compressors [pt ]
288- compressor .Reset (& buf )
289- err := p .lookupProfile (name , compressor , 0 )
290- err = cmp .Or (err , compressor .Close ())
289+ c := p .compressors [pt ]
290+ c .Reset (& buf )
291+ err := p .lookupProfile (name , c , 0 )
292+ err = cmp .Or (err , c .Close ())
291293 return buf .Bytes (), err
292294 }
293295
@@ -435,13 +437,15 @@ func (fdp *fastDeltaProfiler) Delta(data []byte) (b []byte, err error) {
435437 }
436438
437439 fdp .buf .Reset ()
438- fdp .compressor .Reset (& fdp .buf )
439-
440- if err = fdp .dc .Delta (data , fdp .compressor ); err != nil {
441- return nil , fmt .Errorf ("error computing delta: %s" , err .Error ())
442- }
443- if err = fdp .compressor .Close (); err != nil {
444- return nil , fmt .Errorf ("error flushing gzip writer: %s" , err .Error ())
440+ c := fdp .compressor
441+ c .Reset (& fdp .buf )
442+
443+ deltaErr := fdp .dc .Delta (data , c )
444+ closeErr := c .Close ()
445+ if deltaErr != nil {
446+ return nil , fmt .Errorf ("error computing delta: %w" , deltaErr )
447+ } else if closeErr != nil {
448+ return nil , fmt .Errorf ("error flushing compressor: %w" , closeErr )
445449 }
446450 // The returned slice will be retained in case the profile upload fails,
447451 // so we need to return a copy of the buffer's bytes to avoid a data
0 commit comments