Skip to content

Commit

Permalink
WCF: Misc cleanup (#163)
Browse files Browse the repository at this point in the history
* WCF cleanup.

* CHANGELOG update and a bit more cleanup.

* Removed Activity.Current reset logic.
  • Loading branch information
CodeBlanch authored Oct 22, 2021
1 parent 91a1269 commit 136ec1e
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 28 deletions.
5 changes: 5 additions & 0 deletions src/OpenTelemetry.Contrib.Instrumentation.Wcf/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 1.0.0-rc4

* Removed `Propagator` property on `WcfInstrumentationOptions`
([#163](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/163))

## 1.0.0-rc3

* Added `TelemetryServiceBehavior`. **Breaking change** (config update
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace OpenTelemetry.Contrib.Instrumentation.Wcf
{
internal class ActionMetadata
internal sealed class ActionMetadata
{
public string ContractName { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
namespace OpenTelemetry.Contrib.Instrumentation.Wcf.Implementation
{
[EventSource(Name = "OpenTelemetry-Instrumentation-Wcf")]
internal class WcfInstrumentationEventSource : EventSource
internal sealed class WcfInstrumentationEventSource : EventSource
{
public static readonly WcfInstrumentationEventSource Log = new WcfInstrumentationEventSource();

Expand All @@ -36,9 +36,9 @@ public void RequestFilterException(Exception ex)
}

[Event(EventIds.RequestIsFilteredOut, Message = "Request is filtered out.", Level = EventLevel.Verbose)]
public void RequestIsFilteredOut(string eventName)
public void RequestIsFilteredOut()
{
this.WriteEvent(EventIds.RequestIsFilteredOut, eventName);
this.WriteEvent(EventIds.RequestIsFilteredOut);
}

[Event(EventIds.RequestFilterException, Message = "InstrumentationFilter threw exception. Request will not be collected. Exception {0}.", Level = EventLevel.Error)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
if (WcfInstrumentationActivitySource.Options == null || WcfInstrumentationActivitySource.Options.OutgoingRequestFilter?.Invoke(request) == false)
{
WcfInstrumentationEventSource.Log.RequestIsFilteredOut(request.Headers.Action);
WcfInstrumentationEventSource.Log.RequestIsFilteredOut();
return new State
{
SuppressionScope = this.SuppressDownstreamInstrumentation(),
Expand All @@ -71,7 +71,7 @@ public object BeforeSendRequest(ref Message request, IClientChannel channel)
string action = request.Headers.Action;
activity.DisplayName = action;

WcfInstrumentationActivitySource.Options.Propagator.Inject(
Propagators.DefaultTextMapPropagator.Inject(
new PropagationContext(activity.Context, Baggage.Current),
request,
WcfInstrumentationActivitySource.MessageHeaderValueSetter);
Expand Down Expand Up @@ -136,8 +136,7 @@ public void AfterReceiveReply(ref Message reply, object correlationState)

state.SuppressionScope?.Dispose();

Activity activity = state.Activity;
if (activity != null)
if (state.Activity is Activity activity)
{
if (activity.IsAllDataRequested)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace OpenTelemetry.Contrib.Instrumentation.Wcf
/// </summary>
#endif
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface, Inherited = false, AllowMultiple = false)]
public class TelemetryContractBehaviorAttribute : Attribute, IContractBehavior
public sealed class TelemetryContractBehaviorAttribute : Attribute, IContractBehavior
{
/// <inheritdoc />
public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Contrib.Instrumentation.Wcf.Implementation;
using OpenTelemetry.Trace;

Expand All @@ -45,7 +46,7 @@ public object AfterReceiveRequest(ref Message request, IClientChannel channel, I
{
if (WcfInstrumentationActivitySource.Options == null || WcfInstrumentationActivitySource.Options.IncomingRequestFilter?.Invoke(request) == false)
{
WcfInstrumentationEventSource.Log.RequestIsFilteredOut(request.Headers.Action);
WcfInstrumentationEventSource.Log.RequestIsFilteredOut();
return null;
}
}
Expand All @@ -55,7 +56,8 @@ public object AfterReceiveRequest(ref Message request, IClientChannel channel, I
return null;
}

var ctx = WcfInstrumentationActivitySource.Options.Propagator.Extract(default, request, WcfInstrumentationActivitySource.MessageHeaderValuesGetter);
var textMapPropagator = Propagators.DefaultTextMapPropagator;
var ctx = textMapPropagator.Extract(default, request, WcfInstrumentationActivitySource.MessageHeaderValuesGetter);

Activity activity = WcfInstrumentationActivitySource.ActivitySource.StartActivity(
WcfInstrumentationActivitySource.IncomingRequestActivityName,
Expand Down Expand Up @@ -106,11 +108,11 @@ public object AfterReceiveRequest(ref Message request, IClientChannel channel, I
WcfInstrumentationEventSource.Log.EnrichmentException(ex);
}
}
}

if (ctx.Baggage != default)
{
Baggage.Current = ctx.Baggage;
if (!(textMapPropagator is TraceContextPropagator))
{
Baggage.Current = ctx.Baggage;
}
}

return activity;
Expand All @@ -119,8 +121,7 @@ public object AfterReceiveRequest(ref Message request, IClientChannel channel, I
/// <inheritdoc/>
public void BeforeSendReply(ref Message reply, object correlationState)
{
Activity activity = (Activity)correlationState;
if (activity != null)
if (correlationState is Activity activity)
{
if (activity.IsAllDataRequested)
{
Expand All @@ -141,6 +142,11 @@ public void BeforeSendReply(ref Message reply, object correlationState)
}

activity.Stop();

if (!(Propagators.DefaultTextMapPropagator is TraceContextPropagator))
{
Baggage.Current = default;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace OpenTelemetry.Contrib.Instrumentation.Wcf
/// <summary>
/// Constants used for event names when enriching an activity.
/// </summary>
public class WcfEnrichEventNames
public static class WcfEnrichEventNames
{
#if NETFRAMEWORK
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using System;
using System.Diagnostics;
using System.ServiceModel.Channels;
using OpenTelemetry.Context.Propagation;

namespace OpenTelemetry.Contrib.Instrumentation.Wcf
{
Expand All @@ -26,15 +25,6 @@ namespace OpenTelemetry.Contrib.Instrumentation.Wcf
/// </summary>
public class WcfInstrumentationOptions
{
/// <summary>
/// Gets or sets <see cref="TextMapPropagator"/> for context propagation. Default value: <see cref="CompositeTextMapPropagator"/> with <see cref="TraceContextPropagator"/> &amp; <see cref="BaggagePropagator"/>.
/// </summary>
public TextMapPropagator Propagator { get; set; } = new CompositeTextMapPropagator(new TextMapPropagator[]
{
new TraceContextPropagator(),
new BaggagePropagator(),
});

/// <summary>
/// Gets or sets an action to enrich an Activity.
/// </summary>
Expand Down

0 comments on commit 136ec1e

Please sign in to comment.