Guard.cs - Follow-up changes (#2501)

This commit is contained in:
Michael Maxwell 2021-10-26 07:57:34 -07:00 committed by GitHub
parent b1fc7616b1
commit 433b7c8ad1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 46 additions and 17 deletions

View File

@ -24,7 +24,7 @@ namespace OpenTelemetry.Internal
/// <summary>
/// Methods for guarding against exception throwing values.
/// </summary>
public static class Guard
internal static class Guard
{
private const string DefaultParamName = "N/A";
@ -136,11 +136,11 @@ namespace OpenTelemetry.Internal
}
/// <summary>
/// Throw an exception if the value is not within the given range.
/// Throw an exception if the value is not of the expected type.
/// </summary>
/// <param name="value">The value to check.</param>
/// <param name="paramName">The parameter name to use in the thrown exception.</param>
/// <typeparam name="T">The type to attempt to convert to.</typeparam>
/// <typeparam name="T">The type attempted to convert to.</typeparam>
/// <returns>The value casted to the specified type.</returns>
[DebuggerHidden]
[MethodImpl(MethodImplOptions.AggressiveInlining)]

View File

@ -51,7 +51,7 @@ namespace OpenTelemetry.Trace
/// Adds a listener for <see cref="Activity"/> objects created with the given operation name to the <see cref="TracerProviderBuilder"/>.
/// </summary>
/// <remarks>
/// This is provided to capture legacy <see cref="Activity"/> objects created without using the <see cref="System.Diagnostics.ActivitySource"/> API.
/// This is provided to capture legacy <see cref="Activity"/> objects created without using the <see cref="ActivitySource"/> API.
/// </remarks>
/// <param name="operationName">Operation name of the <see cref="Activity"/> objects to capture.</param>
/// <returns>Returns <see cref="TracerProviderBuilder"/> for chaining.</returns>

View File

@ -17,6 +17,7 @@
<ItemGroup>
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\ServiceProviderExtensions.cs" Link="Includes\ServiceProviderExtensions.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\Guard.cs" Link="Includes\Guard.cs" />
</ItemGroup>
</Project>

View File

@ -15,4 +15,8 @@
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry\OpenTelemetry.csproj" />
</ItemGroup>
<ItemGroup>
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\Guard.cs" Link="Includes\Guard.cs" />
</ItemGroup>
</Project>

View File

@ -25,6 +25,7 @@
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\StatusHelper.cs" Link="Includes\StatusHelper.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\IActivityEnumerator.cs" Link="Includes\IActivityEnumerator.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\EnumerationHelper.cs" Link="Includes\EnumerationHelper.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\Guard.cs" Link="Includes\Guard.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\PooledList.cs" Link="Includes\PooledList.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\PeerServiceResolver.cs" Link="Includes\PeerServiceResolver.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\ResourceSemanticConventions.cs" Link="Includes\ResourceSemanticConventions.cs" />

View File

@ -42,6 +42,7 @@
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\StatusHelper.cs" Link="Includes\StatusHelper.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\IActivityEnumerator.cs" Link="Includes\IActivityEnumerator.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\EnumerationHelper.cs" Link="Includes\EnumerationHelper.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\Guard.cs" Link="Includes\Guard.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\PooledList.cs" Link="Includes\PooledList.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\PeerServiceResolver.cs" Link="Includes\PeerServiceResolver.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\ResourceSemanticConventions.cs" Link="Includes\ResourceSemanticConventions.cs" />

View File

@ -24,6 +24,7 @@
<ItemGroup>
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\ServiceProviderExtensions.cs" Link="Includes\ServiceProviderExtensions.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\ExceptionExtensions.cs" Link="Includes\ExceptionExtensions.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\Guard.cs" Link="Includes\Guard.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">

View File

@ -10,6 +10,7 @@
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry\OpenTelemetry.csproj" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\ExceptionExtensions.cs" Link="Includes\ExceptionExtensions.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\Guard.cs" Link="Includes\Guard.cs" />
</ItemGroup>
</Project>

View File

@ -16,6 +16,7 @@
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\StatusHelper.cs" Link="Includes\StatusHelper.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\IActivityEnumerator.cs" Link="Includes\IActivityEnumerator.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\EnumerationHelper.cs" Link="Includes\EnumerationHelper.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\Guard.cs" Link="Includes\Guard.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\PooledList.cs" Link="Includes\PooledList.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\PeerServiceResolver.cs" Link="Includes\PeerServiceResolver.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\ResourceSemanticConventions.cs" Link="Includes\ResourceSemanticConventions.cs" />

View File

@ -10,6 +10,7 @@
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry\OpenTelemetry.csproj" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\ExceptionExtensions.cs" Link="Includes\ExceptionExtensions.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\Guard.cs" Link="Includes\Guard.cs" />
</ItemGroup>
</Project>

View File

@ -7,6 +7,7 @@
<ItemGroup>
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\ExceptionExtensions.cs" Link="Includes\ExceptionExtensions.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\Guard.cs" Link="Includes\Guard.cs" />
</ItemGroup>
<ItemGroup>

View File

@ -12,6 +12,7 @@
<ItemGroup>
<Compile Include="$(RepoRoot)\src\OpenTelemetry\DiagnosticSourceInstrumentation\PropertyFetcher.cs" Link="Includes\PropertyFetcher.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\Guard.cs" Link="Includes\Guard.cs" />
</ItemGroup>
<ItemGroup>

View File

@ -10,6 +10,7 @@
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Instrumentation.GrpcNetClient\GrpcTagHelper.cs" Link="Includes\GrpcTagHelper.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Instrumentation.GrpcNetClient\StatusCanonicalCode.cs" Link="Includes\StatusCanonicalCode.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\ServiceProviderExtensions.cs" Link="Includes\ServiceProviderExtensions.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\Guard.cs" Link="Includes\Guard.cs" />
</ItemGroup>
<ItemGroup>

View File

@ -8,6 +8,7 @@
<ItemGroup>
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Instrumentation.Http\HttpRequestMessageContextPropagation.cs" Link="Includes\HttpRequestMessageContextPropagation.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\Guard.cs" Link="Includes\Guard.cs" />
</ItemGroup>
<ItemGroup>

View File

@ -7,6 +7,10 @@
<IncludeDiagnosticSourceInstrumentationHelpers>true</IncludeDiagnosticSourceInstrumentationHelpers>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\Guard.cs" Link="Includes\Guard.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry\OpenTelemetry.csproj" />
</ItemGroup>

View File

@ -7,6 +7,10 @@
<IncludeDiagnosticSourceInstrumentationHelpers>true</IncludeDiagnosticSourceInstrumentationHelpers>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\Guard.cs" Link="Includes\Guard.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Api\OpenTelemetry.Api.csproj" />
</ItemGroup>

View File

@ -9,6 +9,7 @@
<ItemGroup>
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\ServiceProviderExtensions.cs" Link="Includes\ServiceProviderExtensions.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\Guard.cs" Link="Includes\Guard.cs" />
</ItemGroup>
<ItemGroup>

View File

@ -18,4 +18,8 @@
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Api\OpenTelemetry.Api.csproj" />
</ItemGroup>
<ItemGroup>
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\Guard.cs" Link="Includes\Guard.cs" />
</ItemGroup>
</Project>

View File

@ -48,7 +48,7 @@ namespace OpenTelemetry.Instrumentation
{
Guard.Null(obj, nameof(obj));
if (!this.TryFetch(obj, out T value))
if (!this.TryFetch(obj, out T value, true))
{
throw new ArgumentException($"Unable to fetch property: '{nameof(obj)}'", nameof(obj));
}
@ -61,10 +61,11 @@ namespace OpenTelemetry.Instrumentation
/// </summary>
/// <param name="obj">Object to be fetched.</param>
/// <param name="value">Fetched value.</param>
/// <param name="skipObjNullCheck">Set this to <see langword= "true"/> if we know <paramref name="obj"/> is not <see langword= "null"/>.</param>
/// <returns><see langword= "true"/> if the property was fetched.</returns>
public bool TryFetch(object obj, out T value)
public bool TryFetch(object obj, out T value, bool skipObjNullCheck = false)
{
if (obj == null)
if (!skipObjNullCheck && obj == null)
{
value = default;
return false;

View File

@ -21,10 +21,10 @@ using Xunit;
namespace OpenTelemetry.Tests.Internal
{
public static class GuardTest
public class GuardTest
{
[Fact]
public static void NullTest()
public void NullTest()
{
// Valid
Guard.Null(1);
@ -38,7 +38,7 @@ namespace OpenTelemetry.Tests.Internal
}
[Fact]
public static void NullOrEmptyTest()
public void NullOrEmptyTest()
{
// Valid
Guard.NullOrEmpty("a");
@ -53,7 +53,7 @@ namespace OpenTelemetry.Tests.Internal
}
[Fact]
public static void NullOrWhitespaceTest()
public void NullOrWhitespaceTest()
{
// Valid
Guard.NullOrWhitespace("a");
@ -70,7 +70,7 @@ namespace OpenTelemetry.Tests.Internal
}
[Fact]
public static void InvalidTimeoutTest()
public void InvalidTimeoutTest()
{
// Valid
Guard.InvalidTimeout(Timeout.Infinite);
@ -83,7 +83,7 @@ namespace OpenTelemetry.Tests.Internal
}
[Fact]
public static void RangeIntTest()
public void RangeIntTest()
{
// Valid
Guard.Range(0);
@ -101,7 +101,7 @@ namespace OpenTelemetry.Tests.Internal
}
[Fact]
public static void RangeDoubleTest()
public void RangeDoubleTest()
{
// Valid
Guard.Range(1.0, min: 1.0, max: 1.0);
@ -117,7 +117,7 @@ namespace OpenTelemetry.Tests.Internal
}
[Fact]
public static void TypeTest()
public void TypeTest()
{
// Valid
Guard.Type<int>(0);
@ -130,7 +130,7 @@ namespace OpenTelemetry.Tests.Internal
}
[Fact]
public static void ZeroTest()
public void ZeroTest()
{
// Valid
Guard.Zero(1);