Skip to content

Commit 37a80ca

Browse files
ysolomchenkoKielek
andauthored
[Instrumentation.Wcf] Add RecordException (#2271)
Co-authored-by: Piotr Kiełkowicz <[email protected]>
1 parent 3c6cf48 commit 37a80ca

File tree

7 files changed

+46
-13
lines changed

7 files changed

+46
-13
lines changed

src/OpenTelemetry.Instrumentation.Wcf/.publicApi/PublicAPI.Unshipped.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ OpenTelemetry.Instrumentation.Wcf.WcfInstrumentationOptions.IncomingRequestFilte
2020
OpenTelemetry.Instrumentation.Wcf.WcfInstrumentationOptions.IncomingRequestFilter.set -> void
2121
OpenTelemetry.Instrumentation.Wcf.WcfInstrumentationOptions.OutgoingRequestFilter.get -> System.Func<System.ServiceModel.Channels.Message!, bool>?
2222
OpenTelemetry.Instrumentation.Wcf.WcfInstrumentationOptions.OutgoingRequestFilter.set -> void
23+
OpenTelemetry.Instrumentation.Wcf.WcfInstrumentationOptions.RecordException.get -> bool
24+
OpenTelemetry.Instrumentation.Wcf.WcfInstrumentationOptions.RecordException.set -> void
2325
OpenTelemetry.Instrumentation.Wcf.WcfInstrumentationOptions.SetSoapMessageVersion.get -> bool
2426
OpenTelemetry.Instrumentation.Wcf.WcfInstrumentationOptions.SetSoapMessageVersion.set -> void
2527
OpenTelemetry.Instrumentation.Wcf.WcfInstrumentationOptions.SuppressDownstreamInstrumentation.get -> bool

src/OpenTelemetry.Instrumentation.Wcf/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
* Updated OpenTelemetry core component version(s) to `1.10.0`.
99
([#2317](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/2317))
1010

11+
* Added a `RecordException` property to specify if exceptions should be
12+
recorded (defaults to `false`). This is only supported by client instrumentation.
13+
([#2271](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/2271))
14+
1115
## 1.0.0-rc.18
1216

1317
Released 2024-Oct-28

src/OpenTelemetry.Instrumentation.Wcf/Implementation/ClientChannelInstrumentation.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,10 @@ public static RequestTelemetryState BeforeSendRequest(Message request, Uri? remo
8282
};
8383
}
8484

85-
public static void AfterRequestCompleted(Message? reply, RequestTelemetryState? state)
85+
public static void AfterRequestCompleted(Message? reply, RequestTelemetryState? state, Exception? exception = null)
8686
{
8787
Guard.ThrowIfNull(state);
88-
8988
state.SuppressionScope?.Dispose();
90-
9189
if (state.Activity is Activity activity)
9290
{
9391
if (activity.IsAllDataRequested)
@@ -97,6 +95,11 @@ public static void AfterRequestCompleted(Message? reply, RequestTelemetryState?
9795
#pragma warning disable CS0618 // Type or member is obsolete
9896
activity.SetStatus(Status.Error);
9997
#pragma warning restore CS0618 // Type or member is obsolete
98+
99+
if (WcfInstrumentationActivitySource.Options!.RecordException && exception != null)
100+
{
101+
activity.AddException(exception);
102+
}
100103
}
101104

102105
if (reply != null)

src/OpenTelemetry.Instrumentation.Wcf/Implementation/InstrumentedDuplexChannel.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ public void EndSend(IAsyncResult result)
6464
{
6565
this.Inner.EndSend(asyncResult.Inner);
6666
}
67-
catch (Exception)
67+
catch (Exception ex)
6868
{
69-
ClientChannelInstrumentation.AfterRequestCompleted(null, asyncResult.TelemetryState);
69+
ClientChannelInstrumentation.AfterRequestCompleted(null, asyncResult.TelemetryState, ex);
7070
throw;
7171
}
7272
}
@@ -196,9 +196,9 @@ void ExecuteInChildContext(object? unused)
196196
{
197197
ExecutionContext.Run(executionContext, ExecuteInChildContext, null);
198198
}
199-
catch (Exception)
199+
catch (Exception ex)
200200
{
201-
ClientChannelInstrumentation.AfterRequestCompleted(null, telemetryState);
201+
ClientChannelInstrumentation.AfterRequestCompleted(null, telemetryState, ex);
202202
throw;
203203
}
204204
}

src/OpenTelemetry.Instrumentation.Wcf/Implementation/InstrumentedRequestChannel.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ Message IRequestChannel.Request(Message message)
3030
{
3131
reply = this.Inner.Request(message);
3232
}
33-
catch (Exception)
33+
catch (Exception ex)
3434
{
35-
ClientChannelInstrumentation.AfterRequestCompleted(null, telemetryState);
35+
ClientChannelInstrumentation.AfterRequestCompleted(null, telemetryState, ex);
3636
throw;
3737
}
3838

@@ -50,9 +50,9 @@ Message IRequestChannel.Request(Message message, TimeSpan timeout)
5050
{
5151
reply = this.Inner.Request(message, timeout);
5252
}
53-
catch (Exception)
53+
catch (Exception ex)
5454
{
55-
ClientChannelInstrumentation.AfterRequestCompleted(null, telemetryState);
55+
ClientChannelInstrumentation.AfterRequestCompleted(null, telemetryState, ex);
5656
throw;
5757
}
5858

@@ -86,9 +86,9 @@ Message IRequestChannel.EndRequest(IAsyncResult result)
8686
{
8787
reply = this.Inner.EndRequest(asyncResult.Inner);
8888
}
89-
catch (Exception)
89+
catch (Exception ex)
9090
{
91-
ClientChannelInstrumentation.AfterRequestCompleted(null, asyncResult.TelemetryState);
91+
ClientChannelInstrumentation.AfterRequestCompleted(null, asyncResult.TelemetryState, ex);
9292
throw;
9393
}
9494

src/OpenTelemetry.Instrumentation.Wcf/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,18 @@ on the service contracts you want to instrument:
224224
}
225225
```
226226

227+
## Advanced configuration
228+
229+
This instrumentation can be configured to change the default behavior by using
230+
`WcfInstrumentationOptions`.
231+
232+
### RecordException
233+
234+
This instrumentation automatically sets Activity Status to Error if an unhandled
235+
exception is thrown. Additionally, `RecordException` feature may be turned on,
236+
to store the exception to the Activity itself as ActivityEvent. `RecordException`
237+
is available only on the client side.
238+
227239
## References
228240

229241
* [OpenTelemetry Project](https://opentelemetry.io/)

src/OpenTelemetry.Instrumentation.Wcf/WcfInstrumentationOptions.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,16 @@ public class WcfInstrumentationOptions
4848
/// Gets or sets a value indicating whether or not the SOAP message version should be added as the <see cref="WcfInstrumentationConstants.SoapMessageVersionTag"/> tag. Default value: False.
4949
/// </summary>
5050
public bool SetSoapMessageVersion { get; set; }
51+
52+
/// <summary>
53+
/// Gets or sets a value indicating whether exception will be recorded
54+
/// as an <see cref="ActivityEvent"/> or not. Default value: <see
55+
/// langword="false"/>.
56+
/// </summary>
57+
/// <remarks>
58+
/// <para>For specification details see: <see
59+
/// href="https://github.com/open-telemetry/semantic-conventions/blob/main/docs/exceptions/exceptions-spans.md"
60+
/// />.</para>
61+
/// </remarks>
62+
public bool RecordException { get; set; }
5163
}

0 commit comments

Comments
 (0)