Skip to content

Commit

Permalink
continued working on metrics etc
Browse files Browse the repository at this point in the history
  • Loading branch information
macel94 committed Jan 26, 2024
1 parent bbf8812 commit 35d17d2
Show file tree
Hide file tree
Showing 12 changed files with 347 additions and 241 deletions.
18 changes: 18 additions & 0 deletions src/BlazorPong.Backend.Defaults/BlazorPong.Backend.Defaults.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="OpenTelemetry.Instrumentation.GrpcNetClient" Version="1.6.0-beta.3" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.7.0" />
</ItemGroup>

</Project>
12 changes: 12 additions & 0 deletions src/BlazorPong.Backend.Defaults/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"profiles": {
"BlazorPong.Backend.Defaults": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:54052;http://localhost:54053"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

namespace BlazorPong.Web.Server;
namespace BlazorPong.Backend.Defaults;

public static class WebApplicationBuilderExtensions
public static class StartupExtensions
{
public static IHostApplicationBuilder AddServiceDefaults(this IHostApplicationBuilder builder)
{
Expand All @@ -22,18 +26,23 @@ public static IHostApplicationBuilder ConfigureOpenTelemetry(this IHostApplicati
{
builder.Logging.AddOpenTelemetry(logging =>
{
logging.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(builder.Environment.ApplicationName));
logging.IncludeFormattedMessage = true;
logging.IncludeScopes = true;
logging.AddConsoleExporter();
});

builder.Services.AddOpenTelemetry()
.WithMetrics(metrics =>
{
metrics.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(builder.Environment.ApplicationName));
metrics.AddRuntimeInstrumentation()
.AddBuiltInMeters();
metrics.AddConsoleExporter();
})
.WithTracing(tracing =>
{
tracing.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(builder.Environment.ApplicationName));
if (builder.Environment.IsDevelopment())
{
// We want to view all traces in development
Expand All @@ -43,6 +52,8 @@ public static IHostApplicationBuilder ConfigureOpenTelemetry(this IHostApplicati
tracing.AddAspNetCoreInstrumentation()
.AddGrpcClientInstrumentation()
.AddHttpClientInstrumentation();

tracing.AddConsoleExporter();
});

builder.AddOpenTelemetryExporters();
Expand Down
1 change: 1 addition & 0 deletions src/BlazorPong.SignalR/BlazorPong.SignalR.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\BlazorPong.Backend.Defaults\BlazorPong.Backend.Defaults.csproj" />
<ProjectReference Include="..\BlazorPong.Web\Shared\BlazorPong.Web.Shared.csproj" />
</ItemGroup>
</Project>
5 changes: 3 additions & 2 deletions src/BlazorPong.SignalR/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using BlazorPong.SignalR;
using BlazorPong.Backend.Defaults;
using BlazorPong.SignalR;
using BlazorPong.SignalR.Hubs;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddLogging();
//builder.Services.AddLogging();
builder.AddRedis();
builder.AddAzureSql();
builder.AddHostedServices();
Expand Down
95 changes: 1 addition & 94 deletions src/BlazorPong.SignalR/WebApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;
using BlazorPong.SignalR.Cache;
using BlazorPong.SignalR.Cache;
using BlazorPong.SignalR.EFCore;
using BlazorPong.SignalR.Rooms;
using BlazorPong.SignalR.Rooms.Games;
Expand Down Expand Up @@ -57,92 +52,4 @@ public static void AddHostedServices(this WebApplicationBuilder builder)
builder.Services.AddHostedService<RoomService>();
builder.Services.AddHostedService<GamesService>();
}

public static IHostApplicationBuilder AddServiceDefaults(this IHostApplicationBuilder builder)
{
builder.ConfigureOpenTelemetry();

builder.AddDefaultHealthChecks();

return builder;
}

public static IHostApplicationBuilder ConfigureOpenTelemetry(this IHostApplicationBuilder builder)
{
builder.Logging.AddOpenTelemetry(logging =>
{
logging.IncludeFormattedMessage = true;
logging.IncludeScopes = true;
//logging.AddConsoleExporter();
});

builder.Services.AddOpenTelemetry()
.WithMetrics(metrics =>
{
metrics.AddRuntimeInstrumentation()
.AddBuiltInMeters();
//metrics.AddConsoleExporter();
})
.WithTracing(tracing =>
{
if (builder.Environment.IsDevelopment())
{
// We want to view all traces in development
tracing.SetSampler(new AlwaysOnSampler());
}

tracing.AddAspNetCoreInstrumentation()
.AddGrpcClientInstrumentation()
.AddHttpClientInstrumentation();

//tracing.AddConsoleExporter();
});

builder.AddOpenTelemetryExporters();

return builder;
}

private static IHostApplicationBuilder AddOpenTelemetryExporters(this IHostApplicationBuilder builder)
{
var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);

if (useOtlpExporter)
{
builder.Services.Configure<OpenTelemetryLoggerOptions>(logging => logging.AddOtlpExporter());
builder.Services.ConfigureOpenTelemetryMeterProvider(metrics => metrics.AddOtlpExporter());
builder.Services.ConfigureOpenTelemetryTracerProvider(tracing => tracing.AddOtlpExporter());
}

return builder;
}

public static IHostApplicationBuilder AddDefaultHealthChecks(this IHostApplicationBuilder builder)
{
builder.Services.AddHealthChecks()
// Add a default liveness check to ensure app is responsive
.AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]);

return builder;
}

