Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WCF ServiceBehavior implementation. Resolves #142. #152

Merged
merged 7 commits into from
Sep 7, 2021
2 changes: 1 addition & 1 deletion examples/wcf/client-netframework/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<system.serviceModel>
<extensions>
<behaviorExtensions>
<add name="telemetryExtension" type="OpenTelemetry.Contrib.Instrumentation.Wcf.TelemetryBehaviourExtensionElement, OpenTelemetry.Contrib.Instrumentation.Wcf" />
<add name="telemetryExtension" type="OpenTelemetry.Contrib.Instrumentation.Wcf.TelemetryEndpointBehaviorExtensionElement, OpenTelemetry.Contrib.Instrumentation.Wcf" />
</behaviorExtensions>
</extensions>
<behaviors>
Expand Down
2 changes: 1 addition & 1 deletion examples/wcf/server-netframework/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<system.serviceModel>
<extensions>
<behaviorExtensions>
<add name="telemetryExtension" type="OpenTelemetry.Contrib.Instrumentation.Wcf.TelemetryBehaviourExtensionElement, OpenTelemetry.Contrib.Instrumentation.Wcf" />
<add name="telemetryExtension" type="OpenTelemetry.Contrib.Instrumentation.Wcf.TelemetryEndpointBehaviorExtensionElement, OpenTelemetry.Contrib.Instrumentation.Wcf" />
</behaviorExtensions>
</extensions>
<behaviors>
Expand Down
11 changes: 9 additions & 2 deletions src/OpenTelemetry.Contrib.Instrumentation.Wcf/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@

## Unreleased

