Code review comment: Since these are created in the constructor, how it will react to the dynamic option modification? (for example, when I remove or add a new metrics category, will it reevaluate requests timer etc?)

Code before review:
private RntbdMetricsV2(MeterRegistry registry, RntbdTransportClient client, RntbdEndpoint endpoint) {
Tags tags = Tags.of(endpoint.clientMetricTag());
this.metricCategories = client.getMetricCategories();
if (metricCategories.contains(MetricCategory.DirectRequests)) {
CosmosMeterOptions options = client
.getMeterOptions(CosmosMeterName.DIRECT_REQUEST_LATENCY);
if (options.isEnabled()) {
this.requests = Timer
.builder(options.getMeterName().toString())
.description("RNTBD request latency")
.maximumExpectedValue(Duration.ofSeconds(300))
.publishPercentiles(optionsAccessor.getPercentiles(options))
.publishPercentileHistogram(optionsAccessor.isHistogramPublishingEnabled(options))
.tags(getEffectiveTags(tags, options))
.register(registry);
} else {
this.requests = null;
}
options = client
.getMeterOptions(CosmosMeterName.DIRECT_REQUEST_LATENCY_FAILED);
if (options.isEnabled()) {
this.responseErrors = Timer
.builder(options.getMeterName().toString())
.description("RNTBD failed request latency")
.maximumExpectedValue(Duration.ofSeconds(300))
.publishPercentiles(optionsAccessor.getPercentiles(options))
.publishPercentileHistogram(optionsAccessor.isHistogramPublishingEnabled(options))
.tags(getEffectiveTags(tags, options))
.register(registry);
} else {
this.responseErrors = null;
}
options = client
.getMeterOptions(CosmosMeterName.DIRECT_REQUEST_LATENCY_SUCCESS);
if (options.isEnabled()) {
this.responseSuccesses = Timer
.builder(options.getMeterName().toString())
.description("RNTBD successful request latency")
.maximumExpectedValue(Duration.ofSeconds(300))
.publishPercentiles(optionsAccessor.getPercentiles(options))
.publishPercentileHistogram(optionsAccessor.isHistogramPublishingEnabled(options))
.tags(getEffectiveTags(tags, options))
.register(registry);
} else {
this.responseSuccesses = null;
}
options = client
.getMeterOptions(CosmosMeterName.DIRECT_REQUEST_CONCURRENT_COUNT);
if (options.isEnabled()) {
Gauge.builder(options.getMeterName().toString(), endpoint, RntbdEndpoint::concurrentRequests)
.description("RNTBD concurrent requests (executing or queued request count)")
.tags(getEffectiveTags(tags, options))
.register(registry);
}
options = client
.getMeterOptions(CosmosMeterName.DIRECT_REQUEST_QUEUED_COUNT);
if (options.isEnabled()) {
Gauge.builder(options.getMeterName().toString(), endpoint, RntbdEndpoint::requestQueueLength)
.description("RNTBD queued request count")
.tags(getEffectiveTags(tags, options))
.register(registry);
}
options = client
.getMeterOptions(CosmosMeterName.DIRECT_REQUEST_SIZE_REQUEST);
if (options.isEnabled()) {
this.requestSize = DistributionSummary.builder(options.getMeterName().toString())
.description("RNTBD request size (bytes)")
.baseUnit("bytes")
.tags(getEffectiveTags(tags, options))
.maximumExpectedValue(16_000_000d)
.publishPercentileHistogram(false)
.publishPercentiles()
.register(registry);
} else {
this.requestSize = null;
}
options = client
.getMeterOptions(CosmosMeterName.DIRECT_REQUEST_SIZE_RESPONSE);
if (options.isEnabled()) {
this.responseSize = DistributionSummary.builder(options.getMeterName().toString())
.description("RNTBD response size (bytes)")
.baseUnit("bytes")
.tags(getEffectiveTags(tags, options))
.maximumExpectedValue(16_000_000d)
.publishPercentileHistogram(false)
.publishPercentiles()
.register(registry);
} else {
this.responseSize = null;
}
} else {
this.requests = null;
this.responseErrors = null;
this.responseSuccesses = null;
this.requestSize = null;
this.responseSize= null;
}
if (metricCategories.contains(MetricCategory.DirectEndpoints)) {
CosmosMeterOptions options = client
.getMeterOptions(CosmosMeterName.DIRECT_ENDPOINTS_COUNT);
if (options.isEnabled()) {
Gauge.builder(options.getMeterName().toString(), client, RntbdTransportClient::endpointCount)
.description("RNTBD endpoint count")
.register(registry);
}
options = client
.getMeterOptions(CosmosMeterName.DIRECT_ENDPOINTS_EVICTED);
if (options.isEnabled()) {
FunctionCounter.builder(
options.getMeterName().toString(),
client,
RntbdTransportClient::endpointEvictionCount)
.description("RNTBD endpoint eviction count")
.register(registry);
}
}
if (metricCategories.contains(MetricCategory.DirectChannels)) {
CosmosMeterOptions options = client
.getMeterOptions(CosmosMeterName.DIRECT_CHANNELS_ACQUIRED_COUNT);
if (options.isEnabled()) {
FunctionCounter.builder(
options.getMeterName().toString(),
endpoint,
RntbdEndpoint::totalChannelsAcquiredMetric)
.description("RNTBD acquired channel count")
.tags(getEffectiveTags(tags, options))
.register(registry);
}
options = client
.getMeterOptions(CosmosMeterName.DIRECT_CHANNELS_CLOSED_COUNT);
if (options.isEnabled()) {
FunctionCounter.builder(
options.getMeterName().toString(),
endpoint,
RntbdEndpoint::totalChannelsClosedMetric)
.description("RNTBD closed channel count")
.tags(getEffectiveTags(tags, options))
.register(registry);
}
options = client
.getMeterOptions(CosmosMeterName.DIRECT_CHANNELS_AVAILABLE_COUNT);
if (options.isEnabled()) {
Gauge.builder(options.getMeterName().toString(), endpoint, RntbdEndpoint::channelsAvailableMetric)
.description("RNTBD available channel count")
.tags(getEffectiveTags(tags, options))
.register(registry);
}
}
}

