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