Skip to content

Commit 88a5ddc

Browse files
authored
Merge pull request #81 from DataDog/TRAIN-2439-postgres-logs
Fixes postgres service logging
2 parents 7b83782 + 8a04649 commit 88a5ddc

File tree

5 files changed

+54
-14
lines changed

5 files changed

+54
-14
lines changed

docker-compose.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ services:
2222
- /var/run/docker.sock:/var/run/docker.sock:ro
2323
- /proc/:/host/proc/:ro
2424
- /sys/fs/cgroup/:/host/sys/fs/cgroup:ro
25+
- postgres_logs:/var/log/pg_log:ro
2526
frontend:
2627
build:
2728
context: ./services/frontend
@@ -101,12 +102,15 @@ services:
101102
restart: always
102103
depends_on:
103104
- dd-agent
105+
volumes:
106+
# save postgres logs to a volume for datadog to collect
107+
- postgres_logs:/var/log/pg_log:rw
104108
environment:
105109
- POSTGRES_HOST_AUTH_METHOD=trust
106110
- POSTGRES_USER
107111
- POSTGRES_PASSWORD
108112
- DD_ENV=${DD_ENV-dev}
109-
- DD_SERVICE=storedog-db
113+
- DD_SERVICE=postgres
110114
- DD_VERSION=${DD_VERSION-7}
111115
- DD_AGENT_HOST=dd-agent
112116
- DD_DBM_PROPAGATION_MODE=full
@@ -115,7 +119,7 @@ services:
115119
- DD_PROFILING_ENABLED=true
116120
labels:
117121
com.datadoghq.tags.env: '${DD_ENV}'
118-
com.datadoghq.tags.service: 'storedog-db'
122+
com.datadoghq.tags.service: 'postgres'
119123
com.datadoghq.tags.version: '13'
120124
com.datadoghq.ad.check_names: '["postgres", "postgres"]'
121125
com.datadoghq.ad.init_configs: '[{}, {}]'
@@ -165,7 +169,7 @@ services:
165169
"enabled": true
166170
}
167171
}]'
168-
com.datadoghq.ad.logs: '[{"source": "postgresql", "service": "postgres", "auto_multi_line_detection":true }]'
172+
com.datadoghq.ad.logs: '[{"source": "postgresql", "service": "postgres", "auto_multi_line_detection":true, "path": "/var/log/pg_log/postgresql*.json", "type": "file"}]'
169173
my.custom.label.team: 'database'
170174
redis:
171175
image: redis:6.2-alpine
@@ -375,3 +379,4 @@ services:
375379

376380
volumes:
377381
redis:
382+
postgres_logs:

services/postgres/Dockerfile

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1-
FROM postgres:13-alpine
1+
FROM postgres:15-alpine
2+
3+
# Install dcron and su-exec for user switching
4+
RUN apk update && apk add --no-cache dcron su-exec
5+
6+
# Copy SQL dump and PostgreSQL configuration
27
COPY ./scripts/restore-2024-04-25-22-06-17.sql /docker-entrypoint-initdb.d/
3-
COPY ./postgresql.conf /postgresql.conf
8+
COPY ./scripts/postgresql.conf /postgresql.conf
9+
10+
# Create log directory and set permissions
11+
RUN mkdir -p /var/log/pg_log && \
12+
chown -R postgres:postgres /var/log/pg_log && \
13+
chmod -R 755 /var/log/pg_log
14+
15+
# Copy the cron job file
16+
COPY ./scripts/cleanup-cron /etc/crontabs/root
17+
18+
# Copy and set the entrypoint script
19+
COPY ./scripts/start-services.sh /usr/local/bin/start-services.sh
20+
RUN chmod +x /usr/local/bin/start-services.sh
21+
22+
# Use the custom entrypoint script
23+
ENTRYPOINT ["/usr/local/bin/start-services.sh"]
24+
25+
# Default command
26+
CMD ["postgres", "-c", "config_file=/postgresql.conf"]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*/1 * * * * /usr/bin/find /var/log/pg_log/ -type f -name "*" -mmin +5 -delete >> /var/log/cron.log 2>&1

services/postgres/postgresql.conf renamed to services/postgres/scripts/postgresql.conf

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -423,35 +423,35 @@ min_wal_size = 80MB
423423

424424
# - Where to Log -
425425

426-
log_destination = 'stderr' # Valid values are combinations of
426+
log_destination = 'jsonlog' # Valid values are combinations of
427427
# stderr, csvlog, syslog, and eventlog,
428428
# depending on platform. csvlog
429429
# requires logging_collector to be on.
430430

431431
# This is used when logging to stderr:
432-
# logging_collector = on # Enable capturing of stderr and csvlog
432+
logging_collector = on # Enable capturing of stderr and csvlog
433433
# into log files. Required to be on for
434434
# csvlogs.
435435
# (change requires restart)
436436

437437
# These are only used if logging_collector is on:
438-
log_directory = 'pg_log' # directory where log files are written,
438+
log_directory = '/var/log/pg_log' # directory where log files are written,
439439
# can be absolute or relative to PGDATA
440-
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log' # log file name pattern,
440+
log_filename = 'postgresql-%Y-%m-%d_%H-%M' # log file name pattern,
441441
# can include strftime() escapes
442442
log_file_mode = 0644 # creation mode for log files,
443443
# begin with 0 to use octal notation
444-
#log_truncate_on_rotation = off # If on, an existing log file with the
444+
log_truncate_on_rotation = on # If on, an existing log file with the
445445
# same name as the new log file will be
446446
# truncated rather than appended to.
447447
# But such truncation only occurs on
448448
# time-driven rotation, not on restarts
449449
# or size-driven rotation. Default is
450450
# off, meaning append to existing files
451451
# in all cases.
452-
#log_rotation_age = 1d # Automatic rotation of logfiles will
452+
log_rotation_age = '4min' # Automatic rotation of logfiles will
453453
# happen after that time. 0 disables.
454-
#log_rotation_size = 10MB # Automatic rotation of logfiles will
454+
log_rotation_size = 0 # Automatic rotation of logfiles will
455455
# happen after that much log output.
456456
# 0 disables.
457457

@@ -524,7 +524,7 @@ log_min_duration_statement = 0 # -1 is disabled, 0 logs all statements
524524
#log_checkpoints = off
525525
#log_connections = off
526526
#log_disconnections = off
527-
log_duration = on
527+
# log_duration = on
528528
#log_error_verbosity = default # terse, default, or verbose messages
529529
#log_hostname = off
530530
log_line_prefix= '%m [%p] %d %a %u %h %c ' # special values:
@@ -556,7 +556,7 @@ log_line_prefix= '%m [%p] %d %a %u %h %c ' # special values:
556556
#log_parameter_max_length_on_error = 0 # when logging an error, limit logged
557557
# bind-parameter values to N bytes;
558558
# -1 means print in full, 0 disables
559-
log_statement = 'all' # none, ddl, mod, all
559+
#log_statement = 'all' # none, ddl, mod, all
560560
#log_replication_commands = off
561561
#log_temp_files = -1 # log temporary files equal or larger
562562
# than the specified size in kilobytes;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/sh
2+
3+
# Start cron as root
4+
crond -f &
5+
6+
# Ensure data directory has correct permissions
7+
chown -R postgres:postgres /var/lib/postgresql/data
8+
chmod -R 0700 /var/lib/postgresql/data
9+
10+
# Switch to the postgres user and start PostgreSQL using the original entrypoint
11+
exec su-exec postgres docker-entrypoint.sh "$@"

0 commit comments

Comments
 (0)