PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/atlassian-profiling-adaptor-plugin/src/test/java/ut/com/atlassian/profiling/MetricContextIntegrationTest.java

https://bitbucket.org/atlassian/atlassian-profiling
Java | 173 lines | 114 code | 24 blank | 35 comment | 0 complexity | 3bf4442c7341dba76858df404d6e4233 MD5 | raw file
  1. package ut.com.atlassian.profiling;
  2. import com.atlassian.profiling.metrics.api.context.ContextFragment;
  3. import com.atlassian.profiling.metrics.api.context.MetricContext;
  4. import com.atlassian.profiling.metrics.api.tags.OptionalTag;
  5. import com.atlassian.profiling.metrics.api.tags.TagFactory;
  6. import com.atlassian.profiling.metrics.context.MetricContextAdaptor;
  7. import com.atlassian.profiling.metrics.tags.PrefixedTagFactoryAdaptor;
  8. import com.atlassian.util.profiling.micrometer.MicrometerStrategy;
  9. import com.atlassian.util.profiling.strategy.MetricStrategy;
  10. import io.micrometer.core.instrument.MeterRegistry;
  11. import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
  12. import org.junit.After;
  13. import org.junit.Before;
  14. import org.junit.Test;
  15. import static com.atlassian.util.profiling.MetricTag.UNDEFINED_TAG_VALUE;
  16. import static com.atlassian.util.profiling.Metrics.getConfiguration;
  17. import static com.atlassian.util.profiling.Metrics.metric;
  18. import static com.atlassian.util.profiling.ProfilingConstants.OPTIONAL_TAG_PROPERTY_PREFIX;
  19. import static com.atlassian.util.profiling.StrategiesRegistry.addMetricStrategy;
  20. import static com.atlassian.util.profiling.StrategiesRegistry.removeMetricStrategy;
  21. import static java.lang.System.setProperty;
  22. import static org.junit.Assert.assertEquals;
  23. public class MetricContextIntegrationTest {
  24. private MeterRegistry meterRegistry;
  25. private MetricStrategy metricStrategy;
  26. private MetricContext metricContext;
  27. private TagFactory prefixedTagFactory;
  28. @Before
  29. public void setup() {
  30. // Setup Atlassian Profiling's metrics
  31. meterRegistry = new SimpleMeterRegistry();
  32. metricStrategy = new MicrometerStrategy(meterRegistry);
  33. addMetricStrategy(metricStrategy);
  34. getConfiguration().setEnabled(true);
  35. metricContext = new MetricContextAdaptor();
  36. prefixedTagFactory = new PrefixedTagFactoryAdaptor("");
  37. }
  38. @Test
  39. public void testMetricContext_shouldBeAbleToBePickedUpByMetrics() {
  40. // given
  41. final String tagKey = "tagKey";
  42. final String tagValue = "tagValue";
  43. final OptionalTag tag = prefixedTagFactory.createOptionalTag(tagKey, tagValue);
  44. // when
  45. try (ContextFragment ignored = metricContext.put(tag)) {
  46. metric("irrelevant")
  47. .collect(tagKey)
  48. .incrementCounter(1L);
  49. }
  50. //then
  51. assertEquals("tag value from context should match",
  52. tagValue,
  53. meterRegistry.getMeters().get(0).getId().getTags().get(0).getValue());
  54. }
  55. /**
  56. * This is a side effect of being able to use MetricTagContext for internal tagging of metrics. This also interacts
  57. * with shadowing of tags, i.e we may override vendor tags, and they may override our tags. The API is designed for
  58. * them to provide a prefix, which we'll trust they'll do unless there's a reason not to.
  59. */
  60. @Test
  61. public void testCollectedTags_shouldBeAbleToOverrideProvidedTags() {
  62. // given
  63. final String conflictingTagKey = "tagKey";
  64. final String collectedTagValue = "provided";
  65. // when
  66. try (ContextFragment ignored = metricContext.put(prefixedTagFactory.createOptionalTag(conflictingTagKey, collectedTagValue))) {
  67. metric("irrelevant")
  68. .tag(conflictingTagKey, "providedTagValue")
  69. .collect(conflictingTagKey)
  70. .incrementCounter(1L);
  71. }
  72. //then
  73. assertEquals("tag value from context should match",
  74. collectedTagValue,
  75. meterRegistry.getMeters().get(0).getId().getTags().get(0).getValue());
  76. }
  77. /**
  78. * This isn't ideal, this is resultant behaviour in wanting to be able to use MetricTagContext for internal tagging
  79. * of metrics. This means it's possible for a key clash with App vendors....
  80. */
  81. @Test
  82. public void testProvidedTags_shouldBeAbleToOverrideCollectedTags() {
  83. // given
  84. final String conflictingTagKey = "tagKey";
  85. final String providedTagValue = "provided";
  86. // when
  87. try (ContextFragment ignored = metricContext.put(prefixedTagFactory.createOptionalTag(conflictingTagKey, "collectedTagValue"))) {
  88. metric("irrelevant")
  89. .collect(conflictingTagKey)
  90. .tag(conflictingTagKey, providedTagValue)
  91. .incrementCounter(1L);
  92. }
  93. //then
  94. assertEquals("tag value from context should match",
  95. providedTagValue,
  96. meterRegistry.getMeters().get(0).getId().getTags().get(0).getValue());
  97. }
  98. /**
  99. * We assume the metric author knows best, we don't want to accidentally override a tag and end up with potentially
  100. * inconsistent or misleading information
  101. */
  102. @Test
  103. public void testContextTags_shouldNotOverrideOptionalTags() {
  104. // given
  105. final String tagKey = "tagKey";
  106. final String optionalTagValue = "optionalTagValue";
  107. // setup
  108. final String metricName = "metricName";
  109. setProperty(OPTIONAL_TAG_PROPERTY_PREFIX + metricName, tagKey);
  110. getConfiguration().reloadConfigs();
  111. // when
  112. try (ContextFragment ignored = metricContext.put(prefixedTagFactory.createOptionalTag(tagKey, "context tag value"))) {
  113. metric(metricName)
  114. .optionalTag(tagKey, optionalTagValue)
  115. .incrementCounter(1L);
  116. }
  117. //then
  118. assertEquals("tag value from context should match",
  119. optionalTagValue,
  120. meterRegistry.getMeters().get(0).getId().getTags().get(0).getValue());
  121. }
  122. /**
  123. * We assume the metric author knows best, we don't want to accidentally override a tag and end up with potentially
  124. * inconsistent or misleading information
  125. */
  126. @Test
  127. public void testContextTags_shouldNotOverrideOptionalTagsWithNullValues() {
  128. // given
  129. final String tagKey = "tagKey";
  130. // setup
  131. final String metricName = "metricName";
  132. setProperty(OPTIONAL_TAG_PROPERTY_PREFIX + metricName, tagKey);
  133. getConfiguration().reloadConfigs();
  134. // when
  135. try (ContextFragment ignored = metricContext.put(prefixedTagFactory.createOptionalTag(tagKey, "context tag value"))) {
  136. metric(metricName)
  137. .optionalTag(tagKey, null)
  138. .incrementCounter(1L);
  139. }
  140. //then
  141. assertEquals("tag value from context should match",
  142. UNDEFINED_TAG_VALUE,
  143. meterRegistry.getMeters().get(0).getId().getTags().get(0).getValue());
  144. }
  145. @After
  146. public void teardown() {
  147. removeMetricStrategy(metricStrategy);
  148. }
  149. }