From 1055c057c574d026f96fce9daa88d1968d3ce543 Mon Sep 17 00:00:00 2001 From: "Eli C. Lowry" <83078660+Enkidu93@users.noreply.github.com> Date: Fri, 22 Sep 2023 15:24:44 -0400 Subject: [PATCH] Add open telemetry (#137) * Add basic tracing from OpenTelemetry * Added Mongo instrumentation * Added gRPC instrumentation * Hid console exporter behind development flag * Merge branch 'add_open_telemetry_#58' of https://github.com/sillsdev/serval into add_open_telemetry_#58 --- .../IServiceCollectionExtensions.cs | 1 + src/SIL.DataAccess/SIL.DataAccess.csproj | 1 + src/SIL.DataAccess/Usings.cs | 1 + src/Serval.ApiServer/Serval.ApiServer.csproj | 5 +++++ src/Serval.ApiServer/Startup.cs | 17 ++++++++++++++++- src/Serval.ApiServer/Usings.cs | 3 +++ 6 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/SIL.DataAccess/IServiceCollectionExtensions.cs b/src/SIL.DataAccess/IServiceCollectionExtensions.cs index d3d950df..33e5cb8f 100644 --- a/src/SIL.DataAccess/IServiceCollectionExtensions.cs +++ b/src/SIL.DataAccess/IServiceCollectionExtensions.cs @@ -32,6 +32,7 @@ Action configure services.Configure(options => options.Url = new MongoUrl(connectionString)); services.AddTransient(); var clientSettings = MongoClientSettings.FromConnectionString(connectionString); + clientSettings.ClusterConfigurator = cb => cb.Subscribe(new DiagnosticsActivityEventSubscriber()); clientSettings.LinqProvider = LinqProvider.V2; services.AddSingleton(sp => new MongoClient(clientSettings)); services.AddSingleton( diff --git a/src/SIL.DataAccess/SIL.DataAccess.csproj b/src/SIL.DataAccess/SIL.DataAccess.csproj index 3631ac89..21827fcf 100644 --- a/src/SIL.DataAccess/SIL.DataAccess.csproj +++ b/src/SIL.DataAccess/SIL.DataAccess.csproj @@ -14,6 +14,7 @@ + diff --git a/src/SIL.DataAccess/Usings.cs b/src/SIL.DataAccess/Usings.cs index fb4848bb..63eb16a9 100644 --- a/src/SIL.DataAccess/Usings.cs +++ b/src/SIL.DataAccess/Usings.cs @@ -13,6 +13,7 @@ global using MongoDB.Bson.Serialization; global using MongoDB.Bson.Serialization.Conventions; global using MongoDB.Bson.Serialization.Serializers; +global using MongoDB.Driver.Core.Extensions.DiagnosticSources; global using MongoDB.Driver; global using MongoDB.Driver.Linq; global using Nito.AsyncEx; diff --git a/src/Serval.ApiServer/Serval.ApiServer.csproj b/src/Serval.ApiServer/Serval.ApiServer.csproj index 4179ede2..ae23fe3f 100644 --- a/src/Serval.ApiServer/Serval.ApiServer.csproj +++ b/src/Serval.ApiServer/Serval.ApiServer.csproj @@ -21,6 +21,11 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + diff --git a/src/Serval.ApiServer/Startup.cs b/src/Serval.ApiServer/Startup.cs index 4218e4b5..9cdd3722 100644 --- a/src/Serval.ApiServer/Startup.cs +++ b/src/Serval.ApiServer/Startup.cs @@ -2,13 +2,16 @@ public class Startup { - public Startup(IConfiguration configuration) + public Startup(IConfiguration configuration, IWebHostEnvironment environment) { Configuration = configuration; + Environment = environment; } public IConfiguration Configuration { get; } + public IWebHostEnvironment Environment { get; } + public void ConfigureServices(IServiceCollection services) { services.AddRouting(o => o.LowercaseUrls = true); @@ -157,6 +160,18 @@ public void ConfigureServices(IServiceCollection services) }; }); } + if (Environment.IsDevelopment()) + services + .AddOpenTelemetry() + .WithTracing(builder => + { + builder + .AddAspNetCoreInstrumentation() + .AddHttpClientInstrumentation() + .AddGrpcClientInstrumentation() + .AddSource("MongoDB.Driver.Core.Extensions.DiagnosticSources") + .AddConsoleExporter(); + }); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) diff --git a/src/Serval.ApiServer/Usings.cs b/src/Serval.ApiServer/Usings.cs index a6a10d38..d64037a7 100644 --- a/src/Serval.ApiServer/Usings.cs +++ b/src/Serval.ApiServer/Usings.cs @@ -13,12 +13,15 @@ global using Microsoft.AspNetCore.Authorization; global using Microsoft.AspNetCore.Diagnostics.HealthChecks; global using Microsoft.Extensions.Diagnostics.HealthChecks; +global using Microsoft.Extensions.DependencyInjection; global using Microsoft.IdentityModel.Tokens; global using NJsonSchema; global using NJsonSchema.Generation; global using NSwag; global using NSwag.AspNetCore; global using NSwag.Generation.Processors.Security; +global using OpenTelemetry; +global using OpenTelemetry.Trace; global using Serval.Shared.Contracts; global using Serval.Shared.Controllers; global using Serval.Shared.Models;