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
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// <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 add the the
/// <see cref="TelemetryDispatchMessageInspector"/> to service operations.
/// </summary>
Jaans marked this conversation as resolved.
Show resolved Hide resolved
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)
{
var actionMappings = new Dictionary<string, ActionMetadata>(StringComparer.OrdinalIgnoreCase);
Jaans marked this conversation as resolved.
Show resolved Hide resolved
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));
}
}
}

/// <inheritdoc/>
public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
}
}
#endif
}