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()));
}
}
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();
}
Aggregations