Skip to content

Commit d055006

Browse files
committed
feat(accounting): allow flexible database selection
1 parent 12e8e83 commit d055006

File tree

8 files changed

+393
-84
lines changed

8 files changed

+393
-84
lines changed

docker-compose-mongo.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# docker-compose-mongo.yml
2+
# Use: docker compose -f docker-compose.yml -f docker-compose-mongo.yml up
3+
4+
x-default-logging: &logging
5+
driver: "json-file"
6+
options:
7+
max-size: "5m"
8+
max-file: "2"
9+
tag: "{{.Name}}"
10+
11+
services:
12+
# 1. Remove the Postgres service
13+
postgresql: !reset null
14+
15+
# 2. Add the MongoDB service
16+
mongo:
17+
image: mongo:7
18+
container_name: mongo
19+
restart: unless-stopped
20+
environment:
21+
MONGO_INITDB_DATABASE: otel
22+
MONGO_INITDB_ROOT_USERNAME: otelu
23+
MONGO_INITDB_ROOT_PASSWORD: otelp
24+
OTEL_DOTNET_AUTO_INSTRUMENTATION_MONGODB_ENABLED: true
25+
ports:
26+
- "27017:27017"
27+
healthcheck:
28+
test: ["CMD", "mongosh", "--eval", "db.adminCommand('ping')"]
29+
interval: 10s
30+
timeout: 5s
31+
retries: 5
32+
logging: *logging
33+
networks:
34+
- default
35+
36+
# 3. Configure 'accounting' service for MongoDB
37+
accounting:
38+
environment:
39+
- DB_TYPE=mongo
40+
- DB_CONNECTION_STRING=mongodb://otelu:otelp@mongo:27017/otel?authSource=admin
41+
depends_on:
42+
postgresql: !reset []
43+
otel-collector:
44+
condition: service_started
45+
kafka:
46+
condition: service_healthy
47+
mongo:
48+
condition: service_healthy

docker-compose-mysql.yml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# This is a Docker Compose override file to switch the demo from PostgreSQL to MySQL.
2+
# To use it, run:
3+
# docker compose -f docker-compose.yml -f docker-compose-mysql.yml up
4+
5+
# Use the same default logging as the base file
6+
x-default-logging: &logging
7+
driver: "json-file"
8+
options:
9+
max-size: "5m"
10+
max-file: "2"
11+
tag: "{{.Name}}"
12+
13+
services:
14+
# 1. Remove the Postgres service defined in the base file.
15+
# The `!reset null` syntax completely removes the service.
16+
postgresql: !reset null
17+
18+
# 2. Add the MySQL service.
19+
mysql:
20+
image: mysql:8
21+
# The platform is important for users on ARM-based machines (e.g., Apple M1/M2).
22+
# For x86/amd64 machines, you can remove this line.
23+
platform: linux/arm64
24+
container_name: mysql
25+
restart: unless-stopped
26+
environment:
27+
MYSQL_ROOT_PASSWORD: otelp
28+
MYSQL_DATABASE: otel
29+
MYSQL_USER: otelu
30+
MYSQL_PASSWORD: otelp
31+
ports:
32+
- "3306:3306"
33+
# A healthcheck ensures other services only start after MySQL is ready.
34+
healthcheck:
35+
test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
36+
interval: 10s
37+
timeout: 5s
38+
retries: 5
39+
logging: *logging
40+
networks:
41+
- default
42+
43+
# 3. Override the 'accounting' service to use MySQL.
44+
accounting:
45+
environment:
46+
# Set the DB_TYPE to trigger the MySQL logic in your code.
47+
- DB_TYPE=mysql
48+
# IMPORTANT: Use the correct key-value pair format for the Pomelo driver.
49+
- DB_CONNECTION_STRING=Server=mysql;Port=3306;Database=otel;User=otelu;Password=otelp;
50+
depends_on:
51+
postgresql: !reset []
52+
otel-collector:
53+
condition: service_started
54+
kafka:
55+
condition: service_healthy
56+
# Add a dependency on the new mysql service and wait for it to be healthy.
57+
mysql:
58+
condition: service_healthy

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ services:
3333
restart: unless-stopped
3434
environment:
3535
- KAFKA_ADDR
36+
- DB_TYPE=postgres
3637
- OTEL_EXPORTER_OTLP_ENDPOINT=http://${OTEL_COLLECTOR_HOST}:${OTEL_COLLECTOR_PORT_HTTP}
3738
- OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE
3839
- OTEL_RESOURCE_ATTRIBUTES

src/accounting/Accounting.csproj

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,39 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

3-
<PropertyGroup>
4-
<OutputType>Exe</OutputType>
5-
<TargetFramework>net8.0</TargetFramework>
6-
<ImplicitUsings>enable</ImplicitUsings>
7-
<Nullable>enable</Nullable>
8-
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
9-
</PropertyGroup>
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
9+
<!-- ADDED: Suppress the security audit warning that is failing the build -->
10+
<NoWarn>$(NoWarn);NU1903</NoWarn>
11+
</PropertyGroup>
1012

11-
<ItemGroup>
12-
<PackageReference Include="Confluent.Kafka" Version="2.11.0" />
13-
<PackageReference Include="EFCore.NamingConventions" Version="9.0.0" />
14-
<PackageReference Include="Google.Protobuf" Version="3.31.1" />
15-
<PackageReference Include="Grpc.Tools" Version="2.68.1">
16-
<PrivateAssets>all</PrivateAssets>
17-
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
18-
</PackageReference>
19-
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.7" />
20-
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.22.1" />
21-
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
22-
<PackageReference Include="OpenTelemetry.AutoInstrumentation" Version="1.12.0" />
23-
</ItemGroup>
13+
<ItemGroup>
14+
<PackageReference Include="Confluent.Kafka" Version="2.11.0" />
15+
<PackageReference Include="EFCore.NamingConventions" Version="8.0.3" />
16+
<PackageReference Include="Google.Protobuf" Version="3.31.1" />
17+
<PackageReference Include="Grpc.Tools" Version="2.68.1">
18+
<PrivateAssets>all</PrivateAssets>
19+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
20+
</PackageReference>
21+
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
22+
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.22.1" />
23+
<PackageReference Include="MongoDB.Driver" Version="3.0.0" />
24+
<PackageReference Include="MongoDB.Driver.Core.Extensions.DiagnosticSources" Version="2.1.0" />
25+
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.4" />
26+
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.2" />
27+
<PackageReference Include="OpenTelemetry.AutoInstrumentation" Version="1.9.0" />
28+
</ItemGroup>
2429

25-
<ItemGroup>
26-
<!-- GrpcServices is 'none' so that we do not need to depend on the grpc nuget package, and we only need protobuf support. -->
27-
<Protobuf Include="src\protos\demo.proto" GrpcServices="none" />
28-
</ItemGroup>
30+
<ItemGroup>
31+
<!-- GrpcServices is 'none' so that we do not need to depend on the grpc nuget package, and we only need protobuf support. -->
32+
<Protobuf Include="src/protos/demo.proto" GrpcServices="none" />
33+
</ItemGroup>
2934

30-
<ItemGroup>
31-
<Folder Include="src\protos\" />
32-
</ItemGroup>
35+
<ItemGroup>
36+
<Folder Include="src\protos\" />
37+
</ItemGroup>
3338

34-
</Project>
39+
</Project>

0 commit comments

Comments
 (0)