InnerTimer(MetricContext context, String name, ContextAwareTimer contextAwareTimer) { this.name = name; Optional<MetricContext> parentContext = context.getParent(); if (parentContext.isPresent()) { this.parentTimer = Optional.fromNullable(parentContext.get().contextAwareTimer(name)); } else { this.parentTimer = Optional.absent(); } this.timer = new WeakReference<>(contextAwareTimer); }
InnerMeter(MetricContext context, String name, ContextAwareMeter contextAwareMeter) { this.name = name; Optional<MetricContext> parentContext = context.getParent(); if (parentContext.isPresent()) { this.parentMeter = Optional.fromNullable(parentContext.get().contextAwareMeter(name)); } else { this.parentMeter = Optional.absent(); } this.contextAwareMeter = new WeakReference<>(contextAwareMeter); }
public InnerCounter(MetricContext context, String name, ContextAwareCounter counter) { this.tagged = new Tagged(); this.name = name; Optional<MetricContext> parentContext = context.getParent(); if (parentContext.isPresent()) { this.parentCounter = Optional.fromNullable(parentContext.get().contextAwareCounter(name)); } else { this.parentCounter = Optional.absent(); } this.contextAwareCounter = new WeakReference<>(counter); }
InnerHistogram(MetricContext context, String name, ContextAwareHistogram contextAwareHistogram) { super(new ExponentiallyDecayingReservoir()); this.name = name; Optional<MetricContext> parentContext = context.getParent(); if (parentContext.isPresent()) { this.parentHistogram = Optional.fromNullable(parentContext.get().contextAwareHistogram(name)); } else { this.parentHistogram = Optional.absent(); } this.contextAwareHistogram = new WeakReference<>(contextAwareHistogram); }
InnerTimer(MetricContext context, String name, ContextAwareTimer contextAwareTimer, long windowSize, TimeUnit unit) { super(new SlidingTimeWindowReservoir(windowSize, unit)); this.name = name; Optional<MetricContext> parentContext = context.getParent(); if (parentContext.isPresent()) { this.parentTimer = Optional.fromNullable(parentContext.get().contextAwareTimer(name, windowSize, unit)); } else { this.parentTimer = Optional.absent(); } this.timer = new WeakReference<>(contextAwareTimer); }
InnerHistogram(MetricContext context, String name, ContextAwareHistogram contextAwareHistogram, long windowSize, TimeUnit unit) { super(new SlidingTimeWindowReservoir(windowSize, unit)); this.name = name; Optional<MetricContext> parentContext = context.getParent(); if (parentContext.isPresent()) { this.parentHistogram = Optional.fromNullable(parentContext.get().contextAwareHistogram(name, windowSize, unit)); } else { this.parentHistogram = Optional.absent(); } this.contextAwareHistogram = new WeakReference<>(contextAwareHistogram); }
/** * Send a notification to all targets of this context and to the parent of this context. * @param notification {@link org.apache.gobblin.metrics.notification.Notification} to send. */ public void sendNotification(final Notification notification) { ContextAwareTimer.Context timer = this.notificationTimer.time(); if(!this.notificationTargets.isEmpty()) { for (final Map.Entry<UUID, Function<Notification, Void>> entry : this.notificationTargets.entrySet()) { try { entry.getValue().apply(notification); } catch (RuntimeException exception) { LOG.warn("RuntimeException when running notification target. Skipping.", exception); } } } if(getParent().isPresent()) { getParent().get().sendNotification(notification); } timer.stop(); }
@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 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()); } }
@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 testTaskGetMetrics() { String jobId = "job_456"; String taskId = "task_456"; String jobName = "jobName"; JobState jobState = new JobState(jobName, jobId); JobMetrics jobMetrics = JobMetrics.get(jobState); State props = new State(); props.setProp(ConfigurationKeys.JOB_ID_KEY, jobId); props.setProp(ConfigurationKeys.TASK_ID_KEY, taskId); SourceState sourceState = new SourceState(props, new ArrayList<WorkUnitState>()); WorkUnit workUnit = new WorkUnit(sourceState, null); WorkUnitState workUnitState = new WorkUnitState(workUnit); TaskState taskState = new TaskState(workUnitState); TaskMetrics taskMetrics = new TaskMetrics(taskState); Assert.assertNotNull(taskMetrics.getMetricContext()); Assert.assertTrue(taskMetrics.getMetricContext().getParent().isPresent()); Assert.assertEquals(taskMetrics.getMetricContext().getParent().get(), jobMetrics.getMetricContext()); }
@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")); }
@Test public void test() throws Exception { MetricContextFactory<SimpleScopeType> factory = new MetricContextFactory<>(); Config config = ConfigFactory.parseMap(ImmutableMap.of( BrokerConfigurationKeyGenerator.generateKey(factory, null, null, MetricContextFactory.TAG_KEY + ".tag1"), "value1", BrokerConfigurationKeyGenerator.generateKey(factory, null, SimpleScopeType.GLOBAL, MetricContextFactory.TAG_KEY + ".tag2"), "value2", BrokerConfigurationKeyGenerator.generateKey(factory, null, SimpleScopeType.LOCAL, MetricContextFactory.TAG_KEY + ".tag3"), "value3" )); SharedResourcesBroker<SimpleScopeType> rootBroker = SharedResourcesBrokerFactory.createDefaultTopLevelBroker(config, SimpleScopeType.GLOBAL.defaultScopeInstance()); SharedResourcesBroker<SimpleScopeType> localBroker = rootBroker.newSubscopedBuilder(SimpleScopeType.LOCAL.defaultScopeInstance()).build(); MetricContext localContext = localBroker.getSharedResource(factory, new MetricContextKey()); Map<String, String> tagMap = (Map<String, String>) Tag.toMap(Tag.tagValuesToString(localContext.getTags())); Assert.assertEquals(tagMap.get("tag1"), "value1"); Assert.assertEquals(tagMap.get("tag2"), "value2"); Assert.assertEquals(tagMap.get("tag3"), "value3"); MetricContext globalContext = rootBroker.getSharedResource(factory, new MetricContextKey()); Assert.assertEquals(localContext.getParent().get(), globalContext); tagMap = (Map<String, String>) Tag.toMap(Tag.tagValuesToString(globalContext.getTags())); Assert.assertEquals(tagMap.get("tag1"), "value1"); Assert.assertEquals(tagMap.get("tag2"), "value2"); Assert.assertFalse(tagMap.containsKey("tag3")); }
InnerMeter(MetricContext context, String name, ContextAwareMeter contextAwareMeter) { this.name = name; Optional<MetricContext> parentContext = context.getParent(); if (parentContext.isPresent()) { this.parentMeter = Optional.fromNullable(parentContext.get().contextAwareMeter(name)); } else { this.parentMeter = Optional.absent(); } this.contextAwareMeter = new WeakReference<>(contextAwareMeter); }
InnerTimer(MetricContext context, String name, ContextAwareTimer contextAwareTimer) { this.name = name; Optional<MetricContext> parentContext = context.getParent(); if (parentContext.isPresent()) { this.parentTimer = Optional.fromNullable(parentContext.get().contextAwareTimer(name)); } else { this.parentTimer = Optional.absent(); } this.timer = new WeakReference<>(contextAwareTimer); }
InnerTimer(MetricContext context, String name, ContextAwareTimer contextAwareTimer, long windowSize, TimeUnit unit) { super(new SlidingTimeWindowReservoir(windowSize, unit)); this.name = name; Optional<MetricContext> parentContext = context.getParent(); if (parentContext.isPresent()) { this.parentTimer = Optional.fromNullable(parentContext.get().contextAwareTimer(name, windowSize, unit)); } else { this.parentTimer = Optional.absent(); } this.timer = new WeakReference<>(contextAwareTimer); }
InnerHistogram(MetricContext context, String name, ContextAwareHistogram contextAwareHistogram, long windowSize, TimeUnit unit) { super(new SlidingTimeWindowReservoir(windowSize, unit)); this.name = name; Optional<MetricContext> parentContext = context.getParent(); if (parentContext.isPresent()) { this.parentHistogram = Optional.fromNullable(parentContext.get().contextAwareHistogram(name, windowSize, unit)); } else { this.parentHistogram = Optional.absent(); } this.contextAwareHistogram = new WeakReference<>(contextAwareHistogram); }
InnerHistogram(MetricContext context, String name, ContextAwareHistogram contextAwareHistogram) { super(new ExponentiallyDecayingReservoir()); this.name = name; Optional<MetricContext> parentContext = context.getParent(); if (parentContext.isPresent()) { this.parentHistogram = Optional.fromNullable(parentContext.get().contextAwareHistogram(name)); } else { this.parentHistogram = Optional.absent(); } this.contextAwareHistogram = new WeakReference<>(contextAwareHistogram); }
public InnerCounter(MetricContext context, String name, ContextAwareCounter counter) { this.tagged = new Tagged(); this.name = name; Optional<MetricContext> parentContext = context.getParent(); if (parentContext.isPresent()) { this.parentCounter = Optional.fromNullable(parentContext.get().contextAwareCounter(name)); } else { this.parentCounter = Optional.absent(); } this.contextAwareCounter = new WeakReference<>(counter); }