Skip to content

Conversation

@codelipenghui
Copy link
Contributor

@codelipenghui codelipenghui commented Aug 20, 2025

Summary

Adds OpenTelemetry metrics to expose the status of the producer memory buffer defined by producerMemoryLimit in the Java client. This provides visibility into memory usage and helps with monitoring and alerting on client-side memory consumption.

Motivation

Currently, there's no way to monitor the memory buffer usage of Pulsar Java clients. Users cannot:

  • Track how much memory is being consumed by pending messages
  • Set up alerts when memory usage approaches the configured limit
  • Debug memory-related issues or performance bottlenecks
  • Monitor memory efficiency across different client configurations

Changes

New Metrics

Two new OpenTelemetry metrics are added:

  1. pulsar.client.memory.buffer.usage (bytes)

    • Current memory buffer usage by the client
    • Updates in real-time as messages are queued and sent
  2. pulsar.client.memory.buffer.limit (bytes)

    • Memory buffer limit configured for the client
    • Constant value based on client configuration

Key Features

  • Conditional creation: Metrics are only created when memory limiting is enabled (memoryLimitBytes > 0)
  • Zero overhead: When memory limiting is disabled, no metrics are registered
  • Observable metrics: Automatically report current values via callbacks
  • Existing patterns: Follows established OpenTelemetry infrastructure in the codebase

Implementation Details

  • New ObservableUpDownCounter class that extends existing metrics infrastructure
  • New MemoryBufferStats class manages the observable metrics
  • Extended InstrumentProvider with observable counter support
  • Added memoryLimit() method to MemoryLimitController for metric access
  • Conditional metrics creation in PulsarClientImpl

Usage Example

PulsarClient client = PulsarClient.builder()
    .serviceUrl("pulsar://localhost:6650")
    .memoryLimit(64, SizeUnit.MEGA_BYTES)
    .openTelemetry(openTelemetry) // Your OpenTelemetry instance
    .build();

The metrics will be available as:

pulsar.client.memory.buffer.usage = 1048576  # Current usage in bytes
pulsar.client.memory.buffer.limit = 67108864 # 64MB limit in bytes

Testing

  • Unit tests: MemoryBufferStatsTest with InMemoryMetricReader to verify metric values
  • Integration tests: ClientMetricsTest with real broker to test end-to-end behavior
  • Edge cases: Tests for both enabled and disabled memory limiting scenarios

Verifying this change

  • Unit tests pass
  • Integration tests pass
  • Checkstyle validation passes
  • No performance regression when feature is disabled
  • Backward compatibility maintained

Backward Compatibility

  • Fully backward compatible - no breaking changes
  • Metrics are only created when both OpenTelemetry and memory limiting are configured
  • No performance impact when OpenTelemetry is not configured

Documentation

  • doc
  • doc-required
  • doc-not-needed
  • doc-complete

This commit introduces new OpenTelemetry metrics to monitor the memory
buffer usage in Pulsar Java clients, providing visibility into producer
memory consumption.

New metrics:
- pulsar.client.memory.buffer.usage: Current memory usage in bytes
- pulsar.client.memory.buffer.limit: Configured memory limit in bytes

Key features:
- Metrics are only created when memory limiting is enabled (memoryLimitBytes > 0)
- Zero overhead when memory limiting is disabled
- Follows existing OpenTelemetry patterns in the codebase
- Observable metrics that automatically report current values

Implementation:
- Add ObservableUpDownCounter class for callback-based metrics
- Add MemoryBufferStats to manage memory buffer metrics
- Extend InstrumentProvider with observable counter support
- Add memoryLimit() method to MemoryLimitController for metric access
- Conditional metrics creation in PulsarClientImpl

Testing:
- Comprehensive unit tests with InMemoryMetricReader
- Integration tests covering enabled/disabled scenarios
- End-to-end testing with real broker interactions

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@codelipenghui codelipenghui self-assigned this Aug 20, 2025
@codelipenghui codelipenghui added this to the 4.1.0 milestone Aug 20, 2025
@codelipenghui codelipenghui added area/metrics area/client type/enhancement The enhancements for the existing features or docs. e.g. reduce memory usage of the delayed messages labels Aug 20, 2025
@apache apache deleted a comment from github-actions bot Aug 20, 2025
@codelipenghui codelipenghui changed the title Add OpenTelemetry metrics for client memory buffer usage [improve][client] Add OpenTelemetry metrics for client memory buffer usage Aug 20, 2025
@github-actions github-actions bot added doc-required Your PR changes impact docs and you will update later. and removed doc-label-missing labels Aug 20, 2025
@codelipenghui
Copy link
Contributor Author

Documentation is added by apache/pulsar-site#1032

@github-actions github-actions bot added doc-complete Your PR changes impact docs and the related docs have been already added. and removed doc-required Your PR changes impact docs and you will update later. labels Aug 20, 2025
@codelipenghui codelipenghui merged commit a66e806 into apache:master Aug 20, 2025
71 of 82 checks passed
@codelipenghui codelipenghui deleted the feature/add-client-memory-buffer-metrics branch August 20, 2025 17:30
codelipenghui added a commit that referenced this pull request Sep 10, 2025
manas-ctds pushed a commit to datastax/pulsar that referenced this pull request Sep 12, 2025
…usage (apache#24647)

(cherry picked from commit a66e806)
(cherry picked from commit 02a7bbc)
srinath-ctds pushed a commit to datastax/pulsar that referenced this pull request Sep 12, 2025
…usage (apache#24647)

(cherry picked from commit a66e806)
(cherry picked from commit 02a7bbc)
KannarFr pushed a commit to CleverCloud/pulsar that referenced this pull request Sep 22, 2025
walkinggo pushed a commit to walkinggo/pulsar that referenced this pull request Oct 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/client area/metrics cherry-picked/branch-4.0 doc-complete Your PR changes impact docs and the related docs have been already added. ready-to-test type/enhancement The enhancements for the existing features or docs. e.g. reduce memory usage of the delayed messages

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants