com.opentable.kafka.metrics.OtMetricsReporter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of otj-kafka Show documentation
Show all versions of otj-kafka Show documentation
Kafka integrations component
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.opentable.kafka.metrics;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Pattern;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.SharedMetricRegistries;
import org.apache.commons.lang3.text.StrSubstitutor;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.common.MetricName;
import org.apache.kafka.common.metrics.KafkaMetric;
import org.apache.kafka.common.metrics.MetricsReporter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.AntPathMatcher;
import com.opentable.kafka.util.ClientIdGenerator;
public class OtMetricsReporter implements MetricsReporter {
private static final Logger LOG = LoggerFactory.getLogger(OtMetricsReporter.class);
private static final Pattern blacklistedChars = Pattern.compile("[{}(),=\\[\\]/]");
private final Set metricNames = new HashSet<>();
private final Set metricGroups = new HashSet<>();
private final Map metricTags = new HashMap<>();
private MetricRegistry metricRegistry;
private String prefix;
private final Set groups = new HashSet<>();
private final Set metricMatchers = new HashSet<>();
private final AntPathMatcher matcher = new AntPathMatcher("-");
public OtMetricsReporter() { //NOPMD
/* no args needed for kafka */
}
@Override
public synchronized void init(List metrics) {
metrics.forEach(this::metricChange);
}
@Override
public synchronized void metricChange(KafkaMetric metric) {
// If we qualify to use this metric ...
if (filterMetric(metric)) {
// ... derive its name
final String name = metricName(metric);
try {
// Map the Measureable or kafka.gauge type to a dropwizard Gauge