Skip to content

Commit 20306a2

Browse files
committed
New project to demostrate spring boot, Sleuth and Splunk
0 parents  commit 20306a2

File tree

8 files changed

+302
-0
lines changed

8 files changed

+302
-0
lines changed

.gitignore

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
4+
### STS ###
5+
.apt_generated
6+
.classpath
7+
.factorypath
8+
.project
9+
.settings
10+
.springBeans
11+
12+
### IntelliJ IDEA ###
13+
.idea
14+
*.iws
15+
*.iml
16+
*.ipr
17+
18+
### NetBeans ###
19+
nbproject/private/
20+
build/
21+
nbbuild/
22+
dist/
23+
nbdist/
24+
.nb-gradle/

Readme.md

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
Sample project to show log forwarding from a [Spring Boot](https://projects.spring.io/spring-boot/) application to [Splunk](https://www.splunk.com/) via a file forwarder.
2+
3+
[Sleuth](https://cloud.spring.io/spring-cloud-sleuth/) is also added to generate IDs for each request
4+
5+
## Build
6+
7+
Build the application as a [Docker](https://www.docker.com/) image using:
8+
9+
```bash
10+
mvn clean install
11+
```
12+
13+
## Run
14+
15+
Run the included docker-compose file with:
16+
17+
```bash
18+
docker-compose up
19+
```
20+
21+
This will bring up three containers: A demo application, Splunk, and a Spunk Forwarder.
22+
23+
Shares are forwarded to Splunk via a shared volume between the application and forwarder.
24+
25+
It is possible to send logs to Splunk via Docker's logging mechanism, but the goal here was to use the file forwarder to replicate a production setup.
26+
27+
28+
## Generate logs
29+
30+
Application startup will generate some logs.
31+
32+
Calling the demo endpoint will generate some more logs, with Sleuth trace and Span Ids.
33+
34+
```bash
35+
curl http://localhost:8080?name=test
36+
```
37+
38+
## View Logs
39+
40+
Got to [Splunk login](http://localhost:8000) to view the logs
41+
42+
## Stop
43+
44+
Stop the containers and remove unused volumes with:
45+
46+
```bash
47+
docker-compose down -v
48+
```

docker-compose.yml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
version: '2'
2+
services:
3+
demo-application:
4+
image: barrycommins/spring-boot-sleuth-splunk-demo
5+
environment:
6+
- LOGGING_FILE=/logs/demo-application.log
7+
ports:
8+
- "8080:8080"
9+
volumes:
10+
- log_volume:/logs
11+
splunk:
12+
image: splunk/splunk
13+
hostname: splunk
14+
environment:
15+
- SPLUNK_START_ARGS=--accept-license
16+
- SPLUNK_USER=root
17+
- SPLUNK_ENABLE_LISTEN=9997
18+
ports:
19+
- "8000:8000"
20+
splunkforwarder:
21+
image: splunk/universalforwarder:6.5.3-monitor
22+
hostname: splunkforwarder
23+
environment:
24+
- SPLUNK_START_ARGS=--accept-license --answer-yes
25+
- SPLUNK_FORWARD_SERVER=splunk:9997
26+
- SPLUNK_USER=root
27+
- SPLUNK_ADD=monitor /logs
28+
restart: always
29+
depends_on:
30+
- splunk
31+
volumes:
32+
- log_volume:/logs
33+
volumes:
34+
log_volume:

pom.xml

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.barrycommins</groupId>
7+
<artifactId>spring-boot-sleuth-splunk-demo</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>spring-boot-sleuth-splunk-demo</name>
12+
<description>Demo project for Spring Boot, Sleuth and Splunk</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.5.3.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
<spring-cloud.version>Dalston.SR1</spring-cloud.version>
26+
<docker.image.prefix>barrycommins</docker.image.prefix>
27+
<docker.plugin.version>0.4.13</docker.plugin.version>
28+
</properties>
29+
30+
<dependencies>
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter-actuator</artifactId>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.springframework.cloud</groupId>
37+
<artifactId>spring-cloud-starter-sleuth</artifactId>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-starter-web</artifactId>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-starter-logging</artifactId>
47+
</dependency>
48+
<dependency>
49+
<groupId>net.logstash.logback</groupId>
50+
<artifactId>logstash-logback-encoder</artifactId>
51+
<version>4.9</version>
52+
</dependency>
53+
54+
<dependency>
55+
<groupId>org.springframework.boot</groupId>
56+
<artifactId>spring-boot-starter-test</artifactId>
57+
<scope>test</scope>
58+
</dependency>
59+
</dependencies>
60+
61+
<dependencyManagement>
62+
<dependencies>
63+
<dependency>
64+
<groupId>org.springframework.cloud</groupId>
65+
<artifactId>spring-cloud-dependencies</artifactId>
66+
<version>${spring-cloud.version}</version>
67+
<type>pom</type>
68+
<scope>import</scope>
69+
</dependency>
70+
</dependencies>
71+
</dependencyManagement>
72+
73+
<build>
74+
<plugins>
75+
<plugin>
76+
<groupId>org.springframework.boot</groupId>
77+
<artifactId>spring-boot-maven-plugin</artifactId>
78+
<executions>
79+
<execution>
80+
<goals>
81+
<goal>repackage</goal>
82+
<goal>build-info</goal>
83+
</goals>
84+
</execution>
85+
</executions>
86+
</plugin>
87+
<plugin>
88+
<groupId>com.spotify</groupId>
89+
<artifactId>docker-maven-plugin</artifactId>
90+
<version>${docker.plugin.version}</version>
91+
<executions>
92+
<execution>
93+
<phase>install</phase>
94+
<goals>
95+
<goal>build</goal>
96+
</goals>
97+
</execution>
98+
</executions>
99+
<configuration>
100+
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
101+
<baseImage>openjdk:8-alpine</baseImage>
102+
<!-- @formatter: off-->
103+
<entryPoint>["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/${project.build.finalName}.jar"]
104+
</entryPoint>
105+
<!-- @formatter: on-->
106+
<resources>
107+
<resource>
108+
<targetPath>/</targetPath>
109+
<directory>${project.build.directory}</directory>
110+
<include>${project.build.finalName}.jar</include>
111+
</resource>
112+
</resources>
113+
</configuration>
114+
</plugin>
115+
</plugins>
116+
</build>
117+
118+
119+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.barrycommins;
2+
3+
import org.apache.commons.logging.Log;
4+
import org.apache.commons.logging.LogFactory;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RequestParam;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
@RestController
11+
@RequestMapping("/")
12+
public class DemoController {
13+
14+
private static final Log LOGGER = LogFactory.getLog(DemoController.class);
15+
16+
@GetMapping
17+
public String hello(@RequestParam(name = "name", defaultValue = "World") String name) {
18+
LOGGER.info("name="+name);
19+
return "Hello, " + name;
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.barrycommins;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SpringBootSleuthSplunkDemoApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SpringBootSleuthSplunkDemoApplication.class, args);
11+
}
12+
}

src/main/resources/application.yml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
spring:
2+
application:
3+
name: demo-application

src/main/resources/logback-spring.xml

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
<include resource="org/springframework/boot/logging/logback/base.xml"/>
4+
5+
<springProperty scope="context" name="springAppName" source="spring.application.name"/>
6+
7+
<!-- Appender to log to file in a JSON format -->
8+
<appender name="logstash" class="ch.qos.logback.core.rolling.RollingFileAppender">
9+
<file>${LOG_FILE}.json</file>
10+
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
11+
<fileNamePattern>${LOG_FILE}.json.%d{yyyy-MM-dd}.gz</fileNamePattern>
12+
<maxHistory>7</maxHistory>
13+
</rollingPolicy>
14+
<encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
15+
<providers>
16+
<timestamp>
17+
<timeZone>UTC</timeZone>
18+
</timestamp>
19+
<pattern>
20+
<pattern>
21+
{
22+
"severity": "%level",
23+
"service": "${springAppName:-}",
24+
"trace": "%X{X-B3-TraceId:-}",
25+
"span": "%X{X-B3-SpanId:-}",
26+
"parent": "%X{X-B3-ParentSpanId:-}",
27+
"thread": "%thread",
28+
"class": "%logger{40}",
29+
"message": "%message"
30+
}
31+
<!-- TODO: stacktrace, other fields-->
32+
</pattern>
33+
</pattern>
34+
</providers>
35+
</encoder>
36+
</appender>
37+
38+
<root level="INFO">
39+
<appender-ref ref="logstash"/>
40+
</root>
41+
</configuration>

0 commit comments

Comments
 (0)