Skip to content

Commit 2a7180c

Browse files
committed
OpenTelemetry: Logging, Tracing, Metrics
1 parent 767be72 commit 2a7180c

File tree

36 files changed

+288
-257
lines changed

36 files changed

+288
-257
lines changed

src/Microservices/Common/ClassifiedAds.Infrastructure/ClassifiedAds.Infrastructure.csproj

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,16 @@
4343
<PackageReference Include="MiniProfiler.AspNetCore.Mvc" Version="4.2.22" />
4444
<PackageReference Include="MiniProfiler.EntityFrameworkCore" Version="4.2.22" />
4545
<PackageReference Include="MiniProfiler.Providers.SqlServer" Version="4.2.22" />
46-
<PackageReference Include="OpenTelemetry.Exporter.Jaeger" Version="1.3.1" />
47-
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.3.1" />
48-
<PackageReference Include="OpenTelemetry.Exporter.Zipkin" Version="1.3.1" />
49-
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.0.0-rc8" />
50-
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.0.0-rc8" />
51-
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.0.0-rc8" />
46+
<PackageReference Include="OpenTelemetry.Exporter.Jaeger" Version="1.4.0-rc.1" />
47+
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.4.0-rc.1" />
48+
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol.Logs" Version="1.4.0-rc.1" />
49+
<PackageReference Include="OpenTelemetry.Exporter.Zipkin" Version="1.4.0-rc.1" />
50+
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.4.0-rc.1" />
51+
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.0.0-rc9.10" />
52+
<PackageReference Include="OpenTelemetry.Instrumentation.EntityFrameworkCore" Version="1.0.0-beta.3" />
53+
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.0.0-rc9.10" />
54+
<PackageReference Include="OpenTelemetry.Instrumentation.Process" Version="1.0.0-alpha.3" />
55+
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.1.0-beta.2" />
5256
<PackageReference Include="PuppeteerSharp" Version="8.0.0" />
5357
<PackageReference Include="Quartz" Version="3.5.0" />
5458
<PackageReference Include="RabbitMQ.Client" Version="6.4.0" />

src/Microservices/Common/ClassifiedAds.Infrastructure/DistributedTracing/DistributedTracingExtensions.cs

Lines changed: 0 additions & 60 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace ClassifiedAds.Infrastructure.Logging
2+
{
3+
public class ApplicationInsightsOptions
4+
{
5+
public bool IsEnabled { get; set; }
6+
7+
public string InstrumentationKey { get; set; }
8+
}
9+
}

