@Override public String toString() { return getKey() + KEY_VALUE_SEPARATOR + getValue(); } }
/** * Converts a {@link List} of {@link Tag}s to a {@link Map} of key, value pairs. * * @param tags a {@link List} of {@link Tag}s that should be converted to key, value pairs * * @return a {@link Map} of key, value pairs */ public static <T> Map<? extends String, T> toMap(List<Tag<T>> tags) { ImmutableMap.Builder<String, T> tagsMapBuilder = ImmutableMap.builder(); for (Tag<T> tag : tags) { tagsMapBuilder.put(tag.getKey(), tag.getValue()); } return tagsMapBuilder.build(); }
public static Map<String, String> dumpTags(MetricContext context) { Map<String, String> output = new HashMap<>(); for (Tag<?> tag : context.getTags()) { output.put(tag.getKey(), tag.getValue().toString()); } return output; }
/** * Add tags. * @param tags List of {@link org.apache.gobblin.metrics.Tag} * @return {@code this} */ public T withTags(List<Tag<?>> tags) { for(Tag<?> tag : tags) { this.tags.put(tag.getKey(), tag.getValue().toString()); } return self(); }
/** * Converts a wildcard {@link Tag} to a {@link String} {@link Tag}. This method uses the {@link Object#toString()} * method to convert the wildcard type to a {@link String}. * * @param tag a {@link Tag} that should be converted to a {@link Tag} with value of type {@link String} * * @return a {@link Tag} with a {@link String} value */ public static Tag<String> tagValueToString(Tag<?> tag) { return new Tag<>(tag.getKey(), tag.getValue().toString()); }
@Test(dependsOnMethods = "testAddTags") public void testGetTags() { List<Tag<?>> tags = this.tagged.getTags(); Assert.assertEquals(tags.size(), 2); Assert.assertEquals(tags.get(0).getKey(), JOB_ID_KEY); Assert.assertEquals(tags.get(0).getValue(), JOB_ID); Assert.assertEquals(tags.get(1).getKey(), PROJECT_VERSION_KEY); Assert.assertEquals(tags.get(1).getValue(), PROJECT_VERSION); }
@Test public void testTags() { Tag<String> jobIdTag = new Tag<String>(JOB_ID_KEY, JOB_ID); Assert.assertEquals(jobIdTag.getKey(), JOB_ID_KEY); Assert.assertEquals(jobIdTag.getValue(), JOB_ID); Tag<Integer> projectVersionTag = new Tag<Integer>(PROJECT_VERSION_KEY, PROJECT_VERSION); Assert.assertEquals(projectVersionTag.getKey(), PROJECT_VERSION_KEY); Assert.assertEquals(projectVersionTag.getValue().intValue(), PROJECT_VERSION); } }
@BeforeClass public void setUp() { String contextName = CONTEXT_NAME + "_" + UUID.randomUUID().toString(); this.context = MetricContext.builder(contextName) .addTag(new Tag<String>(JOB_ID_KEY, JOB_ID_PREFIX + 0)) .build(); Assert.assertEquals(this.context.getName(), contextName); Assert.assertTrue(this.context.getParent().isPresent()); Assert.assertEquals(this.context.getParent().get(), RootMetricContext.get()); Assert.assertEquals(this.context.getTags().size(), 3); // uuid and name tag gets added automatically Assert.assertEquals(this.context.getTags().get(0).getKey(), JOB_ID_KEY); Assert.assertEquals(this.context.getTags().get(0).getValue(), JOB_ID_PREFIX + 0); // Second tag should be uuid Assert.assertTrue(this.context.getTags().get(1).getValue().toString() .matches("[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}")); }
@Test public void testChildContext() { this.childContext = this.context.childBuilder(CHILD_CONTEXT_NAME) .addTag(new Tag<String>(TASK_ID_KEY, TASK_ID_PREFIX + 0)) .build(); Assert.assertEquals(this.childContext.getName(), CHILD_CONTEXT_NAME); Assert.assertTrue(this.childContext.getParent().isPresent()); Assert.assertEquals(this.childContext.getParent().get(), this.context); Assert.assertEquals(this.childContext.getTags().size(), 4); Assert.assertEquals(this.childContext.getTags().get(0).getKey(), JOB_ID_KEY); Assert.assertEquals(this.childContext.getTags().get(0).getValue(), JOB_ID_PREFIX + 0); Assert.assertEquals(this.childContext.getTags().get(1).getKey(), MetricContext.METRIC_CONTEXT_ID_TAG_NAME); Assert.assertEquals(this.childContext.getTags().get(2).getKey(), MetricContext.METRIC_CONTEXT_NAME_TAG_NAME); Assert.assertEquals(this.childContext.getTags().get(3).getKey(), TASK_ID_KEY); Assert.assertEquals(this.childContext.getTags().get(3).getValue(), TASK_ID_PREFIX + 0); }
@Test public void kafkaReporterTagsTest() throws IOException { MetricContext metricContext = MetricContext.builder(this.getClass().getCanonicalName() + ".kafkaReporterTagsTest").build(); Counter counter = metricContext.counter("com.linkedin.example.counter"); Tag<?> tag1 = new Tag<>("tag1", "value1"); Tag<?> tag2 = new Tag<>("tag2", 2); MockKafkaPusher pusher = new MockKafkaPusher(); KafkaReporter kafkaReporter = getBuilder(pusher).withTags(Lists.newArrayList(tag1, tag2)).build("localhost:0000", "topic", new Properties()); counter.inc(); kafkaReporter.report(metricContext); try { Thread.sleep(1000); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } MetricReport metricReport = nextReport(pusher.messageIterator()); Assert.assertEquals(4, metricReport.getTags().size()); Assert.assertTrue(metricReport.getTags().containsKey(tag1.getKey())); Assert.assertEquals(metricReport.getTags().get(tag1.getKey()), tag1.getValue().toString()); Assert.assertTrue(metricReport.getTags().containsKey(tag2.getKey())); Assert.assertEquals(metricReport.getTags().get(tag2.getKey()), tag2.getValue().toString()); }
@Test public void kafkaReporterContextTest() throws IOException { Tag<?> tag1 = new Tag<>("tag1", "value1"); MetricContext context = MetricContext.builder("context").addTag(tag1).build(); Counter counter = context.counter("com.linkedin.example.counter"); MockKafkaPusher pusher = new MockKafkaPusher(); KafkaReporter kafkaReporter = getBuilderFromContext(pusher).build("localhost:0000", "topic", new Properties()); counter.inc(); kafkaReporter.report(context); try { Thread.sleep(1000); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } MetricReport metricReport = nextReport(pusher.messageIterator()); Assert.assertEquals(3, metricReport.getTags().size()); Assert.assertTrue(metricReport.getTags().containsKey(tag1.getKey())); Assert.assertEquals(metricReport.getTags().get(tag1.getKey()), tag1.getValue().toString()); }
@Override public String toString() { return getKey() + KEY_VALUE_SEPARATOR + getValue(); } }
/** * Converts a {@link List} of {@link Tag}s to a {@link Map} of key, value pairs. * * @param tags a {@link List} of {@link Tag}s that should be converted to key, value pairs * * @return a {@link Map} of key, value pairs */ public static <T> Map<? extends String, T> toMap(List<Tag<T>> tags) { ImmutableMap.Builder<String, T> tagsMapBuilder = ImmutableMap.builder(); for (Tag<T> tag : tags) { tagsMapBuilder.put(tag.getKey(), tag.getValue()); } return tagsMapBuilder.build(); }
public static Map<String, String> dumpTags(MetricContext context) { Map<String, String> output = new HashMap<>(); for (Tag<?> tag : context.getTags()) { output.put(tag.getKey(), tag.getValue().toString()); } return output; }
/** * Add tags. * @param tags List of {@link org.apache.gobblin.metrics.Tag} * @return {@code this} */ public T withTags(List<Tag<?>> tags) { for(Tag<?> tag : tags) { this.tags.put(tag.getKey(), tag.getValue().toString()); } return self(); }
/** * Converts a wildcard {@link Tag} to a {@link String} {@link Tag}. This method uses the {@link Object#toString()} * method to convert the wildcard type to a {@link String}. * * @param tag a {@link Tag} that should be converted to a {@link Tag} with value of type {@link String} * * @return a {@link Tag} with a {@link String} value */ public static Tag<String> tagValueToString(Tag<?> tag) { return new Tag<>(tag.getKey(), tag.getValue().toString()); }