Skip to content

Commit a1e472e

Browse files
More realistic PostgreSQL setup with dedicated users, database, and monitoring
- Replace the generic `root`/`otelu` users and `otel` database with a dedicated `astronomy_db` owned by `astronomy_user`, keeping the default `postgres` database untouched - Add `monitoring_user` with `pg_monitor` role for read-only observability access across all databases and schemas - Switch OTel Collector postgresql receiver to `monitoring_user` instead of the superuser - Enable `pg_stat_statements` on all databases via `shared_preload_libraries` and `CREATE EXTENSION` - Rename the Compose service and container from `postgresql` to `astronomy-db` for clarity; update `POSTGRES_HOST` accordingly - Set `POSTGRES_PASSWORD` to `changeit` for the default superuser
1 parent 1cdd4e0 commit a1e472e

6 files changed

Lines changed: 47 additions & 33 deletions

File tree

.env

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,9 @@ VALKEY_PORT=6379
170170
VALKEY_ADDR=valkey-cart:${VALKEY_PORT}
171171

172172
# Postgres
173-
POSTGRES_HOST=postgresql
173+
POSTGRES_HOST=astronomy-db
174174
POSTGRES_PORT=5432
175-
POSTGRES_DB=otel
176-
POSTGRES_PASSWORD=otel
175+
POSTGRES_PASSWORD=changeit
177176
POSTGRES_DOCKERFILE=./src/postgresql/Dockerfile
178177

179178
# ********************

docker-compose-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ services:
127127
POSTGRES_PASSWORD: postgres
128128
POSTGRES_USER: postgres
129129
healthcheck:
130-
test: pg_isready -U "$$POSTGRES_USER" -d "$$POSTGRES_DB"
130+
test: pg_isready -U "postgres" -d "$$POSTGRES_DB"
131131
interval: 1s
132132
timeout: 5s
133133
retries: 60

docker-compose.minimal.yml

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -425,11 +425,11 @@ services:
425425
- OTEL_SERVICE_NAME=product-catalog
426426
- OTEL_CONFIG_FILE=/otel-config.yml
427427
- OTEL_SEMCONV_STABILITY_OPT_IN=database
428-
- DB_CONNECTION_STRING=postgres://otelu:otelp@${POSTGRES_HOST}/${POSTGRES_DB}?sslmode=disable
428+
- DB_CONNECTION_STRING=postgres://astronomy_user:astronomy_password@${POSTGRES_HOST}/astronomy_db?sslmode=disable
429429
depends_on:
430430
otel-collector:
431431
condition: service_started
432-
postgresql:
432+
astronomy-db:
433433
condition: service_started
434434
volumes:
435435
- ./otel-config.yml:/otel-config.yml
@@ -460,7 +460,7 @@ services:
460460
- OTEL_SERVICE_NAME=product-reviews
461461
- OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=true
462462
- PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
463-
- DB_CONNECTION_STRING=host=${POSTGRES_HOST} user=otelu password=otelp dbname=${POSTGRES_DB}
463+
- DB_CONNECTION_STRING=host=${POSTGRES_HOST} user=astronomy_user password=astronomy_password dbname=astronomy_db
464464
- LLM_BASE_URL
465465
- OPENAI_API_KEY
466466
- LLM_MODEL
@@ -472,7 +472,7 @@ services:
472472
condition: service_started
473473
llm:
474474
condition: service_started
475-
postgresql:
475+
astronomy-db:
476476
condition: service_started
477477
otel-collector:
478478
condition: service_started
@@ -607,21 +607,20 @@ services:
607607
logging:
608608
*logging
609609

610-
# Postgresql used by Product Reviews service
611-
postgresql:
610+
# PostgreSQL database for the astronomy demo
611+
astronomy-db:
612612
image: ${POSTGRES_IMAGE}
613-
container_name: postgresql
613+
container_name: astronomy-db
614614
deploy:
615615
resources:
616616
limits:
617617
memory: 80M
618618
restart: unless-stopped
619619
ports:
620620
- ${POSTGRES_PORT}
621+
command: postgres -c shared_preload_libraries=pg_stat_statements
621622
environment:
622-
- POSTGRES_USER=root
623623
- POSTGRES_PASSWORD
624-
- POSTGRES_DB
625624
volumes:
626625
- ./src/postgresql/init.sql:/docker-entrypoint-initdb.d/init.sql
627626
logging: *logging

