/** * Inject the tags of this {@link MetricContext} to the given {@link GobblinTrackingEvent} */ private void injectTagsToEvent(GobblinTrackingEvent event) { Map<String, String> originalMetadata = event.getMetadata(); Map<String, Object> tags = getTagMap(); Map<String, String> newMetadata = Maps.newHashMap(); for(Map.Entry<String, Object> entry : tags.entrySet()) { newMetadata.put(entry.getKey(), entry.getValue().toString()); } newMetadata.putAll(originalMetadata); event.setMetadata(newMetadata); }
public void reportRegistry(MetricRegistry registry) { Map<String, String> tags = Maps.newHashMap(); if (registry instanceof MetricContext) { tags = Maps.transformValues(((MetricContext) registry).getTagMap(), new Function<Object, String>() { @Override public String apply(Object input) { return input.toString(); } }); } report(registry.getGauges(this.filter), registry.getCounters(this.filter), registry.getHistograms(this.filter), registry.getMeters(this.filter), registry.getTimers(this.filter), tags); }
@Test public void testInstrumented() { GobblinMetrics gobblinMetrics = GobblinMetrics.get("parent.context"); State state = new State(); state.setProp(ConfigurationKeys.METRICS_ENABLED_KEY, Boolean.toString(true)); state.setProp(Instrumented.METRIC_CONTEXT_NAME_KEY, gobblinMetrics.getName()); Instrumented instrumented = new Instrumented(state, InstrumentedExtractor.class); Assert.assertNotNull(instrumented.getMetricContext()); Assert.assertTrue(instrumented.getMetricContext().getParent().isPresent()); Assert.assertEquals(instrumented.getMetricContext().getParent().get(), gobblinMetrics.getMetricContext()); Map<String, ?> tags = instrumented.getMetricContext().getTagMap(); Map<String, String> expectedTags = new HashMap<>(); expectedTags.put("construct", Constructs.EXTRACTOR.toString()); expectedTags.put("class", InstrumentedExtractor.class.getCanonicalName()); expectedTags.put(MetricContext.METRIC_CONTEXT_ID_TAG_NAME, tags.get(MetricContext.METRIC_CONTEXT_ID_TAG_NAME).toString()); expectedTags.put(MetricContext.METRIC_CONTEXT_NAME_TAG_NAME, tags.get(MetricContext.METRIC_CONTEXT_NAME_TAG_NAME).toString()); Assert.assertEquals(tags.size(), expectedTags.size()); for (Map.Entry<String, ?> tag : tags.entrySet()) { Assert.assertTrue(expectedTags.containsKey(tag.getKey())); Assert.assertEquals(expectedTags.get(tag.getKey()), tag.getValue().toString()); } }
Map<String, ?> tagMap = jobMetrics.getMetricContext().getTagMap(); String contextId = tagMap.get(MetricContext.METRIC_CONTEXT_ID_TAG_NAME).toString(); String contextName = tagMap.get(MetricContext.METRIC_CONTEXT_NAME_TAG_NAME).toString(); Assert.assertNotNull(jobMetrics1.getMetricContext()); tagMap = jobMetrics1.getMetricContext().getTagMap(); Assert.assertEquals(tags.size(), 4); Assert.assertEquals(tagMap.get(MetricContext.METRIC_CONTEXT_ID_TAG_NAME), contextId); Assert.assertNotNull(jobMetrics2.getMetricContext()); tagMap = jobMetrics2.getMetricContext().getTagMap(); Assert.assertEquals(tags.size(), 4); Assert.assertNotEquals(tagMap.get(MetricContext.METRIC_CONTEXT_ID_TAG_NAME), contextId);
@Test public void testWithTags() throws IOException { try ( MetricContext metricContext = MetricContext.builder(this.getClass().getCanonicalName() + ".testGraphiteReporter") .addTag(new Tag<String>("taskId", "task_testjob_123")) .addTag(new Tag<String>("forkBranchName", "fork_1")).build(); GraphiteReporter graphiteReporter = GraphiteReporter.Factory.newBuilder() .withGraphitePusher(graphitePusher) .withMetricContextName(CONTEXT_NAME) .build(new Properties());) { Counter counter = metricContext.counter(MetricRegistry.name(METRIC_PREFIX, COUNTER)); counter.inc(5l); graphiteReporter.report(new TreeMap<String, Gauge>(), metricContext.getCounters(), new TreeMap<String, Histogram>(), new TreeMap<String, Meter>(), new TreeMap<String, Timer>(), metricContext.getTagMap()); Assert.assertEquals(getMetricValue("task_testjob_123.fork_1." + METRIC_PREFIX, COUNTER, Measurements.COUNT), Long.toString(5l)); } }
@Test public void testWithTags() throws IOException { try ( MetricContext metricContext = MetricContext.builder(this.getClass().getCanonicalName() + ".testGraphiteReporter") .addTag(new Tag<String>("taskId", "task_testjob_123")) .addTag(new Tag<String>("forkBranchName", "fork_1")).build(); InfluxDBReporter influxDBReporter = InfluxDBReporter.Factory.newBuilder() .withInfluxDBPusher(influxDBPusher) .withMetricContextName(CONTEXT_NAME) .build(new Properties());) { Counter counter = metricContext.counter(MetricRegistry.name(METRIC_PREFIX, COUNTER)); counter.inc(5l); influxDBReporter.report(new TreeMap<String, Gauge>(), metricContext.getCounters(), new TreeMap<String, Histogram>(), new TreeMap<String, Meter>(), new TreeMap<String, Timer>(), metricContext.getTagMap()); //InfluxDB converts all values to float64 internally Assert.assertEquals(getMetricValue("task_testjob_123.fork_1." + METRIC_PREFIX, COUNTER, Measurements.COUNT), Float.toString(5f)); } }
metricContext.getMeters(), metricContext.getTimers(), metricContext.getTagMap());
metricContext.getMeters(), metricContext.getTimers(), metricContext.getTagMap());
/** * Inject the tags of this {@link MetricContext} to the given {@link GobblinTrackingEvent} */ private void injectTagsToEvent(GobblinTrackingEvent event) { Map<String, String> originalMetadata = event.getMetadata(); Map<String, Object> tags = getTagMap(); Map<String, String> newMetadata = Maps.newHashMap(); for(Map.Entry<String, Object> entry : tags.entrySet()) { newMetadata.put(entry.getKey(), entry.getValue().toString()); } newMetadata.putAll(originalMetadata); event.setMetadata(newMetadata); }
public void reportRegistry(MetricRegistry registry) { Map<String, String> tags = Maps.newHashMap(); if (registry instanceof MetricContext) { tags = Maps.transformValues(((MetricContext) registry).getTagMap(), new Function<Object, String>() { @Override public String apply(Object input) { return input.toString(); } }); } report(registry.getGauges(this.filter), registry.getCounters(this.filter), registry.getHistograms(this.filter), registry.getMeters(this.filter), registry.getTimers(this.filter), tags); }