Skip to content
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

[repo/shared] Prepare to .NET9 #2267

Merged
merged 13 commits into from
Oct 29, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="$(RepoRoot)\src\Shared\ActivityInstrumentationHelper.cs" Link="Includes\ActivityInstrumentationHelper.cs" />
<Compile Include="$(RepoRoot)\src\Shared\AssemblyVersionExtensions.cs" Link="Includes\AssemblyVersionExtensions.cs" />
<Compile Include="$(RepoRoot)\src\Shared\DatabaseSemanticConventionHelper.cs" Link="Includes\DatabaseSemanticConventionHelper.cs" />
<Compile Include="$(RepoRoot)\src\Shared\DiagnosticSourceListener.cs" Link="Includes\DiagnosticSourceListener.cs" />
Expand Down
10 changes: 10 additions & 0 deletions src/Shared/ActivityInstrumentationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,23 @@ internal static class ActivityInstrumentationHelper

private static Action<Activity, ActivitySource> CreateActivitySourceSetter()
{
#if NET
return typeof(Activity).GetProperty("Source")!
.SetMethod!.CreateDelegate<Action<Activity, ActivitySource>>();
#else
return (Action<Activity, ActivitySource>)typeof(Activity).GetProperty("Source")!
.SetMethod!.CreateDelegate(typeof(Action<Activity, ActivitySource>));
#endif
}

