Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare RegistryFactory lazily to use the most-recently-assigned MetricsSettings (3.0) #3661

Merged
merged 3 commits into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,13 @@
* <ul>
* <li>The {@code create} methods create a new {@link RegistryFactory} according to the specified settings (or defaults)
* but keep no reference to such registry factories.</li>
* <li>The {@code getInstance} methods reuse a single {@code RegistryFactory}, initialized using the default
* {@link MetricsSettings} but changeable via {@link #getInstance(MetricsSettings)}.</li>
* <li>The {@code getInstance} methods reuse a single {@code RegistryFactory}. The instance, the creation of which is
* deferred until the first invocation of one of the {@code getInstance} methods, uses either the default
* metrics settings or the metrics settings passed to {@link #getInstance(MetricsSettings)} if that is the first
* {@code getInstance} method invoked.
* Subsequent calls to {@code getInstance(MetricsSettings)} update that same instance with the settings but do not
* create a new instance of the registry factory because previous callers might have saved references to the return
* values from previous {@code getInstance} invocations.</li>
* <li>The {@link RegistryFactory#getInstance(ComponentMetricsSettings)} method which metrics-capable components invoke,
* passing a {@code ComponentMetricsSettings} object to indicate what type of {@code RegistryFactory} they need to use,
* based on their component-specific metrics settings.
Expand All @@ -59,8 +64,11 @@ class RegistryFactoryManager {

private static final RegistryFactoryProvider NO_OP_FACTORY_PROVIDER = (metricsSettings) -> NoOpRegistryFactory.create();

// Instance managed and returned by the {@link getInstance} methods.
private static final RegistryFactory INSTANCE = create();
// Might be changed via getInstance(MetricsSettings).
private static MetricsSettings metricsSettings = MetricsSettings.create();

// Instance managed and returned by the {@link getInstance} methods. Use the latest-provided metrics settings.
private static final LazyValue<RegistryFactory> INSTANCE = LazyValue.create(() -> create(metricsSettings));

// If metrics-capable components ask for a no-op factory, reuse the same one.
private static final RegistryFactory NO_OP_INSTANCE = NoOpRegistryFactory.create();
Expand Down Expand Up @@ -94,17 +102,19 @@ static RegistryFactory create(Config config) {
}

static RegistryFactory getInstance() {
return INSTANCE;
return INSTANCE.get();
}

static synchronized RegistryFactory getInstance(MetricsSettings metricsSettings) {
INSTANCE.update(metricsSettings);
return INSTANCE;
RegistryFactoryManager.metricsSettings = metricsSettings;
RegistryFactory result = INSTANCE.get();
result.update(metricsSettings);
return result;
}

static synchronized RegistryFactory getInstance(ComponentMetricsSettings componentMetricsSettings) {
return componentMetricsSettings.isEnabled()
? INSTANCE
? INSTANCE.get()
: NO_OP_INSTANCE;
}

Expand Down
13 changes: 13 additions & 0 deletions metrics/metrics/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
<excludes>
<exclude>**/TestServerWithKeyPerformanceIndicatorMetrics.java</exclude>
<exclude>**/TestExponentiallyDecayingReservoir.java</exclude>
<exclude>**/TestDisabledEntirely.java</exclude>
</excludes>
</configuration>
</execution>
Expand Down Expand Up @@ -159,6 +160,18 @@
</includes>
</configuration>
</execution>
<execution>
<!-- Run in separate invocation to make sure the RegistryFactory is initialized correctly. -->
<id>check-metrics-entirely-disabled</id>
<goals>
<goal>test</goal>
</goals>
<configuration>
<includes>
<include>**/TestDisabledEntirely.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.helidon.metrics;

import io.helidon.metrics.api.MetricsSettings;

import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.not;

class TestDisabledEntirely {

@Test
void testDisabledCompletely() {
MetricsSettings metricsSettings = MetricsSettings.builder()
.enabled(false)
.build();

io.helidon.metrics.api.RegistryFactory registryFactory =
io.helidon.metrics.api.RegistryFactory.getInstance(metricsSettings);

assertThat("Disabled RegistryFactory impl class",
registryFactory,
not(instanceOf(io.helidon.metrics.RegistryFactory.class)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import io.helidon.common.context.Contexts;
import io.helidon.config.Config;
import io.helidon.config.ConfigValue;
import io.helidon.config.mp.MpConfig;
import io.helidon.metrics.api.MetricsSettings;
import io.helidon.metrics.api.RegistryFactory;
import io.helidon.metrics.serviceapi.MetricsSupport;
Expand Down Expand Up @@ -621,20 +622,17 @@ protected Routing.Builder registerService(@Observes @Priority(LIBRARY_BEFORE + 1
Object adv,
BeanManager bm,
ServerCdiExtension server) {
Set<String> vendorMetricsAdded = new HashSet<>();
Config config = ((Config) ConfigProvider.getConfig()).get("metrics");

// Update the registry factory with the runtime config.
RegistryFactory.getInstance(MetricsSettings.create(config));
Routing.Builder defaultRouting = super.registerService(adv, bm, server);
MetricsSupport metricsSupport = serviceSupport();

registerMetricsForAnnotatedSites();
registerProducers(bm);

Routing.Builder defaultRouting = super.registerService(adv, bm, server);
MetricsSupport metricsSupport = serviceSupport();

Set<String> vendorMetricsAdded = new HashSet<>();
vendorMetricsAdded.add("@default");

Config config = MpConfig.toHelidonConfig(ConfigProvider.getConfig()).get(MetricsSettings.Builder.METRICS_CONFIG_KEY);

// now we may have additional sockets we want to add vendor metrics to
config.get("vendor-metrics-routings")
.asList(String.class)
Expand Down
2 changes: 1 addition & 1 deletion microprofile/metrics/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

requires transitive microprofile.config.api;
requires microprofile.metrics.api;

requires io.helidon.config.mp;

exports io.helidon.microprofile.metrics;

Expand Down