Skip to content

Commit

Permalink
Merge pull request #2 from mariusoe/spring-actuator
Browse files Browse the repository at this point in the history
Adding Spring Boot Web for Actuators and configuring InfluxDB auto-conf
  • Loading branch information
Jonas Kunz authored Dec 6, 2019
2 parents acbe33d + 5c46d0b commit 960c0b0
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 87 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,4 @@
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

application.yml
/.nb-gradle/
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,21 @@ This value can be baselines too, the joining of the series happens within the ba

## Configuration

The application is a Spring-Boot application without any interface. It is configured by placing an `application.yml`
file next to the JAR file.
The application is a Spring-Boot application.
It provides a HTTP endpoint which is running by default on port `8080` which is mainly used for providing health information (`/actuator/health`).
The application is configured by placing an `application.yml` file next to the JAR file.

In the `application.yml` it is first required that you configure the connection to influx:
```
influx:
url: http://localhost:8086
user: "myuser" # OPTIONAL: username used to connect to influx
password: "mypw" # OPTIONAL: password used to connect to influx
spring:
influx:
url: http://localhost:8086
user: "myuser" # OPTIONAL: username used to connect to influx
password: "mypw" # OPTIONAL: password used to connect to influx
connect-timeout: 60s # OPTIONAL: timeout to use when connecting to influx
read-timeout: 60s # OPTIONAL: timeout to use when reading data from influx
write-timeout: 60s # OPTIONAL: timeout to use when writing data to influx
connect-timeout: 60s # OPTIONAL: timeout to use when connecting to influx
read-timeout: 60s # OPTIONAL: timeout to use when reading data from influx
write-timeout: 60s # OPTIONAL: timeout to use when writing data to influx
```

Next you can configure the actual baselining:
Expand Down
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ dependencies {
)

