Skip to content

Commit 634fc90

Browse files
committed
Update heap dump capture for compressed file to use zip extension instead out .out
1 parent b03c8fa commit 634fc90

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

internal/capture/heap.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ func NewHeapDump(javaHome string, pid int, hdPath string, dump bool) *HeapDump {
6161
}
6262
}
6363

64+
// getDestHeapDumpFilename returns the appropriate filename based on compression status
65+
func (t *HeapDump) getDestHeapDumpFilename(isCompressed bool) string {
66+
if isCompressed {
67+
return hdZip
68+
}
69+
return hdOut
70+
}
71+
6472
// Run executes the heap dump capture process and uploads the captured file
6573
// to the specified endpoint.
6674
func (t *HeapDump) Run() (Result, error) {
@@ -76,7 +84,7 @@ func (t *HeapDump) Run() (Result, error) {
7684
logger.Log("detected pre-compressed heap dump file: %s with encoding: %s", t.hdPath, contentEncoding)
7785
}
7886

79-
hd, err = t.getPreCapturedDumpFile()
87+
hd, err = t.getPreCapturedDumpFile(isCompressed)
8088
if err != nil {
8189
return Result{
8290
Msg: fmt.Sprintf("capture heap dump failed: %s", err.Error()),
@@ -109,10 +117,12 @@ func (t *HeapDump) Run() (Result, error) {
109117
return Result{Msg: "skipped heap dump"}, nil
110118
}
111119

120+
tempFilename := t.getDestHeapDumpFilename(isCompressed)
121+
112122
defer func() {
113123
err := hd.Close()
114124
if err != nil && !errors.Is(err, os.ErrClosed) {
115-
logger.Log("failed to close hd file %s cause err: %s", hdOut, err.Error())
125+
logger.Log("failed to close hd file %s cause err: %s", tempFilename, err.Error())
116126
}
117127
}()
118128

@@ -143,9 +153,9 @@ func (t *HeapDump) Run() (Result, error) {
143153
}()
144154

145155
defer func() {
146-
err = os.Remove(hdOut)
156+
err = os.Remove(tempFilename)
147157
if err != nil {
148-
logger.Log("failed to rm hd file %s cause err: %s", hdOut, err.Error())
158+
logger.Log("failed to rm hd file %s cause err: %s", tempFilename, err.Error())
149159
}
150160
}()
151161

@@ -158,7 +168,7 @@ func (t *HeapDump) Run() (Result, error) {
158168
}
159169

160170
// getPreCapturedDumpFile handles the case when a heap dump is pre-captured (using the hdPath field)
161-
func (t *HeapDump) getPreCapturedDumpFile() (*os.File, error) {
171+
func (t *HeapDump) getPreCapturedDumpFile(isCompressed bool) (*os.File, error) {
162172
hdf, err := os.Open(t.hdPath)
163173

164174
// Fallback, try to open the file in the Docker container
@@ -181,7 +191,8 @@ func (t *HeapDump) getPreCapturedDumpFile() (*os.File, error) {
181191
}
182192
}()
183193

184-
hd, err := os.Create(hdOut)
194+
filename := t.getDestHeapDumpFilename(isCompressed)
195+
hd, err := os.Create(filename)
185196
if err != nil {
186197
return nil, err
187198
}
@@ -196,7 +207,7 @@ func (t *HeapDump) getPreCapturedDumpFile() (*os.File, error) {
196207
return nil, err
197208
}
198209

199-
logger.Log("copied heap dump data %s", t.hdPath)
210+
logger.Log("copied heap dump data %s to %s", t.hdPath, filename)
200211
return hd, nil
201212
}
202213

0 commit comments

Comments
 (0)