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

Export Micrometer metrics to Signalfx #12744

Merged
merged 3 commits into from
Oct 16, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 7 additions & 2 deletions extensions/micrometer/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@

<!-- test -->

<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-azure-monitor</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-datadog</artifactId>
Expand All @@ -62,12 +67,12 @@
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-stackdriver</artifactId>
<artifactId>micrometer-registry-signalfx</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-azure-monitor</artifactId>
<artifactId>micrometer-registry-stackdriver</artifactId>
<scope>test</scope>
</dependency>

Expand Down
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.SignalFxMeterRegistryProvider;

/**
* Add support for the SignalFx Meter Registry. Note that the registry may not
* be available at deployment time for some projects: Avoid direct class
* references.
*/
public class SignalFxRegistryProcessor {
private static final Logger log = Logger.getLogger(SignalFxRegistryProcessor.class);

static final String REGISTRY_CLASS_NAME = "io.micrometer.signalfx.SignalFxMeterRegistry";
static final Class<?> REGISTRY_CLASS = MicrometerRecorder.getClassForName(REGISTRY_CLASS_NAME);

public static class SignalFxRegistryEnabled implements BooleanSupplier {
MicrometerConfig mConfig;

@Override
public boolean getAsBoolean() {
return REGISTRY_CLASS != null && mConfig.checkRegistryEnabledWithDefault(mConfig.export.signalfx);
kenfinnigan marked this conversation as resolved.
Show resolved Hide resolved
}
}

@BuildStep(onlyIf = SignalFxRegistryEnabled.class)
MicrometerRegistryProviderBuildItem createSignalFxRegistry(BuildProducer<AdditionalBeanBuildItem> additionalBeans) {

// Add the SignalFx Registry Producer
additionalBeans.produce(
AdditionalBeanBuildItem.builder()
.addBeanClass(SignalFxMeterRegistryProvider.class)
.setUnremovable().build());

// Include the SignalFxMeterRegistryProvider in a possible CompositeMeterRegistry
return new MicrometerRegistryProviderBuildItem(REGISTRY_CLASS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ public class AllRegistriesDisabledTest {
static final QuarkusUnitTest config = new QuarkusUnitTest()
.withConfigurationResource("test-logging.properties")
.overrideConfigKey("quarkus.micrometer.binder-enabled-default", "false")
.overrideConfigKey("quarkus.micrometer.export.datadog.enabled", "false")
.overrideConfigKey("quarkus.micrometer.export.azuremonitor.enabled", "false")
.overrideConfigKey("quarkus.micrometer.export.datadog.enabled", "false")
.overrideConfigKey("quarkus.micrometer.export.jmx.enabled", "false")
.overrideConfigKey("quarkus.micrometer.export.prometheus.enabled", "false")
.overrideConfigKey("quarkus.micrometer.export.signalfx.enabled", "false")
.overrideConfigKey("quarkus.micrometer.export.stackdriver.enabled", "false")
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package io.quarkus.micrometer.deployment.export;

import java.util.Arrays;

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.micrometer.runtime.MicrometerRecorder;
import io.quarkus.test.QuarkusUnitTest;

public class SignalFxEnabledInvalidTest {
static final String REGISTRY_CLASS_NAME = "io.micrometer.signalfx.SignalFxMeterRegistry";
static final Class<?> REGISTRY_CLASS = MicrometerRecorder.getClassForName(REGISTRY_CLASS_NAME);

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.withConfigurationResource("test-logging.properties")
.overrideConfigKey("quarkus.micrometer.export.signalfx.enabled", "true")
.overrideConfigKey("quarkus.micrometer.registry-enabled-default", "false")
.overrideConfigKey("quarkus.micrometer.binder-enabled-default", "false")
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClass(StackdriverRegistryProcessor.REGISTRY_CLASS))
.assertException(t -> {
Assertions.assertEquals(ValidationException.class.getName(), t.getClass().getName(),
"Unexpected exception" + stackToString(t));
});

@Inject
MeterRegistry registry;

@Test
public void testMeterRegistryPresent() {
// SignalFx is enabled (alone, all others disabled)
Assertions.assertNotNull(registry, "A registry should be configured");
Assertions.assertEquals(REGISTRY_CLASS, registry.getClass(), "Should be SignalFxMeterRegistry");
}

static String stackToString(Throwable t) {
StringBuilder sb = new StringBuilder().append("\n");
while (t.getCause() != null) {
t = t.getCause();
}
sb.append(t.getClass()).append(": ").append(t.getMessage()).append("\n");
Arrays.asList(t.getStackTrace()).forEach(x -> sb.append("\t").append(x.toString()).append("\n"));
return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package io.quarkus.micrometer.deployment.export;

import java.util.Arrays;

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.micrometer.runtime.MicrometerRecorder;
import io.quarkus.test.QuarkusUnitTest;

public class SignalFxEnabledInvalidUriTest {
static final String REGISTRY_CLASS_NAME = "io.micrometer.signalfx.SignalFxMeterRegistry";
static final Class<?> REGISTRY_CLASS = MicrometerRecorder.getClassForName(REGISTRY_CLASS_NAME);

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.withConfigurationResource("test-logging.properties")
.overrideConfigKey("quarkus.micrometer.export.signalfx.enabled", "true")
.overrideConfigKey("quarkus.micrometer.export.signalfx.access-token", "required")
.overrideConfigKey("quarkus.micrometer.export.signalfx.uri", "intentionally-bad-uri")
.overrideConfigKey("quarkus.micrometer.registry-enabled-default", "false")
.overrideConfigKey("quarkus.micrometer.binder-enabled-default", "false")
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClass(StackdriverRegistryProcessor.REGISTRY_CLASS))
.assertException(t -> {
Assertions.assertEquals(ValidationException.class.getName(), t.getClass().getName(),
"Unexpected exception" + stackToString(t));
});

@Inject
MeterRegistry registry;

@Test
public void testMeterRegistryPresent() {
// SignalFx is enabled (alone, all others disabled)
Assertions.assertNotNull(registry, "A registry should be configured");
Assertions.assertEquals(REGISTRY_CLASS, registry.getClass(), "Should be SignalFxMeterRegistry");
}

static String stackToString(Throwable t) {
StringBuilder sb = new StringBuilder().append("\n");
while (t.getCause() != null) {
t = t.getCause();
}
sb.append(t.getClass()).append(": ").append(t.getMessage()).append("\n");
Arrays.asList(t.getStackTrace()).forEach(x -> sb.append("\t").append(x.toString()).append("\n"));
return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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 SignalFxEnabledTest {
static final String REGISTRY_CLASS_NAME = "io.micrometer.signalfx.SignalFxMeterRegistry";
static final Class<?> REGISTRY_CLASS = MicrometerRecorder.getClassForName(REGISTRY_CLASS_NAME);

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.withConfigurationResource("test-logging.properties")
.overrideConfigKey("quarkus.micrometer.export.signalfx.enabled", "true")
.overrideConfigKey("quarkus.micrometer.export.signalfx.access-token", "required")
.overrideConfigKey("quarkus.micrometer.registry-enabled-default", "false")
.overrideConfigKey("quarkus.micrometer.binder-enabled-default", "false")
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClass(StackdriverRegistryProcessor.REGISTRY_CLASS));

@Inject
MeterRegistry registry;

@Test
public void testMeterRegistryPresent() {
// SignalFx 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 SignalFxMeterRegistry");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class StackdriverEnabledInvalidTest {
public void testMeterRegistryPresent() {
// Stackdriver is enabled (alone, all others disabled)
Assertions.assertNotNull(registry, "A registry should be configured");
Assertions.assertTrue(REGISTRY_CLASS.equals(registry.getClass()), "Should be StackdriverMeterRegistry");
Assertions.assertEquals(REGISTRY_CLASS, registry.getClass(), "Should be StackdriverMeterRegistry");
}

static String stackToString(Throwable t) {
Expand Down
10 changes: 8 additions & 2 deletions extensions/micrometer/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@

<!-- Registry providers (optional) -->

<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-azure-monitor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-datadog</artifactId>
Expand All @@ -62,14 +67,15 @@
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-stackdriver</artifactId>
<artifactId>micrometer-registry-signalfx</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-azure-monitor</artifactId>
<artifactId>micrometer-registry-stackdriver</artifactId>
<optional>true</optional>
</dependency>

<!-- Dependencies for Binding (optional) -->

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;

import org.eclipse.microprofile.config.Config;
import org.graalvm.nativeimage.ImageInfo;
import org.jboss.logging.Logger;

Expand Down Expand Up @@ -43,7 +42,6 @@
@Recorder
public class MicrometerRecorder {
private static final Logger log = Logger.getLogger(MicrometerRecorder.class);
static final int TRIM_POS = "quarkus.micrometer.export.".length();
static final String DEFAULT_EXCEPTION_TAG_VALUE = "none";
static MicrometerMetricsFactory factory;

Expand Down Expand Up @@ -154,44 +152,6 @@ public void registerMetrics(Consumer<MetricsFactory> consumer) {
consumer.accept(factory);
}

public static Map<String, String> captureProperties(Config config, String prefix) {
final Map<String, String> properties = new HashMap<>();

// Rename and store stackdriver properties
for (String name : config.getPropertyNames()) {
if (name.startsWith(prefix)) {
String key = convertKey(name);
String value = config.getValue(name, String.class);
properties.put(key, value);
}
}
return properties;
}

static String convertKey(String name) {
String key = name.substring(TRIM_POS);
key = MicrometerRecorder.camelHumpify(key);
return key;
}

static String camelHumpify(String s) {
if (s.indexOf('-') >= 0) {
StringBuilder b = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '-') {
i++;
if (i < s.length()) {
b.append(Character.toUpperCase(s.charAt(i)));
}
} else {
b.append(s.charAt(i));
}
}
return b.toString();
}
return s;
}

public static Class<?> getClassForName(String classname) {
Class<?> clazz = null;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ public class AzureMonitorConfig implements MicrometerConfig.CapabilityEnabled {
@ConfigItem
public Optional<Boolean> enabled;

/**
* The path for the azure monitor instrumentationKey
*/
@ConfigItem
public Optional<String> instrumentationKey;

@Override
public Optional<Boolean> getEnabled() {
return enabled;
Expand All @@ -32,8 +26,7 @@ public Optional<Boolean> getEnabled() {
@Override
public String toString() {
return this.getClass().getSimpleName()
+ "{instrumentationKey='" + instrumentationKey
+ ",enabled=" + enabled
+ "{enabled=" + enabled
+ '}';
}
}
Loading