private static Action<Activity, ActivityKind> CreateActivityKindSetter()
{
#if NET
return typeof(Activity).GetProperty("Kind")!
.SetMethod!.CreateDelegate<Action<Activity, ActivityKind>>();
#else
return (Action<Activity, ActivityKind>)typeof(Activity).GetProperty("Kind")!
.SetMethod!.CreateDelegate(typeof(Action<Activity, ActivityKind>));
#endif
}
}
4 changes: 2 additions & 2 deletions src/Shared/DatabaseSemanticConventionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ namespace OpenTelemetry.Internal;
internal static class DatabaseSemanticConventionHelper
{
internal const string SemanticConventionOptInKeyName = "OTEL_SEMCONV_STABILITY_OPT_IN";
internal static readonly char[] Separator = new[] { ',', ' ' };
internal static readonly char[] Separator = [',', ' '];

[Flags]
public enum DatabaseSemanticConvention
internal enum DatabaseSemanticConvention
{
/// <summary>
/// Instructs an instrumentation library to emit the old experimental Database attributes.
Expand Down
25 changes: 18 additions & 7 deletions src/Shared/GrpcTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,7 @@ public static bool TryGetGrpcStatusCodeFromActivity(Activity activity, out int s
{
statusCode = -1;
var grpcStatusCodeTag = activity.GetTagValue(GrpcStatusCodeTagName);
if (grpcStatusCodeTag == null)
{
return false;
}

return int.TryParse(grpcStatusCodeTag as string, out statusCode);
return grpcStatusCodeTag != null && int.TryParse(grpcStatusCodeTag as string, out statusCode);
}

public static bool TryParseRpcServiceAndRpcMethod(string grpcMethod, out string rpcService, out string rpcMethod)
Expand Down Expand Up @@ -66,9 +61,25 @@ public static ActivityStatusCode ResolveSpanStatusForGrpcStatusCode(int statusCo

if (typeof(GrpcStatusCanonicalCode).IsEnumDefined(statusCode))
{
status = ((GrpcStatusCanonicalCode)statusCode) switch
status = (GrpcStatusCanonicalCode)statusCode switch
{
GrpcStatusCanonicalCode.Ok => ActivityStatusCode.Unset,
GrpcStatusCanonicalCode.Cancelled => ActivityStatusCode.Error,
GrpcStatusCanonicalCode.Unknown => ActivityStatusCode.Error,
GrpcStatusCanonicalCode.InvalidArgument => ActivityStatusCode.Error,
GrpcStatusCanonicalCode.DeadlineExceeded => ActivityStatusCode.Error,
GrpcStatusCanonicalCode.NotFound => ActivityStatusCode.Error,
GrpcStatusCanonicalCode.AlreadyExists => ActivityStatusCode.Error,
GrpcStatusCanonicalCode.PermissionDenied => ActivityStatusCode.Error,
GrpcStatusCanonicalCode.ResourceExhausted => ActivityStatusCode.Error,
GrpcStatusCanonicalCode.FailedPrecondition => ActivityStatusCode.Error,
GrpcStatusCanonicalCode.Aborted => ActivityStatusCode.Error,
GrpcStatusCanonicalCode.OutOfRange => ActivityStatusCode.Error,
GrpcStatusCanonicalCode.Unimplemented => ActivityStatusCode.Error,
GrpcStatusCanonicalCode.Internal => ActivityStatusCode.Error,
GrpcStatusCanonicalCode.Unavailable => ActivityStatusCode.Error,
GrpcStatusCanonicalCode.DataLoss => ActivityStatusCode.Error,
GrpcStatusCanonicalCode.Unauthenticated => ActivityStatusCode.Error,
_ => ActivityStatusCode.Error,
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/Shared/NullableAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ internal sealed class MemberNotNullAttribute : Attribute
/// <param name="member">
/// The field or property member that is promised to be not-null.
/// </param>
public MemberNotNullAttribute(string member) => Members = new[] { member };
public MemberNotNullAttribute(string member) => Members = [member];

/// <summary>Initializes the attribute with the list of field and property members.</summary>
/// <param name="members">
Expand Down
18 changes: 6 additions & 12 deletions src/Shared/PropertyFetcher.AOT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,7 @@ public bool TryFetch(
out T? value)
{
var innerFetcher = this.innerFetcher;
if (innerFetcher is null)
{
return TryFetchRare(obj, this.propertyName, ref this.innerFetcher, out value);
}

return innerFetcher.TryFetch(obj, out value);
return innerFetcher is null ? TryFetchRare(obj, this.propertyName, ref this.innerFetcher, out value) : innerFetcher.TryFetch(obj, out value);
}

#if NET
Expand Down Expand Up @@ -189,7 +184,11 @@ private sealed class PropertyFetchInstantiated<TDeclaredObject> : PropertyFetch
public PropertyFetchInstantiated(PropertyInfo property)
{
this.propertyName = property.Name;
#if NET
this.propertyFetch = property.GetMethod!.CreateDelegate<Func<TDeclaredObject, T>>();
#else
this.propertyFetch = (Func<TDeclaredObject, T>)property.GetMethod!.CreateDelegate(typeof(Func<TDeclaredObject, T>));
#endif
}

public override int NumberOfInnerFetchers => this.innerFetcher == null
Expand All @@ -210,12 +209,7 @@ public override bool TryFetch(
}

var innerFetcher = this.innerFetcher;
if (innerFetcher is null)
{
return TryFetchRare(obj, this.propertyName, ref this.innerFetcher, out value);
}

return innerFetcher.TryFetch(obj, out value);
return innerFetcher is null ? TryFetchRare(obj, this.propertyName, ref this.innerFetcher, out value) : innerFetcher.TryFetch(obj, out value);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Shared/RequestDataHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal sealed class RequestDataHelper
// https://github.com/open-telemetry/semantic-conventions/blob/v1.23.0/docs/http/http-spans.md#common-attributes
private const string OtherHttpMethod = "_OTHER";

private static readonly char[] SplitChars = new[] { ',' };
private static readonly char[] SplitChars = [','];

#if NET
private readonly FrozenDictionary<string, string> knownHttpMethods;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,28 @@ namespace OpenTelemetry.Internal.Tests;

public class DatabaseSemanticConventionHelperTests
{
public static IEnumerable<object[]> TestCases => new List<object[]>
{
new object[] { null!, DatabaseSemanticConvention.Old },
new object[] { string.Empty, DatabaseSemanticConvention.Old },
new object[] { " ", DatabaseSemanticConvention.Old },
new object[] { "junk", DatabaseSemanticConvention.Old },
new object[] { "none", DatabaseSemanticConvention.Old },
new object[] { "NONE", DatabaseSemanticConvention.Old },
new object[] { "database", DatabaseSemanticConvention.New },
new object[] { "DATABASE", DatabaseSemanticConvention.New },
new object[] { "database/dup", DatabaseSemanticConvention.Dupe },
new object[] { "DATABASE/DUP", DatabaseSemanticConvention.Dupe },
new object[] { "junk,,junk", DatabaseSemanticConvention.Old },
new object[] { "junk,JUNK", DatabaseSemanticConvention.Old },
new object[] { "junk1,junk2", DatabaseSemanticConvention.Old },
new object[] { "junk,database", DatabaseSemanticConvention.New },
new object[] { "junk,database , database ,junk", DatabaseSemanticConvention.New },
new object[] { "junk,database/dup", DatabaseSemanticConvention.Dupe },
new object[] { "junk, database/dup ", DatabaseSemanticConvention.Dupe },
new object[] { "database/dup,database", DatabaseSemanticConvention.Dupe },
new object[] { "database,database/dup", DatabaseSemanticConvention.Dupe },
};
public static IEnumerable<object[]> TestCases =>
[
[null!, DatabaseSemanticConvention.Old],
[string.Empty, DatabaseSemanticConvention.Old],
[" ", DatabaseSemanticConvention.Old],
["junk", DatabaseSemanticConvention.Old],
["none", DatabaseSemanticConvention.Old],
["NONE", DatabaseSemanticConvention.Old],
["database", DatabaseSemanticConvention.New],
["DATABASE", DatabaseSemanticConvention.New],
["database/dup", DatabaseSemanticConvention.Dupe],
["DATABASE/DUP", DatabaseSemanticConvention.Dupe],
["junk,,junk", DatabaseSemanticConvention.Old],
["junk,JUNK", DatabaseSemanticConvention.Old],
["junk1,junk2", DatabaseSemanticConvention.Old],
["junk,database", DatabaseSemanticConvention.New],
["junk,database , database ,junk", DatabaseSemanticConvention.New],
["junk,database/dup", DatabaseSemanticConvention.Dupe],
["junk, database/dup ", DatabaseSemanticConvention.Dupe],
["database/dup,database", DatabaseSemanticConvention.Dupe],
["database,database/dup", DatabaseSemanticConvention.Dupe],
];

[Fact]
public void VerifyFlags()
Expand All @@ -56,7 +56,11 @@ public void VerifyGetSemanticConventionOptIn_UsingEnvironmentVariable(string inp
{
Environment.SetEnvironmentVariable(SemanticConventionOptInKeyName, input);

#if NET
var expected = Enum.Parse<DatabaseSemanticConvention>(expectedValue);
#else
var expected = Enum.Parse(typeof(DatabaseSemanticConvention), expectedValue);
#endif
Assert.Equal(expected, GetSemanticConventionOptIn(new ConfigurationBuilder().AddEnvironmentVariables().Build()));
}
finally
Expand All @@ -73,7 +77,11 @@ public void VerifyGetSemanticConventionOptIn_UsingIConfiguration(string input, s
.AddInMemoryCollection(new Dictionary<string, string?> { [SemanticConventionOptInKeyName] = input })
.Build();

#if NET
var expected = Enum.Parse<DatabaseSemanticConvention>(expectedValue);
#else
var expected = Enum.Parse(typeof(DatabaseSemanticConvention), expectedValue);
#endif
Assert.Equal(expected, GetSemanticConventionOptIn(configuration));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ namespace OpenTelemetry.Internal.Tests;
public class RequestDataHelperTests : IDisposable
{
public static IEnumerable<object[]> MappingVersionProtocolToVersionData =>
new List<object[]>
{
new object[] { new Version(1, 0), "1.0" },
new object[] { new Version(1, 1), "1.1" },
new object[] { new Version(2, 0), "2" },
new object[] { new Version(3, 0), "3" },
new object[] { new Version(7, 6, 5), "7.6.5" },
};
[
[new Version(1, 0), "1.0"],
[new Version(1, 1), "1.1"],
[new Version(2, 0), "2"],
[new Version(3, 0), "3"],
[new Version(7, 6, 5), "7.6.5"],
];

[Theory]
[InlineData("GET", "GET")]
Expand Down
Loading