Skip to content

Commit fa77d9c

Browse files
authored
feat(GH-42): Result filename starts with the pod name (#45)
1 parent 81abd0c commit fa77d9c

3 files changed

Lines changed: 16 additions & 12 deletions

File tree

internal/cli/adapter/profiling_container_adapter.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func (p profilingContainerAdapter) GetRemoteFile(pod *v1.Pod, containerName stri
127127
return "", errors.Wrap(err, "could not decode remote file")
128128
}
129129

130-
fileName := filepath.Join(target.LocalPath, renameResultFileName(remoteFile.FileName, remoteFile.Timestamp))
130+
fileName := filepath.Join(target.LocalPath, renameResultFileName(target.PodName, remoteFile.FileName, remoteFile.Timestamp))
131131

132132
err = os.WriteFile(fileName, decoded, 0644)
133133
if err != nil {
@@ -275,9 +275,9 @@ func readChunks(downloadChunks []string, fileBuffSize int64) ([]byte, error) {
275275
}
276276

277277
// renameResultFileName renames the result file
278-
func renameResultFileName(fileName string, t time.Time) string {
278+
func renameResultFileName(podName, fileName string, t time.Time) string {
279279
f := stringUtils.SubstringBeforeLast(stringUtils.SubstringAfterLast(fileName, "/"), ".")
280-
return stringUtils.SubstringBefore(f, ".") + "-" + strings.ReplaceAll(t.Format(time.RFC3339), ":", "_") + "." + stringUtils.SubstringAfter(f, ".")
280+
return podName + "-" + stringUtils.SubstringBefore(f, ".") + "-" + strings.ReplaceAll(t.Format(time.RFC3339), ":", "_") + "." + stringUtils.SubstringAfter(f, ".")
281281
}
282282

283283
// renameChunkFileName renames the chunk file

internal/cli/adapter/profiling_container_adapter_test.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,15 @@ func Test_profilingContainerAdapter_HandleProfilingContainerLogs(t *testing.T) {
171171

172172
func Test_renameResultFileName(t *testing.T) {
173173
// Given
174-
fileName := "/tmp/contprof-flamegraph.svg.gz"
174+
podName := "pod-name"
175+
fileName := "/tmp/flamegraph.svg.gz"
175176
timestamp, _ := time.Parse(time.RFC3339, "2023-02-28T11:44:12.678378359Z")
176177

177178
// When
178-
result := renameResultFileName(fileName, timestamp)
179+
result := renameResultFileName(podName, fileName, timestamp)
179180

180181
// Then
181-
assert.Equal(t, "contprof-flamegraph-2023-02-28T11_44_12Z.svg", result)
182+
assert.Equal(t, "pod-name-flamegraph-2023-02-28T11_44_12Z.svg", result)
182183
}
183184

184185
func Test_profilingContainerAdapter_GetRemoteFile(t *testing.T) {
@@ -251,6 +252,7 @@ func Test_profilingContainerAdapter_GetRemoteFile(t *testing.T) {
251252
target: &config.TargetConfig{
252253
LocalPath: "/tmp",
253254
Compressor: compressor.None,
255+
PodName: "pod-name",
254256
},
255257
}
256258
},
@@ -263,10 +265,10 @@ func Test_profilingContainerAdapter_GetRemoteFile(t *testing.T) {
263265
},
264266
then: func(t *testing.T, r result, f fields) {
265267
require.NoError(t, r.err)
266-
assert.Equal(t, filepath.Join(common.TmpDir(), "flamegraph-2023-02-28T11_44_12Z.svg"), r.remoteFile)
268+
assert.Equal(t, filepath.Join(common.TmpDir(), "pod-name-flamegraph-2023-02-28T11_44_12Z.svg"), r.remoteFile)
267269
},
268270
afterEach: func() {
269-
_ = os.Remove(filepath.Join(common.TmpDir(), "flamegraph-2023-02-28T11_44_12Z.svg"))
271+
_ = os.Remove(filepath.Join(common.TmpDir(), "pod-name-flamegraph-2023-02-28T11_44_12Z.svg"))
270272
},
271273
},
272274
{
@@ -567,6 +569,7 @@ func Test_profilingContainerAdapter_GetRemoteFile(t *testing.T) {
567569
target: &config.TargetConfig{
568570
LocalPath: "/other",
569571
Compressor: compressor.None,
572+
PodName: "pod-name",
570573
},
571574
}
572575
},
@@ -579,7 +582,7 @@ func Test_profilingContainerAdapter_GetRemoteFile(t *testing.T) {
579582
},
580583
then: func(t *testing.T, r result, f fields) {
581584
require.Error(t, r.err)
582-
assert.EqualError(t, r.err, "could not write result file: open /other/flamegraph-2023-02-28T11_44_12Z.svg: no such file or directory")
585+
assert.EqualError(t, r.err, "could not write result file: open /other/pod-name-flamegraph-2023-02-28T11_44_12Z.svg: no such file or directory")
583586
},
584587
},
585588
{
@@ -636,6 +639,7 @@ func Test_profilingContainerAdapter_GetRemoteFile(t *testing.T) {
636639
target: &config.TargetConfig{
637640
LocalPath: "/tmp",
638641
Compressor: compressor.None,
642+
PodName: "pod-name",
639643
},
640644
}
641645
},
@@ -648,10 +652,10 @@ func Test_profilingContainerAdapter_GetRemoteFile(t *testing.T) {
648652
},
649653
then: func(t *testing.T, r result, f fields) {
650654
require.NoError(t, r.err)
651-
assert.Equal(t, filepath.Join(common.TmpDir(), "flamegraph-2023-02-28T11_44_12Z.svg"), r.remoteFile)
655+
assert.Equal(t, filepath.Join(common.TmpDir(), "pod-name-flamegraph-2023-02-28T11_44_12Z.svg"), r.remoteFile)
652656
},
653657
afterEach: func() {
654-
_ = os.Remove(filepath.Join(common.TmpDir(), "flamegraph-2023-02-28T11_44_12Z.svg"))
658+
_ = os.Remove(filepath.Join(common.TmpDir(), "pod-name-flamegraph-2023-02-28T11_44_12Z.svg"))
655659
},
656660
},
657661
{

internal/cli/profiler/job_profiler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func NewJobProfiler(podAdapter adapter.PodAdapter, profilingJobAdapter adapter.P
2929
}
3030
}
3131

32-
// Profile runs all the steps of the profiling from the job creation up to obtain the profiling result
32+
// Profile runs all the steps of the profiling from the job creation up to get the profiling result
3333
func (p JobProfiler) Profile(cfg *config.ProfilerConfig) error {
3434
ctx := context.Background()
3535

0 commit comments

Comments
 (0)