use of org.apache.gobblin.metrics.MetricContext in project incubator-gobblin by apache.
the class Instrumented method newContextFromReferenceContext.
/**
* Generates a new {@link org.apache.gobblin.metrics.MetricContext} with the parent and tags taken from the reference context.
* Allows replacing {@link org.apache.gobblin.metrics.Tag} with new input tags.
* This method will not copy any {@link org.apache.gobblin.metrics.Metric} contained in the reference {@link org.apache.gobblin.metrics.MetricContext}.
*
* @param context Reference {@link org.apache.gobblin.metrics.MetricContext}.
* @param newTags New {@link org.apache.gobblin.metrics.Tag} to apply to context. Repeated keys will override old tags.
* @param name Name of the new {@link org.apache.gobblin.metrics.MetricContext}.
* If absent or empty, will modify old name by adding a random integer at the end.
* @return Generated {@link org.apache.gobblin.metrics.MetricContext}.
*/
public static MetricContext newContextFromReferenceContext(MetricContext context, List<Tag<?>> newTags, Optional<String> name) {
String newName = name.orNull();
if (Strings.isNullOrEmpty(newName)) {
UUID uuid = UUID.randomUUID();
String randomIdPrefix = "uuid:";
String oldName = context.getName();
List<String> splitName = Strings.isNullOrEmpty(oldName) ? Lists.<String>newArrayList() : Lists.newArrayList(Splitter.on(".").splitToList(oldName));
if (splitName.size() > 0 && StringUtils.startsWith(Iterables.getLast(splitName), randomIdPrefix)) {
splitName.set(splitName.size() - 1, String.format("%s%s", randomIdPrefix, uuid.toString()));
} else {
splitName.add(String.format("%s%s", randomIdPrefix, uuid.toString()));
}
newName = Joiner.on(".").join(splitName);
}
MetricContext.Builder builder = context.getParent().isPresent() ? context.getParent().get().childBuilder(newName) : MetricContext.builder(newName);
return builder.addTags(context.getTags()).addTags(newTags).build();
}
use of org.apache.gobblin.metrics.MetricContext in project incubator-gobblin by apache.
the class MetricContextFactoryTest method test.
@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"));
}
use of org.apache.gobblin.metrics.MetricContext in project incubator-gobblin by apache.
the class MetricContextFactoryTest method testSubTaggedMetricContext.
@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"));
}
use of org.apache.gobblin.metrics.MetricContext in project incubator-gobblin by apache.
the class ReporterExampleBase method addTask.
private void addTask(int taskIndex, CountDownLatch countDownLatch) {
// Build the context of this task, which is a child of the job's context.
// Tags of the job (parent) context will be inherited automatically.
MetricContext taskContext = this.context.childBuilder("Task" + taskIndex).addTag(new Tag<String>(TASK_ID_KEY, TASK_ID_PREFIX + taskIndex)).build();
Task task = new Task(taskContext, taskIndex, this.totalRecords, countDownLatch);
this.executor.execute(task);
}
use of org.apache.gobblin.metrics.MetricContext in project incubator-gobblin by apache.
the class MetricContextFactory method createResource.
@Override
public SharedResourceFactoryResponse<MetricContext> createResource(SharedResourcesBroker<S> broker, ScopedConfigView<S, MetricContextKey> config) throws NotConfiguredException {
try {
if (config.getKey() instanceof SubTaggedMetricContextKey) {
SubTaggedMetricContextKey key = (SubTaggedMetricContextKey) config.getKey();
MetricContext parent = broker.getSharedResource(this, new MetricContextKey());
MetricContext.Builder builder = parent.childBuilder(key.getMetricContextName());
for (Map.Entry<String, String> entry : key.getTags().entrySet()) {
builder.addTag(new Tag<>(entry.getKey(), entry.getValue()));
}
return new ResourceInstance<>(builder.build());
}
MetricContext parentMetricContext = RootMetricContext.get();
Collection<S> parents = config.getScope().parentScopes();
if (parents != null && !parents.isEmpty()) {
S parentScope = parents.iterator().next();
parentMetricContext = broker.getSharedResourceAtScope(this, config.getKey(), parentScope);
}
// If this is the root scope, append a UUID to the name. This allows having a separate root context per broker.
String metricContextName = parents == null ? config.getScope().name() + "_" + UUID.randomUUID().toString() : broker.selfScope().getScopeId();
MetricContext.Builder builder = parentMetricContext.childBuilder(metricContextName);
builder.addTag(new Tag<>(config.getScope().name(), broker.getScope(config.getScope()).getScopeId()));
for (Map.Entry<String, ConfigValue> entry : ConfigUtils.getConfigOrEmpty(config.getConfig(), TAG_KEY).entrySet()) {
builder.addTag(new Tag<>(entry.getKey(), entry.getValue().unwrapped()));
}
return new ResourceInstance<>(builder.build());
} catch (NoSuchScopeException nsse) {
throw new RuntimeException("Could not create MetricContext.", nsse);
}
}
Aggregations