docker-compose.yml

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ services:
3737
- OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE
3838
- OTEL_RESOURCE_ATTRIBUTES
3939
- OTEL_SERVICE_NAME=accounting
40-
- DB_CONNECTION_STRING=Host=${POSTGRES_HOST};Username=otelu;Password=otelp;Database=${POSTGRES_DB}
40+
- DB_CONNECTION_STRING=Host=${POSTGRES_HOST};Username=astronomy_user;Password=astronomy_password;Database=astronomy_db
4141
- OTEL_DOTNET_AUTO_TRACES_ENTITYFRAMEWORKCORE_INSTRUMENTATION_ENABLED=false
4242
depends_on:
4343
otel-collector:
@@ -504,13 +504,13 @@ services:
504504
- OTEL_SERVICE_NAME=product-catalog
505505
- OTEL_CONFIG_FILE=/otel-config.yml
506506
- OTEL_SEMCONV_STABILITY_OPT_IN=database
507-
- DB_CONNECTION_STRING=postgres://otelu:otelp@${POSTGRES_HOST}/${POSTGRES_DB}?sslmode=disable
507+
- DB_CONNECTION_STRING=postgres://astronomy_user:astronomy_password@${POSTGRES_HOST}/astronomy_db?sslmode=disable
508508
depends_on:
509509
otel-collector:
510510
condition: service_started
511511
flagd:
512512
condition: service_started
513-
postgresql:
513+
astronomy-db:
514514
condition: service_started
515515
volumes:
516516
- ./otel-config.yml:/otel-config.yml
@@ -541,7 +541,7 @@ services:
541541
- OTEL_SERVICE_NAME=product-reviews
542542
- OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=true
543543
- PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
544-
- DB_CONNECTION_STRING=host=${POSTGRES_HOST} user=otelu password=otelp dbname=${POSTGRES_DB}
544+
- DB_CONNECTION_STRING=host=${POSTGRES_HOST} user=astronomy_user password=astronomy_password dbname=astronomy_db
545545
- LLM_BASE_URL
546546
- OPENAI_API_KEY
547547
- LLM_MODEL
@@ -555,7 +555,7 @@ services:
555555
condition: service_started
556556
llm:
557557
condition: service_started
558-
postgresql:
558+
astronomy-db:
559559
condition: service_started
560560
otel-collector:
561561
condition: service_started
@@ -778,21 +778,20 @@ services:
778778
condition: service_started
779779
logging: *logging
780780

781-
# Postgresql used by Accounting service
782-
postgresql:
781+
# PostgreSQL database for the astronomy demo
782+
astronomy-db:
783783
image: ${POSTGRES_IMAGE}
784-
container_name: postgresql
784+
container_name: astronomy-db
785785
deploy:
786786
resources:
787787
limits:
788788
memory: 80M
789789
restart: unless-stopped
790790
ports:
791791
- ${POSTGRES_PORT}
792+
command: postgres -c shared_preload_libraries=pg_stat_statements
792793
environment:
793-
- POSTGRES_USER=root
794794
- POSTGRES_PASSWORD
795-
- POSTGRES_DB
796795
volumes:
797796
- ./src/postgresql/init.sql:/docker-entrypoint-initdb.d/init.sql
798797
logging: *logging

src/otel-collector/otelcol-config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ receivers:
2424
api_version: "1.44"
2525
postgresql:
2626
endpoint: ${POSTGRES_HOST}:${POSTGRES_PORT}
27-
username: root
28-
password: ${POSTGRES_PASSWORD}
27+
username: monitoring_user
28+
password: monitoring_password
2929
metrics:
3030
postgresql.blks_hit:
3131
enabled: true

src/postgresql/init.sql

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
11
-- Copyright The OpenTelemetry Authors
22
-- SPDX-License-Identifier: Apache-2.0
33

4-
CREATE USER otelu WITH PASSWORD 'otelp';
4+
-- Enable pg_stat_statements in the default postgres database
5+
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
6+
7+
-- Create application user
8+
CREATE USER astronomy_user WITH PASSWORD 'astronomy_password';
9+
10+
-- Create application database
11+
CREATE DATABASE astronomy_db OWNER astronomy_user;
12+
13+
-- Create monitoring user with pg_monitor role (visibility on all databases and schemas)
14+
CREATE USER monitoring_user WITH PASSWORD 'monitoring_password';
15+
GRANT pg_monitor TO monitoring_user;
16+
17+
-- Switch to the application database
18+
\connect astronomy_db
19+
20+
-- Enable pg_stat_statements in the application database
21+
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
522

623
-- Accounting Service: create a schema
724
CREATE SCHEMA accounting;
8-
GRANT USAGE ON SCHEMA accounting TO otelu;
25+
GRANT USAGE ON SCHEMA accounting TO astronomy_user;
926

