Skip to content

Commit

Permalink
Add Loki and Grafana for Logging (#100)
Browse files Browse the repository at this point in the history
* Experimental logback-spring setup for Loki and Grafana

* Final commit for this branch

* Add new grafana-admin environment variables to the deployment.yaml

* Add hostname element to Loki in compose.yml
  • Loading branch information
Daniel Villavicencio authored Jun 21, 2024
1 parent 8cd0393 commit 23715b2
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ jobs:
echo "DISCORD_BOT_PUBLIC_KEY=${{ secrets.DISCORD_BOT_PUBLIC_KEY }}" >> .env
echo "MONGO_DB_ROOT_USERNAME=${{ secrets.MONGO_INITDB_ROOT_USERNAME }}" >> .env
echo "MONGO_DB_ROOT_PASSWORD=${{ secrets.MONGO_INITDB_ROOT_PASSWORD }}" >> .env
echo "GRAFANA_ADMIN_USERNAME=${{ secrets.GRAFANA_ADMIN_USERNAME }}" >> .env
echo "GRAFANA_ADMIN_PASSWORD=${{ secrets.GRAFANA_ADMIN_PASSWORD }} >> .env
chmod 600 .env
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ build/
!**/src/test/**/build/
\\
\\.pub
compose.yml

### STS ###
.apt_generated
Expand Down
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ ext {
set('mapStructVersion', '1.5.5.Final')
set('resilienceReactorVersion', '1.6.1')
set('resilienceRateLimiterVersion', '1.6.1')
set('lokiLogbackAppenderVersion', '1.5.1')
}

jar {
Expand All @@ -69,6 +70,7 @@ dependencies {
implementation "commons-codec:commons-codec:${commonsCodecVersion}"
implementation "software.pando.crypto:salty-coffee:${pandoCryptoVersion}"
implementation "org.apache.commons:commons-collections4:${apacheCollectionsVersion}"
implementation "com.github.loki4j:loki-logback-appender:${lokiLogbackAppenderVersion}"
implementation 'org.springframework.boot:spring-boot-starter-actuator'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
Expand Down
49 changes: 48 additions & 1 deletion compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
services:
riven-of-a-thousand-servers:
container_name: rivenbot
image: ${DOCKER_IMAGE_NAME}
env_file:
- .env
Expand All @@ -20,11 +21,13 @@ services:
- bungie_client_id
- bungie_client_secret
redis:
container_name: redis
hostname: redis
image: "redis:7.0.11"
ports:
- "6379:6379"
mongodb:
container_name: mongodb
hostname: mongo
image: "mongo:7.0.3"
ports:
Expand All @@ -36,6 +39,43 @@ services:
secrets:
- "mongo_username"
- "mongo_password"
loki:
container_name: loki
hostname: loki
image: grafana/loki:2.9.0
ports:
- "3100:3100"
grafana:
container_name: grafana
environment:
GF_SECURITY_ADMIN_USER__FILE: /run/secrets/grafana_admin_username
GF_SECURITY_ADMIN_PASSWORD__FILE: /run/secrets/grafana_admin_password
entrypoint:
- sh
- -euc
- |
mkdir -p /etc/grafana/provisioning/datasources
cat <<EOF > /etc/grafana/provisioning/datasources/ds.yaml
datasources:
- name: Loki
type: loki
access: proxy
orgId: 1
url: http://loki:3100
basicAuth: false
isDefault: true
version: 1
editable: false
EOF
/run.sh
image: grafana/grafana-oss:latest
ports:
- "3000:3000"
secrets:
- "grafana_admin_username"
- "grafana_admin_password"
volumes:
- grafana-storage:/var/lib/grafana

secrets:
mongo_username:
Expand All @@ -57,4 +97,11 @@ secrets:
bungie_client_id:
environment: "BUNGIE_CLIENT_ID"
bungie_client_secret:
environment: "BUNGIE_CLIENT_SECRET"
environment: "BUNGIE_CLIENT_SECRET"
grafana_admin_username:
environment: "GRAFANA_ADMIN_USERNAME"
grafana_admin_password:
environment: "GRAFANA_ADMIN_PASSWORD"

volumes:
grafana-storage: { }
44 changes: 44 additions & 0 deletions src/main/resources/logback-spring.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- Local configuration -->
<SpringProfile name="local">
<appender name="LOKI" class="com.github.loki4j.logback.Loki4jAppender">
<http>
<url>http://localhost:3100/loki/api/v1/push</url>
</http>
<format>
<label>
<pattern>level=%level</pattern>
<readMarkers>true</readMarkers>
</label>
<message>
<pattern>%-5level [%.5(${HOSTNAME})] %.10thread %logger{20} | %msg %ex</pattern>
</message>
</format>
</appender>
<root level="INFO">
<appender-ref ref="LOKI"/>
</root>
</SpringProfile>

<!-- Production configuration -->
<SpringProfile name="prod">
<appender name="LOKI" class="com.github.loki4j.logback.Loki4jAppender">
<http>
<url>http://loki:3100/loki/api/v1/push</url>
</http>
<format>
<label>
<pattern>level=%level</pattern>
<readMarkers>true</readMarkers>
</label>
<message>
<pattern>%-5level [%.5(${HOSTNAME})] %.10thread %logger{20} | %msg %ex</pattern>
</message>
</format>
</appender>
<root level="INFO">
<appender-ref ref="LOKI"/>
</root>
</SpringProfile>
</configuration>
2 changes: 1 addition & 1 deletion src/test/resources/__files/bungie/milestone-response.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"response":{"3021174356":{"milestoneHash":3021174356,"startDate":null,"endDate":null,"activities":null},"644555645":{"milestoneHash":644555645,"startDate":null,"endDate":null,"activities":null},"3181387331":{"milestoneHash":3181387331,"startDate":1701795600.000000000,"endDate":1702400400.000000000,"activities":[{"activityHash":2122313384,"challengeObjectiveHashes":[]}]},"4253138191":{"milestoneHash":4253138191,"startDate":1701795600.000000000,"endDate":1702400400.000000000,"activities":null},"3603098564":{"milestoneHash":3603098564,"startDate":1701795600.000000000,"endDate":1702400400.000000000,"activities":null},"3802603984":{"milestoneHash":3802603984,"startDate":null,"endDate":null,"activities":null},"2709491520":{"milestoneHash":2709491520,"startDate":null,"endDate":null,"activities":null},"2594202463":{"milestoneHash":2594202463,"startDate":null,"endDate":null,"activities":null},"3899487295":{"milestoneHash":3899487295,"startDate":null,"endDate":null,"activities":null},"2712317338":{"milestoneHash":2712317338,"startDate":1715702400.000000000,"endDate":1716307200.000000000,"activities":[{"activityHash":1042180643,"challengeObjectiveHashes":["2398860795"]}]},"541780856":{"milestoneHash":541780856,"startDate":1701795600.000000000,"endDate":1702400400.000000000,"activities":[{"activityHash":910380154,"challengeObjectiveHashes":[]}]},"526718853":{"milestoneHash":526718853,"startDate":1715702400.000000000,"endDate":1716307200.000000000,"activities":[{"activityHash":1262462921,"challengeObjectiveHashes":["3211393925"]},{"activityHash":2296818662,"challengeObjectiveHashes":["3211393925"]}]}}}
{"response":{"3021174356":{"milestoneHash":3021174356,"startDate":null,"endDate":null,"activities":null},"644555645":{"milestoneHash":644555645,"startDate":null,"endDate":null,"activities":null},"3181387331":{"milestoneHash":3181387331,"startDate":1701795600.000000000,"endDate":1702400400.000000000,"activities":[{"activityHash":2122313384,"challengeObjectiveHashes":[]}]},"4253138191":{"milestoneHash":4253138191,"startDate":1701795600.000000000,"endDate":1702400400.000000000,"activities":null},"3603098564":{"milestoneHash":3603098564,"startDate":1701795600.000000000,"endDate":1702400400.000000000,"activities":null},"3802603984":{"milestoneHash":3802603984,"startDate":null,"endDate":null,"activities":null},"2709491520":{"milestoneHash":2709491520,"startDate":null,"endDate":null,"activities":null},"2594202463":{"milestoneHash":2594202463,"startDate":null,"endDate":null,"activities":null},"3899487295":{"milestoneHash":3899487295,"startDate":null,"endDate":null,"activities":null},"2712317338":{"milestoneHash":2712317338,"startDate":1718726400.000000000,"endDate":1719331200.000000000,"activities":[{"activityHash":1042180643,"challengeObjectiveHashes":["2398860795"]}]},"541780856":{"milestoneHash":541780856,"startDate":1701795600.000000000,"endDate":1702400400.000000000,"activities":[{"activityHash":910380154,"challengeObjectiveHashes":[]}]},"526718853":{"milestoneHash":526718853,"startDate":1718726400.000000000,"endDate":1719331200.000000000,"activities":[{"activityHash":1262462921,"challengeObjectiveHashes":["3211393925"]},{"activityHash":2296818662,"challengeObjectiveHashes":["3211393925"]}]}}}

0 comments on commit 23715b2

Please sign in to comment.