Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

InfluxDB support with external authentication #170

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@
<name>bintray</name>
<url>https://jcenter.bintray.com</url>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand All @@ -71,6 +75,7 @@
<junit.version>4.12</junit.version>
<mockito.version>1.9.5</mockito.version>
<servlet.version>3.1.0</servlet.version>
<metrics-influxdb.version>master-SNAPSHOT</metrics-influxdb.version>
</properties>
<dependencyManagement>
<dependencies>
Expand Down Expand Up @@ -159,6 +164,11 @@
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.github.davidB</groupId>
<artifactId>metrics-influxdb</artifactId>
<version>${metrics-influxdb.version}</version>
</dependency>
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-core</artifactId>
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/ryantenney/metrics/spring/InfluxdbAuthInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright (C) 2016 Andrey Smorodin ([email protected])
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.ryantenney.metrics.spring;

public interface InfluxdbAuthInfo {
String getUserName();

String getPassword();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Copyright (C) 2016 Andrey Smorodin ([email protected])
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.ryantenney.metrics.spring.reporter;

import static com.ryantenney.metrics.spring.reporter.AbstractReporterFactoryBean.FILTER_PATTERN;
import static com.ryantenney.metrics.spring.reporter.AbstractReporterFactoryBean.FILTER_REF;
import static com.ryantenney.metrics.spring.reporter.InfluxdbReporterFactoryBean.*;

public class InfluxdbReporterElementParser extends AbstractReporterElementParser {
@Override
public String getType() {
return "influxdb";
}

@Override
protected Class<?> getBeanClass() {
return InfluxdbReporterFactoryBean.class;
}

@Override
protected void validate(ValidationContext c) {
c.require(HOST);
c.require(PORT, PORT_NUMBER_REGEX, "Port number is required and must be between 1-65536");
c.require(DATABASE);
c.require(INFLUXDB_CONNECTION_FACTORY_REF);
c.require(PERIOD, DURATION_STRING_REGEX, "Period is required and must be in the form '\\d+(ns|us|ms|s|m|h|d)'");

c.optional(SKIP_IDLE, SKIP_IDLE_REGEX, "Skip idle parameter can be only true or false");
c.optional(RATE_UNIT, TIMEUNIT_STRING_REGEX, "Rate unit must be one of the enum constants from java.util.concurrent.TimeUnit");
c.optional(DURATION_UNIT, TIMEUNIT_STRING_REGEX, "Duration unit must be one of the enum constants from java.util.concurrent.TimeUnit");
c.optional(METRIC_MEASUREMENT_TRANSFORMER);
c.optional(FILTER_PATTERN);
c.optional(FILTER_REF);
if (c.has(FILTER_PATTERN) && c.has(FILTER_REF)) {
c.reject(FILTER_REF, "Reporter element must not specify both the 'filter' and 'filter-ref' attributes");
}

c.rejectUnmatchedProperties();
}

private static final String SKIP_IDLE_REGEX = "^(?:true|false)$";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* Copyright (C) 2016 Andrey Smorodin ([email protected])
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.ryantenney.metrics.spring.reporter;

import com.codahale.metrics.ScheduledReporter;
import com.ryantenney.metrics.spring.InfluxdbAuthInfo;
import metrics_influxdb.InfluxdbReporter;
import metrics_influxdb.api.measurements.MetricMeasurementTransformer;
import metrics_influxdb.api.protocols.InfluxdbProtocols;

import java.util.concurrent.TimeUnit;

public class InfluxdbReporterFactoryBean extends AbstractScheduledReporterFactoryBean<ScheduledReporter> {

//Defaults
public static final String DEFAULT_USERNAME = "admin";
public static final String DEFAULT_PASSWORD = "admin";


//Required
public static final String HOST = "host";
public static final String PORT = "port";
public static final String DATABASE = "database";
public static final String INFLUXDB_CONNECTION_FACTORY_REF = "influxdb-auth-ref";
public static final String PERIOD = "period";

//Optional
public static final String SKIP_IDLE = "skipIdle";
public static final String DURATION_UNIT = "duration-unit";
public static final String RATE_UNIT = "rate-unit";
public static final String METRIC_MEASUREMENT_TRANSFORMER = "metric-measurement-transformer-ref";

@Override
public Class<ScheduledReporter> getObjectType() {
return ScheduledReporter.class;
}

@Override
protected ScheduledReporter createInstance() {
final InfluxdbReporter.Builder reporter = InfluxdbReporter.forRegistry(getMetricRegistry());

final String hostname = getProperty(HOST);
final int port = getProperty(PORT, Integer.TYPE);
final String database = getProperty(DATABASE);
final String skipIdleMetrics = getProperty(SKIP_IDLE);

if ("true".equals(skipIdleMetrics)) {
reporter.skipIdleMetrics(true);
} else {
reporter.skipIdleMetrics(false);
}

if (hasProperty(RATE_UNIT))
reporter.convertRatesTo(getProperty(RATE_UNIT, TimeUnit.class));

if (hasProperty(DURATION_UNIT))
reporter.convertDurationsTo(getProperty(DURATION_UNIT, TimeUnit.class));

String userName = DEFAULT_USERNAME;
String password = DEFAULT_PASSWORD;
if (hasProperty(INFLUXDB_CONNECTION_FACTORY_REF)) {
InfluxdbAuthInfo authInfo = getPropertyRef(INFLUXDB_CONNECTION_FACTORY_REF,
InfluxdbAuthInfo.class);
userName = authInfo.getUserName();
password = authInfo.getPassword();
}

reporter.protocol(InfluxdbProtocols.http(hostname, port, userName, password, database));

MetricMeasurementTransformer mmt = MetricMeasurementTransformer.NOOP;
if (hasProperty(METRIC_MEASUREMENT_TRANSFORMER))
mmt = getPropertyRef(METRIC_MEASUREMENT_TRANSFORMER, MetricMeasurementTransformer.class);
reporter.filter(getMetricFilter()).transformer(mmt);

return reporter.build();
}

@Override
protected long getPeriod() {
return convertDurationString(getProperty(PERIOD));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ com.ryantenney.metrics.spring.reporter.GangliaReporterElementParser
com.ryantenney.metrics.spring.reporter.LibratoReporterElementParser
com.ryantenney.metrics.spring.reporter.NewRelicReporterElementParser
com.ryantenney.metrics.spring.reporter.DatadogReporterElementParser
com.ryantenney.metrics.spring.reporter.InfluxdbReporterElementParser
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
<tool:exports type="com.codahale.metrics.graphite.GraphiteReporter"/>
<tool:exports type="com.palominolabs.metrics.newrelic.NewRelicReporter"/>
<tool:exports type="org.coursera.metrics.datadog.DatadogReporter"/>
<tool:exports type="com.codahale.metrics.ScheduledReporter"/>
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright (C) 2016 Andrey Smorodin ([email protected])
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.ryantenney.metrics.spring;

public class InfluxdbTestAuthImpl implements InfluxdbAuthInfo {
@Override
public String getPassword() {
return "admin";
}

@Override
public String getUserName() {
return "admin";
}
}
3 changes: 3 additions & 0 deletions src/test/resources/reporter-test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<bean id="filter" class="com.ryantenney.metrics.spring.ReporterTest.BarFilter" />
<bean id="mockClock" class="org.mockito.Mockito" factory-method="mock" c:classToMock="com.codahale.metrics.Clock" />
<bean id="mockConnectionFactory" class="org.mockito.Mockito" factory-method="mock" c:classToMock="com.rabbitmq.client.ConnectionFactory" />
<bean id="influxdbTestAuth" class="com.ryantenney.metrics.spring.InfluxdbTestAuthImpl"></bean>

<metrics:reporter type="console" metric-registry="metrics" period="100ms" output-ref="printStream" clock-ref="mockClock" locale="en_GB" timezone="Europe/London" filter="com\..*" rate-unit="HOURS" duration-unit="NANOSECONDS" />
<metrics:reporter type="slf4j" metric-registry="metrics" period="100ms" logger="com.foo.bar" marker="foobar" filter-ref="filter" rate-unit="MINUTES" duration-unit="MICROSECONDS" />
Expand All @@ -43,5 +44,7 @@
<metrics:reporter id="graphite-udp" type="graphite" metric-registry="metrics" transport="udp" period="100ms" host="localhost" port="6666" charset="ISO-8859-1" prefix="metrics-test" clock-ref="mockClock" filter-ref="filter" rate-unit="DAYS" duration-unit="HOURS" />
<metrics:reporter id="graphite-pickle" type="graphite" metric-registry="metrics" transport="pickle" period="100ms" host="localhost" port="6666" batch-size="200" charset="ISO-8859-1" prefix="metrics-test" clock-ref="mockClock" filter-ref="filter" rate-unit="DAYS" duration-unit="HOURS" />
<metrics:reporter id="graphite-rabbitmq" type="graphite" metric-registry="metrics" transport="rabbitmq" period="100ms" connection-factory-ref="mockConnectionFactory" exchange="exchange" prefix="metrics-test" clock-ref="mockClock" filter-ref="filter" rate-unit="DAYS" duration-unit="HOURS" />
<metrics:reporter id="influxdb" type="influxdb" metric-registry="metrics" host="localhost" port="8086"
database="metrics" period="100ms" influxdb-auth-ref="influxdbTestAuth"/>

</beans>