From 37a80ca93d2e9c709e71f6c30c82000ceeb9c346 Mon Sep 17 00:00:00 2001 From: Yevhenii Solomchenko Date: Fri, 15 Nov 2024 13:46:59 +0100 Subject: [PATCH] [Instrumentation.Wcf] Add RecordException (#2271) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Piotr Kiełkowicz --- .../.publicApi/PublicAPI.Unshipped.txt | 2 ++ src/OpenTelemetry.Instrumentation.Wcf/CHANGELOG.md | 4 ++++ .../Implementation/ClientChannelInstrumentation.cs | 9 ++++++--- .../Implementation/InstrumentedDuplexChannel.cs | 8 ++++---- .../Implementation/InstrumentedRequestChannel.cs | 12 ++++++------ src/OpenTelemetry.Instrumentation.Wcf/README.md | 12 ++++++++++++ .../WcfInstrumentationOptions.cs | 12 ++++++++++++ 7 files changed, 46 insertions(+), 13 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.Wcf/.publicApi/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Instrumentation.Wcf/.publicApi/PublicAPI.Unshipped.txt index 2d11286dca..158186f0f4 100644 --- a/src/OpenTelemetry.Instrumentation.Wcf/.publicApi/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Instrumentation.Wcf/.publicApi/PublicAPI.Unshipped.txt @@ -20,6 +20,8 @@ OpenTelemetry.Instrumentation.Wcf.WcfInstrumentationOptions.IncomingRequestFilte OpenTelemetry.Instrumentation.Wcf.WcfInstrumentationOptions.IncomingRequestFilter.set -> void OpenTelemetry.Instrumentation.Wcf.WcfInstrumentationOptions.OutgoingRequestFilter.get -> System.Func? OpenTelemetry.Instrumentation.Wcf.WcfInstrumentationOptions.OutgoingRequestFilter.set -> void +OpenTelemetry.Instrumentation.Wcf.WcfInstrumentationOptions.RecordException.get -> bool +OpenTelemetry.Instrumentation.Wcf.WcfInstrumentationOptions.RecordException.set -> void OpenTelemetry.Instrumentation.Wcf.WcfInstrumentationOptions.SetSoapMessageVersion.get -> bool OpenTelemetry.Instrumentation.Wcf.WcfInstrumentationOptions.SetSoapMessageVersion.set -> void OpenTelemetry.Instrumentation.Wcf.WcfInstrumentationOptions.SuppressDownstreamInstrumentation.get -> bool diff --git a/src/OpenTelemetry.Instrumentation.Wcf/CHANGELOG.md b/src/OpenTelemetry.Instrumentation.Wcf/CHANGELOG.md index e5e4f3a2b7..00310a2e3c 100644 --- a/src/OpenTelemetry.Instrumentation.Wcf/CHANGELOG.md +++ b/src/OpenTelemetry.Instrumentation.Wcf/CHANGELOG.md @@ -8,6 +8,10 @@ * Updated OpenTelemetry core component version(s) to `1.10.0`. ([#2317](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/2317)) +* Added a `RecordException` property to specify if exceptions should be + recorded (defaults to `false`). This is only supported by client instrumentation. + ([#2271](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/2271)) + ## 1.0.0-rc.18 Released 2024-Oct-28 diff --git a/src/OpenTelemetry.Instrumentation.Wcf/Implementation/ClientChannelInstrumentation.cs b/src/OpenTelemetry.Instrumentation.Wcf/Implementation/ClientChannelInstrumentation.cs index f6d4b58058..c6e56f2b34 100644 --- a/src/OpenTelemetry.Instrumentation.Wcf/Implementation/ClientChannelInstrumentation.cs +++ b/src/OpenTelemetry.Instrumentation.Wcf/Implementation/ClientChannelInstrumentation.cs @@ -82,12 +82,10 @@ public static RequestTelemetryState BeforeSendRequest(Message request, Uri? remo }; } - public static void AfterRequestCompleted(Message? reply, RequestTelemetryState? state) + public static void AfterRequestCompleted(Message? reply, RequestTelemetryState? state, Exception? exception = null) { Guard.ThrowIfNull(state); - state.SuppressionScope?.Dispose(); - if (state.Activity is Activity activity) { if (activity.IsAllDataRequested) @@ -97,6 +95,11 @@ public static void AfterRequestCompleted(Message? reply, RequestTelemetryState? #pragma warning disable CS0618 // Type or member is obsolete activity.SetStatus(Status.Error); #pragma warning restore CS0618 // Type or member is obsolete + + if (WcfInstrumentationActivitySource.Options!.RecordException && exception != null) + { + activity.AddException(exception); + } } if (reply != null) diff --git a/src/OpenTelemetry.Instrumentation.Wcf/Implementation/InstrumentedDuplexChannel.cs b/src/OpenTelemetry.Instrumentation.Wcf/Implementation/InstrumentedDuplexChannel.cs index d58157d836..3f1335f066 100644 --- a/src/OpenTelemetry.Instrumentation.Wcf/Implementation/InstrumentedDuplexChannel.cs +++ b/src/OpenTelemetry.Instrumentation.Wcf/Implementation/InstrumentedDuplexChannel.cs @@ -64,9 +64,9 @@ public void EndSend(IAsyncResult result) { this.Inner.EndSend(asyncResult.Inner); } - catch (Exception) + catch (Exception ex) { - ClientChannelInstrumentation.AfterRequestCompleted(null, asyncResult.TelemetryState); + ClientChannelInstrumentation.AfterRequestCompleted(null, asyncResult.TelemetryState, ex); throw; } } @@ -196,9 +196,9 @@ void ExecuteInChildContext(object? unused) { ExecutionContext.Run(executionContext, ExecuteInChildContext, null); } - catch (Exception) + catch (Exception ex) { - ClientChannelInstrumentation.AfterRequestCompleted(null, telemetryState); + ClientChannelInstrumentation.AfterRequestCompleted(null, telemetryState, ex); throw; } } diff --git a/src/OpenTelemetry.Instrumentation.Wcf/Implementation/InstrumentedRequestChannel.cs b/src/OpenTelemetry.Instrumentation.Wcf/Implementation/InstrumentedRequestChannel.cs index 30c245cc91..7d875f224a 100644 --- a/src/OpenTelemetry.Instrumentation.Wcf/Implementation/InstrumentedRequestChannel.cs +++ b/src/OpenTelemetry.Instrumentation.Wcf/Implementation/InstrumentedRequestChannel.cs @@ -30,9 +30,9 @@ Message IRequestChannel.Request(Message message) { reply = this.Inner.Request(message); } - catch (Exception) + catch (Exception ex) { - ClientChannelInstrumentation.AfterRequestCompleted(null, telemetryState); + ClientChannelInstrumentation.AfterRequestCompleted(null, telemetryState, ex); throw; } @@ -50,9 +50,9 @@ Message IRequestChannel.Request(Message message, TimeSpan timeout) { reply = this.Inner.Request(message, timeout); } - catch (Exception) + catch (Exception ex) { - ClientChannelInstrumentation.AfterRequestCompleted(null, telemetryState); + ClientChannelInstrumentation.AfterRequestCompleted(null, telemetryState, ex); throw; } @@ -86,9 +86,9 @@ Message IRequestChannel.EndRequest(IAsyncResult result) { reply = this.Inner.EndRequest(asyncResult.Inner); } - catch (Exception) + catch (Exception ex) { - ClientChannelInstrumentation.AfterRequestCompleted(null, asyncResult.TelemetryState); + ClientChannelInstrumentation.AfterRequestCompleted(null, asyncResult.TelemetryState, ex); throw; } diff --git a/src/OpenTelemetry.Instrumentation.Wcf/README.md b/src/OpenTelemetry.Instrumentation.Wcf/README.md index 773a07ffcd..3a924ed9c5 100644 --- a/src/OpenTelemetry.Instrumentation.Wcf/README.md +++ b/src/OpenTelemetry.Instrumentation.Wcf/README.md @@ -224,6 +224,18 @@ on the service contracts you want to instrument: } ``` +## Advanced configuration + +This instrumentation can be configured to change the default behavior by using +`WcfInstrumentationOptions`. + +### RecordException + +This instrumentation automatically sets Activity Status to Error if an unhandled +exception is thrown. Additionally, `RecordException` feature may be turned on, +to store the exception to the Activity itself as ActivityEvent. `RecordException` +is available only on the client side. + ## References * [OpenTelemetry Project](https://opentelemetry.io/) diff --git a/src/OpenTelemetry.Instrumentation.Wcf/WcfInstrumentationOptions.cs b/src/OpenTelemetry.Instrumentation.Wcf/WcfInstrumentationOptions.cs index 2dd8484897..b876b873f4 100644 --- a/src/OpenTelemetry.Instrumentation.Wcf/WcfInstrumentationOptions.cs +++ b/src/OpenTelemetry.Instrumentation.Wcf/WcfInstrumentationOptions.cs @@ -48,4 +48,16 @@ public class WcfInstrumentationOptions /// Gets or sets a value indicating whether or not the SOAP message version should be added as the tag. Default value: False. /// public bool SetSoapMessageVersion { get; set; } + + /// + /// Gets or sets a value indicating whether exception will be recorded + /// as an or not. Default value: . + /// + /// + /// For specification details see: . + /// + public bool RecordException { get; set; } }