/** * Generates metrics for the instrumentation of this class. */ protected void regenerateMetrics() { // Set up the metrics that are enabled regardless of instrumentation this.recordsIn = this.metricContext.meter(MetricNames.DataWriterMetrics.RECORDS_IN_METER); this.recordsAttempted = this.metricContext.meter(MetricNames.DataWriterMetrics.RECORDS_ATTEMPTED_METER); this.recordsSuccess = this.metricContext.meter(MetricNames.DataWriterMetrics.SUCCESSFUL_WRITES_METER); this.recordsFailed = this.metricContext.meter(MetricNames.DataWriterMetrics.FAILED_WRITES_METER); this.bytesWritten = this.metricContext.meter(MetricNames.DataWriterMetrics.BYTES_WRITTEN_METER); if (isInstrumentationEnabled()) { this.dataWriterTimer = Optional.<Timer>of(this.metricContext.timer(MetricNames.DataWriterMetrics.WRITE_TIMER)); } else { this.dataWriterTimer = Optional.absent(); } }
String randomIdPrefix = "uuid:"; String oldName = context.getName(); List<String> splitName = Strings.isNullOrEmpty(oldName) ? Lists.<String> newArrayList() : Lists.newArrayList(Splitter.on(".").splitToList(oldName)); MetricContext.Builder builder = context.getParent().isPresent() ? context.getParent().get().childBuilder(newName) : MetricContext.builder(newName); return builder.addTags(context.getTags()).addTags(newTags).build();
/** * Submit the event */ public void submit(MetricContext context) { context.submitEvent(build()); } }
@Override public void report() { if (this.filter.isPresent()) { report(this.context.getGauges(this.filter.get()), this.context.getCounters(this.filter.get()), this.context.getHistograms(this.filter.get()), this.context.getMeters(this.filter.get()), this.context.getTimers(this.filter.get())); } else { report(this.context.getGauges(), this.context.getCounters(), this.context.getHistograms(), this.context.getMeters(), this.context.getTimers()); } }
public GobblinHelixPlanningJobLauncherMetrics(String metricsName, final MetricContext metricContext, int windowSizeInMin, HelixJobsMapping jobsMapping) { this.metricsName = metricsName; this.jobsMapping = jobsMapping; this.timeForCompletedPlanningJobs = metricContext.contextAwareTimer(TIMER_FOR_COMPLETED_PLANNING_JOBS, windowSizeInMin, TimeUnit.MINUTES); this.timeForFailedPlanningJobs = metricContext.contextAwareTimer(TIMER_FOR_FAILED_PLANNING_JOBS, windowSizeInMin, TimeUnit.MINUTES); this.numActivePlanningJobs = metricContext.newContextAwareGauge(NUM_ACTIVE_PLANNING_JOBS, ()->getNumOfMappings()); this.contextAwareMetrics.add(timeForCompletedPlanningJobs); this.contextAwareMetrics.add(timeForFailedPlanningJobs); this.contextAwareMetrics.add(numActivePlanningJobs); }
try ( MetricContext metricContext = MetricContext.builder(this.getClass().getCanonicalName() + ".testGraphiteReporter").build(); metricContext.newContextAwareGauge("com.linkedin.example.gauge", new Gauge<Long>() { @Override public Long getValue() { metricContext.register(MetricRegistry.name(METRIC_PREFIX, GAUGE), contextAwareGauge); Counter counter = metricContext.counter(MetricRegistry.name(METRIC_PREFIX, COUNTER)); Meter meter = metricContext.meter(MetricRegistry.name(METRIC_PREFIX, METER)); Histogram histogram = metricContext.histogram(MetricRegistry.name(METRIC_PREFIX, HISTOGRAM)); Timer timer = metricContext.timer(MetricRegistry.name(METRIC_PREFIX, TIMER)); timer.update(3, TimeUnit.SECONDS); graphiteReporter.report(metricContext.getGauges(), metricContext.getCounters(), metricContext.getHistograms(), metricContext.getMeters(), metricContext.getTimers(), metricContext.getTagMap());
@Builder private MetricsUpdater(int depth, int counters, int meters, int histograms, int timers, MetricContext baseContext) { this.depth = depth; this.random = new Random(); MetricContext tmpContext = baseContext == null ? RootMetricContext.get() : baseContext; while(depth > 0) { tmpContext = tmpContext.childBuilder(UUID.randomUUID().toString()).build(); depth--; } this.context = tmpContext; this.counters = Lists.newArrayList(); for(int i = 0; i < counters; i++) { this.counters.add(this.context.counter("gobblin.performance.test.counter" + i)); } this.meters = Lists.newArrayList(); for(int i = 0; i < meters; i++) { this.meters.add(this.context.meter("gobblin.performance.test.meter" + i)); } this.histograms = Lists.newArrayList(); for(int i = 0; i < histograms; i++) { this.histograms.add(this.context.histogram("gobblin.performance.test.histogram" + i)); } this.timers = Lists.newArrayList(); for(int i = 0; i < timers; i++) { this.timers.add(this.context.timer("gobblin.performance.test.timer" + i)); } }
/** * Get a {@link Timer} with the given name prefix and suffixes. * * @param prefix the given name prefix * @param suffixes the given name suffixes * @return a {@link Timer} with the given name prefix and suffixes */ public Timer getTimer(String prefix, String... suffixes) { return this.metricContext.timer(MetricRegistry.name(prefix, suffixes)); }
@Test public void testReport() throws IOException { MetricContext testContext = MetricContext.builder(getClass().getCanonicalName()).build(); FileSystem fs = mock(FileSystem.class); Path failureLogPath = mock(Path.class); testContext.submitEvent(event); verify(fs, never()).append(failureLogPath); verify(outputStream, never()).write(anyByte());
/** * Generates metrics for the instrumentation of this class. */ protected void regenerateMetrics() { // Set up the metrics that are enabled regardless of instrumentation _watermarksInserted = _metricContext.meter(WATERMARKS_INSERTED_METER); _watermarksSwept = _metricContext.meter(WATERMARKS_SWEPT_METER); } }
@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 testTags() throws IOException { MetricContext metricContext = MetricContext.builder(this.getClass().getCanonicalName()).build(); Counter counter = metricContext.counter("com.linkedin.example.counter"); Map<String, String> tags = new HashMap<>(); tags.put("testKey", "testValue"); tags.put("key2", "value2"); OutputStreamReporter reporter = OutputStreamReporter.Factory.newBuilder().withTags(tags).outputTo(this.stream).build(new Properties()); counter.inc(); reporter.report(); Assert.assertTrue(this.stream.toString().contains("key2=value2")); Assert.assertTrue(this.stream.toString().contains("testKey=testValue")); String[] lines = this.stream.toString().split("\n"); Map<String, Set<String>> expected = new HashMap<>(); expectMetrics(expected, lines); Set<String> counterSubMetrics = new HashSet<>(); counterSubMetrics.add("count"); expected.put("com.linkedin.example.counter", counterSubMetrics); reporter.close(); }
}) public void testGetMetrics() { SortedSet<String> names = this.context.getNames(); Assert.assertEquals(names.size(), 6); Assert.assertTrue(names.contains(RECORDS_PROCESSED)); SortedSet<String> childNames = this.childContext.getNames(); Assert.assertEquals(childNames.size(), 4); Assert.assertTrue( Map<String, Metric> metrics = this.context.getMetrics(); Assert.assertEquals(metrics.size(), 6); Assert.assertTrue( Map<String, Counter> counters = this.context.getCounters(); Assert.assertEquals(counters.size(), 1); Assert.assertTrue( counters.containsKey(RECORDS_PROCESSED)); Map<String, Meter> meters = this.context.getMeters(); Assert.assertEquals(meters.size(), 1); Assert.assertTrue( meters.containsKey(RECORD_PROCESS_RATE)); Map<String, Histogram> histograms = this.context.getHistograms(); Assert.assertEquals(histograms.size(), 1); Assert.assertTrue( histograms.containsKey(RECORD_SIZE_DISTRIBUTION));
? gobblinMetrics.get().getMetricContext().childBuilder(klazz.getCanonicalName() + "." + randomId) : MetricContext.builder(klazz.getCanonicalName() + "." + randomId); return builder.addTags(generatedTags).addTags(tags).build();
@Test(dependsOnMethods = { "testGetMetricsWithFilter" }) public void testRemoveMetrics() { Assert.assertTrue(this.childContext.remove(RECORDS_PROCESSED)); Assert.assertTrue(this.childContext.getCounters().isEmpty()); Assert.assertTrue(this.childContext.remove(RECORD_PROCESS_RATE)); Assert.assertTrue(this.childContext.getMeters().isEmpty()); Assert.assertTrue(this.childContext.remove(RECORD_SIZE_DISTRIBUTION)); Assert.assertTrue(this.childContext.getHistograms().isEmpty()); Assert.assertEquals(this.childContext.getNames().size(), 1); }
MetricArray metricArray = new MetricArray(); for (Map.Entry<String, ? extends com.codahale.metrics.Metric> entry : jobMetrics.getMetricContext().getCounters() .entrySet()) { Metric counter = new Metric(); for (Map.Entry<String, ? extends com.codahale.metrics.Metric> entry : jobMetrics.getMetricContext().getMeters() .entrySet()) { Metric meter = new Metric(); for (Map.Entry<String, ? extends com.codahale.metrics.Metric> entry : jobMetrics.getMetricContext().getGauges() .entrySet()) { Metric gauge = new Metric();
private synchronized ExecutorService getExecutorService() { if(!this.executorServiceOptional.isPresent()) { this.executorServiceOptional = Optional.of(MoreExecutors.getExitingExecutorService( (ThreadPoolExecutor) Executors.newCachedThreadPool(ExecutorsUtils.newThreadFactory(Optional.of(LOG), Optional.of("MetricContext-" + getName() + "-%d"))), 5, TimeUnit.MINUTES)); } return this.executorServiceOptional.get(); }
/** * Instantiates {@link com.codahale.metrics.Metric}s. Called once in {@link #startUp()}. Subclasses should override * this method to instantiate their own metrics. */ protected void createMetrics() { this.messagesRead = this.metricContext.counter(RuntimeMetrics.GOBBLIN_KAFKA_HIGH_LEVEL_CONSUMER_MESSAGES_READ); }
/** * Get a new {@link MetricContext.Builder} for building child {@link MetricContext}s. * * @param name name of the child {@link MetricContext} to be built * @return a new {@link MetricContext.Builder} for building child {@link MetricContext}s */ public Builder childBuilder(String name) { return builder(name).hasParent(this); }
@Test public void testSubTaggedMetricContext() throws Exception { MetricContextFactory<SimpleScopeType> factory = new MetricContextFactory<>(); Config config = ConfigFactory.parseMap(ImmutableMap.of( BrokerConfigurationKeyGenerator.generateKey(factory, null, null, MetricContextFactory.TAG_KEY + ".tag1"), "value1" )); SharedResourcesBroker<SimpleScopeType> rootBroker = SharedResourcesBrokerFactory.createDefaultTopLevelBroker(config, SimpleScopeType.GLOBAL.defaultScopeInstance()); MetricContext metricContext = rootBroker.getSharedResource(factory, new SubTaggedMetricContextKey("myMetricContext", ImmutableMap.of("tag2", "value2"))); Map<String, String> tagMap = (Map<String, String>) Tag.toMap(Tag.tagValuesToString(metricContext.getTags())); Assert.assertEquals(metricContext.getName(), "myMetricContext"); Assert.assertEquals(tagMap.get("tag1"), "value1"); Assert.assertEquals(tagMap.get("tag2"), "value2"); MetricContext metricContext2 = rootBroker.getSharedResource(factory, new SubTaggedMetricContextKey("myMetricContext", ImmutableMap.of("tag2", "value2"))); Assert.assertEquals(metricContext, metricContext2); MetricContext metricContext3 = rootBroker.getSharedResource(factory, new SubTaggedMetricContextKey("myMetricContext", ImmutableMap.of("tag3", "value3"))); Assert.assertNotEquals(metricContext, metricContext3); MetricContext parent = rootBroker.getSharedResource(factory, new MetricContextKey()); tagMap = (Map<String, String>) Tag.toMap(Tag.tagValuesToString(parent.getTags())); Assert.assertEquals(metricContext.getParent().get(), parent); Assert.assertEquals(tagMap.get("tag1"), "value1"); Assert.assertFalse(tagMap.containsKey("tag2")); }