* Added enricher for WCF activity
([#126])(<https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/126>)
* Added `TelemetryServiceBehavior`. **Breaking change** (config update
required): Renamed `TelemetryBehaviourExtensionElement` ->
`TelemetryEndpointBehaviorExtensionElement`
([#152](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/152))

## 1.0.0-rc2

* Updated OTel SDK package version to 1.1.0-beta1
([#100](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/100))

* Added enricher for WCF activity
([#126](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/126))
51 changes: 47 additions & 4 deletions src/OpenTelemetry.Contrib.Instrumentation.Wcf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ the clients you want to instrument:
<system.serviceModel>
<extensions>
<behaviorExtensions>
<add name="telemetryExtension" type="OpenTelemetry.Contrib.Instrumentation.Wcf.TelemetryBehaviourExtensionElement, OpenTelemetry.Contrib.Instrumentation.Wcf" />
<add name="telemetryExtension" type="OpenTelemetry.Contrib.Instrumentation.Wcf.TelemetryEndpointBehaviorExtensionElement, OpenTelemetry.Contrib.Instrumentation.Wcf" />
</behaviorExtensions>
</extensions>
<behaviors>
Expand Down Expand Up @@ -103,8 +103,8 @@ Example project available in

## WCF Server Configuration (.NET Framework)

Add the `IDispatchMessageInspector` instrumentation via a behavior extension on
the services you want to instrument:
Add the `IDispatchMessageInspector` instrumentation via an endpoint behavior
extension on the service endpoints you want to instrument:

<!-- markdownlint-disable MD013 -->
```xml
Expand All @@ -113,7 +113,7 @@ the services you want to instrument:
<system.serviceModel>
<extensions>
<behaviorExtensions>
<add name="telemetryExtension" type="OpenTelemetry.Contrib.Instrumentation.Wcf.TelemetryBehaviourExtensionElement, OpenTelemetry.Contrib.Instrumentation.Wcf" />
<add name="telemetryExtension" type="OpenTelemetry.Contrib.Instrumentation.Wcf.TelemetryEndpointBehaviorExtensionElement, OpenTelemetry.Contrib.Instrumentation.Wcf" />
</behaviorExtensions>
</extensions>
<behaviors>
Expand Down Expand Up @@ -149,6 +149,49 @@ Example project available in
[examples/wcf/server-netframework](../../examples/wcf/server-netframework/)
folder.

To add the `IDispatchMessageInspector` instrumentation for all endpoints of a
service, use the service behavior extension on the services you want to
instrument:

<!-- markdownlint-disable MD013 -->
```xml
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<extensions>
<behaviorExtensions>
<add name="telemetryExtension" type="OpenTelemetry.Contrib.Instrumentation.Wcf.TelemetryServiceBehaviorExtensionElement, OpenTelemetry.Contrib.Instrumentation.Wcf" />
</behaviorExtensions>
</extensions>
<behaviors>
<serviceBehaviors>
<behavior name="telemetry">
<telemetryExtension />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<netTcpBinding>
<binding name="netTCPConfig">
<security mode="None" />
</binding>
</netTcpBinding>
</bindings>
<services>
<service name="Examples.Wcf.Server.StatusService" behaviorConfiguration="telemetry">
<endpoint binding="netTcpBinding" bindingConfiguration="netTCPConfig" contract="Examples.Wcf.IStatusServiceContract" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:9090/Telemetry" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
```
<!-- markdownlint-enable MD013 -->

## References

* [OpenTelemetry Project](https://opentelemetry.io/)
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ namespace OpenTelemetry.Contrib.Instrumentation.Wcf
{
#if NETFRAMEWORK
/// <summary>
/// An <see cref="IEndpointBehavior"/> implementation whichs add the <see
/// An <see cref="IEndpointBehavior"/> implementation which adds the <see
/// cref="TelemetryClientMessageInspector"/> to client endpoints and the
/// <see cref="TelemetryDispatchMessageInspector"/> to service endpoints.
/// </summary>
#else
/// <summary>
/// An <see cref="IEndpointBehavior"/> implementation whichs add the <see
/// An <see cref="IEndpointBehavior"/> implementation which adds the <see
/// cref="TelemetryClientMessageInspector"/> to client endpoints.
/// </summary>
#endif
Expand Down Expand Up @@ -62,6 +62,18 @@ public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRu
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
#if NETFRAMEWORK
ApplyDispatchBehaviorToEndpoint(endpointDispatcher);
#endif
}

/// <inheritdoc/>
public void Validate(ServiceEndpoint endpoint)
{
}

#if NETFRAMEWORK
internal static void ApplyDispatchBehaviorToEndpoint(EndpointDispatcher endpointDispatcher)
{
var actionMappings = new Dictionary<string, ActionMetadata>(StringComparer.OrdinalIgnoreCase);

foreach (var dispatchOperation in endpointDispatcher.DispatchRuntime.Operations)
Expand All @@ -74,12 +86,7 @@ public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher e
}

endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new TelemetryDispatchMessageInspector(actionMappings));
#endif
}

/// <inheritdoc/>
public void Validate(ServiceEndpoint endpoint)
{
}
#endif
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="TelemetryBehaviourExtensionElement.cs" company="OpenTelemetry Authors">
// <copyright file="TelemetryEndpointBehaviorExtensionElement.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -21,9 +21,9 @@
namespace OpenTelemetry.Contrib.Instrumentation.Wcf
{
/// <summary>
/// A <see cref="BehaviorExtensionElement"/> for registering <see cref="TelemetryEndpointBehavior"/> on service through configuation.
/// A <see cref="BehaviorExtensionElement"/> for registering <see cref="TelemetryEndpointBehavior"/> on a service endpoint through configuration.
/// </summary>
public class TelemetryBehaviourExtensionElement : BehaviorExtensionElement
public class TelemetryEndpointBehaviorExtensionElement : BehaviorExtensionElement
{
/// <inheritdoc/>
public override Type BehaviorType => typeof(TelemetryEndpointBehavior);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// <copyright file="TelemetryServiceBehavior.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;

namespace OpenTelemetry.Contrib.Instrumentation.Wcf
{
#if NETFRAMEWORK
/// <summary>
/// An <see cref="IServiceBehavior"/> implementation to add the
/// <see cref="TelemetryDispatchMessageInspector"/> to service operations.
/// </summary>
public class TelemetryServiceBehavior : IServiceBehavior
{
/// <inheritdoc />
public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
}

/// <inheritdoc/>
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
foreach (var channelDispatcherBase in serviceHostBase.ChannelDispatchers)
{
var channelDispatcher = (ChannelDispatcher)channelDispatcherBase;
foreach (var endpointDispatcher in channelDispatcher.Endpoints)
{
TelemetryEndpointBehavior.ApplyDispatchBehaviorToEndpoint(endpointDispatcher);
}
}
}

/// <inheritdoc/>
public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
}
}
#endif
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// <copyright file="TelemetryServiceBehaviorExtensionElement.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

#if NETFRAMEWORK
using System;
using System.ServiceModel.Configuration;

namespace OpenTelemetry.Contrib.Instrumentation.Wcf
{
/// <summary>
/// A <see cref="BehaviorExtensionElement"/> for registering <see cref="TelemetryServiceBehavior"/> on a service through configuration.
/// </summary>
public class TelemetryServiceBehaviorExtensionElement : BehaviorExtensionElement
{
/// <inheritdoc/>
public override Type BehaviorType => typeof(TelemetryServiceBehavior);

/// <inheritdoc/>
protected override object CreateBehavior()
{
return new TelemetryServiceBehavior();
}
}
}
#endif