Search in sources :

Example 1 with MetricContext

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();
}
Also used : MetricContext(org.apache.gobblin.metrics.MetricContext) UUID(java.util.UUID)

Example 2 with MetricContext

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"));
}
Also used : MetricContext(org.apache.gobblin.metrics.MetricContext) Config(com.typesafe.config.Config) SimpleScopeType(org.apache.gobblin.broker.SimpleScopeType) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 3 with MetricContext

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"));
}
Also used : MetricContext(org.apache.gobblin.metrics.MetricContext) Config(com.typesafe.config.Config) SimpleScopeType(org.apache.gobblin.broker.SimpleScopeType) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 4 with MetricContext

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);
}
Also used : MetricContext(org.apache.gobblin.metrics.MetricContext) Tag(org.apache.gobblin.metrics.Tag)

Example 5 with MetricContext

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);
    }
}
Also used : ConfigValue(com.typesafe.config.ConfigValue) NoSuchScopeException(org.apache.gobblin.broker.iface.NoSuchScopeException) MetricContext(org.apache.gobblin.metrics.MetricContext) RootMetricContext(org.apache.gobblin.metrics.RootMetricContext) ResourceInstance(org.apache.gobblin.broker.ResourceInstance) Map(java.util.Map)

Aggregations

MetricContext (org.apache.gobblin.metrics.MetricContext)27 Test (org.testng.annotations.Test)20 Properties (java.util.Properties)8 Counter (com.codahale.metrics.Counter)7 Meter (com.codahale.metrics.Meter)7 Tag (org.apache.gobblin.metrics.Tag)7 Timer (com.codahale.metrics.Timer)6 Histogram (com.codahale.metrics.Histogram)5 Map (java.util.Map)4 GobblinTrackingEvent (org.apache.gobblin.metrics.GobblinTrackingEvent)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 Config (com.typesafe.config.Config)3 MetricReport (org.apache.gobblin.metrics.MetricReport)3 KafkaEventReporter (org.apache.gobblin.metrics.kafka.KafkaEventReporter)3 KafkaReporter (org.apache.gobblin.metrics.kafka.KafkaReporter)3 Gauge (com.codahale.metrics.Gauge)2 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)2 SimpleScopeType (org.apache.gobblin.broker.SimpleScopeType)2 NotConfiguredException (org.apache.gobblin.broker.iface.NotConfiguredException)2 ContextAwareGauge (org.apache.gobblin.metrics.ContextAwareGauge)2