diff --git a/src/OpenTelemetry.Instrumentation.Wcf/CHANGELOG.md b/src/OpenTelemetry.Instrumentation.Wcf/CHANGELOG.md index e3080ccd88..745314baa5 100644 --- a/src/OpenTelemetry.Instrumentation.Wcf/CHANGELOG.md +++ b/src/OpenTelemetry.Instrumentation.Wcf/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +* Add target for `net6.0` to ensure that non-vulnerable transient + dependencies are referenced by default for .NET6+. + ([#2243](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/2243)) + ## 1.0.0-rc.17 Released 2024-Jun-18 diff --git a/src/OpenTelemetry.Instrumentation.Wcf/Implementation/AsyncResultWithTelemetryState.cs b/src/OpenTelemetry.Instrumentation.Wcf/Implementation/AsyncResultWithTelemetryState.cs index 7d50404090..cf26181c7c 100644 --- a/src/OpenTelemetry.Instrumentation.Wcf/Implementation/AsyncResultWithTelemetryState.cs +++ b/src/OpenTelemetry.Instrumentation.Wcf/Implementation/AsyncResultWithTelemetryState.cs @@ -15,7 +15,7 @@ public AsyncResultWithTelemetryState(IAsyncResult inner, RequestTelemetryState t public RequestTelemetryState TelemetryState { get; } - object IAsyncResult.AsyncState => this.Inner.AsyncState; + object? IAsyncResult.AsyncState => this.Inner.AsyncState; WaitHandle IAsyncResult.AsyncWaitHandle => this.Inner.AsyncWaitHandle; diff --git a/src/OpenTelemetry.Instrumentation.Wcf/Implementation/ClientChannelInstrumentation.cs b/src/OpenTelemetry.Instrumentation.Wcf/Implementation/ClientChannelInstrumentation.cs index ecba99793d..6fca3efada 100644 --- a/src/OpenTelemetry.Instrumentation.Wcf/Implementation/ClientChannelInstrumentation.cs +++ b/src/OpenTelemetry.Instrumentation.Wcf/Implementation/ClientChannelInstrumentation.cs @@ -82,7 +82,7 @@ public static RequestTelemetryState BeforeSendRequest(Message request, Uri? remo }; } - public static void AfterRequestCompleted(Message? reply, RequestTelemetryState state) + public static void AfterRequestCompleted(Message? reply, RequestTelemetryState? state) { Guard.ThrowIfNull(state); @@ -128,7 +128,7 @@ private static ActionMetadata GetActionMetadata(Message request, string action) if (request.Properties.TryGetValue(TelemetryContextMessageProperty.Name, out object telemetryContextProperty)) { var actionMappings = (telemetryContextProperty as TelemetryContextMessageProperty)?.ActionMappings; - if (actionMappings != null && actionMappings.TryGetValue(action, out ActionMetadata metadata)) + if (actionMappings != null && actionMappings.TryGetValue(action, out var metadata)) { actionMetadata = metadata; } diff --git a/src/OpenTelemetry.Instrumentation.Wcf/Implementation/HttpRequestMessagePropertyWrapper.cs b/src/OpenTelemetry.Instrumentation.Wcf/Implementation/HttpRequestMessagePropertyWrapper.cs index 9864302715..26e62a054a 100644 --- a/src/OpenTelemetry.Instrumentation.Wcf/Implementation/HttpRequestMessagePropertyWrapper.cs +++ b/src/OpenTelemetry.Instrumentation.Wcf/Implementation/HttpRequestMessagePropertyWrapper.cs @@ -32,7 +32,7 @@ public static string Name public static object CreateNew() { AssertHttpEnabled(); - return Activator.CreateInstance(ReflectedValues!.Type); + return Activator.CreateInstance(ReflectedValues!.Type)!; } public static WebHeaderCollection GetHeaders(object httpRequestMessageProperty) @@ -49,23 +49,25 @@ public static WebHeaderCollection GetHeaders(object httpRequestMessageProperty) { type = Type.GetType( "System.ServiceModel.Channels.HttpRequestMessageProperty, System.ServiceModel, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", - true); + true)!; - var headersProp = type.GetProperty("Headers", BindingFlags.Public | BindingFlags.Instance, null, typeof(WebHeaderCollection), Array.Empty(), null); - if (headersProp == null) - { - throw new NotSupportedException("HttpRequestMessageProperty.Headers property not found"); - } + var constructor = type.GetConstructor(Type.EmptyTypes) + ?? throw new NotSupportedException("HttpRequestMessageProperty public parameterless constructor was not found"); + + var headersProp = type.GetProperty("Headers", BindingFlags.Public | BindingFlags.Instance, null, typeof(WebHeaderCollection), Array.Empty(), null) + ?? throw new NotSupportedException("HttpRequestMessageProperty.Headers property not found"); + + var nameProp = type.GetProperty("Name", BindingFlags.Public | BindingFlags.Static, null, typeof(string), Array.Empty(), null) + ?? throw new NotSupportedException("HttpRequestMessageProperty.Name property not found"); - var nameProp = type.GetProperty("Name", BindingFlags.Public | BindingFlags.Static, null, typeof(string), Array.Empty(), null); - if (nameProp == null) + if (nameProp.GetValue(null) is not string name) { - throw new NotSupportedException("HttpRequestMessageProperty.Name property not found"); + throw new NotSupportedException("HttpRequestMessageProperty.Name property was null"); } return new ReflectedInfo( type: type, - name: (string)nameProp.GetValue(null), + name: name, headersFetcher: new PropertyFetcher("Headers")); } catch (Exception ex) diff --git a/src/OpenTelemetry.Instrumentation.Wcf/Implementation/InstrumentedDuplexChannel.cs b/src/OpenTelemetry.Instrumentation.Wcf/Implementation/InstrumentedDuplexChannel.cs index ef58ab4ef8..d58157d836 100644 --- a/src/OpenTelemetry.Instrumentation.Wcf/Implementation/InstrumentedDuplexChannel.cs +++ b/src/OpenTelemetry.Instrumentation.Wcf/Implementation/InstrumentedDuplexChannel.cs @@ -178,20 +178,27 @@ private IAsyncResult SendInternal(Message message, TimeSpan timeout, Func executeSend) { RequestTelemetryState? telemetryState = null; - ContextCallback executeInChildContext = _ => + + void ExecuteInChildContext(object? unused) { telemetryState = ClientChannelInstrumentation.BeforeSendRequest(message, this.RemoteAddress?.Uri); RequestTelemetryStateTracker.PushTelemetryState(message, telemetryState, timeout, OnTelemetryStateTimedOut); executeSend(telemetryState); - }; + } + + var executionContext = ExecutionContext.Capture(); + if (executionContext == null) + { + throw new InvalidOperationException("Cannot fetch execution context"); + } try { - ExecutionContext.Run(ExecutionContext.Capture(), executeInChildContext, null); + ExecutionContext.Run(executionContext, ExecuteInChildContext, null); } catch (Exception) { - ClientChannelInstrumentation.AfterRequestCompleted(null, telemetryState!); + ClientChannelInstrumentation.AfterRequestCompleted(null, telemetryState); throw; } } diff --git a/src/OpenTelemetry.Instrumentation.Wcf/Implementation/InstrumentedRequestChannel.cs b/src/OpenTelemetry.Instrumentation.Wcf/Implementation/InstrumentedRequestChannel.cs index 7c6caa3fce..30c245cc91 100644 --- a/src/OpenTelemetry.Instrumentation.Wcf/Implementation/InstrumentedRequestChannel.cs +++ b/src/OpenTelemetry.Instrumentation.Wcf/Implementation/InstrumentedRequestChannel.cs @@ -99,14 +99,21 @@ Message IRequestChannel.EndRequest(IAsyncResult result) private IAsyncResult InternalBeginRequest(Message message, Func beginRequestDelegate, AsyncCallback callback, object? state) { IAsyncResult? result = null; - ContextCallback executeInChildContext = _ => + + void ExecuteInChildContext(object? unused) { var telemetryState = ClientChannelInstrumentation.BeforeSendRequest(message, ((IRequestChannel)this).RemoteAddress?.Uri); var asyncCallback = AsyncResultWithTelemetryState.GetAsyncCallback(callback, telemetryState); result = new AsyncResultWithTelemetryState(beginRequestDelegate(asyncCallback, state), telemetryState); - }; + } + + var executionContext = ExecutionContext.Capture(); + if (executionContext == null) + { + throw new InvalidOperationException("Cannot fetch execution context"); + } - ExecutionContext.Run(ExecutionContext.Capture(), executeInChildContext, null); + ExecutionContext.Run(executionContext, ExecuteInChildContext, null); return result!; } } diff --git a/src/OpenTelemetry.Instrumentation.Wcf/Implementation/RequestTelemetryStateTracker.cs b/src/OpenTelemetry.Instrumentation.Wcf/Implementation/RequestTelemetryStateTracker.cs index 874d7b3d36..282f0c91e0 100644 --- a/src/OpenTelemetry.Instrumentation.Wcf/Implementation/RequestTelemetryStateTracker.cs +++ b/src/OpenTelemetry.Instrumentation.Wcf/Implementation/RequestTelemetryStateTracker.cs @@ -9,8 +9,8 @@ internal static class RequestTelemetryStateTracker { private static readonly Dictionary OutstandingRequestStates = new Dictionary(); private static readonly SortedSet TimeoutQueue = new SortedSet(); - private static readonly object Sync = new object(); - private static readonly Timer Timer = new Timer(OnTimer); + private static readonly object Sync = new(); + private static readonly Timer Timer = new(OnTimer); private static long currentTimerDueAt = Timeout.Infinite; public static void PushTelemetryState(Message request, RequestTelemetryState telemetryState, TimeSpan timeout, Action timeoutCallback) @@ -50,7 +50,7 @@ public static void PushTelemetryState(Message request, RequestTelemetryState tel } } - private static void OnTimer(object state) + private static void OnTimer(object? state) { List>? timedOutEntries = null; lock (Sync) @@ -139,9 +139,13 @@ public EntryTimeoutProperties(string messageId, long expiresAt, Action - $(NetStandardMinimumSupportedVersion);$(NetFrameworkMinimumSupportedVersion) + net6.0;$(NetStandardMinimumSupportedVersion);$(NetFrameworkMinimumSupportedVersion) OpenTelemetry instrumentation for WCF. $(PackageTags);distributed-tracing;WCF Instrumentation.Wcf- @@ -23,9 +23,10 @@ - + + diff --git a/test/OpenTelemetry.Instrumentation.Wcf.Tests/OpenTelemetry.Instrumentation.Wcf.Tests.csproj b/test/OpenTelemetry.Instrumentation.Wcf.Tests/OpenTelemetry.Instrumentation.Wcf.Tests.csproj index 8343f2c6ae..64ee92e89b 100644 --- a/test/OpenTelemetry.Instrumentation.Wcf.Tests/OpenTelemetry.Instrumentation.Wcf.Tests.csproj +++ b/test/OpenTelemetry.Instrumentation.Wcf.Tests/OpenTelemetry.Instrumentation.Wcf.Tests.csproj @@ -22,8 +22,6 @@ - -