Skip to content

Commit

Permalink
[FLINK-7155][metrics] Add InfluxDB reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
1u0 authored and zentol committed Feb 13, 2019
1 parent f1fa27b commit 3d39cb0
Show file tree
Hide file tree
Showing 18 changed files with 1,122 additions and 0 deletions.
29 changes: 29 additions & 0 deletions docs/monitoring/metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,35 @@ metrics.reporter.grph.protocol: TCP

{% endhighlight %}

### InfluxDB (org.apache.flink.metrics.influxdb.InfluxdbReporter)

In order to use this reporter you must copy `/opt/flink-metrics-influxdb-{{site.version}}.jar` into the `/lib` folder
of your Flink distribution.

Parameters:

- `host` - the InfluxDB server host
- `port` - (optional) the InfluxDB server port, defaults to `8086`
- `db` - the InfluxDB database to store metrics
- `username` - (optional) InfluxDB username used for authentication
- `password` - (optional) InfluxDB username's password used for authentication

Example configuration:

{% highlight yaml %}

metrics.reporter.influxdb.class: org.apache.flink.metrics.influxdb.InfluxdbReporter
metrics.reporter.influxdb.host: localhost
metrics.reporter.influxdb.port: 8086
metrics.reporter.influxdb.db: flink
metrics.reporter.influxdb.username: flink-metrics
metrics.reporter.influxdb.password: qwerty

{% endhighlight %}

The reporter would send metrics using http protocol with default retention policy defined on InfluxDB server.
All Flink metrics variables (see [List of all Variables](#list-of-all-variables)) are exported as InfluxDB tags.

### Prometheus (org.apache.flink.metrics.prometheus.PrometheusReporter)

In order to use this reporter you must copy `/opt/flink-metrics-prometheus{{site.scala_version_suffix}}-{{site.version}}.jar` into the `/lib` folder
Expand Down
7 changes: 7 additions & 0 deletions flink-dist/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,13 @@ under the License.
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-metrics-influxdb_${scala.binary.version}</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-metrics-prometheus_${scala.binary.version}</artifactId>
Expand Down
7 changes: 7 additions & 0 deletions flink-dist/src/main/assemblies/opt.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@
<fileMode>0644</fileMode>
</file>

<file>
<source>../flink-metrics/flink-metrics-influxdb/target/flink-metrics-influxdb_${scala.binary.version}-${project.version}.jar</source>
<outputDirectory>opt/</outputDirectory>
<destName>flink-metrics-influxdb-${project.version}.jar</destName>
<fileMode>0644</fileMode>
</file>

<file>
<source>../flink-metrics/flink-metrics-prometheus/target/flink-metrics-prometheus_${scala.binary.version}-${project.version}.jar</source>
<outputDirectory>opt/</outputDirectory>
Expand Down
174 changes: 174 additions & 0 deletions flink-metrics/flink-metrics-influxdb/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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
https://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.
-->
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.apache.flink</groupId>
<artifactId>flink-metrics</artifactId>
<version>1.8-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>

<artifactId>flink-metrics-influxdb_${scala.binary.version}</artifactId>
<name>flink-metrics-influxdb</name>

<dependencies>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-core</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-runtime_${scala.binary.version}</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-metrics-core</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.influxdb</groupId>
<artifactId>influxdb-java</artifactId>
<version>2.14</version>
</dependency>

<!-- test dependencies -->

<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-metrics-core</artifactId>
<version>${project.version}</version>
<scope>test</scope>
<type>test-jar</type>
</dependency>

<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-test-utils-junit</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
<version>2.19.0</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.11.0</version>
</dependency>
<dependency>
<groupId>com.squareup.okio</groupId>
<artifactId>okio</artifactId>
<version>1.14.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.11</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.11.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.11</version>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<id>shade-flink</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<includes>
<include>org.influxdb:*</include>
<include>com.squareup.moshi:*</include>
<include>com.squareup.okhttp3:*</include>
<include>com.squareup.okio:*</include>
<include>com.squareup.retrofit2:*</include>
<!-- org.msgpack:msgpack-core is not required -->
</includes>
</artifactSet>
<relocations combine.children="append">
<relocation>
<pattern>org.influxdb</pattern>
<shadedPattern>org.apache.flink.metrics.influxdb.shaded.org.influxdb</shadedPattern>
</relocation>
<relocation>
<pattern>com.squareup.moshi</pattern>
<shadedPattern>org.apache.flink.metrics.influxdb.shaded.com.squareup.moshi</shadedPattern>
</relocation>
<relocation>
<pattern>okhttp3</pattern>
<shadedPattern>org.apache.flink.metrics.influxdb.shaded.okhttp3</shadedPattern>
</relocation>
<relocation>
<pattern>okio</pattern>
<shadedPattern>org.apache.flink.metrics.influxdb.shaded.okio</shadedPattern>
</relocation>
<relocation>
<pattern>retrofit2</pattern>
<shadedPattern>org.apache.flink.metrics.influxdb.shaded.retrofit2</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* 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
*
* https://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.flink.metrics.influxdb;

import org.apache.flink.metrics.Counter;
import org.apache.flink.metrics.Gauge;
import org.apache.flink.metrics.Histogram;
import org.apache.flink.metrics.Meter;
import org.apache.flink.metrics.Metric;
import org.apache.flink.metrics.MetricGroup;
import org.apache.flink.metrics.reporter.MetricReporter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
* An abstract reporter with registry for metrics.
* It's same as {@link org.apache.flink.metrics.reporter.AbstractReporter} but with generalized information of metric.
*
* @param <MetricInfo> Custom metric information type
*/
abstract class AbstractReporter<MetricInfo> implements MetricReporter {
protected final Logger log = LoggerFactory.getLogger(getClass());

protected final Map<Gauge<?>, MetricInfo> gauges = new HashMap<>();
protected final Map<Counter, MetricInfo> counters = new HashMap<>();
protected final Map<Histogram, MetricInfo> histograms = new HashMap<>();
protected final Map<Meter, MetricInfo> meters = new HashMap<>();
protected final MetricInfoProvider<MetricInfo> metricInfoProvider;

protected AbstractReporter(MetricInfoProvider<MetricInfo> metricInfoProvider) {
this.metricInfoProvider = metricInfoProvider;
}

@Override
public void notifyOfAddedMetric(Metric metric, String metricName, MetricGroup group) {
final MetricInfo metricInfo = metricInfoProvider.getMetricInfo(metricName, group);
synchronized (this) {
if (metric instanceof Counter) {
counters.put((Counter) metric, metricInfo);
} else if (metric instanceof Gauge) {
gauges.put((Gauge<?>) metric, metricInfo);
} else if (metric instanceof Histogram) {
histograms.put((Histogram) metric, metricInfo);
} else if (metric instanceof Meter) {
meters.put((Meter) metric, metricInfo);
} else {
log.warn("Cannot add unknown metric type {}. This indicates that the reporter " +
"does not support this metric type.", metric.getClass().getName());
}
}
}

@Override
public void notifyOfRemovedMetric(Metric metric, String metricName, MetricGroup group) {
synchronized (this) {
if (metric instanceof Counter) {
counters.remove(metric);
} else if (metric instanceof Gauge) {
gauges.remove(metric);
} else if (metric instanceof Histogram) {
histograms.remove(metric);
} else if (metric instanceof Meter) {
meters.remove(metric);
} else {
log.warn("Cannot remove unknown metric type {}. This indicates that the reporter " +
"does not support this metric type.", metric.getClass().getName());
}
}
}
}
Loading

0 comments on commit 3d39cb0

Please sign in to comment.