Skip to content

Commit

Permalink
Adds telemetry pipeline (#259)
Browse files Browse the repository at this point in the history
* Adds telemetry pipeline. Adds evaluationevent, publisher interface, and a publisher instance for app insights.

* Use await insead of .result

* Reverting unused dependency removal

* Update src/Microsoft.FeatureManagement/FeatureDefinition.cs

Co-authored-by: Jimmy Campbell <jimmyca@microsoft.com>

* Update src/Microsoft.FeatureManagement/FeatureDefinition.cs

Co-authored-by: Jimmy Campbell <jimmyca@microsoft.com>

* Update src/Microsoft.FeatureManagement.AppInsightsTelemetryPublisher/TelemetryPublisherAppInsights.cs

Co-authored-by: Jimmy Campbell <jimmyca@microsoft.com>

* Removed factory pattern for publisher DI

* Update src/Microsoft.FeatureManagement/FeatureManager.cs

Co-authored-by: Jimmy Campbell <jimmyca@microsoft.com>

* Resolve misc. comments

* Adjusts descriptions for FeatureDefinition properties

* Rename AppInsights to ApplicationInsights

* Resolves project reference in sln

* Adjusts ServiceCollectionExtensions to use TryAddSingleton

* Removes langversion from .csproj

* Temp

* Adds explicit null checkts before publishing fields to app insights

* Addressing misc. comments

* Removing project reference for example project

* Syncing telemetry and variants

* Adds null check for feature definitions

* Adjusted TelemetryPublishers to no longer be inserted into DI

* Resolving comments

* Removing Application Insights project

* Converts some variables to inline

* Resolving misc. comments

* Moves AddTelemetryPublisher to be an extension method

* Removing IVariantFeatureManager GetFeatureNamesAsync definition, as it should it it's own PR

* Remove unused dependency

* Resolving comments

* Remove invisible character changes

* Remove Dependency Injection package

* Update src/Microsoft.FeatureManagement/FeatureManagerSnapshot.cs

* Update src/Microsoft.FeatureManagement/IVariantFeatureManager.cs

* Persists cancellation token to PublishEvent and adds null check for null publisher collection

* Adds TargetingContext to the EvaluationResult

* Resolving comments

* Removes TargetingContext from EvaluationEvent for now

* Moves tags, etag, and label under 'TelemetryMetadata'

* Adjusts telemetry metadata to already be a flattened dictionary

* Removes bind in favor of ToDictionary. Removes unused using

---------

Co-authored-by: Jimmy Campbell <jimmyca@microsoft.com>
  • Loading branch information
rossgrambo and jimmyca15 committed Oct 10, 2023
1 parent 1828686 commit e7184f2
Show file tree
Hide file tree
Showing 12 changed files with 483 additions and 182 deletions.
2 changes: 1 addition & 1 deletion Microsoft.FeatureManagement.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp", "examples\Cons
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TargetingConsoleApp", "examples\TargetingConsoleApp\TargetingConsoleApp.csproj", "{6558C21E-CF20-4278-AA08-EB9D1DF29D66}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RazorPages", "examples\RazorPages\RazorPages.csproj", "{BA29A1BB-81D5-4EB1-AF37-6ECF64AF27E2}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RazorPages", "examples\RazorPages\RazorPages.csproj", "{BA29A1BB-81D5-4EB1-AF37-6ECF64AF27E2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests.FeatureManagement.AspNetCore", "tests\Tests.FeatureManagement.AspNetCore\Tests.FeatureManagement.AspNetCore.csproj", "{FC0DC3E2-5646-4AEC-A7DB-2D6167BC3BB4}"
EndProject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ We support

var enabledFor = new List<FeatureFilterConfiguration>();

bool telemetryEnabled = false;

Dictionary<string, string> telemetryMetadata = null;

string val = configurationSection.Value; // configuration[$"{featureName}"];

if (string.IsNullOrEmpty(val))
Expand Down Expand Up @@ -283,6 +287,17 @@ We support
variants.Add(variant);
}
}

telemetryEnabled = configurationSection.GetValue<bool>("TelemetryEnabled");

IConfigurationSection telemetryMetadataSection = configurationSection.GetSection("TelemetryMetadata");

if (telemetryMetadataSection.Exists())
{
telemetryMetadata = new Dictionary<string, string>();

telemetryMetadata = telemetryMetadataSection.GetChildren().ToDictionary(x => x.Key, x => x.Value);
}
}

return new FeatureDefinition()
Expand All @@ -292,7 +307,9 @@ We support
RequirementType = requirementType,
Status = featureStatus,
Allocation = allocation,
Variants = variants
Variants = variants,
TelemetryEnabled = telemetryEnabled,
TelemetryMetadata = telemetryMetadata
};
}

Expand Down
10 changes: 10 additions & 0 deletions src/Microsoft.FeatureManagement/FeatureDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,15 @@ public class FeatureDefinition
/// A list of variant definitions that specify a configuration to return when assigned.
/// </summary>
public IEnumerable<VariantDefinition> Variants { get; set; } = Enumerable.Empty<VariantDefinition>();

/// <summary>
/// A flag to enable or disable sending telemetry events to the registered <see cref="ITelemetryPublisher">.
/// </summary>
public bool TelemetryEnabled { get; set; }

/// <summary>
/// A container for metadata relevant to telemetry.
/// </summary>
public IReadOnlyDictionary<string, string> TelemetryMetadata { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.FeatureManagement.Telemetry;
using System;
using System.Collections.Generic;

namespace Microsoft.FeatureManagement
{
/// <summary>
/// Extensions used to add feature management functionality.
/// </summary>
public static class FeatureManagementBuilderExtensions
{
/// <summary>
/// Adds a telemetry publisher to the feature management system.
/// </summary>
/// <param name="builder">The <see cref="IFeatureManagementBuilder"/> used to customize feature management functionality.</param>
/// <returns>A <see cref="IFeatureManagementBuilder"/> that can be used to customize feature management functionality.</returns>
public static IFeatureManagementBuilder AddTelemetryPublisher<T>(this IFeatureManagementBuilder builder) where T : ITelemetryPublisher
{
builder.AddTelemetryPublisher(sp => ActivatorUtilities.CreateInstance(sp, typeof(T)) as ITelemetryPublisher);

return builder;
}

private static IFeatureManagementBuilder AddTelemetryPublisher(this IFeatureManagementBuilder builder, Func<IServiceProvider, ITelemetryPublisher> factory)
{
builder.Services.Configure<FeatureManagementOptions>(options =>
{
if (options.TelemetryPublisherFactories == null)
{
options.TelemetryPublisherFactories = new List<Func<IServiceProvider, ITelemetryPublisher>>();
}
options.TelemetryPublisherFactories.Add(factory);
});

return builder;
}
}
}
10 changes: 10 additions & 0 deletions src/Microsoft.FeatureManagement/FeatureManagementOptions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
//
using Microsoft.FeatureManagement.Telemetry;
using System;
using System.Collections.Generic;

namespace Microsoft.FeatureManagement
{
/// <summary>
Expand All @@ -22,5 +26,11 @@ public class FeatureManagementOptions
/// The default value is true.
/// </summary>
public bool IgnoreMissingFeatures { get; set; } = true;

/// <summary>
/// Holds a collection of factories that can be used to create <see cref="ITelemetryPublisher"/> instances.
/// This avoids the need to add the publishers to the service collection.
/// </summary>
internal ICollection<Func<IServiceProvider, ITelemetryPublisher>> TelemetryPublisherFactories { get; set; }
}
}
Loading

0 comments on commit e7184f2

Please sign in to comment.