Skip to content

Commit

Permalink
Not all data seems to be flowing but now this uses otel.
Browse files Browse the repository at this point in the history
  • Loading branch information
macel94 committed Jan 25, 2024
1 parent fb81867 commit bbf8812
Show file tree
Hide file tree
Showing 9 changed files with 226 additions and 36 deletions.
9 changes: 5 additions & 4 deletions src/BlazorPong.SignalR/BlazorPong.SignalR.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
<PackageReference Include="Microsoft.AspNetCore.SignalR.StackExchangeRedis" Version="8.0.1" />
<PackageReference Include="StackExchange.Redis" Version="2.7.17" />

<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Exporter.Prometheus.AspNetCore" Version="1.5.0-rc.1" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.7.0" />
<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>

<ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions src/BlazorPong.SignalR/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
.AllowAnyMethod();
});
});
builder.AddServiceDefaults();

var app = builder.Build();
app.UseCors("AllowAnyOriginPolicy");
app.MapDefaultEndpoints();
app.MapHub<GameHub>("/gamehub");

app.Run();
96 changes: 95 additions & 1 deletion src/BlazorPong.SignalR/WebApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
using BlazorPong.SignalR.Cache;
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.EFCore;
using BlazorPong.SignalR.Rooms;
using BlazorPong.SignalR.Rooms.Games;
Expand All @@ -8,6 +13,7 @@

namespace BlazorPong.SignalR;

// THIS FILE IS DUPLICATED ON PURPOSE
public static class WebApplicationBuilderExtensions
{
public static void AddRedis(this WebApplicationBuilder builder)
Expand Down Expand Up @@ -51,4 +57,92 @@ 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");
}
10 changes: 5 additions & 5 deletions src/BlazorPong.Web/Server/BlazorPong.Web.Server.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand All @@ -15,12 +15,12 @@
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="8.0.1" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.6" />

<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Exporter.Prometheus.AspNetCore" Version="1.5.0-rc.1" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.7.0" />
<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.OpenTelemetryProtocol" Version="1.7.0" />
</ItemGroup>

<ItemGroup>
Expand Down
5 changes: 4 additions & 1 deletion src/BlazorPong.Web/Server/Program.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using BlazorPong.Web.Shared;
using BlazorPong.Web.Server;
using BlazorPong.Web.Shared;

var builder = WebApplication.CreateBuilder(args);

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

var app = builder.Build();

Expand All @@ -22,6 +24,7 @@
app.UseStaticFiles();

app.UseRouting();
app.MapDefaultEndpoints();
// Map get for GetBaseClientConfig endpoint that
app.MapGet("/api/GetBaseClientConfig",
() => new BaseClientConfig(app.Configuration["GameHubEndpoint"] ?? throw new InvalidOperationException("GameHubEndpoint not found in config"))
Expand Down
95 changes: 95 additions & 0 deletions src/BlazorPong.Web/Server/WebApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

namespace BlazorPong.Web.Server;

public static class WebApplicationBuilderExtensions
{
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;
});

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

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

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");
}
7 changes: 6 additions & 1 deletion src/Observability/otel-collector-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ exporters:
logging:
loglevel: info
otlp:
endpoint: tempo:4317
endpoint: otlp:4317
tls:
insecure: true
loki:
endpoint: "http://loki:3100/loki/api/v1/push"
tls:
insecure: true
prometheus:
endpoint: "collector:8889"

service:
pipelines:
Expand All @@ -24,3 +26,6 @@ service:
logs:
receivers: [otlp]
exporters: [logging, loki]
metrics:
receivers: [otlp]
exporters: [prometheus]
23 changes: 5 additions & 18 deletions src/Observability/prometheus.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,9 @@ global:
evaluation_interval: 15s

scrape_configs:
- job_name: signalr
honor_timestamps: true
metrics_path: /metrics
scheme: http
# - job_name: tempo
# static_configs:
# - targets: [ 'tempo:3200' ]
- job_name: collector
static_configs:
- targets:
- signalr:9090

- job_name: webapp
honor_timestamps: true
metrics_path: /metrics
scheme: http
static_configs:
- targets:
- webapp:9090

- job_name: tempo
static_configs:
- targets: [ 'tempo:3200' ]
- targets: [ 'collector:8889' ]
Loading

0 comments on commit bbf8812

Please sign in to comment.