Line under review: this.requests = null;

Code after addressing the review:
private RntbdMetricsV2(MeterRegistry registry, RntbdTransportClient client, RntbdEndpoint endpoint) {
this.tags = Tags.of(endpoint.clientMetricTag(), endpoint.tag());
this.client = client;
this.registry = registry;
if (this.client.getMetricCategories().contains(MetricCategory.DirectRequests)) {
CosmosMeterOptions options = client
.getMeterOptions(CosmosMetricName.DIRECT_REQUEST_CONCURRENT_COUNT);
if (options.isEnabled()) {
Gauge.builder(options.getMeterName().toString(), endpoint, RntbdEndpoint::concurrentRequests)
.description("RNTBD concurrent requests (executing or queued request count)")
.tags(getEffectiveTags(tags, options))
.register(registry);
}
options = client
.getMeterOptions(CosmosMetricName.DIRECT_REQUEST_QUEUED_COUNT);
if (options.isEnabled()) {
Gauge.builder(options.getMeterName().toString(), endpoint, RntbdEndpoint::requestQueueLength)
.description("RNTBD queued request count")
.tags(getEffectiveTags(tags, options))
.register(registry);
}
}
if (this.client.getMetricCategories().contains(MetricCategory.DirectEndpoints)) {
CosmosMeterOptions options = client
.getMeterOptions(CosmosMetricName.DIRECT_ENDPOINTS_COUNT);
if (options.isEnabled()) {
Gauge.builder(options.getMeterName().toString(), client, RntbdTransportClient::endpointCount)
.description("RNTBD endpoint count")
.register(registry);
}
options = client
.getMeterOptions(CosmosMetricName.DIRECT_ENDPOINTS_EVICTED);
if (options.isEnabled()) {
FunctionCounter.builder(
options.getMeterName().toString(),
client,
RntbdTransportClient::endpointEvictionCount)
.description("RNTBD endpoint eviction count")
.register(registry);
}
}
if (this.client.getMetricCategories().contains(MetricCategory.DirectChannels)) {
CosmosMeterOptions options = client
.getMeterOptions(CosmosMetricName.DIRECT_CHANNELS_ACQUIRED_COUNT);
if (options.isEnabled()) {
FunctionCounter.builder(
options.getMeterName().toString(),
endpoint,
RntbdEndpoint::totalChannelsAcquiredMetric)
.description("RNTBD acquired channel count")
.tags(getEffectiveTags(tags, options))
.register(registry);
}
options = client
.getMeterOptions(CosmosMetricName.DIRECT_CHANNELS_CLOSED_COUNT);
if (options.isEnabled()) {
FunctionCounter.builder(
options.getMeterName().toString(),
endpoint,
RntbdEndpoint::totalChannelsClosedMetric)
.description("RNTBD closed channel count")
.tags(getEffectiveTags(tags, options))
.register(registry);
}
options = client
.getMeterOptions(CosmosMetricName.DIRECT_CHANNELS_AVAILABLE_COUNT);
if (options.isEnabled()) {
Gauge.builder(options.getMeterName().toString(), endpoint, RntbdEndpoint::channelsAvailableMetric)
.description("RNTBD available channel count")
.tags(getEffectiveTags(tags, options))
.register(registry);
}
}
}