-
Notifications
You must be signed in to change notification settings - Fork 173
Make parsing log filenames future-proof #4518
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
europaul
wants to merge
2
commits into
lf-edge:master
Choose a base branch
from
europaul:newlog-filenames
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+248
−43
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ package types | |
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
@@ -85,21 +86,50 @@ type NewlogMetrics struct { | |
AppMetrics logfileMetrics // App metrics | ||
} | ||
|
||
// GetTimestampFromGzipName - get timestamp from gzip file name | ||
func GetTimestampFromGzipName(fName string) (time.Time, error) { | ||
// here are example file names: | ||
// app.6656f860-7563-4bbf-8bba-051f5942982b.log.1730464687367.gz | ||
// dev.log.keep.1730404601953.gz | ||
// dev.log.upload.1730404601953.gz | ||
// the timestamp is the number between the last two dots | ||
nameParts := strings.Split(fName, ".") | ||
if len(nameParts) < 2 { | ||
return time.Time{}, fmt.Errorf("getTimestampFromGzipName: invalid log file name %s", fName) | ||
var ( | ||
timestampRegex *regexp.Regexp | ||
uuidRegex *regexp.Regexp | ||
) | ||
|
||
func init() { | ||
// Regular expression to match a timestamp | ||
timestampRegex = regexp.MustCompile(`^\d+$`) | ||
|
||
// UUID regex pattern (supports v4 UUIDs like "123e4567-e89b-12d3-a456-426614174000") | ||
uuidRegex = regexp.MustCompile(`[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}`) | ||
} | ||
|
||
// GetTimestampFromFileName extracts a millisecond timestamp from a filename | ||
func GetTimestampFromFileName(filename string) (time.Time, error) { | ||
// Split the filename into parts using dots | ||
parts := strings.Split(filename, ".") | ||
|
||
// Check each part for a timestamp match | ||
for _, part := range parts { | ||
if timestampRegex.MatchString(part) { | ||
// Convert the matched timestamp string to an integer | ||
timestamp, err := strconv.ParseInt(part, 10, 64) | ||
if err != nil { | ||
return time.Time{}, fmt.Errorf("failed to parse timestamp: %s", err) | ||
} | ||
return time.Unix(0, timestamp*int64(time.Millisecond)), nil // Return the first valid timestamp found | ||
} | ||
} | ||
timeStr := nameParts[len(nameParts)-2] | ||
fTime, err := strconv.Atoi(timeStr) | ||
if err != nil { | ||
return time.Time{}, fmt.Errorf("getTimestampFromGzipName: %w", err) | ||
|
||
return time.Time{}, fmt.Errorf("no timestamp found in filename: %s", filename) | ||
} | ||
|
||
// GetUUIDFromFileName extracts a UUID from a filename with dot-delimited parts | ||
func GetUUIDFromFileName(filename string) (string, error) { | ||
// Split the filename into parts using dots | ||
parts := strings.Split(filename, ".") | ||
|
||
// Check each part for a UUID match | ||
for _, part := range parts { | ||
if uuidRegex.MatchString(part) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or call the UUID parser function we use elsewhere? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we mostly use |
||
return part, nil // Return the first UUID found | ||
} | ||
} | ||
return time.Unix(0, int64(fTime)*int64(time.Millisecond)), nil | ||
|
||
return "", fmt.Errorf("no UUID found in filename: %s", filename) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
// Copyright (c) 2025 Zededa, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package types | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/onsi/gomega" | ||
) | ||
|
||
func TestGetTimestampFromFileName(t *testing.T) { | ||
t.Parallel() | ||
g := gomega.NewWithT(t) | ||
|
||
tests := []struct { | ||
name string | ||
filename string | ||
wantTime time.Time | ||
wantError bool | ||
}{ | ||
{ | ||
name: "Valid timestamp in filename", | ||
filename: "dev.log.1731491904032.gz", | ||
wantTime: time.Unix(0, 1731491904032*int64(time.Millisecond)), | ||
wantError: false, | ||
}, | ||
{ | ||
name: "Valid timestamp in regular filename", | ||
filename: "dev.log.1731491904032", | ||
wantTime: time.Unix(0, 1731491904032*int64(time.Millisecond)), | ||
wantError: false, | ||
}, | ||
{ | ||
name: "Valid timestamp with UUID", | ||
filename: "app.8ce1cc69-e1bb-4fe3-9613-e3eb1c5f5c4d.log.1731935033496.gz", | ||
wantTime: time.Unix(0, 1731935033496*int64(time.Millisecond)), | ||
wantError: false, | ||
}, | ||
{ | ||
name: "Two timestamps in filename", | ||
filename: "dev.log.1731935033496.123.gz", | ||
wantTime: time.Unix(0, 1731935033496*int64(time.Millisecond)), | ||
wantError: false, | ||
}, | ||
{ | ||
name: "Invalid timestamp in filename", | ||
filename: "dev.log.invalidtimestamp.gz", | ||
wantTime: time.Time{}, | ||
wantError: true, | ||
}, | ||
{ | ||
name: "No timestamp in filename", | ||
filename: "dev.log.gz", | ||
wantTime: time.Time{}, | ||
wantError: true, | ||
}, | ||
{ | ||
name: "Old timestamp (short format) in filename", | ||
filename: "dev.log.123.gz", | ||
wantTime: time.Unix(0, 123*int64(time.Millisecond)), | ||
wantError: false, | ||
}, | ||
{ | ||
name: "Old timestamp (long format) in filename", | ||
filename: "dev.log.0000000000123.gz", | ||
wantTime: time.Unix(0, 123*int64(time.Millisecond)), | ||
wantError: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
tt := tt // create a new variable to hold the value of tt to avoid being overwritten by the next iteration (needed until Go 1.23) | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
gotTime, err := GetTimestampFromFileName(tt.filename) | ||
if tt.wantError { | ||
g.Expect(err).To(gomega.HaveOccurred()) | ||
} else { | ||
g.Expect(err).NotTo(gomega.HaveOccurred()) | ||
g.Expect(gotTime).To(gomega.Equal(tt.wantTime)) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func FuzzGetTimestampFromFileName(f *testing.F) { | ||
testcases := []string{ | ||
"dev.log.1731491904032.gz", | ||
"app.8ce1cc69-e1bb-4fe3-9613-e3eb1c5f5c4d.log.1731935033496.gz", | ||
"dev.log.invalidtimestamp.gz", | ||
"dev.log.gz", | ||
"dev.log.123456789012.gz", | ||
"dev.log.1234567890123456.gz", | ||
} | ||
|
||
for _, tc := range testcases { | ||
f.Add(tc) | ||
} | ||
|
||
f.Fuzz(func(t *testing.T, filename string) { | ||
_, _ = GetTimestampFromFileName(filename) | ||
}) | ||
} | ||
|
||
func TestGetUUIDFromFileName(t *testing.T) { | ||
t.Parallel() | ||
g := gomega.NewWithT(t) | ||
|
||
tests := []struct { | ||
name string | ||
filename string | ||
wantUUID string | ||
wantError bool | ||
}{ | ||
{ | ||
name: "Valid UUID in filename", | ||
filename: "app.8ce1cc69-e1bb-4fe3-9613-e3eb1c5f5c4d.log.1731935033496.gz", | ||
wantUUID: "8ce1cc69-e1bb-4fe3-9613-e3eb1c5f5c4d", | ||
wantError: false, | ||
}, | ||
{ | ||
name: "Valid UUID in regular filename", | ||
filename: "app.8ce1cc69-e1bb-4fe3-9613-e3eb1c5f5c4d.log.1731935033496", | ||
wantUUID: "8ce1cc69-e1bb-4fe3-9613-e3eb1c5f5c4d", | ||
wantError: false, | ||
}, | ||
{ | ||
name: "Valid UUID with timestamp", | ||
filename: "app.123e4567-e89b-12d3-a456-426614174000.log.1731935033496.gz", | ||
wantUUID: "123e4567-e89b-12d3-a456-426614174000", | ||
wantError: false, | ||
}, | ||
{ | ||
name: "No UUID in filename", | ||
filename: "dev.log.1731491904032.gz", | ||
wantUUID: "", | ||
wantError: true, | ||
}, | ||
{ | ||
name: "Invalid UUID in filename", | ||
filename: "app.invalid-uuid-string.log.1731935033496.gz", | ||
wantUUID: "", | ||
wantError: true, | ||
}, | ||
{ | ||
name: "UUID at the end of filename", | ||
filename: "app.log.1731935033496.8ce1cc69-e1bb-4fe3-9613-e3eb1c5f5c4d.gz", | ||
wantUUID: "8ce1cc69-e1bb-4fe3-9613-e3eb1c5f5c4d", | ||
wantError: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
tt := tt // create a new variable to hold the value of tt to avoid being overwritten by the next iteration (needed until Go 1.23) | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
gotUUID, err := GetUUIDFromFileName(tt.filename) | ||
if tt.wantError { | ||
g.Expect(err).To(gomega.HaveOccurred()) | ||
} else { | ||
g.Expect(err).NotTo(gomega.HaveOccurred()) | ||
g.Expect(gotUUID).To(gomega.Equal(tt.wantUUID)) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func FuzzGetUUIDFromFileName(f *testing.F) { | ||
testcases := []string{ | ||
"app.8ce1cc69-e1bb-4fe3-9613-e3eb1c5f5c4d.log.1731935033496.gz", | ||
"app.123e4567-e89b-12d3-a456-426614174000.log.1731935033496.gz", | ||
"dev.log.1731491904032.gz", | ||
"app.invalid-uuid-string.log.1731935033496.gz", | ||
"app.log.1731935033496.8ce1cc69-e1bb-4fe3-9613-e3eb1c5f5c4d.gz", | ||
} | ||
|
||
for _, tc := range testcases { | ||
f.Add(tc) | ||
} | ||
|
||
f.Fuzz(func(t *testing.T, filename string) { | ||
_, _ = GetUUIDFromFileName(filename) | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I see, we still use this func in another package, edgeveiew:
eve/pkg/edgeview/src/log-search.go
Line 152 in 2edd016
eve/pkg/edgeview/src/system.go
Line 163 in 2edd016
So, we should be careful here...