Skip to content

Commit

Permalink
Application Insights Publisher (#281)
Browse files Browse the repository at this point in the history
* 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
rossgrambo and jimmyca15 authored Oct 12, 2023
1 parent e7184f2 commit 17d6202
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
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));
}
}
}
}
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>

0 comments on commit 17d6202

Please sign in to comment.