Skip to content

Commit

Permalink
Add open telemetry (#137)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Enkidu93 authored Sep 22, 2023
1 parent 321019c commit 1055c05
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/SIL.DataAccess/IServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Action<IMongoDataAccessConfigurator> configure
services.Configure<MongoDataAccessOptions>(options => options.Url = new MongoUrl(connectionString));
services.AddTransient<SIL.DataAccess.IIdGenerator, ObjectIdGenerator>();
var clientSettings = MongoClientSettings.FromConnectionString(connectionString);
clientSettings.ClusterConfigurator = cb => cb.Subscribe(new DiagnosticsActivityEventSubscriber());
clientSettings.LinqProvider = LinqProvider.V2;
services.AddSingleton<IMongoClient>(sp => new MongoClient(clientSettings));
services.AddSingleton(
Expand Down
1 change: 1 addition & 0 deletions src/SIL.DataAccess/SIL.DataAccess.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="6.0.0" />
<PackageReference Include="MongoDB.Driver" Version="2.20.0" />
<PackageReference Include="MongoDB.Driver.Core.Extensions.DiagnosticSources" Version="1.3.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="Nito.AsyncEx" Version="5.1.2" />
<PackageReference Include="SIL.Core" Version="12.0.1" />
Expand Down
1 change: 1 addition & 0 deletions src/SIL.DataAccess/Usings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/Serval.ApiServer/Serval.ApiServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.6.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.6.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.5.1-beta.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.GrpcNetClient" Version="1.5.1-beta.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.5.1-beta.1" />
</ItemGroup>

<ItemGroup>
Expand Down
17 changes: 16 additions & 1 deletion src/Serval.ApiServer/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions src/Serval.ApiServer/Usings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 1055c05

Please sign in to comment.