Skip to content

Commit

Permalink
Ability to configure metrics API programmatically
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-martin committed Jul 10, 2015
1 parent c468f39 commit 8e074e5
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 3 deletions.
17 changes: 17 additions & 0 deletions README.md
Expand Up @@ -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
Expand Down
34 changes: 32 additions & 2 deletions src/main/java/com/stackify/metric/MetricManager.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
}

Expand Down
49 changes: 48 additions & 1 deletion src/test/java/com/stackify/metric/MetricManagerTest.java
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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();
}
}

0 comments on commit 8e074e5

Please sign in to comment.