@Override public List<Tag<?>> getTags() { ImmutableList.Builder<Tag<?>> builder = ImmutableList.builder(); for (Map.Entry<String, Object> entry : this.tags.entrySet()) { builder.add(new Tag<Object>(entry.getKey(), entry.getValue())); } return builder.build(); }
/** * Get additional {@link Tag}s required for any type of reporting. */ private List<? extends Tag<?>> getMetadataTags(String applicationName, String applicationId) { return Tag.fromMap( new ImmutableMap.Builder<String, Object>().put(GobblinClusterMetricTagNames.APPLICATION_NAME, applicationName) .put(GobblinClusterMetricTagNames.APPLICATION_ID, applicationId).build()); }
/** * 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()); }
/** * Build the {@link EventSubmitter} for this class. */ private EventSubmitter buildEventSubmitter(List<? extends Tag<?>> tags) { return new EventSubmitter.Builder(this.runtimeMetricContext, "gobblin.runtime") .addMetadata(Tag.toMap(Tag.tagValuesToString(tags))).build(); }
@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")); }
private static List<Tag<?>> tagsForContainer(State containerState, String applicationName, String taskRunnerId) { ImmutableList.Builder<Tag<?>> tags = new ImmutableList.Builder<>(); tags.add(new Tag<>(GobblinClusterMetricTagNames.APPLICATION_NAME, applicationName)); tags.add(new Tag<>(GobblinClusterMetricTagNames.TASK_RUNNER_ID, taskRunnerId)); tags.addAll(getCustomTagsFromState(containerState)); return tags.build(); } }
@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); } }
/** * Takes a {@link List} of {@link Tag}s and returns a new {@link List} with the original {@link Tag}s as well as any * additional {@link Tag}s returned by {@link ClusterNameTags#getClusterNameTags()}. * * @see ClusterNameTags */ private static List<Tag<?>> addClusterNameTags(List<? extends Tag<?>> tags) { return ImmutableList.<Tag<?>>builder().addAll(tags).addAll(Tag.fromMap(ClusterNameTags.getClusterNameTags())) .build(); }
/** * 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(); }
@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")); }
/** * @return Tags to be applied to the {@link MetricContext} in this object. Called once in {@link #startUp()}. * Subclasses should override this method to add additional tags. */ protected List<Tag<?>> getTagsForMetrics() { List<Tag<?>> tags = Lists.newArrayList(); tags.add(new Tag<>(RuntimeMetrics.TOPIC, this.topic)); tags.add(new Tag<>(RuntimeMetrics.GROUP_ID, this.consumerConfig.groupId())); return tags; }
@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); }
private Compactor getCompactor(CompactorFactory compactorFactory, Optional<CompactorListener> compactorListener) { try { return compactorFactory .createCompactor(this.properties, Tag.fromMap(AzkabanTags.getAzkabanTags()), compactorListener); } catch (CompactorCreationException e) { throw new RuntimeException("Unable to create compactor", e); } }
/** * Build the {@link EventSubmitter} for this class. */ private EventSubmitter buildEventSubmitter(List<? extends Tag<?>> tags) { return new EventSubmitter.Builder(this.runtimeMetricContext, "gobblin.runtime") .addMetadata(Tag.toMap(Tag.tagValuesToString(tags))).build(); }
/** * Converts a {@link Map} of key, value pairs to a {@link List} of {@link Tag}s. Each key, value pair will be used to * create a new {@link Tag}. * * @param tagsMap a {@link Map} of key, value pairs that should be converted into {@link Tag}s * * @return a {@link List} of {@link Tag}s */ public static <T> List<Tag<T>> fromMap(Map<? extends String, T> tagsMap) { ImmutableList.Builder<Tag<T>> tagsBuilder = ImmutableList.builder(); for (Map.Entry<? extends String, T> entry : tagsMap.entrySet()) { tagsBuilder.add(new Tag<>(entry)); } return tagsBuilder.build(); }
@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}")); }
public AzkabanGobblinDaemon(String jobId, Properties props) throws Exception { super(jobId, LOG); List<Tag<?>> tags = Lists.newArrayList(); tags.addAll(Tag.fromMap(AzkabanTags.getAzkabanTags())); RootMetricContext.get(tags); this.daemon = new SchedulerDaemon(props); }