Search in sources :

Example 1 with KafkaMetricException

use of com.ibm.streamsx.kafka.KafkaMetricException in project streamsx.kafka by IBMStreams.

the class MetricsFetcher method getCurrentValue.

/**
 * Gets the current value of the metric given by the metric name.
 * @param metricName the name of the metric for which the value is to be fetched
 * @return the metric value converted for a custom metric by a. The converter is either a specific converter or the default converter.
 * @throws KafkaMetricException the metric value cannot be fetched - No such metric, metric does not match the filter
 */
public long getCurrentValue(MetricName metricName) throws KafkaMetricException {
    final Map<MetricName, ? extends Metric> metrics;
    synchronized (lock) {
        metrics = provider.getMetrics();
        final Metric m = metrics.get(metricName);
        if (m == null)
            throw new KafkaMetricException("No such metric: " + metricName);
        final long val = metricsFilter.getConverter(m).convert(m.metricValue());
        return val;
    }
}
Also used : MetricName(org.apache.kafka.common.MetricName) KafkaMetricException(com.ibm.streamsx.kafka.KafkaMetricException) Metric(org.apache.kafka.common.Metric)

Example 2 with KafkaMetricException

use of com.ibm.streamsx.kafka.KafkaMetricException in project streamsx.kafka by IBMStreams.

the class ProducerMetricsReporter method createOperatorMetricName.

/**
 * Creates a name for a operator custom metric. The name is topic + ":" + metricName.name()
 * for topic metrics or only the kafka metric name for producer metrics.
 *
 * @param metricName
 * @return The name of a the corresponding operator custom metric
 * @throws KafkaMetricException
 */
public static String createOperatorMetricName(MetricName metricName) throws KafkaMetricException {
    final String group = metricName.group();
    String customMetricName = metricName.name();
    if (group.equals(PRODUCER_TOPIC_METRICS_GROUP)) {
        final Map<String, String> tags = metricName.tags();
        if (tags == null)
            throw new KafkaMetricException("no tags for metric: " + group + " - " + customMetricName);
        final String topic = tags.get("topic");
        if (topic == null)
            throw new KafkaMetricException("no tags 'topic' for metric: " + group + " - " + customMetricName);
        return topic + ":" + customMetricName;
    } else if (group.equals(PRODUCER_METRICS_GROUP)) {
        return customMetricName;
    } else
        throw new KafkaMetricException("createCustomMetricName(): no implementation for metric group: " + group);
}
Also used : KafkaMetricException(com.ibm.streamsx.kafka.KafkaMetricException)

Example 3 with KafkaMetricException

use of com.ibm.streamsx.kafka.KafkaMetricException in project streamsx.kafka by IBMStreams.

the class AbstractKafkaConsumerClient method startConsumer.

/**
 * Validates the setup of the consumer client by calling the {@link #validate()} method,
 * creates the Kafka consumer object and starts the consumer and event thread.
 * This method ensures that the event thread is running when it returns.
 * Methods that overwrite this method must call super.startConsumer().
 * @throws InterruptedException The thread has been interrupted.
 * @throws KafkaClientInitializationException The client could not be initialized
 */
public void startConsumer() throws InterruptedException, KafkaClientInitializationException {
    try {
        validate();
    } catch (Exception e) {
        throw new KafkaClientInitializationException(e.getLocalizedMessage(), e);
    }
    consumerInitLatch = new CountDownLatch(1);
    Thread eventThread = getOperatorContext().getThreadFactory().newThread(new Runnable() {

        @Override
        public void run() {
            try {
                maxPollRecords = getMaxPollRecordsFromProperties(kafkaProperties);
                maxPollIntervalMs = getMaxPollIntervalMsFromProperties(kafkaProperties);
                fetchMaxBytes = getFetchMaxBytesFromProperties(kafkaProperties);
                SystemProperties.resolveApplicationDir(getOperatorContext().getPE().getApplicationDirectory().getAbsolutePath());
                consumer = new KafkaConsumer<>(kafkaProperties);
                processing.set(true);
            } catch (Exception e) {
                initializationException = e;
                return;
            } finally {
                // notify that consumer is ready
                consumerInitLatch.countDown();
            }
            try {
                runEventLoop();
            } catch (InterruptedException e) {
                logger.debug("Event thread interrupted. Terminating thread.");
                return;
            } finally {
                processing.set(false);
                logger.info("event thread (tid = " + Thread.currentThread().getId() + ") ended.");
            }
        }
    });
    eventThread.setDaemon(false);
    eventThread.start();
    // wait for consumer thread to be running before returning
    consumerInitLatch.await();
    if (this.metricsFetcher == null) {
        this.metricsFetcher = new MetricsFetcher(getOperatorContext(), new MetricsProvider() {

            @Override
            public Map<MetricName, ? extends org.apache.kafka.common.Metric> getMetrics() {
                return consumer.metrics();
            }

            @Override
            public String createCustomMetricName(MetricName metricName) throws KafkaMetricException {
                return ConsumerMetricsReporter.createOperatorMetricName(metricName);
            }
        }, ConsumerMetricsReporter.getMetricsFilter(), AbstractKafkaClient.METRICS_REPORT_INTERVAL);
    }
    if (initializationException != null)
        throw new KafkaClientInitializationException(initializationException.getLocalizedMessage(), initializationException);
}
Also used : KafkaConsumer(org.apache.kafka.clients.consumer.KafkaConsumer) CountDownLatch(java.util.concurrent.CountDownLatch) SerializationException(org.apache.kafka.common.errors.SerializationException) ConsumerCommitFailedException(com.ibm.streamsx.kafka.ConsumerCommitFailedException) KafkaClientInitializationException(com.ibm.streamsx.kafka.KafkaClientInitializationException) KafkaOperatorRuntimeException(com.ibm.streamsx.kafka.KafkaOperatorRuntimeException) KafkaMetricException(com.ibm.streamsx.kafka.KafkaMetricException) RetriableException(org.apache.kafka.common.errors.RetriableException) UnknownTopicException(com.ibm.streamsx.kafka.UnknownTopicException) KafkaConfigurationException(com.ibm.streamsx.kafka.KafkaConfigurationException) CommitFailedException(org.apache.kafka.clients.consumer.CommitFailedException) MetricName(org.apache.kafka.common.MetricName) KafkaClientInitializationException(com.ibm.streamsx.kafka.KafkaClientInitializationException) MetricsFetcher(com.ibm.streamsx.kafka.clients.metrics.MetricsFetcher) MetricsProvider(com.ibm.streamsx.kafka.clients.metrics.MetricsProvider)

