-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add telemetry with Application Insights and OpenTelemetry (#247)
### Summary & Motivation Configure Application Insights to track requests, exceptions, dependencies, traces, and custom events. To align with Application Insights' shift towards OpenTelemetry, the OpenTelemetry SDK is utilized for collecting all supported telemetry types (requests, exceptions, dependencies, and traces). However, for custom events, the traditional Microsoft.ApplicationInsights SDK is employed. Custom events, termed "analytics events" to distinguish them from DDD Domain and Integration Events, are crucial for tracking specific feature usages for business insights. These events are not to be tracked until after an operation is successfully completed. For instance, `TenantCreated` and `UserCreated` events should only be logged in Application Insights once these entities are securely saved to the Database. To coordinate this, a new `AnalyticsEventCollector` class is introduced to aggregate all events. Additionally, a MediatR pipeline is implemented to send these events to Application Insights *after* the UnitOfWork completes. If the UnitOfWork fails, no events are tracked. While there's a theoretical possibility of tracking failure, this risk is considered acceptable. All CQRS commands have been extended to track analytics events. Events are named in past tense like `TenantCreated`, `TenantUpdated`, `TenantDeleted`, `UserCreated`, `UserUpdated`, and `UserDeleted`. For testing, an `AnalyticEventsCollectorSpy` test double class has been created, and assertions have been added to all tests to ensure that the correct events are tracked. For the few Application layer tests, the assertions are also verifying that the correct event properties are tracked (e.g., `Tenant_Id` and `Event_TenantState`). Change Version generator to yyyy.m.d.HMM format using hour and minutes instead of GitHub run number. This fixes a problem with the application workflow failing when day and month was below 10 and had invalid 0 prefix. E.g `2024.4.5.830` instead of `2024.04.05.0830`. Change the Version generator to use the `yyyy.m.d.HMM` format (e.g. `2023.12.5.932`), incorporating hours and minutes instead of the GitHub run number. This change addresses an issue where the application workflow would fail due to an invalid prefix in dates with days and months below 10. ### Checklist - [x] I have added a Label to the pull-request - [x] I have added tests, and done manual regression tests - [x] I have updated the documentation, if necessary
Showing
22 changed files
with
277 additions
and
34 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
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
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
9 changes: 8 additions & 1 deletion
9
application/account-management/Application/Users/CreateUser.cs
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
8 changes: 7 additions & 1 deletion
8
application/account-management/Application/Users/DeleteUser.cs
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 |
---|---|---|
@@ -1,18 +1,24 @@ | ||
using PlatformPlatform.SharedKernel.ApplicationCore.Cqrs; | ||
using PlatformPlatform.SharedKernel.ApplicationCore.Tracking; | ||
|
||
namespace PlatformPlatform.AccountManagement.Application.Users; | ||
|
||
public sealed record DeleteUserCommand(UserId Id) : ICommand, IRequest<Result>; | ||
|
||
[UsedImplicitly] | ||
public sealed class DeleteUserHandler(IUserRepository userRepository) : IRequestHandler<DeleteUserCommand, Result> | ||
public sealed class DeleteUserHandler( | ||
IUserRepository userRepository, | ||
IAnalyticEventsCollector analyticEventsCollector | ||
) : IRequestHandler<DeleteUserCommand, Result> | ||
{ | ||
public async Task<Result> Handle(DeleteUserCommand command, CancellationToken cancellationToken) | ||
{ | ||
var user = await userRepository.GetByIdAsync(command.Id, cancellationToken); | ||
if (user is null) return Result.NotFound($"User with id '{command.Id}' not found."); | ||
|
||
userRepository.Remove(user); | ||
|
||
analyticEventsCollector.CollectEvent("UserDeleted"); | ||
return Result.Success(); | ||
} | ||
} |
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
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
Oops, something went wrong.