implementation(
"org.springframework.boot:spring-boot-starter",
"org.springframework.boot:spring-boot-starter-web",
"org.springframework.boot:spring-boot-starter-actuator",
"org.hibernate:hibernate-validator:6.1.0.Final",
"org.influxdb:influxdb-java:2.15",
"org.apache.commons:commons-math3:3.0"
Expand Down
48 changes: 8 additions & 40 deletions src/main/java/de/novatec/baselining/InfluxAccess.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package de.novatec.baselining;

import de.novatec.baselining.config.InfluxSettings;
import de.novatec.baselining.data.DataPoint;
import de.novatec.baselining.data.TagValues;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.influxdb.dto.BatchPoints;
import org.influxdb.dto.Point;
import org.influxdb.dto.Query;
Expand All @@ -23,28 +20,11 @@
@Slf4j
public class InfluxAccess {

@Autowired
private InfluxSettings config;
private static final QueryResult EMPTY_QUERY_RESULT = new QueryResult();

@Autowired
private InfluxDB influx;

void connect() {
if (influx == null) {
OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient().newBuilder()
.connectTimeout(config.getConnectTimeout().getSeconds(), TimeUnit.SECONDS)
.readTimeout(config.getReadTimeout().getSeconds(), TimeUnit.SECONDS)
.writeTimeout(config.getWriteTimeout().getSeconds(), TimeUnit.SECONDS);

boolean userEmpty = StringUtils.isEmpty(config.getUser());
boolean passwordEmpty = StringUtils.isEmpty(config.getPassword());
if (userEmpty && passwordEmpty) {
influx = InfluxDBFactory.connect(config.getUrl().toString(), okHttpClientBuilder);
} else {
influx = InfluxDBFactory.connect(config.getUrl().toString(), config.getUser(), config.getPassword(), okHttpClientBuilder);
}
}
}

private String buildTimeFilter(long startMillis, long endMillis) {
return new StringBuilder()
.append(" time >= ").append(startMillis).append("000000")
Expand All @@ -63,17 +43,12 @@ public QueryResult query(String selectFrom, String filter, String groupBy, long
query.append(" AND ").append(filter);
}
query.append(" GROUP BY ").append(groupBy);

try {
connect();
return influx.query(new Query(query.toString()), TimeUnit.MILLISECONDS);
} catch (Throwable t) {
try {
influx.close();
} catch (Exception e) {
log.error("Error closing influx after exception:", e);
}
influx = null;
throw t;
} catch (Exception e) {
log.error("Exception while executing InfluxDB query.", e);
return EMPTY_QUERY_RESULT;
}
}

Expand Down Expand Up @@ -150,16 +125,9 @@ public void writePoints(String database, String retention, Map<String, String> t
builder.points(points);

try {
connect();
influx.write(builder.build());
} catch (Throwable t) {
try {
influx.close();
} catch (Exception e) {
log.error("Error closing influx after exception:", e);
}
influx = null;
throw t;
} catch (Exception e) {
log.error("Exception while writing InfluxDB data.", e);
}
}

Expand Down
34 changes: 0 additions & 34 deletions src/main/java/de/novatec/baselining/config/InfluxSettings.java

This file was deleted.

48 changes: 48 additions & 0 deletions src/main/java/de/novatec/baselining/spring/BeanConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package de.novatec.baselining.spring;

import okhttp3.OkHttpClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.influx.InfluxDbOkHttpClientBuilderProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;

@Configuration
public class BeanConfiguration {

@Value("${spring.influx.connect-timeout:60s}")
private Duration connectTimeout;

@Value("${spring.influx.read-timeout:60s}")
private Duration readTimeout;

@Value("${spring.influx.write-timeout:60s}")
private Duration writeTimeout;

/**
* Used by the {@link org.springframework.boot.autoconfigure.influx.InfluxDbAutoConfiguration} to
* create the {@link org.influxdb.InfluxDB} client.
*
* @return HTTP builder which is used for the InfluxDB client.
*/
@Bean
public InfluxDbOkHttpClientBuilderProvider influxOkHttpClientBuilderProvider() {
return () -> {
OkHttpClient.Builder builder = new OkHttpClient.Builder();

if (connectTimeout != null) {
builder.connectTimeout(connectTimeout);
}
if (connectTimeout != null) {
builder.readTimeout(readTimeout);
}
if (connectTimeout != null) {
builder.writeTimeout(writeTimeout);
}

return builder;
};
}

}
56 changes: 56 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
server:
port: 8080

spring:
influx:
# URL of the InfluxDB
url: http://localhost:8086
# Username for the InfluxDB
user: "root"
# Password for the InfluxDB
password: ""

## OPTIONAL: timeout to use when connecting to influx
# connect-timeout: 60s
## OPTIONAL: timeout to use when reading data from influx
# read-timeout: 60s
## OPTIONAL: timeout to use when writing data to influx
# write-timeout: 60s


baselining:

# When starting up, the service will compute baselines based on historical data
# This defines how far the service should look into the past
backfill: 30d

# Commonly data takes some time until it actual gets to the influxDB
# This property tells the service to wait the given amount of time before updating the baselines.
# E.g. a delay of 30s means that the baselines for 14:00 to 15:00 will be computed at 15:00:30
update-delay: 30s

# #Baselines for gauge metrics
# gauges:
# - precision: 15m
# seasonality: 1d
# input: baseline.autogen.sinus.value
# output: baseline.autogen.sinus_baseline
#
# # Baselines for counters (increase per second)
# counters:
# - precision: 15m
# seasonality: 7d
# windows: [28d, 56d]
# input: telegraf.autogen.http_requests_count.value
# output: baselines.autogen.http_request_rate_weekly
# tags: [http_path]
#
# # Baselines for ratio between two counters (e.g. response time)
# counter-ratios:
# - precision: 15m
# seasonality: 1d
# windows: [15d, 30d]
# input: telegraf.autogen.http_requests_time.counter
# divide-by: telegraf.autogen.http_requests_count.counter
# output: baselines.autogen.http_time_daily
# tags: [http_path]

0 comments on commit 960c0b0

Please sign in to comment.