-
Notifications
You must be signed in to change notification settings - Fork 289
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[wcf] Add support for non-SOAP based payloads (#1251)
Co-authored-by: Piotr Kiełkowicz <pkiekowicz@splunk.com>
- Loading branch information
1 parent
13f9058
commit ed2df8f
Showing
16 changed files
with
625 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
src/OpenTelemetry.Instrumentation.Wcf/Implementation/HttpRequestMessagePropertyWrapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// <copyright file="HttpRequestMessagePropertyWrapper.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.Diagnostics; | ||
using System.Net; | ||
using System.Reflection; | ||
|
||
namespace OpenTelemetry.Instrumentation.Wcf.Implementation; | ||
|
||
/// <summary> | ||
/// This is a reflection-based wrapper around the HttpRequestMessageProperty class. It is done this way so we don't need to | ||
/// have an explicit reference to System.ServiceModel.Http.dll. If the consuming application has a reference to | ||
/// System.ServiceModel.Http.dll then the HttpRequestMessageProperty class will be available (IsHttpFunctionalityEnabled == true). | ||
/// If the consuming application does not have a reference to System.ServiceModel.Http.dll then all http-related functionality | ||
/// will be disabled (IsHttpFunctionalityEnabled == false). | ||
/// </summary> | ||
internal static class HttpRequestMessagePropertyWrapper | ||
{ | ||
private static readonly ReflectedInfo ReflectedValues = Initialize(); | ||
|
||
public static bool IsHttpFunctionalityEnabled => ReflectedValues != null; | ||
|
||
public static string Name | ||
{ | ||
get | ||
{ | ||
AssertHttpEnabled(); | ||
return ReflectedValues.Name; | ||
} | ||
} | ||
|
||
public static object CreateNew() | ||
{ | ||
AssertHttpEnabled(); | ||
return Activator.CreateInstance(ReflectedValues.Type); | ||
} | ||
|
||
public static WebHeaderCollection GetHeaders(object httpRequestMessageProperty) | ||
{ | ||
AssertHttpEnabled(); | ||
AssertIsFrameworkMessageProperty(httpRequestMessageProperty); | ||
return ReflectedValues.HeadersFetcher.Fetch(httpRequestMessageProperty); | ||
} | ||
|
||
private static ReflectedInfo Initialize() | ||
{ | ||
Type type = null; | ||
try | ||
{ | ||
type = Type.GetType( | ||
"System.ServiceModel.Channels.HttpRequestMessageProperty, System.ServiceModel, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", | ||
true); | ||
|
||
var headersProp = type.GetProperty("Headers", BindingFlags.Public | BindingFlags.Instance, null, typeof(WebHeaderCollection), Array.Empty<Type>(), null); | ||
if (headersProp == null) | ||
{ | ||
throw new NotSupportedException("HttpRequestMessageProperty.Headers property not found"); | ||
} | ||
|
||
var nameProp = type.GetProperty("Name", BindingFlags.Public | BindingFlags.Static, null, typeof(string), Array.Empty<Type>(), null); | ||
if (nameProp == null) | ||
{ | ||
throw new NotSupportedException("HttpRequestMessageProperty.Name property not found"); | ||
} | ||
|
||
return new ReflectedInfo | ||
{ | ||
Type = type, | ||
Name = (string)nameProp.GetValue(null), | ||
HeadersFetcher = new PropertyFetcher<WebHeaderCollection>("Headers"), | ||
}; | ||
} | ||
catch (Exception ex) | ||
{ | ||
WcfInstrumentationEventSource.Log.HttpServiceModelReflectionFailedToBind(ex, type?.Assembly); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
[Conditional("DEBUG")] | ||
private static void AssertHttpEnabled() | ||
{ | ||
if (!IsHttpFunctionalityEnabled) | ||
{ | ||
throw new InvalidOperationException("Http functionality is not enabled, check IsHttpFunctionalityEnabled before calling this method"); | ||
} | ||
} | ||
|
||
[Conditional("DEBUG")] | ||
private static void AssertIsFrameworkMessageProperty(object httpRequestMessageProperty) | ||
{ | ||
AssertHttpEnabled(); | ||
if (httpRequestMessageProperty == null || !httpRequestMessageProperty.GetType().Equals(ReflectedValues.Type)) | ||
{ | ||
throw new ArgumentException("Object must be of type HttpRequestMessageProperty"); | ||
} | ||
} | ||
|
||
private sealed class ReflectedInfo | ||
{ | ||
public Type Type; | ||
public string Name; | ||
public PropertyFetcher<WebHeaderCollection> HeadersFetcher; | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/OpenTelemetry.Instrumentation.Wcf/Implementation/TelemetryMessageHeader.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// <copyright file="TelemetryMessageHeader.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.ServiceModel.Channels; | ||
using System.Xml; | ||
|
||
namespace OpenTelemetry.Instrumentation.Wcf.Implementation; | ||
|
||
internal class TelemetryMessageHeader : MessageHeader | ||
{ | ||
private const string NAMESPACE = "https://www.w3.org/TR/trace-context/"; | ||
private string name; | ||
private string value; | ||
|
||
private TelemetryMessageHeader(string name, string value) | ||
{ | ||
this.name = name; | ||
this.value = value; | ||
} | ||
|
||
public override string Name => this.name; | ||
|
||
public string Value => this.value; | ||
|
||
public override string Namespace => NAMESPACE; | ||
|
||
public static TelemetryMessageHeader CreateHeader(string name, string value) | ||
{ | ||
return new TelemetryMessageHeader(name, value); | ||
} | ||
|
||
public static TelemetryMessageHeader FindHeader(string name, MessageHeaders allHeaders) | ||
{ | ||
try | ||
{ | ||
var headerIndex = allHeaders.FindHeader(name, NAMESPACE); | ||
if (headerIndex < 0) | ||
{ | ||
return null; | ||
} | ||
|
||
using var reader = allHeaders.GetReaderAtHeader(headerIndex); | ||
reader.Read(); | ||
return new TelemetryMessageHeader(name, reader.ReadContentAsString()); | ||
} | ||
catch (XmlException) | ||
{ | ||
return null; | ||
} | ||
} | ||
|
||
protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion) | ||
{ | ||
writer.WriteString(this.value); | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
src/OpenTelemetry.Instrumentation.Wcf/Implementation/TelemetryPropagationReader.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// <copyright file="TelemetryPropagationReader.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.Channels; | ||
using OpenTelemetry.Internal; | ||
|
||
namespace OpenTelemetry.Instrumentation.Wcf.Implementation; | ||
|
||
/// <summary> | ||
/// Pre-defined PropagationReader callbacks. | ||
/// </summary> | ||
internal static class TelemetryPropagationReader | ||
{ | ||
private static readonly Func<Message, string, IEnumerable<string>> DefaultReader = Compose(HttpRequestHeaders, SoapMessageHeaders); | ||
|
||
/// <summary> | ||
/// Reads the values from the SOAP message headers. If the message is not a SOAP message it returns null. | ||
/// </summary> | ||
/// <param name="request">The incoming <see cref="Message"/> to read trace context from.</param> | ||
/// <param name="name">The header name being requested.</param> | ||
/// <returns>An enumerable of all the values for the requested header, or null if the requested header was not present on the request.</returns> | ||
public static IEnumerable<string> SoapMessageHeaders(Message request, string name) | ||
{ | ||
Guard.ThrowIfNull(request); | ||
Guard.ThrowIfNull(name); | ||
|
||
if (request.Version == MessageVersion.None) | ||
{ | ||
return null; | ||
} | ||
|
||
var header = TelemetryMessageHeader.FindHeader(name, request.Headers); | ||
return header == null ? null : new[] { header.Value }; | ||
} | ||
|
||
/// <summary> | ||
/// Reads the values from the incoming HTTP request headers. If the message was not made via an HTTP request it returns null. | ||
/// </summary> | ||
/// <param name="request">The incoming <see cref="Message"/> to read trace context from.</param> | ||
/// <param name="name">The header name being requested.</param> | ||
/// <returns>An enumerable of all the values for the requested header, or null if the requested header was not present on the request.</returns> | ||
public static IEnumerable<string> HttpRequestHeaders(Message request, string name) | ||
{ | ||
Guard.ThrowIfNull(request); | ||
Guard.ThrowIfNull(name); | ||
|
||
if (!HttpRequestMessagePropertyWrapper.IsHttpFunctionalityEnabled || !request.Properties.TryGetValue(HttpRequestMessagePropertyWrapper.Name, out var prop)) | ||
{ | ||
return null; | ||
} | ||
|
||
var value = HttpRequestMessagePropertyWrapper.GetHeaders(prop)[name]; | ||
return value == null ? null : new[] { value }; | ||
} | ||
|
||
/// <summary> | ||
/// Reads the values from the incoming HTTP request headers and falls back to SOAP message headers if not found on the HTTP request. | ||
/// </summary> | ||
/// <param name="request">The incoming <see cref="Message"/> to read trace context from.</param> | ||
/// <param name="name">The header name being requested.</param> | ||
/// <returns>An enumerable of all the values for the requested header, or null if the requested header was not present on the request.</returns> | ||
public static IEnumerable<string> Default(Message request, string name) | ||
{ | ||
return DefaultReader(request, name); | ||
} | ||
|
||
/// <summary> | ||
/// Compose multiple PropagationReader callbacks into a single callback. The callbacks | ||
/// are called sequentially and the first one to return a non-null value wins. | ||
/// </summary> | ||
/// <param name="callbacks">The callbacks to compose into a single callback.</param> | ||
/// <returns>The composed callback.</returns> | ||
public static Func<Message, string, IEnumerable<string>> Compose(params Func<Message, string, IEnumerable<string>>[] callbacks) | ||
{ | ||
Guard.ThrowIfNull(callbacks); | ||
Array.ForEach(callbacks, cb => Guard.ThrowIfNull(cb)); | ||
|
||
return (Message request, string name) => | ||
{ | ||
foreach (var reader in callbacks) | ||
{ | ||
var values = reader(request, name); | ||
if (values != null) | ||
{ | ||
return values; | ||
} | ||
} | ||
|
||
return null; | ||
}; | ||
} | ||
} |
Oops, something went wrong.