Skip to content

Conversation

@rorawatlinkedin
Copy link

@rorawatlinkedin rorawatlinkedin commented Dec 1, 2025

This PR adds OpenTelemetry (OTel) metrics instrumentation to the LoadBalancerState sensor.

Changes New Interface: Added LoadBalancerStateOtelMetricsProvider interface for collecting LoadBalancerState metrics via OpenTelemetry

recordClusterCount(String clientName, int regularClusterCount, long symlinkClusterCount)
recordServiceCount(String clientName, int serviceCount)
Purpose: library-side abstraction so host apps can plug an OTel implementation.
No-op Implementation: Added NoOpLoadBalancerStateOtelMetricsProvider as default when metrics are disabled.

JMX bean changes

SimpleLoadBalancerStateJmx now accepts a LoadBalancerStateOtelMetricsProvider and exposes setClientName/getClientName.
The bean calls provider.recordClusterCount(...) from both getClusterCount() and getSymlinkClusterCount(), and provider.recordServiceCount(...) from getServiceCount().
Integration

D2ClientConfig: added field to carry LoadBalancerStateOtelMetricsProvider (defaults to NoOp).
D2ClientBuilder: added setLoadBalancerStateOtelMetricsProvider(...) to inject a provider.
D2ClientJmxManager: accepts the provider, constructs provider-backed SimpleLoadBalancerStateJmx, sets client name before registration, and registers the bean.
Updated load-balancer factory call sites to forward the provider into D2ClientJmxManager.

Metrics Tracked

Gauges:
regular cluster count (recordClusterCount)
symlink cluster count (recordClusterCount)
service count (recordServiceCount)
All callbacks include clientName for attribution.

Testing

Added TestSimpleLoadBalancerStateJmx to verify:
default no-op provider is safe,
mock provider received expected callbacks,
client name propagation works.
Updated test fixtures to match API types (Set for clusters).
Backward compatibility

Fully backward compatible: defaults to NoOp provider when not configured; existing behavior unchanged unless a real provider is supplied.

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.

1 participant