-
Notifications
You must be signed in to change notification settings - Fork 283
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added TestServiceRemotingClientContextPropagation_ShouldInjectActivit…
…yContextAndBaggage test
- Loading branch information
1 parent
2f26254
commit a0d8e56
Showing
12 changed files
with
197 additions
and
71 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
11 changes: 11 additions & 0 deletions
11
src/OpenTelemetry.Instrumentation.ServiceFabricRemoting/AssemblyInfo.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,11 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using System.Runtime.CompilerServices; | ||
|
||
[assembly: CLSCompliant(false)] | ||
#if SIGNED | ||
[assembly: InternalsVisibleTo("OpenTelemetry.Instrumentation.ServiceFabricRemoting.Tests, PublicKey=002400000480000094000000060200000024000052534131000400000100010051c1562a090fb0c9f391012a32198b5e5d9a60e9b80fa2d7b434c9e5ccb7259bd606e66f9660676afc6692b8cdc6793d190904551d2103b7b22fa636dcbb8208839785ba402ea08fc00c8f1500ccef28bbf599aa64ffb1e1d5dc1bf3420a3777badfe697856e9d52070a50c3ea5821c80bef17ca3acffa28f89dd413f096f898")] | ||
#else | ||
[assembly: InternalsVisibleTo("OpenTelemetry.Instrumentation.ServiceFabricRemoting.Tests")] | ||
#endif |
32 changes: 32 additions & 0 deletions
32
src/OpenTelemetry.Instrumentation.ServiceFabricRemoting/ServiceFabricRemotingUtils.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,32 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using System.Text; | ||
using Microsoft.ServiceFabric.Services.Remoting.V2; | ||
|
||
namespace OpenTelemetry.Instrumentation.ServiceFabricRemoting; | ||
|
||
internal static class ServiceFabricRemotingUtils | ||
{ | ||
internal static void InjectTraceContextIntoServiceRemotingRequestMessageHeader(IServiceRemotingRequestMessageHeader requestMessageHeader, string key, string value) | ||
{ | ||
if (!requestMessageHeader.TryGetHeaderValue(key, out byte[] _)) | ||
{ | ||
byte[] valueAsBytes = Encoding.UTF8.GetBytes(value); | ||
|
||
requestMessageHeader.AddHeader(key, valueAsBytes); | ||
} | ||
} | ||
|
||
internal static IEnumerable<string> ExtractTraceContextFromRequestMessageHeader(IServiceRemotingRequestMessageHeader requestMessageHeader, string headerKey) | ||
{ | ||
if (requestMessageHeader.TryGetHeaderValue(headerKey, out byte[] headerValueAsBytes)) | ||
{ | ||
string headerValue = Encoding.UTF8.GetString(headerValueAsBytes); | ||
|
||
return [headerValue]; | ||
} | ||
|
||
return Enumerable.Empty<string>(); | ||
} | ||
} |
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
54 changes: 54 additions & 0 deletions
54
...nTelemetry.Instrumentation.ServiceFabricRemoting.Tests/Mocks/ServiceRemotingClientMock.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,54 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using System.Fabric; | ||
using System.Text; | ||
using Microsoft.ServiceFabric.Services.Remoting.V2; | ||
using Microsoft.ServiceFabric.Services.Remoting.V2.Client; | ||
using OpenTelemetry.Context.Propagation; | ||
using ServiceFabric.Mocks.RemotingV2; | ||
|
||
namespace OpenTelemetry.Instrumentation.ServiceFabricRemoting.Tests; | ||
|
||
internal class ServiceRemotingClientMock : IServiceRemotingClient | ||
{ | ||
public ServiceRemotingClientMock() | ||
{ | ||
} | ||
|
||
public ResolvedServicePartition ResolvedServicePartition { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } | ||
|
||
public string ListenerName { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } | ||
|
||
public ResolvedServiceEndpoint Endpoint { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } | ||
|
||
/// <summary> | ||
/// The RequestResponseAsync method reads the headers from the request and injects them into the response, using OpneTelemetry's TextMapPropagator. | ||
/// </summary> | ||
public Task<IServiceRemotingResponseMessage> RequestResponseAsync(IServiceRemotingRequestMessage requestMessage) | ||
{ | ||
IServiceRemotingRequestMessageHeader requestMessageHeader = requestMessage.GetHeader(); | ||
PropagationContext propagationContext = Propagators.DefaultTextMapPropagator.Extract(default, requestMessageHeader, ServiceFabricRemotingUtils.ExtractTraceContextFromRequestMessageHeader); | ||
|
||
MockServiceRemotingResponseMessage responseMessage = new MockServiceRemotingResponseMessage() | ||
{ | ||
Header = new ServiceRemotingResponseMessageHeaderMock(), | ||
}; | ||
|
||
Propagators.DefaultTextMapPropagator.Inject(new PropagationContext(propagationContext.ActivityContext, propagationContext.Baggage), responseMessage.Header, this.InjectTraceContextIntoServiceRemotingRequestMessageHeader); | ||
|
||
return Task.FromResult<IServiceRemotingResponseMessage>(responseMessage); | ||
} | ||
|
||
public void SendOneWay(IServiceRemotingRequestMessage requestMessage) => throw new NotImplementedException(); | ||
|
||
private void InjectTraceContextIntoServiceRemotingRequestMessageHeader(IServiceRemotingResponseMessageHeader responseMessageHeaders, string key, string value) | ||
{ | ||
if (!responseMessageHeaders.TryGetHeaderValue(key, out byte[] _)) | ||
{ | ||
byte[] valueAsBytes = Encoding.UTF8.GetBytes(value); | ||
|
||
responseMessageHeaders.AddHeader(key, valueAsBytes); | ||
} | ||
} | ||
} |
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
49 changes: 49 additions & 0 deletions
49
...rumentation.ServiceFabricRemoting.Tests/Mocks/ServiceRemotingResponseMessageHeaderMock.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,49 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using System.Fabric; | ||
using System.Globalization; | ||
using Microsoft.ServiceFabric.Services.Remoting.V2; | ||
|
||
namespace OpenTelemetry.Instrumentation.ServiceFabricRemoting.Tests; | ||
|
||
internal class ServiceRemotingResponseMessageHeaderMock : IServiceRemotingResponseMessageHeader | ||
{ | ||
private Dictionary<string, byte[]> headers; | ||
|
||
public ServiceRemotingResponseMessageHeaderMock() | ||
{ | ||
this.headers = new Dictionary<string, byte[]>(); | ||
} | ||
|
||
public void AddHeader(string headerName, byte[] headerValue) | ||
{ | ||
if (this.headers.ContainsKey(headerName)) | ||
{ | ||
throw new FabricElementAlreadyExistsException(string.Format((IFormatProvider)(object)CultureInfo.CurrentCulture, "ErrorHeaderAlreadyExists")); | ||
} | ||
|
||
this.headers[headerName] = headerValue; | ||
} | ||
|
||
public bool CheckIfItsEmpty() | ||
{ | ||
if (this.headers == null || this.headers.Count == 0) | ||
{ | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public bool TryGetHeaderValue(string headerName, out byte[]? headerValue) | ||
{ | ||
headerValue = null; | ||
if (this.headers == null) | ||
{ | ||
return false; | ||
} | ||
|
||
return this.headers.TryGetValue(headerName, out headerValue); | ||
} | ||
} |
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