/atlassian-profiling/src/test/java/com/atlassian/util/profiling/MetricsBuilderTest.java
Java | 331 lines | 258 code | 72 blank | 1 comment | 0 complexity | abaf7333a98c89d9e1621874cb91d519 MD5 | raw file
- package com.atlassian.util.profiling;
- import com.atlassian.plugin.util.PluginKeyStack;
- import com.atlassian.util.profiling.strategy.MetricStrategy;
- import org.junit.After;
- import org.junit.Before;
- import org.junit.BeforeClass;
- import org.junit.Rule;
- import org.junit.Test;
- import org.mockito.ArgumentCaptor;
- import org.mockito.Captor;
- import org.mockito.Mock;
- import org.mockito.Mockito;
- import org.mockito.junit.MockitoJUnit;
- import org.mockito.junit.MockitoRule;
- import java.util.List;
- import java.util.stream.Collectors;
- import static com.atlassian.util.profiling.MetricTag.FROM_PLUGIN_KEY_TAG_KEY;
- import static com.atlassian.util.profiling.MetricTag.INVOKER_PLUGIN_KEY_TAG_KEY;
- import static java.util.Arrays.asList;
- import static java.util.Collections.singleton;
- import static org.hamcrest.MatcherAssert.assertThat;
- import static org.hamcrest.Matchers.equalTo;
- import static org.hamcrest.Matchers.hasItem;
- import static org.hamcrest.Matchers.hasItems;
- import static org.hamcrest.Matchers.not;
- import static org.mockito.ArgumentMatchers.any;
- import static org.mockito.Mockito.never;
- public class MetricsBuilderTest {
- @Rule
- public MockitoRule initRule = MockitoJUnit.rule();
- @Captor
- private ArgumentCaptor<MetricKey> metricCaptor;
- @Mock
- private static MetricStrategy metricStrategy;
- @BeforeClass
- public static void setupSuite() {
- Metrics.getConfiguration().setEnabled(true);
- }
- @Before
- public void setup() {
- StrategiesRegistry.addMetricStrategy(metricStrategy);
- }
- @After
- public void teardown() {
- StrategiesRegistry.removeMetricStrategy(metricStrategy);
- }
- @Test
- public void testMetricName() {
- final String metricName = "metricName";
- Metrics
- .metric(metricName) // method under test
- .startTimer().close();
- Mockito.verify(metricStrategy).startTimer(metricCaptor.capture());
- assertThat(metricCaptor.getValue().getMetricName(), equalTo(metricName));
- }
- @Test
- public void testAddingASingleTag() {
- final String tagKey = "theTagKey";
- final String tagValue = "theTagValue";
- Metrics.metric("ignored")
- .tag(tagKey, tagValue) // method under test
- .startTimer().close();
- Mockito.verify(metricStrategy).startTimer(metricCaptor.capture());
- assertThat(metricCaptor.getValue().getTags(), hasItem(MetricTag.of(tagKey, tagValue)));
- }
- @Test
- public void testAddingVarArgTags() {
- final MetricTag[] metricTags = {MetricTag.of("key1", "val1"), MetricTag.of("key2", "val2")};
- Metrics.metric("ignored")
- .tags(metricTags) // method under test
- .startTimer().close();
- Mockito.verify(metricStrategy).startTimer(metricCaptor.capture());
- assertThat(metricCaptor.getValue().getTags(), hasItems(metricTags));
- }
- @Test
- public void testAddingCollectionOfTags() {
- final MetricTag[] metricTags = {MetricTag.of("key1", "val1"), MetricTag.of("key2", "val2")};
- Metrics.metric("ignored")
- .tags(asList(metricTags)) // method under test
- .startTimer().close();
- Mockito.verify(metricStrategy).startTimer(metricCaptor.capture());
- assertThat(metricCaptor.getValue().getTags(), hasItems(metricTags));
- }
- @Test
- public void testAddingTagsInEveryWay() {
- final String tagKey = "theTagKey";
- final String tagValue = "theTagValue";
- final MetricTag[] varArgsMetricTags =
- {MetricTag.of("varArgKey1", "varArgVal1"), MetricTag.of("varArgKey2", "varArgVal2")};
- final MetricTag[] iterableMetricTags =
- {MetricTag.of("iterableKey1", "iterableVal1"), MetricTag.of("iterableKey2", "iterableVal2")};
- Metrics.metric("ignored")
- // Methods under test
- .tag(tagKey, tagValue)
- .tags(varArgsMetricTags)
- .tags(asList(iterableMetricTags))
- .startTimer().close();
- Mockito.verify(metricStrategy).startTimer(metricCaptor.capture());
- assertThat(metricCaptor.getValue().getTags(), hasItem(MetricTag.of(tagKey, tagValue)));
- assertThat(metricCaptor.getValue().getTags(), hasItems(varArgsMetricTags));
- assertThat(metricCaptor.getValue().getTags(), hasItems(iterableMetricTags));
- }
- @Test
- public void testWithAnalytics() {
- Metrics.metric("ignored")
- .withAnalytics() // method under test
- .startTimer().close();
- Mockito.verify(metricStrategy).startTimer(metricCaptor.capture());
- assertThat(metricCaptor.getValue().getTags(), hasItem(MetricTag.SEND_ANALYTICS));
- }
- @Test
- public void testFromPluginKey() {
- final String pluginKey = "pluginKey";
- Metrics.metric("ignored")
- .fromPluginKey(pluginKey) // method under test
- .startTimer().close();
- Mockito.verify(metricStrategy).startTimer(metricCaptor.capture());
- assertThat(metricCaptor.getValue().getTags(),
- hasItem(MetricTag.of(FROM_PLUGIN_KEY_TAG_KEY, pluginKey)));
- }
- @Test
- public void testFromPluginKey_should_onlySetTheTagOnce() {
- final String firstPluginKey = "firstPluginKey";
- final String secondPluginKey = "secondPluginKey";
- Metrics.metric("ignored")
- .fromPluginKey(firstPluginKey) // method under test
- .fromPluginKey(secondPluginKey) // method under test
- .startTimer().close();
- Mockito.verify(metricStrategy).startTimer(metricCaptor.capture());
- assertThat(metricCaptor.getValue().getTags(),
- hasItem(MetricTag.of(FROM_PLUGIN_KEY_TAG_KEY, secondPluginKey)));
- assertThat(metricCaptor.getValue().getTags(),
- not(hasItem(MetricTag.of(FROM_PLUGIN_KEY_TAG_KEY, firstPluginKey))));
- }
- @Test
- public void testFromPluginKey_should_notAddANullPluginKey() {
- final String nullPluginKey = null;
- Metrics.metric("ignored")
- .fromPluginKey(nullPluginKey) // method under test
- .startTimer().close();
- Mockito.verify(metricStrategy).startTimer(metricCaptor.capture());
- final List<String> tagKeys =
- metricCaptor.getValue().getTags().stream().map(MetricTag::getKey).collect(Collectors.toList());
- assertThat(tagKeys, not(hasItem(FROM_PLUGIN_KEY_TAG_KEY)));
- }
- @Test
- public void testInvokerPluginKey() {
- final String pluginKey = "pluginKey";
- Metrics.metric("ignored")
- .invokerPluginKey(pluginKey) // method under test
- .startTimer().close();
- Mockito.verify(metricStrategy).startTimer(metricCaptor.capture());
- assertThat(metricCaptor.getValue().getTags(),
- hasItem(MetricTag.of(INVOKER_PLUGIN_KEY_TAG_KEY, pluginKey)));
- }
- @Test
- public void testInvokerPluginKey_should_onlySetTheTagOnce() {
- final String firstPluginKey = "firstPluginKey";
- final String secondPluginKey = "secondPluginKey";
- Metrics.metric("ignored")
- .invokerPluginKey(firstPluginKey) // method under test
- .invokerPluginKey(secondPluginKey) // method under test
- .startTimer().close();
- Mockito.verify(metricStrategy).startTimer(metricCaptor.capture());
- assertThat(metricCaptor.getValue().getTags(),
- hasItem(MetricTag.of(INVOKER_PLUGIN_KEY_TAG_KEY, secondPluginKey)));
- assertThat(metricCaptor.getValue().getTags(),
- not(hasItem(MetricTag.of(INVOKER_PLUGIN_KEY_TAG_KEY, firstPluginKey))));
- }
- @Test
- public void testInvokerPluginKey_should_notAddANullPluginKey() {
- final String nullPluginKey = null;
- Metrics.metric("ignored")
- .invokerPluginKey(nullPluginKey) // method under test
- .startTimer().close();
- Mockito.verify(metricStrategy).startTimer(metricCaptor.capture());
- final List<String> tagKeys =
- metricCaptor.getValue().getTags().stream().map(MetricTag::getKey).collect(Collectors.toList());
- assertThat(tagKeys, not(hasItem(INVOKER_PLUGIN_KEY_TAG_KEY)));
- }
- @Test
- public void testWithInvokerPluginKey() {
- final String providedPluginKey = "providedPluginKey";
- PluginKeyStack.push(providedPluginKey);
- Metrics.metric("ignored")
- .withInvokerPluginKey() // method under test
- .startTimer().close();
- Mockito.verify(metricStrategy).startTimer(metricCaptor.capture());
- assertThat(metricCaptor.getValue().getTags(),
- hasItem(MetricTag.of(INVOKER_PLUGIN_KEY_TAG_KEY, providedPluginKey)));
- }
- @Test
- public void testStartTimer() {
- final String trackedMetricName = "uniqueRegularTimerMetricName";
- Metrics.metric(trackedMetricName)
- .startTimer() // method under test
- .close();
- Mockito.verify(metricStrategy).startTimer(metricCaptor.capture());
- assertThat(metricCaptor.getValue().getMetricName(), equalTo(trackedMetricName));
- }
- @Test
- public void testStartLongRunningTimer() {
- final String trackedMetricName = "uniqueLongRunningTimerMetricName";
- Metrics.metric(trackedMetricName)
- .startLongRunningTimer() // method under test
- .close();
- Mockito.verify(metricStrategy).startLongRunningTimer(metricCaptor.capture());
- assertThat(metricCaptor.getValue().getMetricName(), equalTo(trackedMetricName));
- }
- @Test
- public void testIncrementCounter() {
- final MetricKey metricKey = MetricKey.metricKey("counter", MetricTag.of("tagKey", "tagValue"));
- final long trackedUpdateValue = 363;
- Metrics
- .metric(metricKey.getMetricName())
- .tags(metricKey.getTags())
- .incrementCounter(trackedUpdateValue);
- final ArgumentCaptor<Long> updateValueCaptor = ArgumentCaptor.forClass(Long.class);
- Mockito.verify(metricStrategy).incrementCounter(metricCaptor.capture(), updateValueCaptor.capture());
- assertThat(metricCaptor.getValue(), equalTo(metricKey));
- assertThat(updateValueCaptor.getValue(), equalTo(trackedUpdateValue));
- }
- @Test(expected = IllegalArgumentException.class)
- public void testIncrementCounter_shouldNotAcceptNegativeValues() {
- final long negativeNumber = -1L;
- Metrics
- .metric("ignored")
- .incrementCounter(negativeNumber);
- Mockito.verify(metricStrategy, never()).incrementCounter(any(), any());
- }
- @Test
- public void testHistogramUpdate() {
- final String trackedMetricName = "uniqueLongRunningTimerMetricName";
- final long trackedUpdateValue = 77777;
- Metrics.metric(trackedMetricName)
- .histogram() // method under test
- .update(trackedUpdateValue);
- ArgumentCaptor<Long> histogramValueCaptor = ArgumentCaptor.forClass(Long.class);
- Mockito.verify(metricStrategy).updateHistogram(metricCaptor.capture(), histogramValueCaptor.capture());
- assertThat(metricCaptor.getValue().getMetricName(), equalTo(trackedMetricName));
- assertThat(histogramValueCaptor.getValue(), equalTo(trackedUpdateValue));
- }
- @Test
- public void testNullTagValues_shouldShowInOutput() {
- final String tag1Key = "tag1";
- final String tag2Key = "tag2";
- final String tag3Key = "tag3";
- final String[] expectedTagKeys = {tag1Key, tag2Key, tag3Key};
- Metrics.metric("irrelevant")
- .tag(tag1Key, null)
- .tags(MetricTag.of(tag2Key, null))
- .tags(singleton(MetricTag.of(tag3Key, null)))
- .timer().start().close();
- Mockito.verify(metricStrategy).startTimer(metricCaptor.capture());
- final List<String> capturedMetricTags = metricCaptor.getValue().getTags().stream()
- .map(MetricTag::getKey).collect(Collectors.toList());
- assertThat(capturedMetricTags, hasItems(expectedTagKeys));
- }
- }