Skip to content

Commit ccdcbe6

Browse files
Extended observability configurability
1 parent 6f440a1 commit ccdcbe6

File tree

7 files changed

+74
-23
lines changed

7 files changed

+74
-23
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<ImplicitUsings>enable</ImplicitUsings>
55
<Nullable>enable</Nullable>
66
<IsPackable>true</IsPackable>
7-
<Version>2.3.14</Version>
7+
<Version>2.3.16</Version>
88
<Authors>Andrey Serdyuk</Authors>
99
<Company>TaskHub</Company>
1010
<Title>TaskHub.Shared - Reusable Primitives for .NET Microservices</Title>

TaskHub.Observability.OpenTelemetry/OpenTelemetryBootstrap.cs renamed to TaskHub.Observability.OpenTelemetry/Bootstrap/OpenTelemetryBootstrap.cs

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,55 @@
44
using OpenTelemetry.Metrics;
55
using OpenTelemetry.Resources;
66
using OpenTelemetry.Trace;
7+
using TaskHub.Observability.OpenTelemetry.Options;
8+
using TaskHub.Observability.OpenTelemetry.Tools;
79

8-
namespace TaskHub.Observability.OpenTelemetry;
10+
namespace TaskHub.Observability.OpenTelemetry.Bootstrap;
911

1012
public static class OpenTelemetryBootstrap
1113
{
12-
public static void AddAppOpenTelemetry(this IServiceCollection services, Action<IServiceProvider, OpenTelemetryOptions> action)
14+
public static void AddAppOpenTelemetry(this IServiceCollection services, Action<OpenTelemetryOptions> action)
1315
{
14-
using var sp = services.BuildServiceProvider();
1516
var options = new OpenTelemetryOptions();
16-
action(sp, options);
17+
action(options);
1718

1819
services.AddOpenTelemetry().ConfigureResource(t =>
1920
{
2021
t.AddService(serviceName: options.ServiceName);
2122
t.AddAttributes([new("deployment.environment", options.Environment)]);
2223
}).WithTracing(t =>
2324
{
24-
t.AddSource("CommandsBus", "NominatimClient", "NominatimService");
25-
t.AddSource([.. options.Sources]);
26-
27-
if (options.IsHttpTracesEnabled)
25+
if (options.Sampling.IsEnabled)
26+
{
27+
switch (options.Sampling.Type)
28+
{
29+
case SamplingType.AlwaysOn:
30+
t.SetSampler(new AlwaysOnSampler());
31+
break;
32+
case SamplingType.AlwaysOff:
33+
t.SetSampler(new AlwaysOffSampler());
34+
break;
35+
case SamplingType.TraceIdRatio:
36+
t.SetSampler(new TraceIdRatioBasedSampler(options.Sampling.Probability));
37+
break;
38+
case SamplingType.ParentBased:
39+
t.SetSampler(new ParentBasedSampler(new TraceIdRatioBasedSampler(options.Sampling.Probability)));
40+
break;
41+
default:
42+
t.SetSampler(new AlwaysOffSampler());
43+
break;
44+
}
45+
}
46+
else
2847
{
29-
t.AddHttpClientInstrumentation(o => o.RecordException = options.RecordException);
48+
t.SetSampler(new AlwaysOffSampler());
3049
}
3150

32-
t.AddHttpClientInstrumentation(o =>
51+
t.SetSampler(new TraceIdRatioBasedSampler(0.1f));
52+
t.AddSource("CommandsBus", "NominatimClient", "NominatimService");
53+
t.AddSource([.. options.Sources]);
54+
55+
if (options.IsHttpTracesEnabled) t.AddHttpClientInstrumentation(o =>
3356
{
3457
o.RecordException = options.RecordException;
3558
o.FilterHttpRequestMessage = req =>
@@ -45,10 +68,11 @@ public static void AddAppOpenTelemetry(this IServiceCollection services, Action<
4568
return false;
4669
}
4770

48-
4971
return true;
5072
};
51-
}).AddAspNetCoreInstrumentation(o =>
73+
});
74+
75+
if (options.IsAspNetTracesEnabled) t.AddAspNetCoreInstrumentation(o =>
5276
{
5377
o.RecordException = options.RecordException;
5478
o.Filter = ctx =>
@@ -61,22 +85,26 @@ public static void AddAppOpenTelemetry(this IServiceCollection services, Action<
6185

6286
return options.Ignore.Any(ignore => path.StartsWithSegments(ignore)) == false;
6387
};
64-
}).AddEntityFrameworkCoreInstrumentation(cntx => cntx.EnrichWithIDbCommand = (activity, command) =>
88+
});
89+
90+
if (options.IsEFCoreTracesEnabled) t.AddEntityFrameworkCoreInstrumentation(cntx => cntx.EnrichWithIDbCommand = (activity, command) =>
6591
{
6692
var sql = command.CommandText?.TrimStart() ?? string.Empty;
6793
var space = sql.IndexOf(' ');
6894
var verb = space > 0 ? sql[..space].ToUpperInvariant() : "SQL";
69-
var table = Tools.ExtractTableName(sql, verb);
95+
var table = OtelTools.ExtractTableName(sql, verb);
7096

7197
activity.DisplayName = $"SQL {verb} {table}".Trim();
7298
activity.SetTag("db.statement", sql);
7399
activity.SetTag("db.system", command.Connection?.GetType().Name);
74100
activity.SetTag("db.table", table);
75101
activity.SetTag("db.name", command.Connection?.Database);
76-
}).AddOtlpExporter(cntx =>
102+
});
103+
104+
t.AddOtlpExporter(cntx =>
77105
{
78106
cntx.Endpoint = new Uri(options.TracingEndpoint);
79-
cntx.Protocol = OtlpExportProtocol.Grpc;
107+
cntx.Protocol = options.Protocol == "grpc" ? OtlpExportProtocol.Grpc : OtlpExportProtocol.HttpProtobuf;
80108
});
81109
});
82110
}