Example 4 with KafkaMetricException

use of com.ibm.streamsx.kafka.KafkaMetricException in project streamsx.kafka by IBMStreams.

the class MetricFilter method getConverter.

/**
 * Returns the metric converter for a metric.
 * @param m the Kafka metric
 * @return the converter for the given metric
 * @throws KafkaMetricException The Metric does not apply the the filter.
 */
public MetricConverter getConverter(Metric m) throws KafkaMetricException {
    final MetricName metricName = m.metricName();
    if (!apply(m)) {
        throw new KafkaMetricException("Metric " + metricName.group() + ":" + metricName.name() + " does not apply to the filter");
    }
    MetricConverter c = filters.get(metricName.group()).get(metricName.name());
    return c == null ? this.defaultConverter : c;
}
Also used : MetricName(org.apache.kafka.common.MetricName) KafkaMetricException(com.ibm.streamsx.kafka.KafkaMetricException)

Example 5 with KafkaMetricException

use of com.ibm.streamsx.kafka.KafkaMetricException in project streamsx.kafka by IBMStreams.

the class AbstractCustomMetricReporter method metricRemoval.

/**
 * This is called whenever a metric is removed.
 * It sets the value of the corresponding operator metric,
 * if there is one, to {@value #METRIC_NOT_APPLICABLE} as custom metrics cannot be deleted from operators.
 *
 * @param metric The metric that is removed from Kafka
 */
@Override
public void metricRemoval(KafkaMetric metric) {
    trace.log(DEBUG_LEVEL, "metricRemoval(): " + metric.metricName());
    synchronized (this) {
        if (getFilter().apply(metric)) {
            final MetricName metricName = metric.metricName();
            try {
                final String customMetricName = createCustomMetricName(metricName);
                OperatorMetrics operatorMetrics = operatorCtx.getMetrics();
                if (operatorMetrics.getCustomMetrics().containsKey(customMetricName)) {
                    operatorMetrics.getCustomMetric(customMetricName).setValue(METRIC_NOT_APPLICABLE);
                }
            } catch (KafkaMetricException e) {
                trace.warn("Cannot derive custom metric from Kafka metric '" + metricName + "': " + e.getLocalizedMessage());
            }
        } else {
            trace.log(DEBUG_LEVEL, "Kafka metric NOT exposed as custom metric: " + metric.metricName());
        }
    }
}
Also used : MetricName(org.apache.kafka.common.MetricName) OperatorMetrics(com.ibm.streams.operator.metrics.OperatorMetrics) KafkaMetricException(com.ibm.streamsx.kafka.KafkaMetricException)

Aggregations

KafkaMetricException (com.ibm.streamsx.kafka.KafkaMetricException)6 MetricName (org.apache.kafka.common.MetricName)5 OperatorMetrics (com.ibm.streams.operator.metrics.OperatorMetrics)2 ConsumerCommitFailedException (com.ibm.streamsx.kafka.ConsumerCommitFailedException)1 KafkaClientInitializationException (com.ibm.streamsx.kafka.KafkaClientInitializationException)1 KafkaConfigurationException (com.ibm.streamsx.kafka.KafkaConfigurationException)1 KafkaOperatorRuntimeException (com.ibm.streamsx.kafka.KafkaOperatorRuntimeException)1 UnknownTopicException (com.ibm.streamsx.kafka.UnknownTopicException)1 MetricsFetcher (com.ibm.streamsx.kafka.clients.metrics.MetricsFetcher)1 MetricsProvider (com.ibm.streamsx.kafka.clients.metrics.MetricsProvider)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 CommitFailedException (org.apache.kafka.clients.consumer.CommitFailedException)1 KafkaConsumer (org.apache.kafka.clients.consumer.KafkaConsumer)1 Metric (org.apache.kafka.common.Metric)1 RetriableException (org.apache.kafka.common.errors.RetriableException)1 SerializationException (org.apache.kafka.common.errors.SerializationException)1