|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package com.addthis.metrics3.reporter.config; |
| 16 | + |
| 17 | +import java.util.List; |
| 18 | +import java.util.LinkedList; |
| 19 | +import java.util.Properties; |
| 20 | +import java.util.StringJoiner; |
| 21 | + |
| 22 | +import com.codahale.metrics.MetricRegistry; |
| 23 | +import com.addthis.metrics.reporter.config.HostPort; |
| 24 | +import com.addthis.metrics.reporter.config.AbstractKafkaReporterConfig; |
| 25 | + |
| 26 | +import org.slf4j.Logger; |
| 27 | +import org.slf4j.LoggerFactory; |
| 28 | + |
| 29 | +import io.github.hengyunabc.metrics.KafkaReporter; |
| 30 | +import kafka.producer.ProducerConfig; |
| 31 | + |
| 32 | + |
| 33 | +public class KafkaReporterConfig extends AbstractKafkaReporterConfig implements MetricsReporterConfigThree |
| 34 | +{ |
| 35 | + private static final Logger log = LoggerFactory.getLogger(KafkaReporterConfig.class); |
| 36 | + |
| 37 | + private MetricRegistry registry; |
| 38 | + |
| 39 | + private KafkaReporter reporter; |
| 40 | + |
| 41 | + private boolean checkClass(String className) { |
| 42 | + if (!isClassAvailable(className)) |
| 43 | + { |
| 44 | + log.error("Tried to enable KafkaReporter, but class {} was not found", className); |
| 45 | + return false; |
| 46 | + } else |
| 47 | + { |
| 48 | + return true; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public boolean enable(MetricRegistry registry) { |
| 54 | + this.registry = registry; |
| 55 | + |
| 56 | + boolean success = checkClass("com.addthis.metrics.reporter.config.AbstractKafkaReporterConfig"); |
| 57 | + if (!success) |
| 58 | + { |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + List<HostPort> hosts = getFullHostList(); |
| 63 | + if (hosts == null || hosts.isEmpty()) |
| 64 | + { |
| 65 | + log.error("No hosts specified, cannot enable KafkaReporter"); |
| 66 | + return false; |
| 67 | + } |
| 68 | + |
| 69 | + log.info("Enabling KafkaReporter to {}", ""); |
| 70 | + try |
| 71 | + { |
| 72 | + StringJoiner brokerList = new StringJoiner(","); |
| 73 | + for (HostPort host : getFullHostList()) { |
| 74 | + brokerList.add(host.toString()); |
| 75 | + } |
| 76 | + |
| 77 | + Properties props = new Properties(); |
| 78 | + props.put("metadata.broker.list", brokerList.toString()); |
| 79 | + props.put("serializer.class", getSerializer()); |
| 80 | + props.put("partitioner.class", getPartitioner()); |
| 81 | + props.put("request.required.acks", getRequiredAcks()); |
| 82 | + ProducerConfig config = new ProducerConfig(props); |
| 83 | + |
| 84 | + reporter = KafkaReporter.forRegistry(registry) |
| 85 | + .config(config) |
| 86 | + .topic(getTopic()) |
| 87 | + .hostName(getHostname()) |
| 88 | + .ip(getIp()) |
| 89 | + .labels(getResolvedLabels()) |
| 90 | + .prefix(getResolvedPrefix()) |
| 91 | + .filter(MetricFilterTransformer.generateFilter(getPredicate())) |
| 92 | + .build(); |
| 93 | + |
| 94 | + reporter.start(getPeriod(), getRealTimeunit()); |
| 95 | + } |
| 96 | + catch (Exception e) |
| 97 | + { |
| 98 | + log.error("Failure while Enabling KafkaReporter", e); |
| 99 | + return false; |
| 100 | + } |
| 101 | + return true; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void report() { |
| 106 | + if (reporter != null) { |
| 107 | + reporter.report(); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | +} |
0 commit comments