Skip to content
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
merged 4 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
Copy link
Member

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.

}
}
}
}
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>