Skip to content

Redis health checks need distinct OpenMetrics labels for multiple clients #743

Description

@pfcodes

Problem

health_check.contrib.redis.Redis can be configured multiple times with different client_factory values, which is useful when a Django app has separate Redis clients for cache, channels, Celery, Constance, etc.

However, the Redis check does not expose an alias or any other repr-enabled identifying field. The OpenMetrics renderer builds metric labels from result.check.labels, and HealthCheck.labels only includes dataclass fields with repr=True. Because Redis.client and Redis.client_factory are repr=False, multiple Redis checks all emit the same labels:

django_health_check_status{check="Redis"} 1
django_health_check_status{check="Redis"} 1
django_health_check_status{check="Redis"} 1

That makes the individual Redis checks indistinguishable in Prometheus and can create duplicate samples for the same metric/label set in a single scrape.

Example

checks = [
    (
        "health_check.contrib.redis.Redis",
        {"client_factory": lambda: RedisClient.from_url(settings.CHANNELS_REDIS_URL)},
    ),
    (
        "health_check.contrib.redis.Redis",
        {"client_factory": lambda: RedisClient.from_url(settings.CONSTANCE_REDIS_URL)},
    ),
    (
        "health_check.contrib.redis.Redis",
        {"client_factory": lambda: RedisClient.from_url(settings.CELERY_BROKER_URL)},
    ),
]

Expected behavior

The Redis check should support a stable human-readable label, matching the existing pattern used by checks such as Cache, Database, and Storage, which expose an alias field.

For example:

(
    "health_check.contrib.redis.Redis",
    {
        "alias": "channels",
        "client_factory": lambda: RedisClient.from_url(settings.CHANNELS_REDIS_URL),
    },
)

Would emit distinct OpenMetrics labels:

django_health_check_status{check="Redis",alias="channels"} 1

Proposed fix

Add an optional alias: str | None = None field to Redis. This should be backwards-compatible because existing callers can omit it, while apps with multiple Redis checks can opt into distinct labels.

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions