-
Notifications
You must be signed in to change notification settings - Fork 114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Application Insights Publisher #281
Merged
rossgrambo
merged 4 commits into
preview
from
rossgrambo/telemetry-app-insights-publisher
Oct 12, 2023
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
beca3ea
Adds Application Insights publisher
rossgrambo c6694f0
Change readonly to const
rossgrambo 2238877
Update src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/…
rossgrambo 3f7c067
Update src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/…
rossgrambo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is technically an argument exception, not an argumentnullexception.
The method argument is not null, but the state of the argument is invalid.
I've seen both used, pointing it out though.