-
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 + Jaeger] Support loading environment variables from IConfiguration in Traces & Metrics #3720
Merged
CodeBlanch
merged 8 commits into
open-telemetry:main
from
CodeBlanch:sdk-envvars-iconfiguration-support
Oct 12, 2022
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
81cc629
Support retrieval of environment variables through IConfiguration in …
CodeBlanch d4ccabf
Update Jaeger to load environment variables through IConfiguration.
CodeBlanch 1405478
Warning fix.
CodeBlanch b20705e
CHANGELOG patch.
CodeBlanch df5b8d3
Bug fixes.
CodeBlanch 142dbd2
Warning cleanup.
CodeBlanch 305cfe5
Code review.
CodeBlanch cff5bc7
Merge from main.
CodeBlanch 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
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,164 @@ | ||
// <copyright file="ConfigurationExtensions.cs" company="OpenTelemetry Authors"> | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
|
||
#nullable enable | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
#if !NETFRAMEWORK && !NETSTANDARD2_0 | ||
using System.Diagnostics.CodeAnalysis; | ||
#endif | ||
using System.Globalization; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace OpenTelemetry.Internal; | ||
|
||
internal static class ConfigurationExtensions | ||
{ | ||
public delegate bool TryParseFunc<T>( | ||
string value, | ||
#if !NETFRAMEWORK && !NETSTANDARD2_0 | ||
[NotNullWhen(true)] | ||
#endif | ||
out T? parsedValue); | ||
|
||
public static bool TryGetStringValue( | ||
this IConfiguration configuration, | ||
string key, | ||
#if !NETFRAMEWORK && !NETSTANDARD2_0 | ||
[NotNullWhen(true)] | ||
#endif | ||
out string? value) | ||
{ | ||
value = configuration.GetValue<string?>(key, null); | ||
|
||
return !string.IsNullOrWhiteSpace(value); | ||
} | ||
|
||
public static bool TryGetUriValue( | ||
this IConfiguration configuration, | ||
string key, | ||
#if !NETFRAMEWORK && !NETSTANDARD2_0 | ||
[NotNullWhen(true)] | ||
#endif | ||
out Uri? value) | ||
{ | ||
if (!configuration.TryGetStringValue(key, out var stringValue)) | ||
{ | ||
value = null; | ||
return false; | ||
} | ||
|
||
if (!Uri.TryCreate(stringValue, UriKind.Absolute, out value)) | ||
{ | ||
throw new FormatException($"{key} environment variable has an invalid value: '{stringValue}'"); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public static bool TryGetIntValue( | ||
this IConfiguration configuration, | ||
string key, | ||
out int value) | ||
{ | ||
if (!configuration.TryGetStringValue(key, out var stringValue)) | ||
{ | ||
value = default; | ||
return false; | ||
} | ||
|
||
if (!int.TryParse(stringValue, NumberStyles.None, CultureInfo.InvariantCulture, out value)) | ||
{ | ||
throw new FormatException($"{key} environment variable has an invalid value: '{stringValue}'"); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public static bool TryGetValue<T>( | ||
this IConfiguration configuration, | ||
string key, | ||
TryParseFunc<T> tryParseFunc, | ||
#if !NETFRAMEWORK && !NETSTANDARD2_0 | ||
[NotNullWhen(true)] | ||
#endif | ||
out T? value) | ||
{ | ||
if (!configuration.TryGetStringValue(key, out var stringValue)) | ||
{ | ||
value = default; | ||
return false; | ||
} | ||
|
||
if (!tryParseFunc(stringValue!, out value)) | ||
{ | ||
throw new FormatException($"{key} environment variable has an invalid value: '{stringValue}'"); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public static IServiceCollection RegisterOptionsFactory<T>( | ||
this IServiceCollection services, | ||
Func<IConfiguration, T> optionsFactoryFunc) | ||
where T : class | ||
{ | ||
Debug.Assert(services != null, "services was null"); | ||
Debug.Assert(optionsFactoryFunc != null, "optionsFactoryFunc was null"); | ||
|
||
services!.TryAddSingleton<IOptionsFactory<T>>(sp => | ||
{ | ||
return new DelegatingOptionsFactory<T>( | ||
optionsFactoryFunc!, | ||
sp.GetRequiredService<IConfiguration>(), | ||
sp.GetServices<IConfigureOptions<T>>(), | ||
sp.GetServices<IPostConfigureOptions<T>>(), | ||
sp.GetServices<IValidateOptions<T>>()); | ||
}); | ||
|
||
return services!; | ||
} | ||
|
||
private sealed class DelegatingOptionsFactory<T> : OptionsFactory<T> | ||
where T : class | ||
{ | ||
private readonly Func<IConfiguration, T> optionsFactoryFunc; | ||
private readonly IConfiguration configuration; | ||
|
||
public DelegatingOptionsFactory( | ||
Func<IConfiguration, T> optionsFactoryFunc, | ||
IConfiguration configuration, | ||
IEnumerable<IConfigureOptions<T>> setups, | ||
IEnumerable<IPostConfigureOptions<T>> postConfigures, | ||
IEnumerable<IValidateOptions<T>> validations) | ||
: base(setups, postConfigures, validations) | ||
{ | ||
Debug.Assert(optionsFactoryFunc != null, "optionsFactoryFunc was null"); | ||
Debug.Assert(configuration != null, "configuration was null"); | ||
|
||
this.optionsFactoryFunc = optionsFactoryFunc!; | ||
this.configuration = configuration!; | ||
} | ||
|
||
protected override T CreateInstance(string name) | ||
=> this.optionsFactoryFunc(this.configuration); | ||
} | ||
} |
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.
Just a bug reminder #3690 😉
Of course, it can (and should) be addressed in a separate issue. Feel free to resolve this comment.