diff --git a/README.md b/README.md index a8c8e04..db41768 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,23 @@ stackify.environment=YOUR_ENVIRONMENT Note: *If you are logging from a device that has the stackify-agent installed, the environment setting is optional. We will use the environment associated to your device in Stackify.* +#### Programmatic Configuration (Optional) + +Instead of providing a properties file in your classpath, you can configure the Metrics API programmatically: +``` +ApiConfiguration.Builder builder = ApiConfiguration.newBuilder(); +builder.apiKey("YOUR_API_KEY"); +builder.application("YOUR_APPLICATION_NAME"); +builder.environment("YOUR_ENVIRONMENT"); +ApiConfiguration config = builder.build(); + +MetricManager.configure(config); +``` + +This needs to be done at application startup before any other interactions with the Metrics API. + +Note: *If you are logging from a device that has the stackify-agent installed, the environment setting is optional. We will use the environment associated to your device in Stackify.* + #### Gauge Metric ```java diff --git a/src/main/java/com/stackify/metric/MetricManager.java b/src/main/java/com/stackify/metric/MetricManager.java index 2b5de67..0b0c36d 100644 --- a/src/main/java/com/stackify/metric/MetricManager.java +++ b/src/main/java/com/stackify/metric/MetricManager.java @@ -24,6 +24,7 @@ import com.stackify.api.common.ApiConfiguration; import com.stackify.api.common.ApiConfigurations; import com.stackify.api.common.AppIdentityService; +import com.stackify.api.common.EnvironmentDetails; import com.stackify.metric.impl.MetricBackgroundService; import com.stackify.metric.impl.MetricCollector; import com.stackify.metric.impl.MetricMonitorService; @@ -70,15 +71,42 @@ public static MetricCollector getCollector() { return COLLECTOR; } + + /** + * Manually configure the metrics api + * @param config API configuration + */ + public static synchronized void configure(final ApiConfiguration config) { + + ApiConfiguration.Builder builder = ApiConfiguration.newBuilder(); + builder.apiUrl(config.getApiUrl()); + builder.apiKey(config.getApiKey()); + builder.application(config.getApplication()); + builder.environment(config.getEnvironment()); + + if (config.getEnvDetail() == null) + { + builder.envDetail(EnvironmentDetails.getEnvironmentDetail(config.getApplication(), config.getEnvironment())); + } + else + { + builder.envDetail(config.getEnvDetail()); + } + + CONFIG = builder.build(); + } + /** * Start up the background thread that is processing metrics */ private static synchronized void startup() { try { - CONFIG = ApiConfigurations.fromProperties(); - + if (CONFIG == null) { + CONFIG = ApiConfigurations.fromProperties(); + } + ObjectMapper objectMapper = new ObjectMapper(); AppIdentityService appIdentityService = new AppIdentityService(CONFIG, objectMapper, true); @@ -105,6 +133,8 @@ public static synchronized void shutdown() { } catch (Throwable t) { LOGGER.error("Exception stopping Stackify Metrics API service", t); } + + INITIALIZED.compareAndSet(true, false); } } diff --git a/src/test/java/com/stackify/metric/MetricManagerTest.java b/src/test/java/com/stackify/metric/MetricManagerTest.java index 0c770be..7c1cfed 100644 --- a/src/test/java/com/stackify/metric/MetricManagerTest.java +++ b/src/test/java/com/stackify/metric/MetricManagerTest.java @@ -15,6 +15,7 @@ */ package com.stackify.metric; +import org.junit.After; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -23,6 +24,8 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import com.stackify.api.common.ApiConfiguration; +import com.stackify.api.common.ApiConfigurations; import com.stackify.api.common.AppIdentityService; import com.stackify.metric.impl.MetricBackgroundService; import com.stackify.metric.impl.MetricCollector; @@ -34,9 +37,17 @@ * @author Eric Martin */ @RunWith(PowerMockRunner.class) -@PrepareForTest({MetricManager.class, AppIdentityService.class, MetricMonitorService.class, MetricSender.class, MetricBackgroundService.class}) +@PrepareForTest({MetricManager.class, AppIdentityService.class, MetricMonitorService.class, MetricSender.class, MetricBackgroundService.class, ApiConfigurations.class}) public class MetricManagerTest { + /** + * tearDown + */ + @After + public void tearDown() { + MetricManager.shutdown(); + } + /** * testGetCollectorAndShutdown * @throws Exception @@ -73,4 +84,40 @@ public void testGetCollectorAndShutdown() throws Exception { Mockito.verify(background).stop(); } + + /** + * testManualConfig + * @throws Exception + */ + @Test + public void testManualConfig() throws Exception { + + PowerMockito.mockStatic(ApiConfigurations.class); + PowerMockito.when(ApiConfigurations.fromProperties()).thenThrow(new RuntimeException()); + + ApiConfiguration config = Mockito.mock(ApiConfiguration.class); + + MetricManager.configure(config); + + AppIdentityService ais = Mockito.mock(AppIdentityService.class); + PowerMockito.whenNew(AppIdentityService.class).withAnyArguments().thenReturn(ais); + + MetricMonitorService mms = Mockito.mock(MetricMonitorService.class); + PowerMockito.whenNew(MetricMonitorService.class).withAnyArguments().thenReturn(mms); + + MetricSender sender = Mockito.mock(MetricSender.class); + PowerMockito.whenNew(MetricSender.class).withAnyArguments().thenReturn(sender); + + MetricBackgroundService background = PowerMockito.mock(MetricBackgroundService.class); + PowerMockito.whenNew(MetricBackgroundService.class).withAnyArguments().thenReturn(background); + + MetricCollector collector = MetricManager.getCollector(); + Assert.assertNotNull(collector); + + Mockito.verify(background).start(); + + MetricManager.shutdown(); + + Mockito.verify(background).stop(); + } }