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

Exposing App Insights endpoint configs #2376

Closed
wants to merge 3 commits into from
Closed
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
Expand Up @@ -99,6 +99,21 @@ public TimeSpan QuickPulseInitializationDelay {
/// </summary>
public bool EnableDependencyTracking { get; set; } = true;

/// <summary>
/// Gets or sets the endpoint address for the telemetry channel
/// </summary>
public string EndpointAddress { get; set; }

/// <summary>
/// Gets or sets the Profile Query Endpoint for the Application Id Provider
/// </summary>
public string ProfileQueryEndpoint { get; set; }

/// <summary>
/// Gets or sets the Quick Pulse Service Endpoint for the Quick Pulse Telemetry Module
/// </summary>
public string QuickPulseServiceEndpoint { get; set; }

/// <summary>
/// Gets or sets HTTP request collection options.
/// </summary>
Expand Down Expand Up @@ -163,7 +178,10 @@ public string Format()
{ nameof(HttpAutoCollectionOptions), httpOptions },
{ nameof(LiveMetricsInitializationDelay), LiveMetricsInitializationDelay },
{ nameof(EnableLiveMetrics), EnableLiveMetrics },
{ nameof(EnableDependencyTracking), EnableDependencyTracking }
{ nameof(EnableDependencyTracking), EnableDependencyTracking },
{ nameof(EndpointAddress), EndpointAddress },
{ nameof(ProfileQueryEndpoint), ProfileQueryEndpoint },
{ nameof(QuickPulseServiceEndpoint), QuickPulseServiceEndpoint }
};

return options.ToString(Formatting.Indented);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,19 @@ public static IServiceCollection AddApplicationInsights(this IServiceCollection
services.AddSingleton<ITelemetryInitializer, WebJobsTelemetryInitializer>();
services.AddSingleton<ITelemetryInitializer, MetricSdkVersionTelemetryInitializer>();
services.AddSingleton<QuickPulseInitializationScheduler>();
services.AddSingleton<QuickPulseTelemetryModule>();

services.AddSingleton<QuickPulseTelemetryModule>(provider =>
{
ApplicationInsightsLoggerOptions options = provider.GetService<IOptions<ApplicationInsightsLoggerOptions>>().Value;
if (!string.IsNullOrEmpty(options.QuickPulseServiceEndpoint))
{
return new QuickPulseTelemetryModule
{
QuickPulseServiceEndpoint = options.QuickPulseServiceEndpoint
};
}
return new QuickPulseTelemetryModule();
});

services.AddSingleton<ITelemetryModule>(provider =>
{
Expand Down Expand Up @@ -85,7 +97,18 @@ public static IServiceCollection AddApplicationInsights(this IServiceCollection
return NullTelemetryModule.Instance;
});

services.AddSingleton<IApplicationIdProvider, ApplicationInsightsApplicationIdProvider>();
services.AddSingleton<IApplicationIdProvider, ApplicationInsightsApplicationIdProvider>(provider =>
{
ApplicationInsightsLoggerOptions options = provider.GetService<IOptions<ApplicationInsightsLoggerOptions>>().Value;
if (!string.IsNullOrEmpty(options.ProfileQueryEndpoint))
{
return new ApplicationInsightsApplicationIdProvider
{
ProfileQueryEndpoint = options.ProfileQueryEndpoint
};
}
return new ApplicationInsightsApplicationIdProvider();
});

services.AddSingleton<ITelemetryModule>(provider =>
{
Expand Down Expand Up @@ -134,7 +157,19 @@ public static IServiceCollection AddApplicationInsights(this IServiceCollection

services.AddSingleton<ITelemetryModule, AppServicesHeartbeatTelemetryModule>();

services.AddSingleton<ITelemetryChannel, ServerTelemetryChannel>();
services.AddSingleton<ITelemetryChannel, ServerTelemetryChannel>(provider =>
{
ApplicationInsightsLoggerOptions options = provider.GetService<IOptions<ApplicationInsightsLoggerOptions>>().Value;
if (!string.IsNullOrEmpty(options.EndpointAddress))
{
return new ServerTelemetryChannel
{
EndpointAddress = options.EndpointAddress
};
}
return new ServerTelemetryChannel();
});

services.AddSingleton<TelemetryConfiguration>(provider =>
{
ApplicationInsightsLoggerOptions options = provider.GetService<IOptions<ApplicationInsightsLoggerOptions>>().Value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private IHost ConfigureHost(LogLevel minLevel = LogLevel.Information, HttpAutoCo
})
.ConfigureServices(services =>
{
ServiceDescriptor quickPulse = services.Single(s => s.ImplementationType == typeof(QuickPulseTelemetryModule));
ServiceDescriptor quickPulse = services.Single(s => s.ServiceType.Name == nameof(QuickPulseTelemetryModule));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find a way to add the 3 AppInsights singletons with an ImplementationType and I don't think I fully understand the ramifications of adding them without one. This is the only thing I've found in the code that was counting on these modules having an ImplementationType, but I'm not sure if there are others.

services.Remove(quickPulse);
services.AddSingleton<ITelemetryModule, QuickPulseTelemetryModule>(s => new QuickPulseTelemetryModule()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,37 @@ public void DependencyInjectionConfiguration_ConfiguresActiveOnlyOnce()
}
}

[Fact]
public void DependencyInjectionConfiguration_ConfiguresEndpoints()
{
var endpointAddress = "http://endpoint.address.com/";
var quickPulseServiceEndpoint = "http://quickpulse.serviceendpoint.com/";
var profileQueryEndpoint = "http://profile.queryendpoint.com/";
using (var host = new HostBuilder()
.ConfigureLogging(b =>
{
b.AddApplicationInsightsWebJobs(o =>
{
o.EndpointAddress = endpointAddress;
o.QuickPulseServiceEndpoint = quickPulseServiceEndpoint;
o.ProfileQueryEndpoint = profileQueryEndpoint;
});
})
.Build())
{
var config = host.Services.GetService<TelemetryConfiguration>();

Assert.Equal(endpointAddress, config.TelemetryChannel.EndpointAddress);

Assert.IsType<ApplicationInsightsApplicationIdProvider>(config.ApplicationIdProvider);
Assert.Equal(profileQueryEndpoint, ((ApplicationInsightsApplicationIdProvider)config.ApplicationIdProvider).ProfileQueryEndpoint);

var modules = host.Services.GetServices<ITelemetryModule>().ToList();
var quickPulseTelemetryModule = modules.OfType<QuickPulseTelemetryModule>().Single();
Assert.Equal(quickPulseServiceEndpoint, quickPulseTelemetryModule.QuickPulseServiceEndpoint);
}
}

[Fact]
public void CreateFilterOptions_MinLevel()
{
Expand Down