add application insights telemetry settings class to configure multiple common settings
This commit is contained in:
Родитель
95a072e8d1
Коммит
7aeb05b0a4
|
@ -0,0 +1,13 @@
|
|||
namespace Microsoft.ApplicationInsights.AspNet.Config
|
||||
{
|
||||
internal static class ConfigurationConstants
|
||||
{
|
||||
public const string InstrumentationKeyFromConfig = "ApplicationInsights:InstrumentationKey";
|
||||
public const string DeveloperModeFromConfig = "ApplicationInsights:TelemetryChannel:DeveloperMode";
|
||||
public const string EndpointAddressFromConfig = "ApplicationInsights:TelemetryChannel:EndpointAddress";
|
||||
|
||||
public const string InstrumentationKeyForWebSites = "APPINSIGHTS_INSTRUMENTATIONKEY";
|
||||
public const string DeveloperModeForWebSites = "APPINSIGHTS_DEVELOPER_MODE";
|
||||
public const string EndpointAddressForWebSites = "APPINSIGHTS_ENDPOINTADDRESS";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,71 @@
|
|||
namespace Microsoft.ApplicationInsights.AspNet.Config
|
||||
{
|
||||
using Microsoft.Framework.ConfigurationModel;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
internal class TelemetryConfigurationSource : IConfigurationSource
|
||||
{
|
||||
/// <summary>
|
||||
/// Developer mode setting value.
|
||||
/// </summary>
|
||||
public bool? DeveloperMode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Instrumentation key setting value.
|
||||
/// </summary>
|
||||
public string InstrumentationKey { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Endpoint address setting value.
|
||||
/// </summary>
|
||||
public string EndpointAddress { get; set; }
|
||||
|
||||
public void Load()
|
||||
{
|
||||
}
|
||||
|
||||
public IEnumerable<string> ProduceSubKeys(IEnumerable<string> earlierKeys, string prefix, string delimiter)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public void Set(string key, string value)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public bool TryGet(string key, out string value)
|
||||
{
|
||||
switch (key)
|
||||
{
|
||||
case ConfigurationConstants.InstrumentationKeyFromConfig:
|
||||
case ConfigurationConstants.InstrumentationKeyForWebSites:
|
||||
if (this.InstrumentationKey != null)
|
||||
{
|
||||
value = this.InstrumentationKey;
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
case ConfigurationConstants.DeveloperModeFromConfig:
|
||||
case ConfigurationConstants.DeveloperModeForWebSites:
|
||||
if (this.DeveloperMode != null)
|
||||
{
|
||||
value = this.DeveloperMode.Value.ToString();
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
case ConfigurationConstants.EndpointAddressFromConfig:
|
||||
case ConfigurationConstants.EndpointAddressForWebSites:
|
||||
if (this.EndpointAddress != null)
|
||||
{
|
||||
value = this.EndpointAddress;
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
value = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -13,6 +13,7 @@
|
|||
using Microsoft.AspNet.Mvc.Rendering;
|
||||
using Microsoft.Framework.ConfigurationModel;
|
||||
using Microsoft.ApplicationInsights.AspNet.JavaScript;
|
||||
using Microsoft.ApplicationInsights.AspNet.Config;
|
||||
|
||||
public static class ApplicationInsightsExtensions
|
||||
{
|
||||
|
@ -26,13 +27,6 @@
|
|||
return app.UseMiddleware<ExceptionTrackingMiddleware>();
|
||||
}
|
||||
|
||||
public static IApplicationBuilder SetApplicationInsightsTelemetryDeveloperMode(this IApplicationBuilder app)
|
||||
{
|
||||
var telemetryConfiguration = app.ApplicationServices.GetRequiredService<TelemetryConfiguration>();
|
||||
telemetryConfiguration.TelemetryChannel.DeveloperMode = true;
|
||||
return app;
|
||||
}
|
||||
|
||||
public static void AddApplicationInsightsTelemetry(this IServiceCollection services, IConfiguration config)
|
||||
{
|
||||
services.AddSingleton<IContextInitializer, DomainNameRoleInstanceContextInitializer>();
|
||||
|
@ -47,8 +41,8 @@
|
|||
services.AddSingleton<TelemetryConfiguration>(serviceProvider =>
|
||||
{
|
||||
var telemetryConfiguration = TelemetryConfiguration.CreateDefault();
|
||||
AddInstrumentationKey(config, telemetryConfiguration);
|
||||
telemetryConfiguration.TelemetryChannel = serviceProvider.GetService<ITelemetryChannel>() ?? telemetryConfiguration.TelemetryChannel;
|
||||
AddTelemetryConfiguration(config, telemetryConfiguration);
|
||||
AddServicesToCollection(serviceProvider, telemetryConfiguration.ContextInitializers);
|
||||
AddServicesToCollection(serviceProvider, telemetryConfiguration.TelemetryInitializers);
|
||||
return telemetryConfiguration;
|
||||
|
@ -65,19 +59,73 @@
|
|||
});
|
||||
}
|
||||
|
||||
private static void AddInstrumentationKey(IConfiguration config, TelemetryConfiguration telemetryConfiguration)
|
||||
public static IConfigurationSourceRoot AddApplicaitonInsightsSettings(this IConfigurationSourceRoot configurationSourceRoot, bool? developerMode = null, string endpointAddress = null, string instrumentationKey = null)
|
||||
{
|
||||
// Read from configuration
|
||||
// Config.json will look like this:
|
||||
//
|
||||
// "ApplicationInsights": {
|
||||
// "InstrumentationKey": "11111111-2222-3333-4444-555555555555"
|
||||
// }
|
||||
string instrumentationKey = config.Get("ApplicationInsights:InstrumentationKey");
|
||||
var configSource = new TelemetryConfigurationSource()
|
||||
{
|
||||
DeveloperMode = developerMode,
|
||||
EndpointAddress = endpointAddress,
|
||||
InstrumentationKey = instrumentationKey
|
||||
};
|
||||
configurationSourceRoot.Add(configSource);
|
||||
|
||||
return configurationSourceRoot;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read from configuration
|
||||
/// Config.json will look like this:
|
||||
///
|
||||
/// "ApplicationInsights": {
|
||||
/// "InstrumentationKey": "11111111-2222-3333-4444-555555555555"
|
||||
/// "TelemetryChannel": {
|
||||
/// EndpointAddress: "http://dc.services.visualstudio.com/v2/track",
|
||||
/// DeveloperMode: true
|
||||
/// }
|
||||
/// }
|
||||
/// Values can also be read from environment variables to support azure web sites configuration:
|
||||
/// </summary>
|
||||
/// <param name="config">Configuration to read variables from.</param>
|
||||
/// <param name="telemetryConfiguration">Telemetry configuration to populate</param>
|
||||
private static void AddTelemetryConfiguration(IConfiguration config, TelemetryConfiguration telemetryConfiguration)
|
||||
{
|
||||
string instrumentationKey = config.Get(ConfigurationConstants.InstrumentationKeyForWebSites);
|
||||
if (string.IsNullOrWhiteSpace(instrumentationKey))
|
||||
{
|
||||
instrumentationKey = config.Get(ConfigurationConstants.InstrumentationKeyFromConfig);
|
||||
}
|
||||
if (!string.IsNullOrWhiteSpace(instrumentationKey))
|
||||
{
|
||||
telemetryConfiguration.InstrumentationKey = instrumentationKey;
|
||||
}
|
||||
|
||||
string developerModeValue = config.Get(ConfigurationConstants.DeveloperModeForWebSites);
|
||||
if (string.IsNullOrWhiteSpace(developerModeValue))
|
||||
{
|
||||
developerModeValue = config.Get(ConfigurationConstants.DeveloperModeFromConfig);
|
||||
}
|
||||
if (!string.IsNullOrWhiteSpace(developerModeValue))
|
||||
{
|
||||
bool developerMode = false;
|
||||
if (bool.TryParse(developerModeValue, out developerMode))
|
||||
{
|
||||
telemetryConfiguration.TelemetryChannel.DeveloperMode = developerMode;
|
||||
}
|
||||
}
|
||||
|
||||
string endpointAddress = config.Get(ConfigurationConstants.EndpointAddressForWebSites);
|
||||
if (string.IsNullOrWhiteSpace(endpointAddress))
|
||||
{
|
||||
endpointAddress = config.Get(ConfigurationConstants.EndpointAddressFromConfig);
|
||||
}
|
||||
if (!string.IsNullOrWhiteSpace(endpointAddress))
|
||||
{
|
||||
var channel = telemetryConfiguration.TelemetryChannel as InProcessTelemetryChannel;
|
||||
if (channel != null)
|
||||
{
|
||||
channel.EndpointAddress = endpointAddress;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void AddServicesToCollection<T>(IServiceProvider serviceProvider, ICollection<T> collection)
|
||||
|
|
|
@ -235,5 +235,24 @@ namespace System.Net.NetworkInformation
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace Microsoft.ApplicationInsights.Channel
|
||||
{
|
||||
public sealed class InProcessTelemetryChannel : ITelemetryChannel
|
||||
{
|
||||
public string EndpointAddress { get; set; }
|
||||
public bool DeveloperMode { get; set; }
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
public void Initialize(TelemetryConfiguration configuration)
|
||||
{
|
||||
}
|
||||
public void Send(ITelemetry item)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
|
|
@ -23,23 +23,6 @@
|
|||
return services;
|
||||
}
|
||||
|
||||
public static class SetApplicationInsightsTelemetryDeveloperMode
|
||||
{
|
||||
[Fact]
|
||||
public static void ChangesDeveloperModeOfTelemetryChannelInTelemetryConfigurationInContainerToTrue()
|
||||
{
|
||||
var telemetryChannel = new FakeTelemetryChannel();
|
||||
var telemetryConfiguration = new TelemetryConfiguration { TelemetryChannel = telemetryChannel };
|
||||
var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();
|
||||
services.AddInstance(telemetryConfiguration);
|
||||
var app = new ApplicationBuilder(services.BuildServiceProvider());
|
||||
|
||||
app.SetApplicationInsightsTelemetryDeveloperMode();
|
||||
|
||||
Assert.True(telemetryChannel.DeveloperMode);
|
||||
}
|
||||
}
|
||||
|
||||
public static class AddApplicationInsightsTelemetry
|
||||
{
|
||||
[Theory]
|
||||
|
@ -98,6 +81,130 @@
|
|||
Assert.Equal("11111111-2222-3333-4444-555555555555", telemetryConfiguration.InstrumentationKey);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void RegistersTelemetryConfigurationFactoryMethodThatReadsDeveloperModeFromConfiguration()
|
||||
{
|
||||
var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();
|
||||
var config = new Configuration().AddJsonFile("content\\config.json");
|
||||
|
||||
services.AddApplicationInsightsTelemetry(config);
|
||||
|
||||
IServiceProvider serviceProvider = services.BuildServiceProvider();
|
||||
var telemetryConfiguration = serviceProvider.GetRequiredService<TelemetryConfiguration>();
|
||||
Assert.Equal(true, telemetryConfiguration.TelemetryChannel.DeveloperMode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void RegistersTelemetryConfigurationFactoryMethodThatReadsEndpointAddressFromConfiguration()
|
||||
{
|
||||
var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();
|
||||
var config = new Configuration().AddJsonFile("content\\config.json");
|
||||
|
||||
services.AddApplicationInsightsTelemetry(config);
|
||||
|
||||
IServiceProvider serviceProvider = services.BuildServiceProvider();
|
||||
var telemetryConfiguration = serviceProvider.GetRequiredService<TelemetryConfiguration>();
|
||||
Assert.Equal("http://localhost:1234/v2/track/", ((InProcessTelemetryChannel)telemetryConfiguration.TelemetryChannel).EndpointAddress);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void RegistersTelemetryConfigurationFactoryMethodThatReadsInstrumentationKeyFromEnvironment()
|
||||
{
|
||||
var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();
|
||||
|
||||
Environment.SetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY", "11111111-2222-3333-4444-555555555555");
|
||||
var config = new Configuration().AddEnvironmentVariables();
|
||||
try
|
||||
{
|
||||
services.AddApplicationInsightsTelemetry(config);
|
||||
|
||||
IServiceProvider serviceProvider = services.BuildServiceProvider();
|
||||
var telemetryConfiguration = serviceProvider.GetRequiredService<TelemetryConfiguration>();
|
||||
Assert.Equal("11111111-2222-3333-4444-555555555555", telemetryConfiguration.InstrumentationKey);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Environment.SetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY", null);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void RegistersTelemetryConfigurationFactoryMethodThatReadsDeveloperModeFromEnvironment()
|
||||
{
|
||||
var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();
|
||||
Environment.SetEnvironmentVariable("APPINSIGHTS_DEVELOPER_MODE", "true");
|
||||
var config = new Configuration().AddEnvironmentVariables();
|
||||
try
|
||||
{
|
||||
services.AddApplicationInsightsTelemetry(config);
|
||||
|
||||
IServiceProvider serviceProvider = services.BuildServiceProvider();
|
||||
var telemetryConfiguration = serviceProvider.GetRequiredService<TelemetryConfiguration>();
|
||||
Assert.Equal(true, telemetryConfiguration.TelemetryChannel.DeveloperMode);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Environment.SetEnvironmentVariable("APPINSIGHTS_DEVELOPER_MODE", null);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void RegistersTelemetryConfigurationFactoryMethodThatReadsEndpointAddressFromEnvironment()
|
||||
{
|
||||
var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();
|
||||
Environment.SetEnvironmentVariable("APPINSIGHTS_ENDPOINTADDRESS", "http://localhost:1234/v2/track/");
|
||||
var config = new Configuration().AddEnvironmentVariables();
|
||||
try
|
||||
{
|
||||
services.AddApplicationInsightsTelemetry(config);
|
||||
|
||||
IServiceProvider serviceProvider = services.BuildServiceProvider();
|
||||
var telemetryConfiguration = serviceProvider.GetRequiredService<TelemetryConfiguration>();
|
||||
Assert.Equal("http://localhost:1234/v2/track/", ((InProcessTelemetryChannel)telemetryConfiguration.TelemetryChannel).EndpointAddress);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Environment.SetEnvironmentVariable("APPINSIGHTS_ENDPOINTADDRESS", null);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void RegistersTelemetryConfigurationFactoryMethodThatReadsInstrumentationKeyFromSettings()
|
||||
{
|
||||
var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();
|
||||
|
||||
var config = new Configuration().AddApplicaitonInsightsSettings(instrumentationKey: "11111111-2222-3333-4444-555555555555");
|
||||
services.AddApplicationInsightsTelemetry(config);
|
||||
|
||||
IServiceProvider serviceProvider = services.BuildServiceProvider();
|
||||
var telemetryConfiguration = serviceProvider.GetRequiredService<TelemetryConfiguration>();
|
||||
Assert.Equal("11111111-2222-3333-4444-555555555555", telemetryConfiguration.InstrumentationKey);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void RegistersTelemetryConfigurationFactoryMethodThatReadsDeveloperModeFromSettings()
|
||||
{
|
||||
var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();
|
||||
var config = new Configuration().AddApplicaitonInsightsSettings(developerMode: true);
|
||||
services.AddApplicationInsightsTelemetry(config);
|
||||
|
||||
IServiceProvider serviceProvider = services.BuildServiceProvider();
|
||||
var telemetryConfiguration = serviceProvider.GetRequiredService<TelemetryConfiguration>();
|
||||
Assert.Equal(true, telemetryConfiguration.TelemetryChannel.DeveloperMode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void RegistersTelemetryConfigurationFactoryMethodThatReadsEndpointAddressFromSettings()
|
||||
{
|
||||
var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();
|
||||
var config = new Configuration().AddApplicaitonInsightsSettings(endpointAddress: "http://localhost:1234/v2/track/");
|
||||
services.AddApplicationInsightsTelemetry(config);
|
||||
|
||||
IServiceProvider serviceProvider = services.BuildServiceProvider();
|
||||
var telemetryConfiguration = serviceProvider.GetRequiredService<TelemetryConfiguration>();
|
||||
Assert.Equal("http://localhost:1234/v2/track/", ((InProcessTelemetryChannel)telemetryConfiguration.TelemetryChannel).EndpointAddress);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void RegistersTelemetryConfigurationFactoryMethodThatPopulatesItWithContextInitializersFromContainer()
|
||||
{
|
||||
|
|
|
@ -1,5 +1,9 @@
|
|||
{
|
||||
"ApplicationInsights": {
|
||||
"InstrumentationKey": "11111111-2222-3333-4444-555555555555"
|
||||
}
|
||||
"ApplicationInsights": {
|
||||
"InstrumentationKey": "11111111-2222-3333-4444-555555555555",
|
||||
"TelemetryChannel": {
|
||||
"DeveloperMode": true,
|
||||
"EndpointAddress": "http://localhost:1234/v2/track/"
|
||||
}
|
||||
}
|
||||
}
|
|
@ -40,6 +40,8 @@ namespace Mvc6Framework45.FunctionalTests
|
|||
// This reads the configuration keys from the secret store.
|
||||
// For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
|
||||
configuration.AddUserSecrets();
|
||||
|
||||
configuration.AddApplicaitonInsightsSettings(developerMode: true);
|
||||
}
|
||||
configuration.AddEnvironmentVariables();
|
||||
Configuration = configuration;
|
||||
|
@ -108,8 +110,6 @@ namespace Mvc6Framework45.FunctionalTests
|
|||
app.UseBrowserLink();
|
||||
app.UseErrorPage(ErrorPageOptions.ShowAll);
|
||||
app.UseDatabaseErrorPage(DatabaseErrorPageOptions.ShowAll);
|
||||
// Set immediate delivery for Application Insights events.
|
||||
app.SetApplicationInsightsTelemetryDeveloperMode();
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
@ -37,13 +37,6 @@ namespace SampleWebAPIIntegration
|
|||
{
|
||||
app.UseFunctionalTestTelemetryChannel();
|
||||
|
||||
// Add the following to the request pipeline only in development environment.
|
||||
if (string.Equals(env.EnvironmentName, "Development", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
// Set immediate delivery for Application Insights events.
|
||||
app.SetApplicationInsightsTelemetryDeveloperMode();
|
||||
}
|
||||
|
||||
// Add Application Insights monitoring to the request pipeline as a very first middleware.
|
||||
app.UseApplicationInsightsRequestTelemetry();
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче