Skip to content

Commit 649c687

Browse files
author
Mikalai Alimenkou
committed
Added support for structured logs in ECS format and logs collection to ELK
1 parent 4f077dc commit 649c687

File tree

6 files changed

+111
-13
lines changed

6 files changed

+111
-13
lines changed

compose.yaml

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,45 @@ services:
1818
- "4318:4318" # OTLP HTTP ingest
1919
- "5778:5778" # Remote sampling (HTTP)
2020
- "9411:9411" # Zipkin compatible ingest (optional)
21-
restart: unless-stopped
21+
22+
elastic:
23+
image: docker.elastic.co/elasticsearch/elasticsearch:9.1.5
24+
container_name: library-elastic
25+
environment:
26+
- discovery.type=single-node
27+
- xpack.security.enabled=false
28+
- ES_JAVA_OPTS=-Xms1g -Xmx1g
29+
- cluster.name=library-logs
30+
ports:
31+
- "9200:9200"
32+
healthcheck:
33+
test: ["CMD-SHELL", "curl -f http://localhost:9200/_cluster/health || exit 1"]
34+
interval: 10s
35+
timeout: 5s
36+
retries: 30
37+
start_period: 40s
38+
39+
kibana:
40+
image: docker.elastic.co/kibana/kibana:9.1.5
41+
container_name: library-kibana
42+
environment:
43+
- ELASTICSEARCH_HOSTS=http://elastic:9200
44+
ports:
45+
- "5601:5601"
46+
depends_on:
47+
elastic:
48+
condition: service_healthy
49+
50+
otel-collector:
51+
image: otel/opentelemetry-collector-contrib:0.137.0
52+
container_name: library-otel-collector
53+
command: [ "--config=/etc/otelcol/config.yaml" ]
54+
volumes:
55+
- ./otel/otel-collector-config.yaml:/etc/otelcol/config.yaml:ro
56+
- ./logs:/var/log/app
57+
ports:
58+
- "4315:4317" # OTLP gRPC
59+
- "4316:4318" # OTLP HTTP
60+
depends_on:
61+
elastic:
62+
condition: service_healthy

otel/otel-collector-config.yaml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
extensions:
2+
file_storage:
3+
directory: /var/log/app/.otel-file-storage
4+
create_directory: true
5+
6+
receivers:
7+
filelog:
8+
include: [ /var/log/app/library-logs.json ]
9+
start_at: end
10+
storage: file_storage
11+
operators:
12+
- type: json_parser
13+
parse_from: body
14+
otlp:
15+
protocols:
16+
http:
17+
grpc:
18+
19+
processors:
20+
batch: {}
21+
22+
exporters:
23+
debug:
24+
verbosity: detailed
25+
26+
elasticsearch:
27+
endpoint: http://elastic:9200
28+
mapping:
29+
mode: raw
30+
sending_queue:
31+
enabled: true
32+
33+
service:
34+
extensions: [file_storage]
35+
pipelines:
36+
logs:
37+
receivers: [filelog,otlp]
38+
processors: [batch]
39+
exporters: [elasticsearch, debug]

src/main/resources/application-admin.yaml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,14 @@ spring:
1111
enabled: true
1212

1313
management:
14+
observations:
15+
annotations:
16+
enabled: true
1417
otlp:
15-
logging:
16-
export:
17-
enabled: false
1818
tracing:
1919
endpoint: # define Jaeger endpoint
2020
export:
2121
enabled: true
22-
logging:
23-
export:
24-
enabled: false
2522
tracing:
2623
enabled: true
2724
sampling:
@@ -63,4 +60,8 @@ management:
6360
os:
6461
enabled: true
6562
process:
66-
enabled: true
63+
enabled: true
64+
metrics:
65+
distribution:
66+
percentiles-histogram:
67+
http.server.requests: true

src/main/resources/application-dev.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,9 @@ logging:
2727
level:
2828
web: DEBUG
2929
include-application-group: false
30-
include-application-name: false
30+
include-application-name: false
31+
structured:
32+
ecs:
33+
service:
34+
environment: local-dev
35+
node-name: laptop

src/main/resources/application.yaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ server:
1313
max-queue-capacity: 1000
1414
basedir: tomcat
1515
accesslog:
16-
enabled: true
17-
pattern: '%t %a "%r" %s (%D ms)'
16+
enabled: true
17+
pattern: '%t %a "%r" %s (%D ms)'
1818
compression:
1919
enabled: true
2020
min-response-size: 512
@@ -79,6 +79,9 @@ library:
7979
logging:
8080
file:
8181
name: library.log
82+
structured:
83+
format:
84+
console: ecs
8285

8386
springdoc:
8487
packages-to-scan: com.xpinjection.library.adaptors.api

src/main/resources/logback-spring.xml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<configuration>
33
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
4-
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
54

65
<springProfile name="json-logs">
76
<springProperty name="app" source="spring.application.name" defaultValue="app"/>
@@ -51,7 +50,17 @@
5150
</root>
5251
</springProfile>
5352

54-
<springProfile name="!json-logs">
53+
<springProfile name="structured-logs">
54+
<include resource="org/springframework/boot/logging/logback/structured-console-appender.xml"/>
55+
56+
<root level="INFO">
57+
<appender-ref ref="CONSOLE"/>
58+
</root>
59+
</springProfile>
60+
61+
<springProfile name="!json-logs &amp; !structured-logs">
62+
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
63+
5564
<root level="INFO">
5665
<appender-ref ref="CONSOLE"/>
5766
</root>

0 commit comments

Comments
 (0)