src/Microservices/Common/ClassifiedAds.Infrastructure/Logging/LoggingExtensions.cs

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
using Microsoft.Extensions.Hosting;
44
using Microsoft.Extensions.Logging;
55
using Microsoft.Extensions.Logging.EventLog;
6+
using OpenTelemetry.Logs;
7+
using OpenTelemetry.Resources;
8+
using OpenTelemetry.Trace;
69
using Serilog;
710
using Serilog.Exceptions;
811
using Serilog.Formatting.Json;
@@ -116,7 +119,7 @@ private static Serilog.Events.LogEventLevel GetLogLevel(string context, LoggingO
116119
{
117120
context = context.Replace("\"", string.Empty);
118121
string level = "Default";
119-
var matches = options.LogLevel.Keys.Where(k => context.StartsWith(k));
122+
var matches = options.LogLevel.Keys.Where(k => context.StartsWith(k, StringComparison.OrdinalIgnoreCase));
120123

121124
if (matches.Any())
122125
{
@@ -141,7 +144,7 @@ public static IWebHostBuilder UseClassifiedAdsLogger(this IWebHostBuilder builde
141144

142145
LoggingOptions options = SetDefault(logOptions(context.Configuration));
143146

144-
if (options.EventLog != null && options.EventLog.IsEnabled)
147+
if (options.EventLog != null && options.EventLog.IsEnabled && OperatingSystem.IsWindows())
145148
{
146149
logging.AddEventLog(new EventLogSettings
147150
{
@@ -150,6 +153,30 @@ public static IWebHostBuilder UseClassifiedAdsLogger(this IWebHostBuilder builde
150153
});
151154
}
152155

156+
if (options?.ApplicationInsights?.IsEnabled ?? false)
157+
{
158+
logging.AddApplicationInsights(
159+
configureTelemetryConfiguration: (config) =>
160+
{
161+
config.ConnectionString = options.ApplicationInsights.InstrumentationKey;
162+
},
163+
configureApplicationInsightsLoggerOptions: (options) => { });
164+
}
165+
166+
if (options?.OpenTelemetry?.IsEnabled ?? false)
167+
{
168+
var resourceBuilder = ResourceBuilder.CreateDefault().AddService(options.OpenTelemetry.ServiceName);
169+
170+
logging.AddOpenTelemetry(configure =>
171+
{
172+
configure.SetResourceBuilder(resourceBuilder)
173+
.AddOtlpExporter(otlpOptions =>
174+
{
175+
otlpOptions.Endpoint = new Uri(options.OpenTelemetry.Otlp.Endpoint);
176+
});
177+
});
178+
}
179+
153180
context.HostingEnvironment.UseClassifiedAdsLogger(options);
154181
});
155182

@@ -171,7 +198,7 @@ public static IHostBuilder UseClassifiedAdsLogger(this IHostBuilder builder, Fun
171198

172199
LoggingOptions options = SetDefault(logOptions(context.Configuration));
173200

174-
if (options.EventLog != null && options.EventLog.IsEnabled)
201+
if (options.EventLog != null && options.EventLog.IsEnabled && OperatingSystem.IsWindows())
175202
{
176203
logging.AddEventLog(new EventLogSettings
177204
{
@@ -180,6 +207,30 @@ public static IHostBuilder UseClassifiedAdsLogger(this IHostBuilder builder, Fun
180207
});
181208
}
182209

210+
if (options?.ApplicationInsights?.IsEnabled ?? false)
211+
{
212+
logging.AddApplicationInsights(
213+
configureTelemetryConfiguration: (config) =>
214+
{
215+
config.ConnectionString = options.ApplicationInsights.InstrumentationKey;
216+
},
217+
configureApplicationInsightsLoggerOptions: (options) => { });
218+
}
219+
220+
if (options?.OpenTelemetry?.IsEnabled ?? false)
221+
{
222+
var resourceBuilder = ResourceBuilder.CreateDefault().AddService(options.OpenTelemetry.ServiceName);
223+
224+
logging.AddOpenTelemetry(configure =>
225+
{
226+
configure.SetResourceBuilder(resourceBuilder)
227+
.AddOtlpExporter(otlpOptions =>
228+
{
229+
otlpOptions.Endpoint = new Uri(options.OpenTelemetry.Otlp.Endpoint);
230+
});
231+
});
232+
}
233+
183234
context.HostingEnvironment.UseClassifiedAdsLogger(options);
184235
});
185236

src/Microservices/Common/ClassifiedAds.Infrastructure/Logging/LoggingOptions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,9 @@ public class LoggingOptions
1111
public ElasticsearchOptions Elasticsearch { get; set; }
1212

1313
public EventLogOptions EventLog { get; set; }
14+
15+
public ApplicationInsightsOptions ApplicationInsights { get; set; }
16+
17+
public OpenTelemetryOptions OpenTelemetry { get; set; }
1418
}
1519
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace ClassifiedAds.Infrastructure.Logging
2+
{
3+
public class OpenTelemetryOptions
4+
{
5+
public bool IsEnabled { get; set; }
6+
7+
public string ServiceName { get; set; }
8+
9+
public OtlpOptions Otlp { get; set; }
10+
}
11+
12+
public class OtlpOptions
13+
{
14+
public string Endpoint { get; set; }
15+
}
16+
}

src/Microservices/Common/ClassifiedAds.Infrastructure/Monitoring/AzureApplicationInsights/AzureApplicationInsightsServiceCollectionExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ public static class AzureApplicationInsightsServiceCollectionExtensions
88
{
99
public static IServiceCollection AddAzureApplicationInsights(this IServiceCollection services, AzureApplicationInsightsOptions azureApplicationInsightsOptions = null)
1010
{
11-
1211
if (azureApplicationInsightsOptions?.IsEnabled ?? false)
1312
{
1413
services.AddApplicationInsightsTelemetry(opt =>

src/Microservices/Common/ClassifiedAds.Infrastructure/Monitoring/MonitoringExtensions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using ClassifiedAds.Infrastructure.Monitoring.AzureApplicationInsights;
22
using ClassifiedAds.Infrastructure.Monitoring.MiniProfiler;
3+
using ClassifiedAds.Infrastructure.Monitoring.OpenTelemetry;
34
using Microsoft.AspNetCore.Builder;
45
using Microsoft.Extensions.DependencyInjection;
56

@@ -19,6 +20,11 @@ public static IServiceCollection AddMonitoringServices(this IServiceCollection s
1920
services.AddAzureApplicationInsights(monitoringOptions.AzureApplicationInsights);
2021
}
2122

23+
if (monitoringOptions?.OpenTelemetry?.IsEnabled ?? false)
24+
{
25+
services.AddClassifiedAdsOpenTelemetry(monitoringOptions.OpenTelemetry);
26+
}
27+
2228
return services;
2329
}
2430

src/Microservices/Common/ClassifiedAds.Infrastructure/Monitoring/MonitoringOptions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using ClassifiedAds.Infrastructure.Monitoring.AzureApplicationInsights;
22
using ClassifiedAds.Infrastructure.Monitoring.MiniProfiler;
3+
using ClassifiedAds.Infrastructure.Monitoring.OpenTelemetry;
34

45
namespace ClassifiedAds.Infrastructure.Monitoring
56
{
@@ -8,5 +9,7 @@ public class MonitoringOptions
89
public MiniProfilerOptions MiniProfiler { get; set; }
910

1011
public AzureApplicationInsightsOptions AzureApplicationInsights { get; set; }
12+
13+
public OpenTelemetryOptions OpenTelemetry { get; set; }
1114
}
1215
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using OpenTelemetry;
3+
using OpenTelemetry.Metrics;
4+
using OpenTelemetry.Resources;
5+
using OpenTelemetry.Trace;
6+
using System;
7+
using System.Reflection;
8+
9+
namespace ClassifiedAds.Infrastructure.Monitoring.OpenTelemetry
10+
{
11+
public static class OpenTelemetryExtensions
12+
{
13+
public static IServiceCollection AddClassifiedAdsOpenTelemetry(this IServiceCollection services, OpenTelemetryOptions options = null)
14+
{
15+
if (options == null || !options.IsEnabled)
16+
{
17+
return services;
18+
}
19+
20+
var resourceBuilder = ResourceBuilder.CreateDefault().AddService(options.ServiceName);
21+
22+
services.AddOpenTelemetry()
23+
.ConfigureResource(configureResource =>
24+
{
25+
configureResource.AddService(
26+
serviceName: options.ServiceName,
27+
serviceVersion: Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "unknown",
28+
serviceInstanceId: Environment.MachineName);
29+
})
30+
.WithTracing(builder =>
31+
{
32+
builder
33+
.SetSampler(new AlwaysOnSampler())
34+
.AddAspNetCoreInstrumentation()
35+
.AddEntityFrameworkCoreInstrumentation()
36+
.AddHttpClientInstrumentation();
37+
38+
if (options?.Jaeger?.IsEnabled ?? false)
39+
{
40+
builder.AddJaegerExporter(jaegerOptions =>
41+
{
42+
jaegerOptions.AgentHost = options.Jaeger.AgentHost;
43+
jaegerOptions.AgentPort = options.Jaeger.AgentPort;
44+
});
45+
}
46+
47+
if (options?.Zipkin?.IsEnabled ?? false)
48+
{
49+
builder.AddZipkinExporter(zipkinOptions =>
50+
{
51+
zipkinOptions.Endpoint = new Uri(options.Zipkin.Endpoint);
52+
});
53+
}
54+
55+
if (options?.Otlp?.IsEnabled ?? false)
56+
{
57+
builder.AddOtlpExporter(otlpOptions =>
58+
{
59+
otlpOptions.Endpoint = new Uri(options.Otlp.Endpoint);
60+
});
61+
}
62+
})
63+
.WithMetrics(builder =>
64+
{
65+
builder
66+
.AddRuntimeInstrumentation()
67+
.AddProcessInstrumentation()
68+
.AddHttpClientInstrumentation()
69+
.AddAspNetCoreInstrumentation();
70+
71+
if (options?.Otlp?.IsEnabled ?? false)
72+
{
73+
builder.AddOtlpExporter(otlpOptions =>
74+
{
75+
otlpOptions.Endpoint = new Uri(options.Otlp.Endpoint);
76+
});
77+
}
78+
})
79+
.StartWithHost();
80+
81+
return services;
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)