Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fixes and test updates.
  • Loading branch information
CodeBlanch committed Feb 7, 2023
commit c088a9dc869113c4616179e00f42efc688b9e3a1
3 changes: 2 additions & 1 deletion build/Common.props
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
<MicrosoftCodeCoveragePkgVer>[17.4.1]</MicrosoftCodeCoveragePkgVer>
<MicrosoftExtensionsDependencyInjectionPkgVer>[3.1.0,)</MicrosoftExtensionsDependencyInjectionPkgVer>
<MicrosoftExtensionsDependencyInjectionAbstractionsPkgVer>$(MicrosoftExtensionsDependencyInjectionPkgVer)</MicrosoftExtensionsDependencyInjectionAbstractionsPkgVer>
<MicrosoftExtensionsHostingAbstractionsPkgVer>[2.1.0,)</MicrosoftExtensionsHostingAbstractionsPkgVer>
<MicrosoftExtensionsHostingPkgVer>[2.1.0,)</MicrosoftExtensionsHostingPkgVer>
<MicrosoftExtensionsHostingAbstractionsPkgVer>$(MicrosoftExtensionsHostingPkgVer)</MicrosoftExtensionsHostingAbstractionsPkgVer>
<MicrosoftExtensionsLoggingPkgVer>[3.1.0,)</MicrosoftExtensionsLoggingPkgVer>
<MicrosoftExtensionsLoggingConfigurationPkgVer>$(MicrosoftExtensionsLoggingPkgVer)</MicrosoftExtensionsLoggingConfigurationPkgVer>
<MicrosoftExtensionsOptionsPkgVer>[3.1.0,)</MicrosoftExtensionsOptionsPkgVer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,24 @@

#nullable enable

using System;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.EnvironmentVariables;

namespace Microsoft.Extensions.Configuration
namespace OpenTelemetry.Internal;

/// <summary>
/// Extension methods for registering <see cref="EnvironmentVariablesConfigurationProvider"/> with <see cref="IConfigurationBuilder"/>.
/// </summary>
internal static class EnvironmentVariablesExtensions
{
/// <summary>
/// Extension methods for registering <see cref="EnvironmentVariablesConfigurationProvider"/> with <see cref="IConfigurationBuilder"/>.
/// Adds an <see cref="IConfigurationProvider"/> that reads configuration values from environment variables.
/// </summary>
internal static class EnvironmentVariablesExtensions
/// <param name="configurationBuilder">The <see cref="IConfigurationBuilder"/> to add to.</param>
/// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
public static IConfigurationBuilder AddEnvironmentVariables(this IConfigurationBuilder configurationBuilder)
{
/// <summary>
/// Adds an <see cref="IConfigurationProvider"/> that reads configuration values from environment variables.
/// </summary>
/// <param name="configurationBuilder">The <see cref="IConfigurationBuilder"/> to add to.</param>
/// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
public static IConfigurationBuilder AddEnvironmentVariables(this IConfigurationBuilder configurationBuilder)
{
configurationBuilder.Add(new EnvironmentVariablesConfigurationSource());
return configurationBuilder;
}

/// <summary>
/// Adds an <see cref="IConfigurationProvider"/> that reads configuration values from environment variables
/// with a specified prefix.
/// </summary>
/// <param name="configurationBuilder">The <see cref="IConfigurationBuilder"/> to add to.</param>
/// <param name="prefix">The prefix that environment variable names must start with. The prefix will be removed from the environment variable names.</param>
/// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
public static IConfigurationBuilder AddEnvironmentVariables(
this IConfigurationBuilder configurationBuilder,
string? prefix)
{
configurationBuilder.Add(new EnvironmentVariablesConfigurationSource { Prefix = prefix });
return configurationBuilder;
}

/// <summary>
/// Adds an <see cref="IConfigurationProvider"/> that reads configuration values from environment variables.
/// </summary>
/// <param name="builder">The <see cref="IConfigurationBuilder"/> to add to.</param>
/// <param name="configureSource">Configures the source.</param>
/// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
public static IConfigurationBuilder AddEnvironmentVariables(this IConfigurationBuilder builder, Action<EnvironmentVariablesConfigurationSource>? configureSource)
=> builder.Add(configureSource);
configurationBuilder.Add(new EnvironmentVariablesConfigurationSource());
return configurationBuilder;
}
}
56 changes: 33 additions & 23 deletions src/OpenTelemetry/Internal/HostingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,17 @@ internal static class HostingHelper
{
private static readonly object LockObject = new();
private static bool initialized;
private static Type? hostedServiceImplementation;

public static void AddOpenTelemetryHostedServiceIntoServiceCollection(IServiceCollection services)
{
lock (LockObject)
if (TryAddOpenTelemetryHostedServiceIntoServiceCollection(services, out var reason))
{
if (!initialized)
{
try
{
if (TryAddOpenTelemetryHostedServiceIntoServiceCollection(services, out var reason))
{
OpenTelemetrySdkEventSource.Log.HostedServiceRegistered();
}
else
{
OpenTelemetrySdkEventSource.Log.HostedServiceRegistrationSkipped(reason);
}
}
catch (Exception ex)
{
OpenTelemetrySdkEventSource.Log.HostedServiceRegistrationFailure(ex);
}

initialized = true;
}
OpenTelemetrySdkEventSource.Log.HostedServiceRegistered();
}
else
{
OpenTelemetrySdkEventSource.Log.HostedServiceRegistrationSkipped(reason);
}
}

Expand All @@ -67,7 +53,6 @@ private static bool TryAddOpenTelemetryHostedServiceIntoServiceCollection(IServi
// Note: This is for .NET Framework and/or .NET Standard 2.0 targets.
bool isDynamicCodeSupported = true;
#endif

if (!isDynamicCodeSupported)
{
reason = "Dynamic code not supported";
Expand All @@ -83,8 +68,33 @@ private static bool TryAddOpenTelemetryHostedServiceIntoServiceCollection(IServi
return false;
}

lock (LockObject)
{
if (!initialized)
{
try
{
hostedServiceImplementation = CreateHostedServiceImplementation(iHostedServiceType);
}
catch (Exception ex)
{
OpenTelemetrySdkEventSource.Log.HostedServiceRegistrationFailure(ex);
}
finally
{
initialized = true;
}
}
}

if (hostedServiceImplementation == null)
{
reason = "Initialization failure";
return false;
}

services.TryAddSingleton<TelemetryHostedService>();
services.TryAddSingleton(iHostedServiceType, CreateHostedServiceImplementation(iHostedServiceType));
services.TryAddSingleton(iHostedServiceType, hostedServiceImplementation);

reason = null;
return true;
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@

#if NET6_0_OR_GREATER

using System;
using System.Collections.Generic;
using System.Diagnostics.Metrics;
using System.Net;
using System.Threading.Tasks;

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
Expand Down Expand Up @@ -89,7 +86,7 @@ private static async Task RunMetricsTest(Action<MeterProviderBuilder> configure,
using var host = await new HostBuilder()
.ConfigureWebHost(webBuilder => webBuilder
.UseTestServer()
.ConfigureServices(services => services.AddOpenTelemetry().WithMetrics(configure).StartWithHost())
.ConfigureServices(services => services.AddOpenTelemetry().WithMetrics(configure))
.Configure(app => app.Run(httpContext =>
{
testAction.Invoke();
Expand Down
Loading