-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added dependency injection for postgres
- Loading branch information
Henning Normann
committed
Oct 24, 2023
1 parent
883c377
commit dc5318e
Showing
5 changed files
with
94 additions
and
6 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
56 changes: 56 additions & 0 deletions
56
src/Altinn.Notifications.Persistence/Repository/TelemetryTracker.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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System.Diagnostics; | ||
using Microsoft.ApplicationInsights; | ||
using Npgsql; | ||
|
||
namespace Altinn.Notifications.Persistence.Repository | ||
{ | ||
/// <summary> | ||
/// Helper to track application insights dependencies for PostgreSQL invocations | ||
/// </summary> | ||
public class TelemetryTracker : IDisposable | ||
Check warning on line 10 in src/Altinn.Notifications.Persistence/Repository/TelemetryTracker.cs GitHub Actions / Build, test & analyze
|
||
{ | ||
private readonly DateTime _startTime = DateTime.Now; | ||
private readonly Stopwatch _timer = Stopwatch.StartNew(); | ||
private readonly TelemetryClient _telemetryClient; | ||
private readonly NpgsqlCommand _cmd; | ||
private bool _tracked = false; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="TelemetryTracker"/> class. | ||
/// </summary> | ||
/// <param name="telemetryClient">Telemetry client from DI</param> | ||
/// <param name="cmd">The npgsql cmd</param> | ||
public TelemetryTracker(TelemetryClient telemetryClient, NpgsqlCommand cmd) | ||
{ | ||
_telemetryClient = telemetryClient; | ||
_cmd = cmd; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public void Dispose() | ||
{ | ||
if (!_tracked) | ||
{ | ||
Track(false); | ||
_tracked = true; | ||
} | ||
|
||
GC.SuppressFinalize(this); | ||
} | ||
|
||
/// <summary> | ||
/// Track the PostgreSQL invocation | ||
/// <paramref name="success">Outcome of invocation</paramref> | ||
/// </summary> | ||
public void Track(bool success = true) | ||
{ | ||
_timer.Stop(); | ||
if (_telemetryClient != null) | ||
{ | ||
_telemetryClient.TrackDependency("Postgres", _cmd.CommandText, _cmd.CommandText, _startTime, _timer.Elapsed, success); | ||
} | ||
|
||
_tracked = true; | ||
} | ||
} | ||
} |
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