public static WebApplication MapDefaultEndpoints(this WebApplication app)
{
// All health checks must pass for app to be considered ready to accept traffic after starting
app.MapHealthChecks("/health");

// Only health checks tagged with the "live" tag must pass for app to be considered alive
app.MapHealthChecks("/alive", new HealthCheckOptions
{
Predicate = r => r.Tags.Contains("live")
});

return app;
}

private static MeterProviderBuilder AddBuiltInMeters(this MeterProviderBuilder meterProviderBuilder) =>
meterProviderBuilder.AddMeter(
"Microsoft.AspNetCore.Hosting",
"Microsoft.AspNetCore.Server.Kestrel",
"System.Net.Http");
}
1 change: 1 addition & 0 deletions src/BlazorPong.Web/Server/BlazorPong.Web.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\BlazorPong.Backend.Defaults\BlazorPong.Backend.Defaults.csproj" />
<ProjectReference Include="..\Client\BlazorPong.Web.Client.csproj" />
<ProjectReference Include="..\Shared\BlazorPong.Web.Shared.csproj" />
</ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/BlazorPong.Web/Server/Program.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using BlazorPong.Web.Server;
using BlazorPong.Backend.Defaults;
using BlazorPong.Web.Shared;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllersWithViews();
builder.Services.AddLogging();
//builder.Services.AddLogging();
builder.Services.AddRazorPages();
builder.AddServiceDefaults();

Expand Down
8 changes: 8 additions & 0 deletions src/BlazorPong.sln
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorPong.Web.Components",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorPong.SignalR", "BlazorPong.SignalR\BlazorPong.SignalR.csproj", "{7F5AC8D2-4CDC-4F14-9F31-267A0F08EBB2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorPong.Backend.Defaults", "BlazorPong.Backend.Defaults\BlazorPong.Backend.Defaults.csproj", "{C2F7FB59-356C-476A-800A-B69DB26D1208}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -74,6 +76,12 @@ Global
{7F5AC8D2-4CDC-4F14-9F31-267A0F08EBB2}.Release|Any CPU.Build.0 = Release|Any CPU
{7F5AC8D2-4CDC-4F14-9F31-267A0F08EBB2}.ReleaseAOT|Any CPU.ActiveCfg = Release|Any CPU
{7F5AC8D2-4CDC-4F14-9F31-267A0F08EBB2}.ReleaseAOT|Any CPU.Build.0 = Release|Any CPU
{C2F7FB59-356C-476A-800A-B69DB26D1208}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C2F7FB59-356C-476A-800A-B69DB26D1208}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C2F7FB59-356C-476A-800A-B69DB26D1208}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C2F7FB59-356C-476A-800A-B69DB26D1208}.Release|Any CPU.Build.0 = Release|Any CPU
{C2F7FB59-356C-476A-800A-B69DB26D1208}.ReleaseAOT|Any CPU.ActiveCfg = Release|Any CPU
{C2F7FB59-356C-476A-800A-B69DB26D1208}.ReleaseAOT|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
5 changes: 3 additions & 2 deletions src/Observability/otel-collector-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ exporters:
logging:
loglevel: info
otlp:
endpoint: otlp:4317
endpoint: tempo:4317
tls:
insecure: true
loki:
endpoint: "http://loki:3100/loki/api/v1/push"
tls:
insecure: true
prometheus:
endpoint: "collector:8889"
send_timestamps: true
endpoint: collector:8889

service:
pipelines:
Expand Down
Loading

0 comments on commit 35d17d2

Please sign in to comment.