-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add open telemetry and refactor setup
- Loading branch information
Showing
5 changed files
with
260 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace Kemkas.Web.Config; | ||
|
||
public class ExternalAuthOptions | ||
{ | ||
public string? ClientId { get; set; } | ||
|
||
public string? ClientSecret { get; set; } | ||
|
||
public bool IsValid() => ClientId != null && ClientSecret != null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using OpenTelemetry.Metrics; | ||
using OpenTelemetry.Resources; | ||
using OpenTelemetry.Trace; | ||
|
||
namespace Kemkas.Web.Config; | ||
|
||
public static class WebApplicationBuilderExtensions | ||
{ | ||
public static void AddAuth(this WebApplicationBuilder builder) | ||
{ | ||
var externalAuthConfigSection = builder.Configuration.GetSection("Authentication"); | ||
var authenticationBuilder = builder.Services.AddAuthentication(); | ||
|
||
var googleAuthOptions = new ExternalAuthOptions(); | ||
externalAuthConfigSection.GetSection("Google").Bind(googleAuthOptions); | ||
if (googleAuthOptions.IsValid()) | ||
{ | ||
authenticationBuilder.AddGoogle(options => | ||
{ | ||
options.ClientId = googleAuthOptions.ClientId!; | ||
options.ClientSecret = googleAuthOptions.ClientSecret!; | ||
}); | ||
} | ||
|
||
var facebookAuthOptions = new ExternalAuthOptions(); | ||
externalAuthConfigSection.GetSection("Facebook").Bind(facebookAuthOptions); | ||
if (facebookAuthOptions.IsValid()) | ||
{ | ||
authenticationBuilder.AddFacebook(options => | ||
{ | ||
options.ClientId = facebookAuthOptions.ClientId!; | ||
options.ClientSecret = facebookAuthOptions.ClientSecret!; | ||
}); | ||
} | ||
|
||
var discordAuthOptions = new ExternalAuthOptions(); | ||
externalAuthConfigSection.GetSection("Discord").Bind(discordAuthOptions); | ||
if (discordAuthOptions.IsValid()) | ||
{ | ||
authenticationBuilder.AddDiscord(options => | ||
{ | ||
options.ClientId = discordAuthOptions.ClientId!; | ||
options.ClientSecret = discordAuthOptions.ClientSecret!; | ||
}); | ||
} | ||
} | ||
|
||
public static void AddOpenTelemetry(this WebApplicationBuilder builder) | ||
{ | ||
var otlResourceBuilder = ResourceBuilder.CreateDefault().AddService("kemkas-api"); | ||
var openTelemetryEndpoint = builder.Configuration.GetSection("monitoring")["url"]; | ||
|
||
builder.Services.AddOpenTelemetry().WithTracing(tracerProviderBuilder => | ||
{ | ||
tracerProviderBuilder | ||
.SetResourceBuilder(otlResourceBuilder) | ||
.AddAspNetCoreInstrumentation() | ||
.AddConsoleExporter(); | ||
if (openTelemetryEndpoint != null) | ||
{ | ||
tracerProviderBuilder.AddOtlpExporter(opt => opt.Endpoint = new Uri(openTelemetryEndpoint)); | ||
} | ||
}) | ||
.WithMetrics(meterProviderBuilder => | ||
{ | ||
meterProviderBuilder | ||
.SetResourceBuilder(otlResourceBuilder) | ||
.AddAspNetCoreInstrumentation() | ||
.AddConsoleExporter(); | ||
if (openTelemetryEndpoint != null) | ||
{ | ||
meterProviderBuilder.AddOtlpExporter(opt => opt.Endpoint = new Uri(openTelemetryEndpoint)); | ||
} | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters