Skip to content

Commit

Permalink
fix(telemetry): record silent error (#1585)
Browse files Browse the repository at this point in the history
Signed-off-by: Salim Afiune Maya <[email protected]>
  • Loading branch information
afiune authored Mar 7, 2024
1 parent ee03ba2 commit fdabe92
Showing 1 changed file with 33 additions and 17 deletions.
50 changes: 33 additions & 17 deletions cli/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func dailyComponentUpdateAvailable(componentName string) (bool, error) {
err := versionCache.StoreCache(cacheFile)

if err != nil {
cli.Event.Error = err.Error()
cli.Event.FeatureData = map[string]interface{}{"silent_error": err.Error()}
return false, err
}

Expand All @@ -193,12 +193,40 @@ func dailyComponentUpdateAvailable(componentName string) (bool, error) {
}
}

func firstTimeVersionCheck(dir, file string) error {
var (
err error
currentVersion *lwupdater.Version
)
defer func() {
cli.Event.Feature = featDailyVerCheck
if err != nil {
cli.Event.FeatureData = map[string]interface{}{"silent_error": err.Error()}
}
cli.SendHoneyvent()
}()

if err = os.MkdirAll(dir, 0755); err != nil {
return err
}

currentVersion, err = versionCheck()
if err != nil {
return err
}

cli.Log.Debugw("storing version cache", "content", currentVersion)
err = currentVersion.StoreCache(file)
return err
}

// dailyVersionCheck will execute a version check on a daily basis, the function uses
// the file ~/.config/lacework/version_cache to track the last check time
func dailyVersionCheck() error {
if cli.JSONOutput() || !isCheckEnabled() {
return nil
}

cacheDir, err := cache.CacheDir()
if err != nil {
return err
Expand All @@ -207,19 +235,7 @@ func dailyVersionCheck() error {
cacheFile := path.Join(cacheDir, VersionCacheFile)
if !file.FileExists(cacheFile) {
// first time running the daily version check, create directory
if err := os.MkdirAll(cacheDir, 0755); err != nil {
return err
}

currentVersion, err := versionCheck()
if err != nil {
return err
}

cli.Log.Debugw("storing version cache", "content", currentVersion)
if err := currentVersion.StoreCache(cacheFile); err != nil {
return err
}
return firstTimeVersionCheck(cacheDir, cacheFile)
}

cli.Log.Debugw("verifying cached version", "cache_file", cacheFile)
Expand All @@ -244,17 +260,17 @@ func dailyVersionCheck() error {
cli.Log.Debugw("storing new version cache", "content", versionCache)
err := versionCache.StoreCache(cacheFile)
if err != nil {
cli.Event.Error = err.Error()
cli.Event.FeatureData = map[string]interface{}{"silent_error": err.Error()}
return err
}

lwv, err := versionCheck()
cli.Event.DurationMs = time.Since(nowTime).Milliseconds()
if err != nil {
cli.Event.Error = err.Error()
cli.Event.FeatureData = map[string]interface{}{"silent_error": err.Error()}
return err
}

cli.Event.DurationMs = time.Since(nowTime).Milliseconds()
cli.Event.FeatureData = lwv
return nil
}
Expand Down

0 comments on commit fdabe92

Please sign in to comment.