-
Notifications
You must be signed in to change notification settings - Fork 773
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SDK] Remove the dependency on M.E.C.EnvironmentVariables (#4092)
* Remove the dependency on Microsoft.Extensions.Configuration.EnvironmentVariables from SDK. * Added notes. * CHANGELOG patch.
- Loading branch information
1 parent
84b3778
commit 03af717
Showing
9 changed files
with
209 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
src/OpenTelemetry/Internal/EnvironmentVariables/EnvironmentVariablesConfigurationProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// <auto-generated /> (Turns off StyleCop analysis in this file.) | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#nullable enable | ||
|
||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
|
||
namespace Microsoft.Extensions.Configuration.EnvironmentVariables | ||
{ | ||
/// <summary> | ||
/// An environment variable based <see cref="ConfigurationProvider"/>. | ||
/// </summary> | ||
internal sealed class EnvironmentVariablesConfigurationProvider : ConfigurationProvider | ||
{ | ||
private const string MySqlServerPrefix = "MYSQLCONNSTR_"; | ||
private const string SqlAzureServerPrefix = "SQLAZURECONNSTR_"; | ||
private const string SqlServerPrefix = "SQLCONNSTR_"; | ||
private const string CustomConnectionStringPrefix = "CUSTOMCONNSTR_"; | ||
|
||
private readonly string _prefix; | ||
private readonly string _normalizedPrefix; | ||
|
||
/// <summary> | ||
/// Initializes a new instance. | ||
/// </summary> | ||
public EnvironmentVariablesConfigurationProvider() | ||
{ | ||
_prefix = string.Empty; | ||
_normalizedPrefix = string.Empty; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance with the specified prefix. | ||
/// </summary> | ||
/// <param name="prefix">A prefix used to filter the environment variables.</param> | ||
public EnvironmentVariablesConfigurationProvider(string? prefix) | ||
{ | ||
_prefix = prefix ?? string.Empty; | ||
_normalizedPrefix = Normalize(_prefix); | ||
} | ||
|
||
/// <summary> | ||
/// Loads the environment variables. | ||
/// </summary> | ||
public override void Load() => | ||
Load(Environment.GetEnvironmentVariables()); | ||
|
||
/// <summary> | ||
/// Generates a string representing this provider name and relevant details. | ||
/// </summary> | ||
/// <returns> The configuration name. </returns> | ||
public override string ToString() | ||
=> $"{GetType().Name} Prefix: '{_prefix}'"; | ||
|
||
internal void Load(IDictionary envVariables) | ||
{ | ||
var data = new Dictionary<string, string?>(StringComparer.OrdinalIgnoreCase); | ||
|
||
IDictionaryEnumerator e = envVariables.GetEnumerator(); | ||
try | ||
{ | ||
while (e.MoveNext()) | ||
{ | ||
string key = (string)e.Entry.Key; | ||
string? value = (string?)e.Entry.Value; | ||
|
||
if (key.StartsWith(MySqlServerPrefix, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
HandleMatchedConnectionStringPrefix(data, MySqlServerPrefix, "MySql.Data.MySqlClient", key, value); | ||
} | ||
else if (key.StartsWith(SqlAzureServerPrefix, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
HandleMatchedConnectionStringPrefix(data, SqlAzureServerPrefix, "System.Data.SqlClient", key, value); | ||
} | ||
else if (key.StartsWith(SqlServerPrefix, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
HandleMatchedConnectionStringPrefix(data, SqlServerPrefix, "System.Data.SqlClient", key, value); | ||
} | ||
else if (key.StartsWith(CustomConnectionStringPrefix, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
HandleMatchedConnectionStringPrefix(data, CustomConnectionStringPrefix, null, key, value); | ||
} | ||
else | ||
{ | ||
AddIfNormalizedKeyMatchesPrefix(data, Normalize(key), value); | ||
} | ||
} | ||
} | ||
finally | ||
{ | ||
(e as IDisposable)?.Dispose(); | ||
} | ||
|
||
Data = data; | ||
} | ||
|
||
private void HandleMatchedConnectionStringPrefix(Dictionary<string, string?> data, string connectionStringPrefix, string? provider, string fullKey, string? value) | ||
{ | ||
string normalizedKeyWithoutConnectionStringPrefix = Normalize(fullKey.Substring(connectionStringPrefix.Length)); | ||
|
||
// Add the key-value pair for connection string, and optionally provider name | ||
AddIfNormalizedKeyMatchesPrefix(data, $"ConnectionStrings:{normalizedKeyWithoutConnectionStringPrefix}", value); | ||
if (provider != null) | ||
{ | ||
AddIfNormalizedKeyMatchesPrefix(data, $"ConnectionStrings:{normalizedKeyWithoutConnectionStringPrefix}_ProviderName", provider); | ||
} | ||
} | ||
|
||
private void AddIfNormalizedKeyMatchesPrefix(Dictionary<string, string?> data, string normalizedKey, string? value) | ||
{ | ||
if (normalizedKey.StartsWith(_normalizedPrefix, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
data[normalizedKey.Substring(_normalizedPrefix.Length)] = value; | ||
} | ||
} | ||
|
||
private static string Normalize(string key) => key.Replace("__", ConfigurationPath.KeyDelimiter); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/OpenTelemetry/Internal/EnvironmentVariables/EnvironmentVariablesConfigurationSource.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// <auto-generated /> (Turns off StyleCop analysis in this file.) | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#nullable enable | ||
|
||
namespace Microsoft.Extensions.Configuration.EnvironmentVariables | ||
{ | ||
/// <summary> | ||
/// Represents environment variables as an <see cref="IConfigurationSource"/>. | ||
/// </summary> | ||
internal sealed class EnvironmentVariablesConfigurationSource : IConfigurationSource | ||
{ | ||
/// <summary> | ||
/// A prefix used to filter environment variables. | ||
/// </summary> | ||
public string? Prefix { get; set; } | ||
|
||
/// <summary> | ||
/// Builds the <see cref="EnvironmentVariablesConfigurationProvider"/> for this source. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="IConfigurationBuilder"/>.</param> | ||
/// <returns>A <see cref="EnvironmentVariablesConfigurationProvider"/></returns> | ||
public IConfigurationProvider Build(IConfigurationBuilder builder) | ||
{ | ||
return new EnvironmentVariablesConfigurationProvider(Prefix); | ||
} | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/OpenTelemetry/Internal/EnvironmentVariables/EnvironmentVariablesExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// <auto-generated /> (Turns off StyleCop analysis in this file.) | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#nullable enable | ||
|
||
using System; | ||
using Microsoft.Extensions.Configuration.EnvironmentVariables; | ||
|
||
namespace Microsoft.Extensions.Configuration | ||
{ | ||
/// <summary> | ||
/// Extension methods for registering <see cref="EnvironmentVariablesConfigurationProvider"/> with <see cref="IConfigurationBuilder"/>. | ||
/// </summary> | ||
internal static class EnvironmentVariablesExtensions | ||
{ | ||
/// <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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters