crowdsecurity/crowdsec · error

getting LP usage metrics by origin %s: %w

Error message

getting LP usage metrics by origin %s: %w

What it means

GetLPUsageMetricsByMachineID queries metric rows filtered by machine ID (and optionally not-yet-pushed). It returns this wrapped error when the SELECT itself fails — not when there are simply no rows (that returns an empty slice). The %w carries the raw database error for errors.Is/As inspection.

Source

Thrown at pkg/database/metrics.go:42

	return metric, nil
}

func (c *Client) GetLPUsageMetricsByMachineID(ctx context.Context, machineId string, toSend bool) ([]*ent.Metric, error) {
	query := c.Ent.Metric.Query().
		Where(
			metric.GeneratedTypeEQ(metric.GeneratedTypeLP),
			metric.GeneratedByEQ(machineId),
		)

	if toSend {
		query = query.Where(metric.PushedAtIsNil())
	}

	metrics, err := query.All(ctx)
	if err != nil {
		c.Log.Warningf("GetLPUsageMetricsByOrigin: %s", err)
		return nil, fmt.Errorf("getting LP usage metrics by origin %s: %w", machineId, err)
	}

	return metrics, nil
}

func (c *Client) GetBouncerUsageMetricsByName(ctx context.Context, bouncerName string) ([]*ent.Metric, error) {
	metrics, err := c.Ent.Metric.Query().
		Where(
			metric.GeneratedTypeEQ(metric.GeneratedTypeRC),
			metric.GeneratedByEQ(bouncerName),
			metric.PushedAtIsNil(),
		).
		All(ctx)
	if err != nil {
		c.Log.Warningf("GetBouncerUsageMetricsByName: %s", err)
		return nil, fmt.Errorf("getting bouncer usage metrics by name %s: %w", bouncerName, err)
	}

View on GitHub (pinned to 909b515798)

Solutions

  1. Check the Warningf log 'GetLPUsageMetricsByOrigin: <err>' for the underlying query error.
  2. Fix filesystem permissions on the DB file so the crowdsec user can read it.
  3. Verify DB integrity with sqlite3 'PRAGMA integrity_check' and restore from backup if corrupted.
  4. Retry on transient errors; an empty result is returned as an empty slice with nil error, so no special handling is needed for 'no rows'.

Example fix

// caller guard before/after
// before: assuming error means 'no rows'
metrics, err := client.GetLPUsageMetricsByMachineID(ctx, machineID, true)
if err != nil { return err } // it's a real query failure, not empty data
// after: distinguish empty vs failure
if err != nil { return fmt.Errorf("usage metrics query: %w", err) }
if len(metrics) == 0 { /* nothing to push */ }
Defensive patterns

Strategy: try-catch

Try / catch

metrics, err := client.GetLPUsageMetricsByMachineID(ctx, machineID, true)
if err != nil {
    return fmt.Errorf("querying LP usage metrics: %w", err) // real query failure
}
// len(metrics)==0 means no rows, not an error

Prevention

When it happens

Trigger: Calling GetLPUsageMetricsByMachineID when the ent query fails: DB file unreadable/locked, disk I/O error, corrupted SQLite DB, or context canceled mid-query.

Common situations: CrowdSec data directory owned by the wrong user (permission denied on the sqlite file); NFS-mounted DB with locking problems; DB corrupted after an unclean shutdown.

Understand the failure class

Background: Database query failed: Internal Server Error 500s wrapping SQL, Prisma, and connection failures — what to check first — this error's family across 16 libraries.

Related errors


AI-assisted analysis of crowdsecurity/crowdsec@909b515798 (2026-09-06). Data as JSON: /api/errors/7a51b94e98b8ea19. Report an issue: GitHub.