TaskHub.Observability.OpenTelemetry/OpenTelemetryOptions.cs renamed to TaskHub.Observability.OpenTelemetry/Options/OpenTelemetryOptions.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
namespace TaskHub.Observability.OpenTelemetry;
1+
namespace TaskHub.Observability.OpenTelemetry.Options;
22

33
public class OpenTelemetryOptions
44
{
55
public string TracingEndpoint { get; set; } = string.Empty;
6+
public string Protocol { get; set; } = string.Empty;
67
public string DbName { get; set; } = string.Empty;
78
public string ServiceName { get; set; } = string.Empty;
89
public string ServiceVersion { get; set; } = string.Empty;
910
public string Environment { get; set; } = string.Empty;
1011
public bool RecordException { get; set; } = false;
1112
public bool SetDbStatementForText { get; set; } = false;
1213
public bool IsHttpTracesEnabled { get; set; } = true;
14+
public bool IsAspNetTracesEnabled { get; set; } = true;
15+
public bool IsEFCoreTracesEnabled { get; set; } = true;
1316
public List<string> Sources { get; set; } = [];
1417
public List<string> Ignore { get; set; } = [];
18+
public SamplingOptions Sampling { get; set; } = new();
1519
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace TaskHub.Observability.OpenTelemetry.Options;
2+
3+
public class SamplingOptions
4+
{
5+
public bool IsEnabled { get; set; }
6+
public SamplingType Type { get; set; } = SamplingType.AlwaysOn;
7+
public float Probability { get; set; } = 1.0f;
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace TaskHub.Observability.OpenTelemetry.Options;
2+
3+
public enum SamplingType
4+
{
5+
AlwaysOn,
6+
AlwaysOff,
7+
TraceIdRatio,
8+
ParentBased
9+
}

TaskHub.Observability.OpenTelemetry/ExtractTableName.cs renamed to TaskHub.Observability.OpenTelemetry/Tools/ExtractTableName.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
namespace TaskHub.Observability.OpenTelemetry;
1+
namespace TaskHub.Observability.OpenTelemetry.Tools;
22

3-
public class Tools
3+
public class OtelTools
44
{
55
public static string ExtractTableName(string sql, string verb)
66
{

TaskHub.Shared.Bootstraper.Full/FullHostBuilder.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using Prometheus;
66
using TaskHub.Observability.Logger;
77
using TaskHub.Observability.Metrics.Implementation.Bootstrap;
8-
using TaskHub.Observability.OpenTelemetry;
8+
using TaskHub.Observability.OpenTelemetry.Bootstrap;
99
using TaskHub.Shared.Authorization.Identity.Bootstrap;
1010
using TaskHub.Shared.Bootstraper.Basic;
1111
using TaskHub.Shared.Commands.Implementation.Bootstrap;
@@ -44,10 +44,12 @@ protected override void AppStart()
4444
base.AppStart();
4545
Builder.Host.AddAppSerilog();
4646
Builder.Services.AddServiceInfo();
47-
Builder.Services.AddAppOpenTelemetry((sp, o) =>
47+
48+
Builder.Services.AddAppOpenTelemetry(o =>
4849
{
4950
Builder.Configuration.GetSection("OpenTelemetry").Bind(o);
50-
var appInfo = sp.GetRequiredService<ServiceInfo>();
51+
52+
var appInfo = Builder.Configuration.GetSection("ServiceInfo").Get<ServiceInfo>() ?? new();
5153
o.ServiceName = appInfo.Name;
5254
o.ServiceVersion = appInfo.Version;
5355
});

0 commit comments

Comments
 (0)