From b907e05c7426b348828d95a65aec6169b0abf49f Mon Sep 17 00:00:00 2001 From: Jaans Date: Wed, 8 Sep 2021 04:43:50 +1000 Subject: [PATCH] Add WCF ServiceBehavior implementation. Resolves #142. (#152) * Add WCF ServiceBehavior implementation. Resolves #142. * Update src/OpenTelemetry.Contrib.Instrumentation.Wcf/TelemetryServiceBehavior.cs Co-authored-by: Mikel Blanchard * In response to PR #152 comments. Refactor common code that adds dispatch behavior to endpoint from both TelemetryServiceBehavior and TelemetryEndpointBehaviorUpdate src/OpenTelemetry.Contrib.Instrumentation.Wcf/TelemetryServiceBehavior.cs Co-authored-by: Mikel Blanchard * Fixed lint errors in README & updated CHANGELOG. Co-authored-by: Mikel Blanchard --- examples/wcf/client-netframework/App.config | 2 +- examples/wcf/server-netframework/App.config | 2 +- .../CHANGELOG.md | 11 +++- .../README.md | 51 +++++++++++++++-- .../TelemetryEndpointBehavior.cs | 23 +++++--- ...emetryEndpointBehaviorExtensionElement.cs} | 6 +- .../TelemetryServiceBehavior.cs | 56 +++++++++++++++++++ ...elemetryServiceBehaviorExtensionElement.cs | 38 +++++++++++++ 8 files changed, 170 insertions(+), 19 deletions(-) rename src/OpenTelemetry.Contrib.Instrumentation.Wcf/{TelemetryBehaviourExtensionElement.cs => TelemetryEndpointBehaviorExtensionElement.cs} (80%) create mode 100644 src/OpenTelemetry.Contrib.Instrumentation.Wcf/TelemetryServiceBehavior.cs create mode 100644 src/OpenTelemetry.Contrib.Instrumentation.Wcf/TelemetryServiceBehaviorExtensionElement.cs diff --git a/examples/wcf/client-netframework/App.config b/examples/wcf/client-netframework/App.config index 4716577152..f1a84e2671 100644 --- a/examples/wcf/client-netframework/App.config +++ b/examples/wcf/client-netframework/App.config @@ -3,7 +3,7 @@ - + diff --git a/examples/wcf/server-netframework/App.config b/examples/wcf/server-netframework/App.config index 0046bbfe76..aa7d7f0bdc 100644 --- a/examples/wcf/server-netframework/App.config +++ b/examples/wcf/server-netframework/App.config @@ -3,7 +3,7 @@ - + diff --git a/src/OpenTelemetry.Contrib.Instrumentation.Wcf/CHANGELOG.md b/src/OpenTelemetry.Contrib.Instrumentation.Wcf/CHANGELOG.md index 9746083399..ba5d2ea60b 100644 --- a/src/OpenTelemetry.Contrib.Instrumentation.Wcf/CHANGELOG.md +++ b/src/OpenTelemetry.Contrib.Instrumentation.Wcf/CHANGELOG.md @@ -2,8 +2,15 @@ ## Unreleased -* Added enricher for WCF activity - ([#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)) diff --git a/src/OpenTelemetry.Contrib.Instrumentation.Wcf/README.md b/src/OpenTelemetry.Contrib.Instrumentation.Wcf/README.md index daf3e9dbf5..3c8c76422a 100644 --- a/src/OpenTelemetry.Contrib.Instrumentation.Wcf/README.md +++ b/src/OpenTelemetry.Contrib.Instrumentation.Wcf/README.md @@ -59,7 +59,7 @@ the clients you want to instrument: - + @@ -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: ```xml @@ -113,7 +113,7 @@ the services you want to instrument: - + @@ -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: + + +```xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +``` + + ## References * [OpenTelemetry Project](https://opentelemetry.io/) diff --git a/src/OpenTelemetry.Contrib.Instrumentation.Wcf/TelemetryEndpointBehavior.cs b/src/OpenTelemetry.Contrib.Instrumentation.Wcf/TelemetryEndpointBehavior.cs index 8c3cfa6698..55b1f7d98b 100644 --- a/src/OpenTelemetry.Contrib.Instrumentation.Wcf/TelemetryEndpointBehavior.cs +++ b/src/OpenTelemetry.Contrib.Instrumentation.Wcf/TelemetryEndpointBehavior.cs @@ -24,13 +24,13 @@ namespace OpenTelemetry.Contrib.Instrumentation.Wcf { #if NETFRAMEWORK /// - /// An implementation whichs add the implementation which adds the to client endpoints and the /// to service endpoints. /// #else /// - /// An implementation whichs add the implementation which adds the to client endpoints. /// #endif @@ -62,6 +62,18 @@ public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRu public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) { #if NETFRAMEWORK + ApplyDispatchBehaviorToEndpoint(endpointDispatcher); +#endif + } + + /// + public void Validate(ServiceEndpoint endpoint) + { + } + +#if NETFRAMEWORK + internal static void ApplyDispatchBehaviorToEndpoint(EndpointDispatcher endpointDispatcher) + { var actionMappings = new Dictionary(StringComparer.OrdinalIgnoreCase); foreach (var dispatchOperation in endpointDispatcher.DispatchRuntime.Operations) @@ -74,12 +86,7 @@ public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher e } endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new TelemetryDispatchMessageInspector(actionMappings)); -#endif - } - - /// - public void Validate(ServiceEndpoint endpoint) - { } +#endif } } diff --git a/src/OpenTelemetry.Contrib.Instrumentation.Wcf/TelemetryBehaviourExtensionElement.cs b/src/OpenTelemetry.Contrib.Instrumentation.Wcf/TelemetryEndpointBehaviorExtensionElement.cs similarity index 80% rename from src/OpenTelemetry.Contrib.Instrumentation.Wcf/TelemetryBehaviourExtensionElement.cs rename to src/OpenTelemetry.Contrib.Instrumentation.Wcf/TelemetryEndpointBehaviorExtensionElement.cs index 380dde1a21..bf41d4779a 100644 --- a/src/OpenTelemetry.Contrib.Instrumentation.Wcf/TelemetryBehaviourExtensionElement.cs +++ b/src/OpenTelemetry.Contrib.Instrumentation.Wcf/TelemetryEndpointBehaviorExtensionElement.cs @@ -1,4 +1,4 @@ -// +// // Copyright The OpenTelemetry Authors // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -21,9 +21,9 @@ namespace OpenTelemetry.Contrib.Instrumentation.Wcf { /// - /// A for registering on service through configuation. + /// A for registering on a service endpoint through configuration. /// - public class TelemetryBehaviourExtensionElement : BehaviorExtensionElement + public class TelemetryEndpointBehaviorExtensionElement : BehaviorExtensionElement { /// public override Type BehaviorType => typeof(TelemetryEndpointBehavior); diff --git a/src/OpenTelemetry.Contrib.Instrumentation.Wcf/TelemetryServiceBehavior.cs b/src/OpenTelemetry.Contrib.Instrumentation.Wcf/TelemetryServiceBehavior.cs new file mode 100644 index 0000000000..0ce05d329c --- /dev/null +++ b/src/OpenTelemetry.Contrib.Instrumentation.Wcf/TelemetryServiceBehavior.cs @@ -0,0 +1,56 @@ +// +// 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. +// + +using System; +using System.Collections.Generic; +using System.ServiceModel; +using System.ServiceModel.Description; +using System.ServiceModel.Dispatcher; + +namespace OpenTelemetry.Contrib.Instrumentation.Wcf +{ +#if NETFRAMEWORK + /// + /// An implementation to add the + /// to service operations. + /// + public class TelemetryServiceBehavior : IServiceBehavior + { + /// + public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters) + { + } + + /// + 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); + } + } + } + + /// + public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) + { + } + } +#endif +} diff --git a/src/OpenTelemetry.Contrib.Instrumentation.Wcf/TelemetryServiceBehaviorExtensionElement.cs b/src/OpenTelemetry.Contrib.Instrumentation.Wcf/TelemetryServiceBehaviorExtensionElement.cs new file mode 100644 index 0000000000..515cdc2703 --- /dev/null +++ b/src/OpenTelemetry.Contrib.Instrumentation.Wcf/TelemetryServiceBehaviorExtensionElement.cs @@ -0,0 +1,38 @@ +// +// 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. +// + +#if NETFRAMEWORK +using System; +using System.ServiceModel.Configuration; + +namespace OpenTelemetry.Contrib.Instrumentation.Wcf +{ + /// + /// A for registering on a service through configuration. + /// + public class TelemetryServiceBehaviorExtensionElement : BehaviorExtensionElement + { + /// + public override Type BehaviorType => typeof(TelemetryServiceBehavior); + + /// + protected override object CreateBehavior() + { + return new TelemetryServiceBehavior(); + } + } +} +#endif