1027
-- Accounting Service: create tables
1128
CREATE TABLE accounting."order" (
@@ -38,11 +55,11 @@ CREATE TABLE accounting.orderitem (
3855
);
3956

4057
-- Accounting Service: grant permission to schema
41-
GRANT SELECT, INSERT, UPDATE ON ALL TABLES IN SCHEMA accounting TO otelu;
58+
GRANT SELECT, INSERT, UPDATE ON ALL TABLES IN SCHEMA accounting TO astronomy_user;
4259

4360
-- Product Review Service: create a schema
4461
CREATE SCHEMA reviews;
45-
GRANT USAGE ON SCHEMA reviews TO otelu;
62+
GRANT USAGE ON SCHEMA reviews TO astronomy_user;
4663

4764
-- Product Review Service: create tables
4865
CREATE TABLE reviews.productreviews (
@@ -57,7 +74,7 @@ CREATE TABLE reviews.productreviews (
5774
CREATE INDEX product_id_index ON reviews.productreviews (product_id);
5875

5976
-- Product Review Service: grant permission to schema
60-
GRANT SELECT, INSERT, UPDATE ON ALL TABLES IN SCHEMA reviews TO otelu;
77+
GRANT SELECT, INSERT, UPDATE ON ALL TABLES IN SCHEMA reviews TO astronomy_user;
6178

6279
-- Product Review Service: add product review data
6380
INSERT INTO reviews.productreviews (product_id, username, description, score)
@@ -124,7 +141,7 @@ VALUES
124141

125142
-- Product Catalog Service: create a schema
126143
CREATE SCHEMA catalog;
127-
GRANT USAGE ON SCHEMA catalog TO otelu;
144+
GRANT USAGE ON SCHEMA catalog TO astronomy_user;
128145

129146
-- Product Catalog Service: create tables
130147
CREATE TABLE catalog.products (
@@ -139,7 +156,7 @@ CREATE TABLE catalog.products (
139156
);
140157

141158
-- Product Catalog Service: grant permission to schema
142-
GRANT SELECT ON ALL TABLES IN SCHEMA catalog TO otelu;
159+
GRANT SELECT ON ALL TABLES IN SCHEMA catalog TO astronomy_user;
143160

144161
-- Product Catalog Service: add product data
145162
INSERT INTO catalog.products (id, name, description, picture, price_currency_code, price_units, price_nanos, categories)
@@ -153,4 +170,4 @@ VALUES
153170
('LS4PSXUNUM', 'Red Flashlight', 'This 3-in-1 device features a 3-mode red flashlight, a hand warmer, and a portable power bank for recharging your personal electronics on the go. Whether you use it to light the way at an astronomy star party, a night walk, or wildlife research, ThermoTorch 3 Astro Red''s rugged, IPX4-rated design will withstand your everyday activities.', 'RedFlashlight.jpg', 'USD', 57, 80000000, 'accessories,flashlights'),
154171
('9SIQT8TOJO', 'Optical Tube Assembly', 'Capturing impressive deep-sky astroimages is easier than ever with Rowe-Ackermann Schmidt Astrograph (RASA) V2, the perfect companion to today''s top DSLR or astronomical CCD cameras. This fast, wide-field f/2.2 system allows for shorter exposure times compared to traditional f/10 astroimaging, without sacrificing resolution. Because shorter sub-exposure times are possible, your equatorial mount won''t need to accurately track over extended periods. The short focal length also lessens equatorial tracking demands. In many cases, autoguiding will not be required.', 'OpticalTubeAssembly.jpg', 'USD', 3599, 0, 'accessories,telescopes,assembly'),
155172
('6E92ZMYYFZ', 'Solar Filter', 'Enhance your viewing experience with EclipSmart Solar Filter for 8" telescopes. With two Velcro straps and four self-adhesive Velcro pads for added safety, you can be assured that the solar filter cannot be accidentally knocked off and will provide Solar Safe, ISO compliant viewing.', 'SolarFilter.jpg', 'USD', 69, 950000000, 'accessories,telescopes'),
156-
('HQTGWGPNH4', 'The Comet Book', 'A 16th-century treatise on comets, created anonymously in Flanders (now northern France) and now held at the Universitätsbibliothek Kassel. Commonly known as The Comet Book (or Kometenbuch in German), its full title translates as "Comets and their General and Particular Meanings, According to Ptolomeé, Albumasar, Haly, Aliquind and other Astrologers". The image is from https://publicdomainreview.org/collection/the-comet-book, made available by the Universitätsbibliothek Kassel under a CC-BY SA 4.0 license (https://creativecommons.org/licenses/by-sa/4.0/)', 'TheCometBook.jpg', 'USD', 0, 990000000, 'books');
173+
('HQTGWGPNH4', 'The Comet Book', 'A 16th-century treatise on comets, created anonymously in Flanders (now northern France) and now held at the Universitätsbibliothek Kassel. Commonly known as The Comet Book (or Kometenbuch in German), its full title translates as "Comets and their General and Particular Meanings, According to Ptolomeé, Albumasar, Haly, Aliquind and other Astrologers". The image is from https://publicdomainreview.org/collection/the-comet-book, made available by the Universitätsbibliothek Kassel under a CC-BY SA 4.0 license (https://creativecommons.org/licenses/by-sa/4.0/)', 'TheCometBook.jpg', 'USD', 0, 990000000, 'books');

0 commit comments

Comments
 (0)