Search in sources :

Example 1 with Constructs

use of org.apache.gobblin.Constructs 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

Constructs (org.apache.gobblin.Constructs)1 ForkOperator (org.apache.gobblin.fork.ForkOperator)1 GobblinMetrics (org.apache.gobblin.metrics.GobblinMetrics)1 MetricContext (org.apache.gobblin.metrics.MetricContext)1 Tag (org.apache.gobblin.metrics.Tag)1 Extractor (org.apache.gobblin.source.extractor.Extractor)1