Skip to content

Commit

Permalink
Merge pull request #4 from felipe-loka/feat/add-loki
Browse files Browse the repository at this point in the history
Feat/add loki
  • Loading branch information
felipe-loka authored Feb 21, 2024
2 parents 3d1f260 + ff0a9e3 commit 2a58f06
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 20 deletions.
20 changes: 15 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Created by https://www.toptal.com/developers/gitignore/api/node,visualstudiocode,macos
# Edit at https://www.toptal.com/developers/gitignore?templates=node,visualstudiocode,macos

### macOS ###
##############################
###### macOS
##############################
# General
.DS_Store
.AppleDouble
Expand Down Expand Up @@ -34,7 +36,9 @@ Temporary Items
# iCloud generated files
*.icloud

### Node ###
##############################
###### nodeJS
##############################
# Logs
logs
*.log
Expand Down Expand Up @@ -165,7 +169,6 @@ dist
.yarn/install-state.gz
.pnp.*

### Node Patch ###
# Serverless Webpack directories
.webpack/

Expand All @@ -174,7 +177,10 @@ dist
# SvelteKit build / generate output
.svelte-kit

### VisualStudioCode ###
##############################
###### vsCode
##############################

.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
Expand All @@ -193,4 +199,8 @@ dist
.history
.ionide

# End of https://www.toptal.com/developers/gitignore/api/node,visualstudiocode,macos
##############################
###### Local
##############################
tempo-data
.data
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ COPY package.json ./
RUN npm install pnpm -g
RUN pnpm install --prod
COPY --from=build /app/dist ./
COPY src/config/database/migrations ./src/config/database/migrations
EXPOSE 3000
CMD ["node", "./src/index.js"]
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Poll API
# poll API

The code in this repository corresponds to an API responsible for managing a basic poll system.

Expand Down Expand Up @@ -119,4 +119,4 @@ The following requirements should be implemented in next versions:

- Add observability to the application: metrics, traces
- Add cache layer (e.g. Redis) to minimize latency time
- Validate the need of using a pool of connections to manage the connection to the database
- Validate the need of using a poll of connections to manage the connection to the database
4 changes: 4 additions & 0 deletions config/fluent-bit-parsers.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[PARSER]
Name docker-json
Match *
Format json
36 changes: 36 additions & 0 deletions config/fluent-bit.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# File config: https://docs.fluentbit.io/manual/administration/configuring-fluent-bit/classic-mode/configuration-file

# https://docs.fluentbit.io/manual/v/1.3/service
[SERVICE]
Flush 1
Log_Level debug
Parsers_File /fluent-bit/etc/fluent-bit-parsers.conf

# https://docs.fluentbit.io/manual/pipeline/inputs/forward
[INPUTS]
Name forward
Listen 0.0.0.0
Port 24224
Buffer_Chunk_Size 1M
Buffer_Max_Size 6M

# https://docs.fluentbit.io/manual/pipeline/filters/parser
[FILTER]
Name parser
Match *
Key_Name log
Parser docker-json
Reserve_Data True

# https://docs.fluentbit.io/manual/pipeline/filters/standard-output
[OUTPUT]
name stdout
match *

# https://docs.fluentbit.io/manual/pipeline/outputs/loki
[OUTPUT]
name loki
match *
host loki
port 3100
labels collector=fluentbit, container_name=$container_name
9 changes: 0 additions & 9 deletions config/fluent-bit.yaml

This file was deleted.

7 changes: 7 additions & 0 deletions config/grafana.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: 1
datasources:
- name: Loki
type: loki
access: proxy
url: http://loki:3100
basicAuth: false
38 changes: 38 additions & 0 deletions config/loki.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
auth_enabled: false

server:
http_listen_port: 3100

memberlist:
join_members:
- loki:7946

schema_config:
configs:
- from: 2021-08-01
store: tsdb
object_store: s3
schema: v12
index:
prefix: index_
period: 24h

common:
path_prefix: /loki
replication_factor: 1
storage:
s3:
endpoint: minio:9000
insecure: true
bucketnames: loki-data
access_key_id: loki
secret_access_key: supersecret
s3forcepathstyle: true
ring:
kvstore:
store: memberlist

ruler:
storage:
s3:
bucketnames: loki-ruler
52 changes: 50 additions & 2 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,66 @@ services:
- "${DB_PORT}:${DB_PORT}"
restart: always
command: --default-authentication-plugin=mysql_native_password
healthcheck:
test: mysqladmin ping -h localhost -u $DB_USERNAME -p$DB_PASSWORD
interval: 60s
timeout: 1s
retries: 3
start_period: 5s

fluent-bit:
image: fluent/fluent-bit:2.2.0
container_name: fluent-bit
networks:
- pollNetwork
volumes:
- ./config/fluent-bit.yaml:/fluent-bit/etc/fluent-bit.yaml
- ./config/fluent-bit.conf:/fluent-bit/etc/fluent-bit.conf
- ./config/fluent-bit-parsers.conf:/fluent-bit/etc/fluent-bit-parsers.conf
ports:
- "24224:24224/tcp"
- "24224:24224/udp"
command: -c /fluent-bit/etc/fluent-bit.yaml
command: -c /fluent-bit/etc/fluent-bit.conf -log.level=debug

loki:
image: grafana/loki:2.9.0
container_name: loki
networks:
- pollNetwork
ports:
- 3100:3100
- 7946:7946
- 9095:9095
command:
- -config.file=/etc/loki/local-config.yaml
- -print-config-stderr=true
healthcheck:
test: wget --no-verbose --tries=1 --spider http://localhost:3100/ready || exit 1
interval: 15s
timeout: 5s
retries: 5

grafana:
image: grafana/grafana:10.2.1
container_name: grafana
networks:
- pollNetwork
environment:
- GF_PATHS_PROVISIONING=/etc/grafana/provisioning
- GF_AUTH_ANONYMOUS_ENABLED=true
- GF_AUTH_ANONYMOUS_ORG_ROLE=Admin
ports:
- "3001:3000"
volumes:
- ./config/grafana.yaml:/etc/grafana/provisioning/datasources/ds.yaml
entrypoint:
- sh
- -euc
- /run.sh
healthcheck:
test: wget --no-verbose --tries=1 --spider http://localhost:3000/api/health || exit 1
interval: 10s
timeout: 5s
retries: 5

networks:
pollNetwork:
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/voteController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const create = async (req: Request, res: Response): Promise<void> => {
const poll = await getPoll(body.pollId)
if (poll === null) {
res.status(404).send(buildErrorResponse(
'You can not vote in a poll that does not exist. Verify if the given pool id is correct.'
'You can not vote in a poll that does not exist. Verify if the given poll id is correct.'
))
return
} else {
Expand Down
2 changes: 1 addition & 1 deletion test/vote.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('POST /vote', () => {

expect(response.statusCode).toBe(404)
expect(response.body).toStrictEqual({
message: 'You can not vote in a poll that does not exist. Verify if the given pool id is correct.',
message: 'You can not vote in a poll that does not exist. Verify if the given poll id is correct.',
status: 'failed'
})
})
Expand Down

0 comments on commit 2a58f06

Please sign in to comment.