Skip to content

feat(client): add process-level and per-client custom attributes to OTel metrics - #3573

Merged
PavelPashov merged 3 commits into
redis:masterfrom
PavelPashov:feat/otel-custom-attributes
Nov 13, 2026
Merged

PavelPashov merged 3 commits into
redis:masterfrom
PavelPashov:feat/otel-custom-attributes

Conversation

@PavelPashov

@PavelPashov PavelPashov commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Description

This pull request adds custom attributes to node-redis OpenTelemetry metrics, so users can label metrics with their own values, such as the service, team or environment. Users who run several clients in one process can also tell those clients apart in dashboards. Before this, the only per-client attribute was a random pool name, and command metrics didn't carry it.

Attributes can be set at two levels:

  • Process: customAttributes in OpenTelemetry.init({ metrics }). Applies to every metric from every client in the process.
  • Client: metrics: { customAttributes } in the client options. Applies to every metric that client emits. Pools pass it through their client options, clusters through defaults, and sentinels through nodeClientOptions and sentinelClientOptions, the same way other per-client options work.
OpenTelemetry.init({ metrics: { enabled: true, customAttributes: { env: 'production' } } });

const orders = createClient({ metrics: { customAttributes: { app: 'orders' } } });
const billing = createCluster({
  rootNodes,
  defaults: { metrics: { customAttributes: { app: 'billing' } } },
});

When a key is set more than once, the most specific value wins: client attributes, then process attributes, then the attributes node-redis sets. Two clients in one process never share or mix attributes.


Checklist

  • Does npm test pass with this change (including linting)?
  • Is the new or changed code fully tested?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?

Note

Medium Risk
Touches all OTel metric attribute paths and close/unregister ordering; misuse of high-cardinality or built-in attribute keys can skew dashboards or backend cost.

Overview
Adds custom OpenTelemetry metric attributes at two levels: process-wide via OpenTelemetry.init({ metrics: { customAttributes } }) and per client via metrics.customAttributes on client options (clusters/sentinels use the same patterns as other per-client settings). A new withCustomAttributes helper merges labels onto every instrument—client overrides process, which overrides built-ins—so multi-client processes can separate metrics without cross-mixing.

Client-side cache eviction/request events now carry clientId through invalidation and capacity paths so CSC metrics stay attributed to the right client. close() defers metrics registry unregister until after the connection-closed event is published, preserving per-client labels on the closed-connection metric.

Documentation, the otel-metrics example, and broad unit/e2e tests cover merge precedence, overriding built-in keys, isolation across clients/clusters, and real cache evictions.

Reviewed by Cursor Bugbot for commit 81a5af5. Bugbot is set up for automated code reviews on this repo. Configure here.

@PavelPashov
PavelPashov force-pushed the feat/otel-custom-attributes branch 2 times, most recently from f992653 to 0343a43 Compare November 2, 2026 16:29
@PavelPashov
PavelPashov marked this pull request as ready for review November 3, 2026 11:10
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-09T14:38:15.712470Z 81a5af5 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f4c4bdc61c

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

@PavelPashov PavelPashov changed the title feat(client): add customAttributes to OTel metrics config feat(client): add process-level and per-client custom attributes to OTel metrics Nov 3, 2026
@PavelPashov
PavelPashov force-pushed the feat/otel-custom-attributes branch from 62c09e4 to e25b553 Compare November 3, 2026 16:19
PavelPashov and others added 2 commits November 3, 2026 18:20
Let callers set metrics.customAttributes in OpenTelemetry.init. Every
metric data point from every client in the process carries them, and
a custom key replaces a built-in attribute with the same key.

withCustomAttributes merges them at each recording site. With no
custom attributes set, the call site's object passes through as is.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Let callers set metrics.customAttributes in the client options. Every
metric that client emits carries them, over the process-level ones,
and two clients in one process never share or mix them. Pools pass
them through their client options, clusters through defaults, and
sentinels through nodeClientOptions and sentinelClientOptions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@PavelPashov
PavelPashov force-pushed the feat/otel-custom-attributes branch 3 times, most recently from 69f47c6 to 69a416d Compare November 9, 2026 14:16
Unregister the client from the metrics registry only after destroySocket()
publishes CONNECTION_CLOSED, so close() still resolves the client's
attributes on redis.client.connection.closed.

Pass the client ID through the client-side cache, so eviction events
(full, ttl, invalidation) carry the client's attributes on
redis.client.csc.evictions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@PavelPashov
PavelPashov force-pushed the feat/otel-custom-attributes branch from 69a416d to 81a5af5 Compare November 9, 2026 14:34

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 81a5af5. Configure here.

Comment thread packages/client/lib/client/index.ts Resolved
Comment thread packages/client/lib/client/index.ts Resolved
@PavelPashov
PavelPashov merged commit 75cf9ab into redis:master Nov 13, 2026
15 checks passed
@PavelPashov
PavelPashov deleted the feat/otel-custom-attributes branch November 13, 2026 10:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants