Created
April 6, 2022 20:46
-
-
Save mivano/8daf58df15cf3cbbab2477e34d405865 to your computer and use it in GitHub Desktop.
Telemetry wrappers
This file contains hidden or 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
public sealed class EmptyTelemetryWrapper : ITelemetryWrapper | |
{ | |
/// <inheritdoc /> | |
public void Dispose() | |
{ | |
} | |
/// <inheritdoc /> | |
public void SetType(string type) | |
{ | |
} | |
/// <inheritdoc /> | |
public void SetSuccess(bool status) | |
{ | |
} | |
/// <inheritdoc /> | |
public void SetData(string data) | |
{ | |
} | |
/// <inheritdoc /> | |
public string GetOperationId() | |
{ | |
return null; | |
} | |
/// <inheritdoc /> | |
public string GetTelemetryId() | |
{ | |
return null; | |
} | |
} |
This file contains hidden or 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
public interface ITelemetryWrapper : IDisposable | |
{ | |
/// <summary> | |
/// Set Telemetry type | |
/// </summary> | |
/// <param name="type"></param> | |
void SetType(string type); | |
/// <summary> | |
/// Set Telemetry success | |
/// </summary> | |
/// <param name="success"></param> | |
void SetSuccess(bool success); | |
/// <summary> | |
/// Set Telemetry data | |
/// </summary> | |
/// <param name="data"></param> | |
void SetData(string data); | |
/// <summary> | |
/// Get Telemetry operation id. | |
/// </summary> | |
/// <returns></returns> | |
string GetOperationId(); | |
/// <summary> | |
/// Get Telemetry id. | |
/// </summary> | |
/// <returns></returns> | |
string GetTelemetryId(); | |
} |
This file contains hidden or 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
public static class TelemetryFactory | |
{ | |
#pragma warning disable CA1801 // Review unused parameters //bug in CA | |
/// <summary> | |
/// Creates a <see cref="ITelemetryWrapper"/> that uses <see cref="TelemetryClient"/> | |
/// or an empty dummy if <paramref name="client"/> is null. | |
/// </summary> | |
/// <typeparam name="TTelemetry"></typeparam> | |
/// <returns></returns> | |
public static ITelemetryWrapper WrapTelemetry<TTelemetry>(this TelemetryClient client, TTelemetry telemetry) | |
where TTelemetry : OperationTelemetry, new() | |
{ | |
if (client == null) | |
return new EmptyTelemetryWrapper(); | |
return new TelemetryWrapper<TTelemetry>(client, telemetry); | |
} | |
#pragma warning restore CA1801 // Review unused parameters | |
} |
This file contains hidden or 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
public sealed class TelemetryWrapper<TOperationTelemetry> : ITelemetryWrapper | |
where TOperationTelemetry : OperationTelemetry, new() | |
{ | |
private readonly TelemetryClient _telemetryClient; | |
private readonly TOperationTelemetry _telemetry; | |
private readonly IOperationHolder<TOperationTelemetry> _operationHolder; | |
/// <inheritdoc /> | |
public void SetType(string type) | |
{ | |
if (_telemetry is DependencyTelemetry dt) | |
{ | |
dt.Type = type; | |
} | |
} | |
/// <inheritdoc /> | |
public void SetSuccess(bool success) | |
{ | |
if (_telemetry != null) _telemetry.Success = success; | |
} | |
/// <inheritdoc /> | |
public void SetData(string data) | |
{ | |
if (_telemetry is DependencyTelemetry dt) | |
{ | |
dt.Data = data; | |
} | |
} | |
/// <inheritdoc /> | |
public string GetOperationId() | |
{ | |
return _telemetry?.Context?.Operation?.Id; | |
} | |
/// <inheritdoc /> | |
public string GetTelemetryId() | |
{ | |
return _telemetry?.Id; | |
} | |
/// <summary> | |
/// Creates a new instance. | |
/// </summary> | |
/// <param name="telemetryClient"></param> | |
/// <param name="telemetry"></param> | |
public TelemetryWrapper(TelemetryClient telemetryClient, TOperationTelemetry telemetry) | |
{ | |
_telemetry = telemetry; | |
_telemetryClient = telemetryClient; | |
_operationHolder = telemetryClient.StartOperation(telemetry); | |
} | |
/// <inheritdoc /> | |
public void Dispose() | |
{ | |
_telemetry?.Stop(); | |
_telemetryClient?.Track(_telemetry); | |
_telemetryClient?.StopOperation(_operationHolder); | |
_operationHolder?.Dispose(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment