@@ -1842,9 +1842,9 @@ func GetStackTrace() (bytes.Buffer, error) {
18421842 return profBuf , err
18431843}
18441844
1845- // RotateProfilesIfNeeded will remove old files if there are more than
1845+ // RotateFilenamesIfNeeded will remove old files if there are more than
18461846// 10 matching the given filename pattern.
1847- func RotateProfilesIfNeeded (filename string ) error {
1847+ func RotateFilenamesIfNeeded (filename string ) error {
18481848 existingFiles , err := filepath .Glob (filename )
18491849 if err != nil {
18501850 return fmt .Errorf ("Error listing existing profiles in %q: %w" , filename , err )
@@ -1868,6 +1868,19 @@ func LogStackTraces(ctx context.Context, logDirectory string, stackTrace bytes.B
18681868 // log to console
18691869 _ , _ = fmt .Fprintf (os .Stderr , "Stack trace:\n %s\n " , stackTrace .String ())
18701870
1871+ err := writeStackTraceFile (ctx , logDirectory , timestamp , stackTrace )
1872+ if err != nil {
1873+ return
1874+ }
1875+
1876+ rotatePath := filepath .Join (logDirectory , StackFilePrefix + "*.log" )
1877+ err = RotateFilenamesIfNeeded (rotatePath )
1878+ if err != nil {
1879+ WarnfCtx (ctx , "Error rotating stack trace files in path %s: %v" , rotatePath , err )
1880+ }
1881+ }
1882+
1883+ func writeStackTraceFile (ctx context.Context , logDirectory , timestamp string , stackTrace bytes.Buffer ) error {
18711884 filename := filepath .Join (logDirectory , StackFilePrefix + timestamp + ".log" )
18721885 file , err := os .Create (filename )
18731886 defer func () {
@@ -1878,16 +1891,13 @@ func LogStackTraces(ctx context.Context, logDirectory string, stackTrace bytes.B
18781891 }()
18791892 if err != nil {
18801893 WarnfCtx (ctx , "Error opening stack trace file %s: %v" , filename , err )
1894+ return err
18811895 }
18821896
18831897 _ , err = file .WriteString (fmt .Sprintf ("Stack trace:\n %s\n " , stackTrace .String ()))
18841898 if err != nil {
18851899 WarnfCtx (ctx , "Error writing stack trace to file %s: %v" , filename , err )
1900+ return err
18861901 }
1887-
1888- rotatePath := filepath .Join (logDirectory , StackFilePrefix + "*.log" )
1889- err = RotateProfilesIfNeeded (rotatePath )
1890- if err != nil {
1891- WarnfCtx (ctx , "Error rotating stack trace files in path %s: %v" , rotatePath , err )
1892- }
1902+ return nil
18931903}
0 commit comments