Skip to content

Commit

Permalink
86: Added open telemetry metrics with prometheus exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
jarmatys committed May 25, 2024
1 parent b997c26 commit ea9ea63
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 15 deletions.
3 changes: 2 additions & 1 deletion API/ASSISTENTE.API/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

application
.MapHubs()
.MapHealthChecks();
.MapHealthChecks()
.UseOpenTelemetry();

await application.RunAsync();
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="MassTransit" Version="8.2.1" />
<PackageReference Include="Npgsql.OpenTelemetry" Version="8.0.3" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Exporter.Prometheus.AspNetCore" Version="1.9.0-alpha.1" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.EntityFrameworkCore" Version="1.0.0-beta.11" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.8.1" />
</ItemGroup>

</Project>
42 changes: 32 additions & 10 deletions API/ASSISTENTE.Common.OpenTelemetry/DependencyInjection.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System.Reflection;
using ASSISTENTE.Common.Settings.Sections;
using MassTransit.Logging;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Npgsql;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

Expand All @@ -13,26 +13,48 @@ public static class DependencyInjection
{
private static string ApplicationName() => Assembly.GetEntryAssembly()?.GetName().Name ?? "Unknown";

public static WebApplicationBuilder AddOpenTelemetry(this WebApplicationBuilder builder, OpenTelemetrySection openTelemetry)
public static WebApplicationBuilder AddOpenTelemetry(
this WebApplicationBuilder builder,
OpenTelemetrySection openTelemetry)
{
builder.Services
.AddOpenTelemetry()
.ConfigureResource(resource => resource.AddService(ApplicationName()))
.WithMetrics(metrics =>
{
metrics
.AddRuntimeInstrumentation()
.AddHttpClientInstrumentation()
.AddAspNetCoreInstrumentation()
.AddPrometheusExporter();

// TODO: here we can add our custom metrics by using AddMeter() method
})
.WithTracing(tracing =>
{
tracing
.AddSource(DiagnosticHeaders.DefaultListenerName)
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddSource(DiagnosticConfig.MassTransitSource)
.AddAspNetCoreInstrumentation(configuration =>
{
// TODO: Verify if we need to add some configuration here
})
.AddHttpClientInstrumentation(configuration =>
{
// TODO: Verify if we need to add some configuration here
})
.AddEntityFrameworkCoreInstrumentation()
.AddNpgsql();

tracing.AddOtlpExporter(otlpOptions =>
{
otlpOptions.Endpoint = new Uri(openTelemetry.Url);
});

tracing.AddOtlpExporter(otlpOptions => { otlpOptions.Endpoint = new Uri(openTelemetry.Url); });
});

return builder;
}

public static WebApplication UseOpenTelemetry(this WebApplication app)
{
app.UseOpenTelemetryPrometheusScrapingEndpoint();

return app;
}
}
6 changes: 6 additions & 0 deletions API/ASSISTENTE.Common.OpenTelemetry/DiagnosticConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace ASSISTENTE.Common.OpenTelemetry;

internal static class DiagnosticConfig
{
public const string MassTransitSource = "MassTransit"; // MassTransit.Logging -> DiagnosticHeaders.DefaultListenerName;
}
3 changes: 2 additions & 1 deletion API/ASSISTENTE.Worker.Sync/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
.AddOpenTelemetry(settings.OpenTelemetry);

var application = builder.Build()
.MapHealthChecks();
.MapHealthChecks()
.UseOpenTelemetry();

await application.RunAsync();
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,9 @@ Prerequisites: `Docker`
| Seq | 1006 | UI | - |
| Seq | 1007 | Logs API | - |
| Blazor UI | 1008 | Assistente UI | - |
| Internal API | 1009 | Assistente API | - |
| Worker | 1010 | Assistente worker | - |
| Internal API | 1009 | Assistente API | `/metrics` |
| Worker | 1010 | Assistente worker | `/metrics` |
| Jaeger | 1011 | OpenTelemetry Collector | - |
| Jaeger | 1012 | UI | - |
| Jaeger | 1014 | API | `/metrics` |

1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ services:
ports:
- 1011:4317
- 1012:16686
- 1014:14269
environment:
- JAEGER_DISABLED=true

Expand Down

0 comments on commit ea9ea63

Please sign in to comment.