Search in sources :

Example 26 with MetricContext

use of org.apache.gobblin.metrics.MetricContext in project incubator-gobblin by apache.

the class InfluxDBEventReporterTest method testSimpleEvent.

@Test
public void testSimpleEvent() throws IOException {
    try (MetricContext metricContext = MetricContext.builder(this.getClass().getCanonicalName() + ".testInfluxDBReporter1").build();
        InfluxDBEventReporter influxEventReporter = getBuilder(metricContext).build()) {
        Map<String, String> metadata = Maps.newHashMap();
        metadata.put(JobEvent.METADATA_JOB_ID, "job1");
        metadata.put(TaskEvent.METADATA_TASK_ID, "task1");
        metricContext.submitEvent(GobblinTrackingEvent.newBuilder().setName(JobEvent.TASKS_SUBMITTED).setNamespace(NAMESPACE).setMetadata(metadata).build());
        try {
            Thread.sleep(100);
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
        influxEventReporter.report();
        try {
            Thread.sleep(100);
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
        TimestampedValue retrievedEvent = influxDB.getMetric("gobblin.metrics.job1.task1.events.TasksSubmitted");
        Assert.assertEquals(retrievedEvent.getValue(), "0.0");
        Assert.assertTrue(retrievedEvent.getTimestamp() <= (System.currentTimeMillis()));
    }
}
Also used : TimestampedValue(org.apache.gobblin.metrics.test.TimestampedValue) MetricContext(org.apache.gobblin.metrics.MetricContext) Test(org.testng.annotations.Test)

Example 27 with MetricContext

use of org.apache.gobblin.metrics.MetricContext in project incubator-gobblin by apache.

the class Instrumented method getMetricContext.

/**
 * Get a {@link org.apache.gobblin.metrics.MetricContext} to be used by an object needing instrumentation.
 *
 * <p>
 * This method will read the property "metrics.context.name" from the input State, and will attempt
 * to find a MetricContext with that name in the global instance of {@link org.apache.gobblin.metrics.GobblinMetricsRegistry}.
 * If it succeeds, the generated MetricContext will be a child of the retrieved Context, otherwise it will
 * be a parent-less context.
 * </p>
 * <p>
 * The method will automatically add two tags to the context:
 * <ul>
 *  <li> construct will contain the name of the {@link org.apache.gobblin.Constructs} that klazz represents. </li>
 *  <li> class will contain the canonical name of the input class. </li>
 * </ul>
 * </p>
 *
 * @param state {@link org.apache.gobblin.configuration.State} used to find the parent MetricContext.
 * @param klazz Class of the object needing instrumentation.
 * @param tags Additional tags to add to the returned context.
 * @return A {@link org.apache.gobblin.metrics.MetricContext} with the appropriate tags and parent.
 */
public static MetricContext getMetricContext(State state, Class<?> klazz, List<Tag<?>> tags) {
    int randomId = RAND.nextInt(Integer.MAX_VALUE);
    List<Tag<?>> generatedTags = Lists.newArrayList();
    Constructs construct = null;
    if (Converter.class.isAssignableFrom(klazz)) {
        construct = Constructs.CONVERTER;
    } else if (ForkOperator.class.isAssignableFrom(klazz)) {
        construct = Constructs.FORK_OPERATOR;
    } else if (RowLevelPolicy.class.isAssignableFrom(klazz)) {
        construct = Constructs.ROW_QUALITY_CHECKER;
    } else if (Extractor.class.isAssignableFrom(klazz)) {
        construct = Constructs.EXTRACTOR;
    } else if (DataWriter.class.isAssignableFrom(klazz)) {
        construct = Constructs.WRITER;
    }
    if (construct != null) {
        generatedTags.add(new Tag<>(GobblinMetricsKeys.CONSTRUCT_META, construct.toString()));
    }
    if (!klazz.isAnonymousClass()) {
        generatedTags.add(new Tag<>(GobblinMetricsKeys.CLASS_META, klazz.getCanonicalName()));
    }
    Optional<GobblinMetrics> gobblinMetrics = state.contains(METRIC_CONTEXT_NAME_KEY) ? GobblinMetricsRegistry.getInstance().get(state.getProp(METRIC_CONTEXT_NAME_KEY)) : Optional.<GobblinMetrics>absent();
    MetricContext.Builder builder = gobblinMetrics.isPresent() ? gobblinMetrics.get().getMetricContext().childBuilder(klazz.getCanonicalName() + "." + randomId) : MetricContext.builder(klazz.getCanonicalName() + "." + randomId);
    return builder.addTags(generatedTags).addTags(tags).build();
}
Also used : MetricContext(org.apache.gobblin.metrics.MetricContext) ForkOperator(org.apache.gobblin.fork.ForkOperator) GobblinMetrics(org.apache.gobblin.metrics.GobblinMetrics) Tag(org.apache.gobblin.metrics.Tag) Extractor(org.apache.gobblin.source.extractor.Extractor) Constructs(org.apache.gobblin.Constructs)

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