Skip to content

Commit e7cf799

Browse files
author
Magne Helleborg
authored
Add MongoDB default Guid serialization (#249)
* Added configuration for MongoDB Guid representation * Allow access to the underlying connection string / MongoUrl for each tenant
1 parent 56af7e2 commit e7cf799

19 files changed

Lines changed: 165 additions & 152 deletions

Directory.Packages.props

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
<PackageVersion Include="MongoDB.Driver" Version="3.0.0" />
4343
<PackageVersion Include="MongoDB.Driver.Core.Extensions.DiagnosticSources" Version="2.0.0" />
4444
<PackageVersion Include="Moq" Version="4.18.4" />
45-
<PackageVersion Include="FluentAssertions" Version="6.12.1" />
45+
<PackageVersion Include="FluentAssertions" Version="6.12.2" />
4646
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
4747
<PackageVersion Include="Polly" Version="8.4.1" />
4848
<PackageVersion Include="Proto.Actor" Version="1.7.1-alpha.0.1" />
@@ -53,10 +53,12 @@
5353
<PackageVersion Include="Swashbuckle.AspNetCore" Version="6.9.0" />
5454
<PackageVersion Include="System.Linq.Async" Version="6.0.1" />
5555
<PackageVersion Include="System.Collections.Immutable" Version="8.0.0" />
56+
<PackageVersion Include="System.Formats.Asn1" Version="8.0.1" />
5657
<PackageVersion Include="System.Net.Http" Version="4.3.4" />
5758
<PackageVersion Include="System.Reactive" Version="6.0.1" />
5859
<PackageVersion Include="System.Security.Claims" Version="4.3.0" />
5960
<PackageVersion Include="System.Text.Json" Version="8.0.5" />
61+
<PackageVersion Include="System.Text.RegularExpressions" Version="4.3.1" />
6062
<PackageVersion Include="OpenTelemetry.Api" Version="1.9.0" />
6163
<PackageVersion Include="OpenTelemetry.Instrumentation.Runtime" Version="1.9.0" />
6264
<PackageVersion Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.9.0" />

Samples/Environment/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ services:
88
- 27017:27017
99

1010
runtime:
11-
image: dolittle/runtime:9.6.1
11+
image: dolittle/runtime:9.7.0
1212
volumes:
1313
- ./runtime.yml:/app/.dolittle/runtime.yml
1414
- ./appsettings.json:/app/appsettings.json
Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,51 @@
1-
version: "3"
21
services:
32
tempo:
4-
image: grafana/tempo:latest
3+
image: grafana/tempo:2.6.1
54
command: [ "-config.file=/etc/tempo.yaml" ]
65
volumes:
7-
- ./tempo-local.yaml:/etc/tempo.yaml
6+
- ./tempo.yaml:/etc/tempo.yaml
87
- ./tempo-data:/tmp/tempo
98
ports:
10-
- "3200:3200" # tempo
9+
# - "3200:3200" # tempo
10+
- "127.0.0.1:3200:3200"
1111
# - "14268:14268" # jaeger ingest
1212
# - "4317:4317" # otlp grpc
1313
# - "4318:4318" # otlp http
1414
# - "9411:9411" # zipkin
1515

1616
otel-collector:
17-
image: otel/opentelemetry-collector-contrib:0.52.0
17+
image: otel/opentelemetry-collector-contrib:0.101.0
1818
depends_on:
1919
- tempo
2020
command: [ "--config=/etc/otel-collector.yaml", "--set=service.telemetry.logs.level=debug"]
2121
volumes:
2222
- ./otel-collector.yaml:/etc/otel-collector.yaml
2323
ports:
24-
- "4317:4317" # otlp grpc
24+
- "127.0.0.1:4317:4317" # otlp grpc
2525

2626
loki:
27-
image: grafana/loki:2.5.0
27+
image: grafana/loki:3.2.1
2828
ports:
29-
- "3100:3100"
29+
# - "3100:3100"
30+
- "127.0.0.1:3100:3100"
3031
command: -config.file=/etc/loki/local-config.yaml
3132

3233
prometheus:
33-
image: prom/prometheus:latest
34-
command: [ "--config.file=/etc/prometheus.yaml", "--web.enable-remote-write-receiver", "--enable-feature=exemplar-storage"]
34+
image: prom/prometheus:v2.54.1
35+
command: [ "--config.file=/etc/prometheus.yaml", "--web.enable-remote-write-receiver", "--enable-feature=otlp-write-receiver,exemplar-storage"]
3536
volumes:
3637
- ./prometheus.yaml:/etc/prometheus.yaml
3738
ports:
3839
- "9090:9090"
3940

4041
grafana:
41-
image: grafana/grafana:8.5.3
42+
image: grafana/grafana:11.2.2
4243
volumes:
4344
- ./grafana-datasources.yaml:/etc/grafana/provisioning/datasources/datasources.yaml
4445
environment:
4546
- GF_AUTH_ANONYMOUS_ENABLED=true
4647
- GF_AUTH_ANONYMOUS_ORG_ROLE=Admin
4748
- GF_AUTH_DISABLE_LOGIN_FORM=true
49+
- GF_FEATURE_TOGGLES_ENABLE=traceqlEditor traceQLStreaming metricsSummary
4850
ports:
49-
- "3000:3000"
51+
- "4000:3000"

Samples/Environment/grafana/otel-collector.yaml

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,33 @@ exporters:
88
endpoint: tempo:4317
99
tls:
1010
insecure: true
11+
otlphttp:
12+
endpoint: http://prometheus:9090/api/v1/otlp
13+
tls:
14+
insecure: true
1115

1216
loki:
13-
endpoint: http://loki:3100/loki/api/v1/push
14-
format: json
17+
endpoint: "http://loki:3100/loki/api/v1/push"
1518
tls:
1619
insecure: true
17-
# tenant_id: "example"
18-
labels:
19-
resource:
20-
container.name: "container_name"
21-
service.name: "service_name"
22-
service.version: "service_version"
23-
service.instance.id: "service_instance_id"
20+
# labels:
21+
# resource:
22+
# container.name: "container_name"
23+
# service.name: "service_name"
24+
# service.version: "service_version"
25+
# service.instance.id: "service_instance_id"
26+
# prometheusremotewrite:
27+
# endpoint: http://prometheus:9090/api/v1/write
28+
# target_info:
2429

2530
processors:
2631
batch:
32+
timeout: 200ms
33+
resource:
34+
attributes:
35+
- key: environment
36+
value: "dev"
37+
action: upsert
2738

2839
extensions:
2940
health_check:
@@ -35,9 +46,13 @@ service:
3546
pipelines:
3647
traces:
3748
receivers: [otlp]
38-
processors: [batch]
49+
processors: [resource, batch]
3950
exporters: [otlp]
51+
metrics:
52+
receivers: [otlp]
53+
processors: [resource, batch]
54+
exporters: [otlphttp]
4055
logs:
4156
receivers: [otlp]
42-
processors: [batch]
57+
processors: [resource, batch]
4358
exporters: [loki]

Samples/Environment/grafana/tempo-local.yaml

Lines changed: 0 additions & 61 deletions
This file was deleted.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
stream_over_http_enabled: true
2+
server:
3+
http_listen_port: 3200
4+
log_level: info
5+
6+
query_frontend:
7+
search:
8+
duration_slo: 5s
9+
throughput_bytes_slo: 1.073741824e+09
10+
trace_by_id:
11+
duration_slo: 5s
12+
13+
distributor:
14+
receivers: # this configuration will listen on all ports and protocols that tempo is capable of.
15+
jaeger: # the receives all come from the OpenTelemetry collector. more configuration information can
16+
protocols: # be found there: https://github.com/open-telemetry/opentelemetry-collector/tree/main/receiver
17+
thrift_http: #
18+
grpc: # for a production deployment you should only enable the receivers you need!
19+
thrift_binary:
20+
thrift_compact:
21+
zipkin:
22+
otlp:
23+
protocols:
24+
http:
25+
grpc:
26+
opencensus:
27+
28+
ingester:
29+
max_block_duration: 5m # cut the headblock when this much time passes. this is being set for demo purposes and should probably be left alone normally
30+
31+
compactor:
32+
compaction:
33+
block_retention: 1h # overall Tempo trace retention. set for demo purposes
34+
35+
metrics_generator:
36+
registry:
37+
external_labels:
38+
source: tempo
39+
cluster: docker-compose
40+
storage:
41+
path: /tmp/tempo/generator/wal
42+
remote_write:
43+
- url: http://prometheus:9090/api/v1/write
44+
send_exemplars: true
45+
46+
storage:
47+
trace:
48+
backend: local # backend configuration to use
49+
wal:
50+
path: /tmp/tempo/wal # where to store the the wal locally
51+
local:
52+
path: /tmp/tempo/blocks
53+
54+
overrides:
55+
defaults:
56+
metrics_generator:
57+
processors: [service-graphs, span-metrics] # enables metrics generator

Source/Resources/MongoDB/DolittleMongoConventions.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// Copyright (c) Dolittle. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4+
using MongoDB.Bson;
45
using MongoDB.Bson.Serialization;
56
using MongoDB.Bson.Serialization.Conventions;
7+
using MongoDB.Bson.Serialization.Serializers;
68

79
namespace Dolittle.SDK.Resources.MongoDB;
810

@@ -22,7 +24,7 @@ public static class DolittleMongoConventions
2224
/// <summary>
2325
/// Ensured that the default Mongo Conventions are registered;
2426
/// </summary>
25-
public static void EnsureConventionsAreRegistered()
27+
public static void EnsureConventionsAreRegistered(GuidRepresentation defaultRepresentation)
2628
{
2729
if (_isRegistered)
2830
{
@@ -36,6 +38,10 @@ public static void EnsureConventionsAreRegistered()
3638
}
3739
_isRegistered = true;
3840
BsonSerializer.RegisterSerializationProvider(new ConceptSerializationProvider());
41+
if (defaultRepresentation != GuidRepresentation.Unspecified)
42+
{
43+
BsonSerializer.RegisterSerializer(new GuidSerializer(defaultRepresentation));
44+
}
3945
var pack = new ConventionPack();
4046
pack.AddClassMapConvention("Ignore extra elements", _ => _.SetIgnoreExtraElements(true));
4147
ConventionRegistry.Register(ConventionPackName, pack, _ => true);

Source/Resources/MongoDB/IMongoDBResource.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,14 @@ public interface IMongoDBResource
1717
/// <param name="databaseSettingsCallback">The <see cref="Action{T}"/> callback for creating <see cref="MongoDatabaseSettings"/> used to create the <see cref="IMongoDatabase"/>.</param>
1818
/// <returns>An <see cref="IMongoDatabase"/>.</returns>
1919
IMongoDatabase GetDatabase(Action<MongoDatabaseSettings>? databaseSettingsCallback = default);
20+
21+
/// <summary>
22+
/// Gets the connection string that is used to connect to the MongoDB resource.
23+
/// </summary>
24+
string ConnectionString { get; }
25+
26+
/// <summary>
27+
/// Gets the <see cref="MongoUrl"/> that is used to connect to the MongoDB resource.
28+
/// </summary>
29+
MongoUrl ConnectionUrl { get; }
2030
}

Source/Resources/MongoDB/Internal/MongoDBResource.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ namespace Dolittle.SDK.Resources.MongoDB.Internal;
1212
/// </summary>
1313
public class MongoDBResource : IMongoDBResource
1414
{
15-
readonly MongoUrl _mongoUrl;
1615
readonly MongoClient _mongoClient;
1716

1817
/// <summary>
@@ -23,8 +22,9 @@ public class MongoDBResource : IMongoDBResource
2322
public MongoDBResource(GetMongoDBResponse runtimeMongoDbResponse,
2423
Action<MongoClientSettings>? clientSettingsCallback)
2524
{
26-
_mongoUrl = MongoUrl.Create(runtimeMongoDbResponse.ConnectionString);
27-
var clientSettings = MongoClientSettings.FromUrl(_mongoUrl);
25+
ConnectionString = runtimeMongoDbResponse.ConnectionString;
26+
ConnectionUrl = MongoUrl.Create(runtimeMongoDbResponse.ConnectionString);
27+
var clientSettings = MongoClientSettings.FromUrl(ConnectionUrl);
2828
ConfigureSettings(clientSettingsCallback, clientSettings);
2929

3030
_mongoClient = new MongoClient(clientSettings.Freeze());
@@ -34,12 +34,16 @@ public MongoDBResource(GetMongoDBResponse runtimeMongoDbResponse,
3434
/// <inheritdoc />
3535
public IMongoDatabase GetDatabase(Action<MongoDatabaseSettings>? databaseSettingsCallback = default)
3636
{
37-
DolittleMongoConventions.EnsureConventionsAreRegistered();
3837
var databaseSettings = new MongoDatabaseSettings();
3938
databaseSettingsCallback?.Invoke(databaseSettings);
40-
return _mongoClient.GetDatabase(_mongoUrl.DatabaseName, databaseSettings);
39+
return _mongoClient.GetDatabase(ConnectionUrl.DatabaseName, databaseSettings);
4140
}
4241

42+
public string ConnectionString { get; }
43+
44+
/// <inheritdoc />
45+
public MongoUrl ConnectionUrl { get; }
46+
4347
/// <summary>
4448
/// Configures <see cref="MongoClientSettings"/> class.
4549
/// </summary>

0 commit comments

Comments
 (0)