MetricNameUtils { public static String getMetricName(String metricName, Map<String, String> tags) { Preconditions.checkArgument(tags.size() <= MAX_ALLOWED_TAGS, "Too many tags set on the metric %s. " + "Maximum allowed number of tags is %s, found %s.", metricName, MAX_ALLOWED_TAGS, tags.size()); validateMetricComponentName(metricName, "metric"); validateMetricTagNames(tags); return tags.entrySet().stream() .map(e -> e.getKey() + EQUALS + e.getValue()) .collect(Collectors.joining(DELIMITER, metricName + DELIMITER, "")); } private  MetricNameUtils(); static String getMetricName(String metricName, Map<String, String> tags);  }
@Test public void shouldReturnNameWithSingleTag() throws Exception { String metricName = MetricNameUtils.getMetricName(METRIC_NAME, ImmutableMap.of(TAG_KEY_1, TAG_VALUE_1)); assertThat(metricName).isEqualTo("metricName;tag1=tagVal1"); }
@Test public void shouldReturnNameWithMultipleTags() throws Exception { String metricName = MetricNameUtils.getMetricName(METRIC_NAME, ImmutableMap.of(TAG_KEY_2, TAG_VALUE_2, TAG_KEY_1, TAG_VALUE_1)); assertThat(metricName).isIn("metricName;tag1=tagVal1;tag2=tagVal2", "metricName;tag2=tagVal2;tag1=tagVal1"); }
@Test public void shouldThrowIfMetricNameContainsSemiColon() throws Exception { assertThatThrownBy(() -> MetricNameUtils.getMetricName("metric;Name", ImmutableMap.of(TAG_KEY_1, TAG_VALUE_1, TAG_KEY_2, TAG_VALUE_2))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("The metric name: metric;Name contains the forbidden character: ;"); }
@Test public void shouldThrowIfMetricNameContainsEqualSign() throws Exception { assertThatThrownBy(() -> MetricNameUtils.getMetricName("metric=Name", ImmutableMap.of(TAG_KEY_1, TAG_VALUE_1, TAG_KEY_2, TAG_VALUE_2))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("The metric name: metric=Name contains the forbidden character: ="); }
@Test public void shouldThrowIfFirstMetricTagKeyContainsSemiColon() throws Exception { assertThatThrownBy(() -> MetricNameUtils.getMetricName(METRIC_NAME, ImmutableMap.of("tag;1", TAG_VALUE_1, TAG_KEY_2, TAG_VALUE_2))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("The tag key name: tag;1 contains the forbidden character: ;"); }
@Test public void shouldThrowIfFirstMetricTagValueContainsSemiColon() throws Exception { assertThatThrownBy(() -> MetricNameUtils.getMetricName(METRIC_NAME, ImmutableMap.of(TAG_KEY_1, "tag;Val1", TAG_KEY_2, TAG_VALUE_2))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("The tag value name: tag;Val1 contains the forbidden character: ;"); }
@Test public void shouldThrowIfFirstMetricTagKeyContainsEquals() throws Exception { assertThatThrownBy(() -> MetricNameUtils.getMetricName(METRIC_NAME, ImmutableMap.of("tag=1", TAG_VALUE_1, TAG_KEY_2, TAG_VALUE_2))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("The tag key name: tag=1 contains the forbidden character: ="); }
@Test public void shouldThrowIfFirstMetricTagValueContainsEquals() throws Exception { assertThatThrownBy(() -> MetricNameUtils.getMetricName(METRIC_NAME, ImmutableMap.of(TAG_KEY_1, "tag=Val1", TAG_KEY_2, TAG_VALUE_2))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("The tag value name: tag=Val1 contains the forbidden character: ="); }
@Test public void shouldThrowIfSecondMetricTagValueContainsEquals() throws Exception { assertThatThrownBy(() -> MetricNameUtils.getMetricName(METRIC_NAME, ImmutableMap.of(TAG_KEY_1, TAG_VALUE_1, TAG_KEY_2, "tagVal=2"))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("The tag value name: tagVal=2 contains the forbidden character: ="); }
@Test public void shouldThrowForFirstEncounteredErrorMultipleMetricArgsContainInvalidCharacters() throws Exception { assertThatThrownBy(() -> MetricNameUtils.getMetricName("metric;Name", ImmutableMap.of("tag;1", TAG_VALUE_1, TAG_KEY_2, "tagVal=2"))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("The metric name: metric;Name contains the forbidden character: ;"); }
@Test public void shouldReturnANameIfTryingToRegisterMetricWithExactlyTenTags() throws Exception { Map<String, String> tags = IntStream.range(0, 10) .boxed() .collect(Collectors.toMap(i -> "tag" + i, i -> "tagVal" + i)); assertThat(tags.size()).isEqualTo(10); assertThat(MetricNameUtils.getMetricName(METRIC_NAME, tags)).contains(METRIC_NAME); }
@Test public void shouldThrowIfTryingToRegisterMetricWithMoreThanTenTags() throws Exception { Map<String, String> tags = IntStream.range(0, 11) .boxed() .collect(Collectors.toMap(i -> "tag" + i, i -> "tagVal" + i)); assertThat(tags.size()).isEqualTo(11); assertThatThrownBy(() -> MetricNameUtils.getMetricName(METRIC_NAME, tags)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("Too many tags set on the metric metricName. " + "Maximum allowed number of tags is 10, found 11."); }