-
Notifications
You must be signed in to change notification settings - Fork 40.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configurable implementation of gauge writer to export metrics into InfluxDB. Fixes gh-5688
- Loading branch information
Showing
4 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
...r/src/main/java/org/springframework/boot/actuate/metrics/writer/InfluxDBMetricWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Copyright 2012-2013 the original author or authors. | ||
* | ||
* 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 org.springframework.boot.actuate.metrics.writer; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.influxdb.InfluxDB; | ||
import org.influxdb.dto.Point; | ||
|
||
import org.springframework.boot.actuate.metrics.Metric; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* A {@link GaugeWriter} that writes the metric updates to InfluxDB. | ||
* | ||
* @author Mateusz Klimaszewski | ||
*/ | ||
public class InfluxDBMetricWriter implements GaugeWriter { | ||
|
||
private static final String DEFAULT_DATABASE_NAME = "metrics"; | ||
private static final int DEFAULT_BATCH_ACTIONS = 500; | ||
private static final int DEFAULT_FLUSH_DURATION = 30; | ||
|
||
private final InfluxDB influxDB; | ||
private final String databaseName; | ||
|
||
public InfluxDBMetricWriter(Builder builder) { | ||
this.influxDB = builder.influxDB; | ||
this.databaseName = builder.databaseName; | ||
this.influxDB.createDatabase(this.databaseName); | ||
this.influxDB.enableBatch(builder.batchActions, builder.flushDuration, | ||
builder.flushDurationTimeUnit); | ||
this.influxDB.setLogLevel(builder.logLevel); | ||
} | ||
|
||
@Override | ||
public void set(Metric<?> value) { | ||
Point point = Point.measurement(value.getName()) | ||
.time(value.getTimestamp().getTime(), TimeUnit.MILLISECONDS) | ||
.addField("value", value.getValue()) | ||
.build(); | ||
this.influxDB.write(this.databaseName, value.getName(), point); | ||
} | ||
|
||
/** | ||
* {@link InfluxDBMetricWriter} builder with possibility to change default arguments | ||
*/ | ||
public static class Builder { | ||
private final InfluxDB influxDB; | ||
private String databaseName = DEFAULT_DATABASE_NAME; | ||
private int batchActions = DEFAULT_BATCH_ACTIONS; | ||
private int flushDuration = DEFAULT_FLUSH_DURATION; | ||
private TimeUnit flushDurationTimeUnit = TimeUnit.SECONDS; | ||
private InfluxDB.LogLevel logLevel = InfluxDB.LogLevel.BASIC; | ||
|
||
public Builder(InfluxDB influxDB) { | ||
Assert.notNull(influxDB, "InfluxDB must not be null"); | ||
this.influxDB = influxDB; | ||
} | ||
|
||
public Builder databaseName(String databaseName) { | ||
this.databaseName = databaseName; | ||
return this; | ||
} | ||
|
||
public Builder batchActions(int batchActions) { | ||
this.batchActions = batchActions; | ||
return this; | ||
} | ||
|
||
public Builder flushDuration(int flushDuration, TimeUnit flushDurationTimeUnit) { | ||
this.flushDuration = flushDuration; | ||
this.flushDurationTimeUnit = flushDurationTimeUnit; | ||
return this; | ||
} | ||
|
||
public Builder logLevel(InfluxDB.LogLevel logLevel) { | ||
this.logLevel = logLevel; | ||
return this; | ||
} | ||
|
||
public InfluxDBMetricWriter build() { | ||
return new InfluxDBMetricWriter(this); | ||
} | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
.../test/java/org/springframework/boot/actuate/metrics/writer/InfluxDBMetricWriterTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright 2012-2013 the original author or authors. | ||
* | ||
* 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 org.springframework.boot.actuate.metrics.writer; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.influxdb.InfluxDB; | ||
import org.influxdb.dto.Point; | ||
import org.junit.Test; | ||
import org.mockito.ArgumentCaptor; | ||
|
||
import org.springframework.boot.actuate.metrics.Metric; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Matchers.any; | ||
import static org.mockito.Matchers.anyString; | ||
import static org.mockito.Matchers.eq; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
|
||
/** | ||
* Tests for {@link InfluxDBMetricWriter}. | ||
* | ||
* @author Mateusz Klimaszewski | ||
*/ | ||
public class InfluxDBMetricWriterTests { | ||
|
||
private InfluxDB influxDB = mock(InfluxDB.class); | ||
private InfluxDBMetricWriter influxDBMetricWriter; | ||
|
||
@Test | ||
public void builderNonDefaultOptions() { | ||
this.influxDBMetricWriter = new InfluxDBMetricWriter.Builder(this.influxDB) | ||
.databaseName("testDatabaseName") | ||
.batchActions(1000) | ||
.flushDuration(10, TimeUnit.MILLISECONDS) | ||
.logLevel(InfluxDB.LogLevel.FULL).build(); | ||
ArgumentCaptor<String> databaseNameCaptor = ArgumentCaptor.forClass( | ||
String.class); | ||
ArgumentCaptor<Integer> bashActionsCaptor = ArgumentCaptor.forClass( | ||
Integer.class); | ||
ArgumentCaptor<Integer> flushDurationCaptor = ArgumentCaptor.forClass( | ||
Integer.class); | ||
ArgumentCaptor<TimeUnit> flushDurationTimeUnitCaptor = ArgumentCaptor.forClass( | ||
TimeUnit.class); | ||
ArgumentCaptor<InfluxDB.LogLevel> logLevelCaptor = ArgumentCaptor.forClass( | ||
InfluxDB.LogLevel.class); | ||
verify(this.influxDB).createDatabase(databaseNameCaptor.capture()); | ||
verify(this.influxDB).enableBatch(bashActionsCaptor.capture(), | ||
flushDurationCaptor.capture(), flushDurationTimeUnitCaptor.capture()); | ||
verify(this.influxDB).setLogLevel(logLevelCaptor.capture()); | ||
assertThat("testDatabaseName").isEqualTo(databaseNameCaptor.getValue()); | ||
assertThat(1000).isEqualTo(bashActionsCaptor.getValue().intValue()); | ||
assertThat(10).isEqualTo(flushDurationCaptor.getValue().intValue()); | ||
assertThat(TimeUnit.MILLISECONDS).isEqualTo(flushDurationTimeUnitCaptor.getValue()); | ||
assertThat(InfluxDB.LogLevel.FULL).isEqualTo(logLevelCaptor.getValue()); | ||
} | ||
|
||
@Test | ||
public void setMetric() { | ||
this.influxDBMetricWriter = new InfluxDBMetricWriter.Builder(this.influxDB) | ||
.build(); | ||
Metric<Number> metric = new Metric<Number>("testName", 1); | ||
this.influxDBMetricWriter.set(metric); | ||
verify(this.influxDB, times(1)).write(anyString(), eq(metric.getName()), | ||
any(Point.class)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters