-
Notifications
You must be signed in to change notification settings - Fork 772
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
[SDK] Remove the dependency on M.E.C.EnvironmentVariables #4092
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alanwest @cijothomas I was just chatting with @reyang about this. So the new feature we are supporting in 1.4.0 is we will look for environment variables using IConfiguration. This code is only ever used for Sdk.Create* style to build IConfiguration from environment variables as backwards compatibility for users without hosting. So strictly speaking, we do not need this connection string logic. We don't support it in 1.3.0 and we don't need to in 1.4.0. Thinking we should remove this and slim it down as a follow-up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#4106