fleet/server/contexts/ctxerr/statistics.go
Scott Gress 59f96651b6
Update to Go 1.24.1 (#27506)
For #26713 

# Details

This PR updates Fleet and its related tools and binaries to use Go
version 1.24.1.

Scanning through the changelog, I didn't see anything relevant to Fleet
that requires action. The only possible breaking change I spotted was:

> As [announced](https://tip.golang.org/doc/go1.23#linux) in the Go 1.23
release notes, Go 1.24 requires Linux kernel version 3.2 or later.

Linux kernel 3.2 was released in January of 2012, so I think we can
commit to dropping support for earlier kernel versions.

The new [tools directive](https://tip.golang.org/doc/go1.24#tools) is
interesting as it means we can move away from using `tools.go` files,
but it's not a required update.

# Checklist for submitter

If some of the following don't apply, delete the relevant line.

<!-- Note that API documentation changes are now addressed by the
product design team. -->

- [X] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
- [x] Manual QA for all new/changed functionality
- For Orbit and Fleet Desktop changes:
- [X] Make sure fleetd is compatible with the latest released version of
Fleet
   - [x] Orbit runs on macOS  , Linux   and Windows. 
- [x] Manual QA must be performed in the three main OSs, macOS ,
Windows and Linux .
2025-03-31 11:14:09 -05:00

90 lines
2.3 KiB
Go

package ctxerr
import (
"context"
"encoding/json"
"github.com/fleetdm/fleet/v4/server/fleet"
)
type ErrorAgg struct {
Count int `json:"count"`
Loc []string `json:"loc"`
Metadata json.RawMessage `json:"metadata,omitempty"`
}
// Aggregate retrieves all errors in the store and returns an aggregated,
// json-formatted summary containing:
// - The number of occurrences of each error
// - A reduced stack trace used for debugging the error
// - Additional metadata present for vital errors
func Aggregate(ctx context.Context) (json.RawMessage, error) {
const maxTraceLen = 3
empty := json.RawMessage("[]")
storedErrs, err := Retrieve(ctx)
if err != nil {
return empty, Wrap(ctx, err, "retrieve on aggregation")
}
aggs := make([]ErrorAgg, len(storedErrs))
for i, stored := range storedErrs {
var ferr []fleetErrorJSON
if err = json.Unmarshal(stored.Chain, &ferr); err != nil {
return empty, Wrap(ctx, err, "unmarshal on aggregation")
}
stack := aggregateStack(ferr, maxTraceLen)
meta := getVitalMetadata(ferr)
aggs[i] = ErrorAgg{stored.Count, stack, meta}
}
return json.Marshal(aggs)
}
// aggregateStack creates a single stack trace by joining all the stack traces in
// an error chain
func aggregateStack(chain []fleetErrorJSON, maxStack int) []string {
stack := make([]string, maxStack)
stackIdx := 0
out:
for _, e := range chain {
for _, m := range e.Stack {
if stackIdx >= maxStack {
break out
}
stack[stackIdx] = m
stackIdx++
}
}
return stack[:stackIdx]
}
func getVitalMetadata(chain []fleetErrorJSON) json.RawMessage {
for _, e := range chain {
if len(e.Data) > 0 {
// Currently, only vital fleetd errors contain metadata.
// Note: vital errors should not contain any sensitive info
var fleetdErr fleet.FleetdError
var err error
if err = json.Unmarshal(e.Data, &fleetdErr); err != nil || !fleetdErr.Vital {
continue
}
export := map[string]interface{}{
"error_source": fleetdErr.ErrorSource,
"error_source_version": fleetdErr.ErrorSourceVersion,
"error_message": fleetdErr.ErrorMessage,
"error_additional_info": fleetdErr.ErrorAdditionalInfo,
}
var meta json.RawMessage
if meta, err = json.Marshal(export); err != nil {
return nil
}
return meta
}
}
return nil
}