Skip to content

Commit

Permalink
[Instrumentation.Wcf] Add RecordException (#2271)
Browse files Browse the repository at this point in the history
Co-authored-by: Piotr Kiełkowicz <pkiekowicz@splunk.com>
  • Loading branch information
ysolomchenko and Kielek authored Nov 15, 2024
1 parent 3c6cf48 commit 37a80ca
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ OpenTelemetry.Instrumentation.Wcf.WcfInstrumentationOptions.IncomingRequestFilte
OpenTelemetry.Instrumentation.Wcf.WcfInstrumentationOptions.IncomingRequestFilter.set -> void
OpenTelemetry.Instrumentation.Wcf.WcfInstrumentationOptions.OutgoingRequestFilter.get -> System.Func<System.ServiceModel.Channels.Message!, bool>?
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
Expand Down
4 changes: 4 additions & 0 deletions src/OpenTelemetry.Instrumentation.Wcf/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
12 changes: 12 additions & 0 deletions src/OpenTelemetry.Instrumentation.Wcf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
12 changes: 12 additions & 0 deletions src/OpenTelemetry.Instrumentation.Wcf/WcfInstrumentationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <see cref="WcfInstrumentationConstants.SoapMessageVersionTag"/> tag. Default value: False.
/// </summary>
public bool SetSoapMessageVersion { get; set; }

/// <summary>
/// Gets or sets a value indicating whether exception will be recorded
/// as an <see cref="ActivityEvent"/> or not. Default value: <see
/// langword="false"/>.
/// </summary>
/// <remarks>
/// <para>For specification details see: <see
/// href="https://github.com/open-telemetry/semantic-conventions/blob/main/docs/exceptions/exceptions-spans.md"
/// />.</para>
/// </remarks>
public bool RecordException { get; set; }
}

0 comments on commit 37a80ca

Please sign in to comment.