From 2e6c17516580a2cd8bb0e3428c795796de688972 Mon Sep 17 00:00:00 2001 From: Jaans Date: Sun, 5 Sep 2021 00:06:16 +1000 Subject: [PATCH 1/4] Add WCF ServiceBehavior implementation. Resolves #142. --- .../TelemetryServiceBehavior.cs | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/OpenTelemetry.Contrib.Instrumentation.Wcf/TelemetryServiceBehavior.cs diff --git a/src/OpenTelemetry.Contrib.Instrumentation.Wcf/TelemetryServiceBehavior.cs b/src/OpenTelemetry.Contrib.Instrumentation.Wcf/TelemetryServiceBehavior.cs new file mode 100644 index 0000000000..76eef154ea --- /dev/null +++ b/src/OpenTelemetry.Contrib.Instrumentation.Wcf/TelemetryServiceBehavior.cs @@ -0,0 +1,66 @@ +// +// 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 add the 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) + { + var actionMappings = new Dictionary(StringComparer.OrdinalIgnoreCase); + foreach (var dispatchOperation in endpointDispatcher.DispatchRuntime.Operations) + { + actionMappings[dispatchOperation.Action] = new ActionMetadata + { + ContractName = $"{endpointDispatcher.ContractNamespace}{endpointDispatcher.ContractName}", + OperationName = dispatchOperation.Name, + }; + } + + endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new TelemetryDispatchMessageInspector(actionMappings)); + } + } + } + + /// + public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) + { + } + } +#endif +} From 73937fbcf3871ebed0d5ddde3a7d939fe8b9c8a0 Mon Sep 17 00:00:00 2001 From: Jaans Date: Sun, 5 Sep 2021 18:36:09 +1000 Subject: [PATCH 2/4] Update src/OpenTelemetry.Contrib.Instrumentation.Wcf/TelemetryServiceBehavior.cs Co-authored-by: Mikel Blanchard --- .../TelemetryServiceBehavior.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenTelemetry.Contrib.Instrumentation.Wcf/TelemetryServiceBehavior.cs b/src/OpenTelemetry.Contrib.Instrumentation.Wcf/TelemetryServiceBehavior.cs index 76eef154ea..c98dcaaa4e 100644 --- a/src/OpenTelemetry.Contrib.Instrumentation.Wcf/TelemetryServiceBehavior.cs +++ b/src/OpenTelemetry.Contrib.Instrumentation.Wcf/TelemetryServiceBehavior.cs @@ -24,7 +24,7 @@ namespace OpenTelemetry.Contrib.Instrumentation.Wcf { #if NETFRAMEWORK /// - /// An implementation add the the + /// An implementation to add the /// to service operations. /// public class TelemetryServiceBehavior : IServiceBehavior From c6fae3639293e98bd887e2cda89df50ef11d6bae Mon Sep 17 00:00:00 2001 From: Jaans Date: Sun, 5 Sep 2021 18:36:09 +1000 Subject: [PATCH 3/4] 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 --- .../TelemetryEndpointBehavior.cs | 23 ++++++++++++------- .../TelemetryServiceBehavior.cs | 14 ++--------- 2 files changed, 17 insertions(+), 20 deletions(-) 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/TelemetryServiceBehavior.cs b/src/OpenTelemetry.Contrib.Instrumentation.Wcf/TelemetryServiceBehavior.cs index 76eef154ea..0ce05d329c 100644 --- a/src/OpenTelemetry.Contrib.Instrumentation.Wcf/TelemetryServiceBehavior.cs +++ b/src/OpenTelemetry.Contrib.Instrumentation.Wcf/TelemetryServiceBehavior.cs @@ -24,7 +24,7 @@ namespace OpenTelemetry.Contrib.Instrumentation.Wcf { #if NETFRAMEWORK /// - /// An implementation add the the + /// An implementation to add the /// to service operations. /// public class TelemetryServiceBehavior : IServiceBehavior @@ -42,17 +42,7 @@ public void ApplyDispatchBehavior(ServiceDescription serviceDescription, Service var channelDispatcher = (ChannelDispatcher)channelDispatcherBase; foreach (var endpointDispatcher in channelDispatcher.Endpoints) { - var actionMappings = new Dictionary(StringComparer.OrdinalIgnoreCase); - foreach (var dispatchOperation in endpointDispatcher.DispatchRuntime.Operations) - { - actionMappings[dispatchOperation.Action] = new ActionMetadata - { - ContractName = $"{endpointDispatcher.ContractNamespace}{endpointDispatcher.ContractName}", - OperationName = dispatchOperation.Name, - }; - } - - endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new TelemetryDispatchMessageInspector(actionMappings)); + TelemetryEndpointBehavior.ApplyDispatchBehaviorToEndpoint(endpointDispatcher); } } } From 147db062314fbcb114ee3472c7aed59886071163 Mon Sep 17 00:00:00 2001 From: Mikel Blanchard Date: Tue, 7 Sep 2021 11:26:55 -0700 Subject: [PATCH 4/4] Fixed lint errors in README & updated CHANGELOG. --- .../CHANGELOG.md | 11 +++++++++-- .../README.md | 10 +++++----- 2 files changed, 14 insertions(+), 7 deletions(-) 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 ec12adf588..3c8c76422a 100644 --- a/src/OpenTelemetry.Contrib.Instrumentation.Wcf/README.md +++ b/src/OpenTelemetry.Contrib.Instrumentation.Wcf/README.md @@ -103,8 +103,8 @@ Example project available in ## WCF Server Configuration (.NET Framework) -Add the `IDispatchMessageInspector` instrumentation via an endpoint behavior extension on -the service endpoints you want to instrument: +Add the `IDispatchMessageInspector` instrumentation via an endpoint behavior +extension on the service endpoints you want to instrument: ```xml @@ -149,8 +149,9 @@ 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: +To add the `IDispatchMessageInspector` instrumentation for all endpoints of a +service, use the service behavior extension on the services you want to +instrument: ```xml @@ -191,7 +192,6 @@ the services you want to instrument: ``` - ## References * [OpenTelemetry Project](https://opentelemetry.io/)