Clean up error handling, add uptime to analytics

This commit is contained in:
Matthew Kilgore
2025-07-12 11:48:55 -04:00
parent a73d5887c7
commit e5ba3bb10e
2 changed files with 10 additions and 6 deletions

View File

@@ -404,11 +404,11 @@ func run(cfg *config.Config) error {
if cfg.Options.AllowAnalytics {
runner.AddPlugin(NewTask("send-analytics", time.Duration(24)*time.Hour, func(ctx context.Context) {
log.Debug().Msg("running send analytics")
analytics.Send(version, build())
err := analytics.Send(version, build())
if err != nil {
log.Error().
Err(err).
Msg("failed to send analytics")
Msg("failed to send scheduled analytics")
}
}))
}

View File

@@ -11,6 +11,8 @@ import (
"github.com/rs/zerolog/log"
)
var startTime = time.Now()
type Data struct {
Domain string `json:"domain"`
Name string `json:"name"`
@@ -18,7 +20,7 @@ type Data struct {
Props map[string]interface{} `json:"props"`
}
func Send(version, buildInfo string) {
func Send(version, buildInfo string) error {
hostData, _ := host.Info()
analytics := Data{
Domain: "homebox.software",
@@ -32,18 +34,19 @@ func Send(version, buildInfo string) {
"platform_version": hostData.PlatformVersion,
"kernel_arch": hostData.KernelArch,
"virt_type": hostData.VirtualizationSystem,
"uptime_sec": time.Since(startTime).Seconds(),
},
}
jsonBody, err := json.Marshal(analytics)
if err != nil {
log.Error().Err(err).Msg("failed to marshal analytics data")
return
return err
}
bodyReader := bytes.NewReader(jsonBody)
req, err := http.NewRequest("POST", "https://a.sysadmins.zone/api/event", bodyReader)
if err != nil {
log.Error().Err(err).Msg("failed to create analytics request")
return
return err
}
req.Header.Set("Content-Type", "application/json")
@@ -56,7 +59,7 @@ func Send(version, buildInfo string) {
res, err := client.Do(req)
if err != nil {
log.Error().Err(err).Msg("failed to send analytics request")
return
return err
}
defer func() {
@@ -65,4 +68,5 @@ func Send(version, buildInfo string) {
log.Error().Err(err).Msg("failed to close response body")
}
}()
return nil
}