-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Application Insights Publisher (#281)
* Adds Application Insights publisher * Change readonly to const * Update src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/ApplicationInsightsTelemetryPublisher.cs Co-authored-by: Jimmy Campbell <jimmyca@microsoft.com> * Update src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/ApplicationInsightsTelemetryPublisher.cs Co-authored-by: Jimmy Campbell <jimmyca@microsoft.com> --------- Co-authored-by: Jimmy Campbell <jimmyca@microsoft.com>
- Loading branch information
1 parent
e7184f2
commit 17d6202
Showing
2 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
....FeatureManagement.Telemetry.ApplicationInsights/ApplicationInsightsTelemetryPublisher.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,70 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
// | ||
using Microsoft.ApplicationInsights; | ||
|
||
namespace Microsoft.FeatureManagement.Telemetry.ApplicationInsights | ||
{ | ||
/// <summary> | ||
/// Used to publish data from evaluation events to Application Insights | ||
/// </summary> | ||
public class ApplicationInsightsTelemetryPublisher : ITelemetryPublisher | ||
{ | ||
private const string _eventName = "FeatureEvaluation"; | ||
private readonly TelemetryClient _telemetryClient; | ||
|
||
public ApplicationInsightsTelemetryPublisher(TelemetryClient telemetryClient) | ||
{ | ||
_telemetryClient = telemetryClient ?? throw new ArgumentNullException(nameof(telemetryClient)); | ||
} | ||
|
||
/// <summary> | ||
/// Publishes a custom event to Application Insights using data from the given evaluation event. | ||
/// </summary> | ||
/// <param name="evaluationEvent"> The event to publish.</param> | ||
/// <param name="cancellationToken"> A cancellation token.</param> | ||
/// <returns>Returns a ValueTask that represents the asynchronous operation</returns> | ||
public ValueTask PublishEvent(EvaluationEvent evaluationEvent, CancellationToken cancellationToken) | ||
{ | ||
ValidateEvent(evaluationEvent); | ||
|
||
FeatureDefinition featureDefinition = evaluationEvent.FeatureDefinition; | ||
|
||
Dictionary<string, string> properties = new Dictionary<string, string>() | ||
{ | ||
{ "FeatureName", featureDefinition.Name }, | ||
{ "IsEnabled", evaluationEvent.IsEnabled.ToString() } | ||
}; | ||
|
||
if (evaluationEvent.Variant != null) | ||
{ | ||
properties["Variant"] = evaluationEvent.Variant.Name; | ||
} | ||
|
||
if (featureDefinition.TelemetryMetadata != null) | ||
{ | ||
foreach (KeyValuePair<string, string> kvp in featureDefinition.TelemetryMetadata) | ||
{ | ||
properties[kvp.Key] = kvp.Value; | ||
} | ||
} | ||
|
||
_telemetryClient.TrackEvent(_eventName, properties); | ||
|
||
return new ValueTask(); | ||
} | ||
|
||
private void ValidateEvent(EvaluationEvent evaluationEvent) | ||
{ | ||
if (evaluationEvent == null) | ||
{ | ||
throw new ArgumentNullException(nameof(evaluationEvent)); | ||
} | ||
|
||
if (evaluationEvent.FeatureDefinition == null) | ||
{ | ||
throw new ArgumentNullException(nameof(evaluationEvent.FeatureDefinition)); | ||
} | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...etry.ApplicationInsights/Microsoft.FeatureManagement.Telemetry.ApplicationInsights.csproj
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,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.21.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Microsoft.FeatureManagement\Microsoft.FeatureManagement.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |