Github user bbende commented on a diff in the pull request: https://github.com/apache/nifi/pull/2171#discussion_r142979242 --- Diff: nifi-nar-bundles/nifi-metrics-reporting-bundle/nifi-metrics-reporting-task/src/main/java/org/apache/nifi/metrics/reporting/reporter/service/GraphiteMetricReporterService.java --- @@ -0,0 +1,147 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.nifi.metrics.reporting.reporter.service; + +import com.codahale.metrics.MetricRegistry; +import com.codahale.metrics.ScheduledReporter; +import com.codahale.metrics.graphite.Graphite; +import com.codahale.metrics.graphite.GraphiteReporter; +import com.codahale.metrics.graphite.GraphiteSender; +import org.apache.nifi.annotation.documentation.CapabilityDescription; +import org.apache.nifi.annotation.documentation.Tags; +import org.apache.nifi.annotation.lifecycle.OnDisabled; +import org.apache.nifi.annotation.lifecycle.OnEnabled; +import org.apache.nifi.components.PropertyDescriptor; +import org.apache.nifi.controller.AbstractControllerService; +import org.apache.nifi.controller.ConfigurationContext; +import org.apache.nifi.metrics.reporting.task.MetricsReportingTask; +import org.apache.nifi.processor.util.StandardValidators; + +import javax.net.SocketFactory; +import java.io.IOException; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * A controller service that provides metric reporters for graphite, can be used by {@link MetricsReportingTask}. + * + * @author Omer Hadari + */ +@Tags({"metrics", "reporting", "graphite"}) +@CapabilityDescription("A controller service that provides metric reporters for graphite. " + + "Used by MetricsReportingTask.") +public class GraphiteMetricReporterService extends AbstractControllerService implements MetricReporterService { + + /** Points to the hostname of the graphite listener. */ + public static final PropertyDescriptor HOST = new PropertyDescriptor.Builder() + .name("host") + .displayName("host") + .description("The hostname of the carbon listener") + .required(true) + .addValidator(StandardValidators.URI_VALIDATOR) + .build(); + + /** Points to the port on which the graphite server listens. */ + public static final PropertyDescriptor PORT = new PropertyDescriptor.Builder() + .name("port") + .displayName("port") + .description("The port on which carbon listens") + .required(true) + .addValidator(StandardValidators.PORT_VALIDATOR) + .build(); + + /** Points to the charset name that the graphite server expects. */ + public static final PropertyDescriptor CHARSET = new PropertyDescriptor.Builder() + .name("charset") + .displayName("charset") + .description("The charset used by the graphite server") + .required(true) + .defaultValue("UTF-8") + .addValidator(StandardValidators.CHARACTER_SET_VALIDATOR) + .build(); + + /** List of property descriptors used by the service. */ + private static final List<PropertyDescriptor> properties; + + static { + final List<PropertyDescriptor> props = new ArrayList<>(); + props.add(HOST); + props.add(PORT); + properties = Collections.unmodifiableList(props); + } + + /** Graphite sender, a connection to the server. */ + private GraphiteSender graphiteSender; + + /** + * Create the {@link #graphiteSender} according to configuration. + * + * @param context used to access properties. + */ + @OnEnabled + public void onEnabled(final ConfigurationContext context) { + String host = context.getProperty(HOST).getValue(); + int port = context.getProperty(PORT).asInteger(); + Charset charset = Charset.forName(context.getProperty(CHARSET).getValue()); + graphiteSender = createSender(host, port, charset); + } + + /** + * Close the graphite sender. + * + * @throws IOException if failed to close the connection. + */ + @OnDisabled + public void shutdown() throws IOException { + try { + graphiteSender.close(); + } finally { + graphiteSender = null; + } + } + + /** + * Use the {@link #graphiteSender} in order to create a reporter. + * + * @param metricRegistry registry with the metrics to report. + * @return a reporter instance. + */ + @Override + public ScheduledReporter createReporter(MetricRegistry metricRegistry) { + return GraphiteReporter.forRegistry(metricRegistry).build(graphiteSender); + + } + + /** + * Create a sender. + * + * @param host the hostname of the server to connect to. + * @param port the port on which the server listens. + * @param charset the charset in which the server expects logs. + * @return The created sender. + */ + protected GraphiteSender createSender(String host, int port, Charset charset) { + return new Graphite(host, port, SocketFactory.getDefault(), charset); --- End diff -- Is there ever a case where the metrics would be sent over an SSL/TLS connection?
---