Skip to content

Commit

Permalink
feat: added TelemetryInitializer and Startup where its configured
Browse files Browse the repository at this point in the history
  • Loading branch information
robinaasan committed Jan 14, 2025
1 parent 1f310fe commit bdbd70e
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
using Equinor.SubSurfAppManagementMonitoringNuGet.HealthServices.smda;

namespace Equinor.SubSurfAppManagementMonitoringNuGet.Authentication;

/// <summary>
/// Defines methods for achieving accesstokens
/// Defines the interface which must be implemented for achieving accesstokens for default HealthCheckServices such as smda: <see cref="SmdaHealthCheck"/>
/// </summary>
public interface IAccessTokenService
{
/// <summary>
/// Get access token on behalf of the app
/// </summary>
Task<string> GetAccessTokenAsync(string resourceId);

/// <summary>
/// Get access token on behalf of the user
/// Get access token on behalf of the caller
/// </summary>
Task<string> GetAccessTokenOnBehalfOfAsync(string resourceId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using System.Security.Claims;

namespace Equinor.SubSurfAppManagementMonitoringNuGet.Config;

public class AuditTelemetryInitializer : ITelemetryInitializer
{
private readonly IHttpContextAccessor _contextAccessor;
private readonly IWebHostEnvironment _env;

public AuditTelemetryInitializer(IHttpContextAccessor contextAccessor, IWebHostEnvironment env)
{
_contextAccessor = contextAccessor;
_env = env;
}

public void Initialize(ITelemetry telemetry)
{
var httpContext = _contextAccessor.HttpContext;
try
{
if (httpContext?.Connection.RemoteIpAddress != null)
{
telemetry.Context.Location.Ip = httpContext.Connection.RemoteIpAddress.ToString();
}

if (httpContext?.Request?.Headers["User-Agent"] is not null)
{
telemetry.Context.GlobalProperties["user-agent"] = httpContext.Request.Headers["User-Agent"].ToString();
}

if (httpContext?.User?.Identity?.IsAuthenticated == true &&
httpContext.User.HasClaim(c => c.Type == ClaimTypes.Name))
{
telemetry.Context.User.AuthenticatedUserId =
httpContext.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value;
;
}
}
catch
{
telemetry.Context.User.AuthenticatedUserId = "<unknown>";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<PackageReference Include="Ardalis.GuardClauses" Version="5.0.0" />
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0" />
<PackageReference Include="AspNetCore.HealthChecks" Version="1.0.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.22.0" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.0" />
<PackageReference Include="Microsoft.Identity.Client" Version="4.66.2" />
Expand Down
25 changes: 25 additions & 0 deletions Equinor.SubSurfAppManagementMonitoringNuGet/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Equinor.SubSurfAppManagementMonitoringNuGet.Config;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Equinor.SubSurfAppManagementMonitoringNuGet;

public static class Startup
{
/// <summary>
/// Configures the default services and dependencies for the nuget package
/// </summary>
/// <param name="services"></param>
/// <param name="Configuration"></param>
public static void Configure(IServiceCollection services, IConfiguration Configuration)
{
ConfigureDependencyInjection(services);
}

private static void ConfigureDependencyInjection(IServiceCollection services)
{
services.AddSingleton<ITelemetryInitializer, AuditTelemetryInitializer>();
}

}

0 comments on commit bdbd70e

Please sign in to comment.