-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12448 from mqs24d/feature/micrometer-azuremonitor
Integrate AzureMonitorRegistry into Micrometer Extension
- Loading branch information
Showing
11 changed files
with
253 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
.../src/main/java/io/quarkus/micrometer/deployment/export/AzureMonitorRegistryProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package io.quarkus.micrometer.deployment.export; | ||
|
||
import java.util.function.BooleanSupplier; | ||
|
||
import org.jboss.logging.Logger; | ||
|
||
import io.quarkus.arc.deployment.AdditionalBeanBuildItem; | ||
import io.quarkus.deployment.annotations.BuildProducer; | ||
import io.quarkus.deployment.annotations.BuildStep; | ||
import io.quarkus.micrometer.deployment.MicrometerRegistryProviderBuildItem; | ||
import io.quarkus.micrometer.runtime.MicrometerRecorder; | ||
import io.quarkus.micrometer.runtime.config.MicrometerConfig; | ||
import io.quarkus.micrometer.runtime.export.AzureMonitorMeterRegistryProvider; | ||
|
||
/** | ||
* Add support for the Azure Monitor Meter Registry. Note that the registry may not | ||
* be available at deployment time for some projects: Avoid direct class | ||
* references. | ||
*/ | ||
public class AzureMonitorRegistryProcessor { | ||
private static final Logger log = Logger.getLogger(AzureMonitorRegistryProcessor.class); | ||
|
||
static final String REGISTRY_CLASS_NAME = "io.micrometer.azuremonitor.AzureMonitorMeterRegistry"; | ||
static final Class<?> REGISTRY_CLASS = MicrometerRecorder.getClassForName(REGISTRY_CLASS_NAME); | ||
|
||
public static class AzureMonitorEnabled implements BooleanSupplier { | ||
MicrometerConfig mConfig; | ||
|
||
@Override | ||
public boolean getAsBoolean() { | ||
return REGISTRY_CLASS != null && mConfig.checkRegistryEnabledWithDefault(mConfig.export.azuremonitor); | ||
} | ||
} | ||
|
||
@BuildStep(onlyIf = AzureMonitorEnabled.class) | ||
MicrometerRegistryProviderBuildItem createAzureMonitorRegistry(BuildProducer<AdditionalBeanBuildItem> additionalBeans) { | ||
|
||
// Add the AzureMonitor Registry Producer | ||
additionalBeans.produce( | ||
AdditionalBeanBuildItem.builder() | ||
.addBeanClass(AzureMonitorMeterRegistryProvider.class) | ||
.setUnremovable().build()); | ||
|
||
// Include the AzureMonitorMeterRegistry in a possible CompositeMeterRegistry | ||
return new MicrometerRegistryProviderBuildItem(REGISTRY_CLASS); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...src/test/java/io/quarkus/micrometer/deployment/export/AzureMonitorEnabledInvalidTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package io.quarkus.micrometer.deployment.export; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.micrometer.core.instrument.MeterRegistry; | ||
import io.micrometer.core.instrument.config.validate.ValidationException; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class AzureMonitorEnabledInvalidTest { | ||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withConfigurationResource("test-logging.properties") | ||
.overrideConfigKey("quarkus.micrometer.binder-enabled-default", "false") | ||
.overrideConfigKey("quarkus.micrometer.export.azuremonitor.enabled", "true") | ||
.overrideConfigKey("quarkus.micrometer.registry-enabled-default", "false") | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) | ||
.addClass(AzureMonitorRegistryProcessor.REGISTRY_CLASS)) | ||
.assertException(t -> { | ||
Assertions.assertEquals(ValidationException.class.getName(), t.getClass().getName()); | ||
}); | ||
|
||
@Inject | ||
MeterRegistry registry; | ||
|
||
@Test | ||
public void testMeterRegistryPresent() { | ||
Assertions.fail("Runtime should not have initialized with missing instrumentationKey"); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...oyment/src/test/java/io/quarkus/micrometer/deployment/export/AzureMonitorEnabledTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package io.quarkus.micrometer.deployment.export; | ||
|
||
import java.util.Set; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.micrometer.core.instrument.MeterRegistry; | ||
import io.micrometer.core.instrument.composite.CompositeMeterRegistry; | ||
import io.quarkus.micrometer.runtime.MicrometerRecorder; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class AzureMonitorEnabledTest { | ||
static final String REGISTRY_CLASS_NAME = "io.micrometer.azuremonitor.AzureMonitorMeterRegistry"; | ||
static final Class<?> REGISTRY_CLASS = MicrometerRecorder.getClassForName(REGISTRY_CLASS_NAME); | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withConfigurationResource("test-logging.properties") | ||
.overrideConfigKey("quarkus.micrometer.binder-enabled-default", "false") | ||
.overrideConfigKey("quarkus.micrometer.export.azuremonitor.enabled", "true") | ||
.overrideConfigKey("quarkus.micrometer.export.azuremonitor.instrumentation-key", "TEST") | ||
.overrideConfigKey("quarkus.micrometer.registry-enabled-default", "false") | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) | ||
.addClass(AzureMonitorRegistryProcessor.REGISTRY_CLASS)); | ||
|
||
@Inject | ||
MeterRegistry registry; | ||
|
||
@Test | ||
public void testMeterRegistryPresent() { | ||
// AzureMonitor is enabled (alone, all others disabled) | ||
Assertions.assertNotNull(registry, "A registry should be configured"); | ||
Set<MeterRegistry> subRegistries = ((CompositeMeterRegistry) registry).getRegistries(); | ||
Assertions.assertEquals( | ||
REGISTRY_CLASS, subRegistries.iterator().next().getClass(), | ||
"Should be AzureMonitorMeterRegistry"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...ometer/runtime/src/main/java/io/quarkus/micrometer/runtime/config/AzureMonitorConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package io.quarkus.micrometer.runtime.config; | ||
|
||
import java.util.Optional; | ||
|
||
import io.quarkus.runtime.annotations.ConfigGroup; | ||
import io.quarkus.runtime.annotations.ConfigItem; | ||
|
||
@ConfigGroup | ||
public class AzureMonitorConfig implements MicrometerConfig.CapabilityEnabled { | ||
/** | ||
* Support for export to AzureMonitor. | ||
* <p> | ||
* Support for AzureMonitor will be enabled if micrometer | ||
* support is enabled, the AzureMonitorMeterRegistry is on the classpath | ||
* and either this value is true, or this value is unset and | ||
* {@code quarkus.micrometer.registry-enabled-default} is true. | ||
*/ | ||
@ConfigItem | ||
public Optional<Boolean> enabled; | ||
|
||
/** | ||
* The path for the azure monitor instrumentationKey | ||
*/ | ||
@ConfigItem | ||
public Optional<String> instrumentationKey; | ||
|
||
@Override | ||
public Optional<Boolean> getEnabled() { | ||
return enabled; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return this.getClass().getSimpleName() | ||
+ "{instrumentationKey='" + instrumentationKey | ||
+ ",enabled=" + enabled | ||
+ '}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...src/main/java/io/quarkus/micrometer/runtime/export/AzureMonitorMeterRegistryProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package io.quarkus.micrometer.runtime.export; | ||
|
||
import java.util.Map; | ||
|
||
import javax.enterprise.inject.Produces; | ||
import javax.inject.Singleton; | ||
|
||
import org.eclipse.microprofile.config.Config; | ||
|
||
import io.micrometer.azuremonitor.AzureMonitorConfig; | ||
import io.micrometer.azuremonitor.AzureMonitorMeterRegistry; | ||
import io.micrometer.azuremonitor.AzureMonitorNamingConvention; | ||
import io.micrometer.core.instrument.Clock; | ||
import io.quarkus.arc.DefaultBean; | ||
import io.quarkus.micrometer.runtime.MicrometerRecorder; | ||
|
||
@Singleton | ||
public class AzureMonitorMeterRegistryProvider { | ||
static final String PREFIX = "quarkus.micrometer.export.azuremonitor."; | ||
|
||
@Produces | ||
@Singleton | ||
@DefaultBean | ||
public AzureMonitorConfig configure(Config config) { | ||
final Map<String, String> properties = MicrometerRecorder.captureProperties(config, PREFIX); | ||
|
||
return new AzureMonitorConfig() { | ||
@Override | ||
public String get(String key) { | ||
return properties.get(key); | ||
} | ||
}; | ||
} | ||
|
||
@Produces | ||
@DefaultBean | ||
public AzureMonitorNamingConvention namingConvention() { | ||
return new AzureMonitorNamingConvention(); | ||
} | ||
|
||
@Produces | ||
@Singleton | ||
public AzureMonitorMeterRegistry registry(AzureMonitorConfig config, Clock clock) { | ||
return new AzureMonitorMeterRegistry(config, clock); | ||
} | ||
} |