crowdsecurity/crowdsec · error

getting bouncer usage metrics by name %s: %w

Error message

getting bouncer usage metrics by name %s: %w

What it means

GetBouncerUsageMetricsByName fetches un-pushed metric rows whose GeneratedBy equals the given bouncer name. This error wraps a failing SELECT; it is not raised when the bouncer simply has no metrics (empty slice, nil error).

Source

Thrown at pkg/database/metrics.go:58

	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)
	}

	return metrics, nil
}

func (c *Client) MarkUsageMetricsAsSent(ctx context.Context, ids []int) error {
	_, err := c.Ent.Metric.Update().
		Where(metric.IDIn(ids...)).
		SetPushedAt(time.Now().UTC()).
		Save(ctx)
	if err != nil {
		c.Log.Warningf("MarkUsageMetricsAsSent: %s", err)
		return fmt.Errorf("marking usage metrics as sent: %w", err)
	}

	return nil
}

View on GitHub (pinned to 909b515798)

Solutions

  1. Read the Warningf log 'GetBouncerUsageMetricsByName: <err>' for the underlying cause.
  2. Fix DB file ownership/permissions or move the DB off NFS.
  3. Run an integrity check and restore the DB if corrupted.
  4. Retry; transient lock contention usually resolves on the next attempt.

Example fix

// before
m, err := c.GetBouncerUsageMetricsByName(ctx, name)
if err != nil { return err }
// after: treat empty as normal, error as fatal
if err != nil { return fmt.Errorf("bouncer metrics: %w", err) }
if len(m) == 0 { return nil }
Defensive patterns

Strategy: try-catch

Try / catch

m, err := client.GetBouncerUsageMetricsByName(ctx, bouncerName)
if err != nil {
    return fmt.Errorf("bouncer metrics lookup failed: %w", err)
}
if len(m) == 0 { /* bouncer simply has no unpushed metrics */ }

Prevention

When it happens

Trigger: Calling GetBouncerUsageMetricsByName when the ent query fails: DB locked by a concurrent writer, unreadable/corrupt SQLite file, disk I/O errors, or canceled context.

Common situations: cscli/crowdsec contention on the SQLite DB; corrupted DB after power loss; wrong permissions on /var/lib/crowdsec/crowdsec.db after a restore.

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/f2621f96815347fd. Report an issue: GitHub.