Skip to content

Commit 3836a01

Browse files
[ServiceFabricRemoting] Respect TransportSettings (#4148)
1 parent a32c828 commit 3836a01

4 files changed

Lines changed: 133 additions & 11 deletions

File tree

‎src/OpenTelemetry.Instrumentation.ServiceFabricRemoting/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
* Updated OpenTelemetry core component version(s) to `1.15.2`.
66
([#4080](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/4080))
77

8+
* Ensure that `TransportSettings` configuration is applied to created
9+
instances of `IServiceRemotingClientFactory`.
10+
([#4148](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/4148))
11+
812
## 1.15.0-beta.1
913

1014
Released 2026-Jan-21

‎src/OpenTelemetry.Instrumentation.ServiceFabricRemoting/ServiceRemoting/TraceContextEnrichedServiceRemotingProviderAttribute.cs‎

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ namespace OpenTelemetry.Instrumentation.ServiceFabricRemoting;
2020
public sealed class TraceContextEnrichedServiceRemotingProviderAttribute : FabricTransportServiceRemotingProviderAttribute
2121
{
2222
private const string DefaultV2listenerName = "V2Listener";
23+
private const string DefaultTransportSettingsSectionName = "TransportSettings";
2324

2425
/// <summary>
2526
/// Initializes a new instance of the <see cref="TraceContextEnrichedServiceRemotingProviderAttribute"/> class.
@@ -30,6 +31,10 @@ public TraceContextEnrichedServiceRemotingProviderAttribute()
3031
this.RemotingListenerVersion = RemotingListenerVersion.V2;
3132
}
3233

34+
internal static Func<FabricTransportRemotingSettings?> RemotingSettingsLoader { get; set; } = LoadRemotingSettings;
35+
36+
internal static Func<FabricTransportRemotingListenerSettings?> ListenerSettingsLoader { get; set; } = LoadListenerSettings;
37+
3338
/// <summary>
3439
/// Creates a V2 service remoting listener for remoting the service interface.
3540
/// </summary>
@@ -66,11 +71,7 @@ public override Dictionary<string, Func<ServiceContext, IService, IServiceRemoti
6671
/// </returns>
6772
public override IServiceRemotingClientFactory CreateServiceRemotingClientFactoryV2(IServiceRemotingCallbackMessageHandler? callbackMessageHandler)
6873
{
69-
var fabricTransportRemotingSettings = new FabricTransportRemotingSettings();
70-
fabricTransportRemotingSettings.MaxMessageSize = this.GetAndValidateMaxMessageSize(fabricTransportRemotingSettings.MaxMessageSize);
71-
fabricTransportRemotingSettings.OperationTimeout = this.GetAndValidateOperationTimeout(fabricTransportRemotingSettings.OperationTimeout);
72-
fabricTransportRemotingSettings.KeepAliveTimeout = this.GetAndValidateKeepAliveTimeout(fabricTransportRemotingSettings.KeepAliveTimeout);
73-
fabricTransportRemotingSettings.ConnectTimeout = this.GetConnectTimeout(fabricTransportRemotingSettings.ConnectTimeout);
74+
var fabricTransportRemotingSettings = this.GetRemotingSettings();
7475

7576
var fabricTransportServiceRemotingClientFactory = new FabricTransportServiceRemotingClientFactory(
7677
fabricTransportRemotingSettings,
@@ -82,15 +83,56 @@ public override IServiceRemotingClientFactory CreateServiceRemotingClientFactory
8283
return new TraceContextEnrichedServiceRemotingClientFactoryAdapter(fabricTransportServiceRemotingClientFactory);
8384
}
8485

85-
private FabricTransportRemotingListenerSettings GetListenerSettings()
86+
internal FabricTransportRemotingListenerSettings GetListenerSettings()
87+
{
88+
var settings = ListenerSettingsLoader() ?? new FabricTransportRemotingListenerSettings();
89+
90+
settings.MaxMessageSize = this.GetAndValidateMaxMessageSize(settings.MaxMessageSize);
91+
settings.OperationTimeout = this.GetAndValidateOperationTimeout(settings.OperationTimeout);
92+
settings.KeepAliveTimeout = this.GetAndValidateKeepAliveTimeout(settings.KeepAliveTimeout);
93+
94+
return settings;
95+
}
96+
97+
internal FabricTransportRemotingSettings GetRemotingSettings()
8698
{
87-
var listenerSettings = new FabricTransportRemotingListenerSettings();
99+
var settings = RemotingSettingsLoader() ?? new FabricTransportRemotingSettings();
88100

89-
listenerSettings.MaxMessageSize = this.GetAndValidateMaxMessageSize(listenerSettings.MaxMessageSize);
90-
listenerSettings.OperationTimeout = this.GetAndValidateOperationTimeout(listenerSettings.OperationTimeout);
91-
listenerSettings.KeepAliveTimeout = this.GetAndValidateKeepAliveTimeout(listenerSettings.KeepAliveTimeout);
101+
settings.MaxMessageSize = this.GetAndValidateMaxMessageSize(settings.MaxMessageSize);
102+
settings.OperationTimeout = this.GetAndValidateOperationTimeout(settings.OperationTimeout);
103+
settings.KeepAliveTimeout = this.GetAndValidateKeepAliveTimeout(settings.KeepAliveTimeout);
104+
settings.ConnectTimeout = this.GetConnectTimeout(settings.ConnectTimeout);
92105

93-
return listenerSettings;
106+
return settings;
107+
}
108+
109+
private static FabricTransportRemotingSettings? LoadRemotingSettings() =>
110+
TryLoadSettings(() =>
111+
FabricTransportRemotingSettings.TryLoadFrom(DefaultTransportSettingsSectionName, out var settings, filepath: null, configPackageName: null)
112+
? settings
113+
: null);
114+
115+
private static FabricTransportRemotingListenerSettings? LoadListenerSettings() =>
116+
TryLoadSettings(() =>
117+
FabricTransportRemotingListenerSettings.TryLoadFrom(DefaultTransportSettingsSectionName, out var settings, configPackageName: null)
118+
? settings
119+
: null);
120+
121+
private static T? TryLoadSettings<T>(Func<T?> loader)
122+
where T : class
123+
{
124+
try
125+
{
126+
return loader();
127+
}
128+
catch (DllNotFoundException)
129+
{
130+
return null;
131+
}
132+
catch (TypeInitializationException ex) when (ex.InnerException is DllNotFoundException)
133+
{
134+
return null;
135+
}
94136
}
95137

96138
private long GetAndValidateMaxMessageSize(long maxMessageSizeDefault)

‎test/OpenTelemetry.Instrumentation.ServiceFabricRemoting.Tests/OpenTelemetry.Instrumentation.ServiceFabricRemoting.Tests.csproj‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
<ProjectReference Include="..\..\src\OpenTelemetry.Instrumentation.ServiceFabricRemoting\OpenTelemetry.Instrumentation.ServiceFabricRemoting.csproj" />
2121
</ItemGroup>
2222

23+
<ItemGroup>
24+
<Compile Include="$(RepoRoot)\src\Shared\Lock.cs" Link="Includes\Lock.cs" />
25+
</ItemGroup>
26+
2327
<ItemGroup>
2428
<!-- Include must be the fully qualified .NET type name of the Attribute to create. -->
2529
<AssemblyAttribute Include="OpenTelemetry.Instrumentation.ServiceFabricRemoting.TraceContextEnrichedServiceRemotingProviderAttribute" />

‎test/OpenTelemetry.Instrumentation.ServiceFabricRemoting.Tests/ServiceFabricRemotingTests.cs‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
using System.Diagnostics;
5+
using System.Fabric;
56
using System.Text;
67
using Microsoft.ServiceFabric.Actors;
78
using Microsoft.ServiceFabric.Actors.Runtime;
9+
using Microsoft.ServiceFabric.Services.Remoting.FabricTransport;
10+
using Microsoft.ServiceFabric.Services.Remoting.FabricTransport.Runtime;
811
using Microsoft.ServiceFabric.Services.Remoting.V2;
912
using Microsoft.ServiceFabric.Services.Remoting.V2.Runtime;
1013
using OpenTelemetry.Context.Propagation;
@@ -22,6 +25,7 @@ public class ServiceFabricRemotingTests
2225
private const string BaggageKey = "SomeBaggageKey";
2326
private const string BaggageValue = "SomeBaggageValue";
2427
private static readonly ActivitySource ActivitySource = new("ServiceFabricRemotingTests");
28+
private static readonly Lock TransportSettingsLock = new();
2529

2630
[Fact]
2731
public async Task TestStatefulServiceContextPropagation_ShouldExtractActivityContextAndBaggage()
@@ -144,6 +148,74 @@ public async Task TestServiceRemotingClientContextPropagation_ShouldInjectActivi
144148
Assert.Equal(BaggageValue, propagationContext.Baggage.GetBaggage(BaggageKey));
145149
}
146150

151+
[Fact]
152+
public void ServiceRemotingProviderListenerSettings_LoadConfiguredTransportDefaults()
153+
{
154+
lock (TransportSettingsLock)
155+
{
156+
var originalLoader = TraceContextEnrichedServiceRemotingProviderAttribute.ListenerSettingsLoader;
157+
158+
try
159+
{
160+
TraceContextEnrichedServiceRemotingProviderAttribute.ListenerSettingsLoader = () => new FabricTransportRemotingListenerSettings
161+
{
162+
MaxQueueSize = 35,
163+
SecurityCredentials = new X509Credentials(),
164+
};
165+
166+
var provider = new TraceContextEnrichedServiceRemotingProviderAttribute
167+
{
168+
MaxMessageSize = 17,
169+
};
170+
171+
var actual = provider.GetListenerSettings();
172+
173+
Assert.Equal(35, actual.MaxQueueSize);
174+
Assert.Equal(CredentialType.X509, actual.SecurityCredentials.CredentialType);
175+
Assert.Equal(17, actual.MaxMessageSize);
176+
}
177+
finally
178+
{
179+
TraceContextEnrichedServiceRemotingProviderAttribute.ListenerSettingsLoader = originalLoader;
180+
}
181+
}
182+
}
183+
184+
[Fact]
185+
public void ServiceRemotingProviderListenerSettings_LoadConfiguredRemotingSettings()
186+
{
187+
lock (TransportSettingsLock)
188+
{
189+
var originalLoader = TraceContextEnrichedServiceRemotingProviderAttribute.RemotingSettingsLoader;
190+
191+
try
192+
{
193+
TraceContextEnrichedServiceRemotingProviderAttribute.RemotingSettingsLoader = () => new FabricTransportRemotingSettings
194+
{
195+
MaxQueueSize = 35,
196+
ConnectTimeout = TimeSpan.FromMilliseconds(7),
197+
SecurityCredentials = new X509Credentials(),
198+
};
199+
200+
var provider = new TraceContextEnrichedServiceRemotingProviderAttribute
201+
{
202+
MaxMessageSize = 17,
203+
};
204+
205+
var actual = provider.GetRemotingSettings();
206+
207+
Assert.Equal(35, actual.MaxQueueSize);
208+
Assert.Equal(CredentialType.X509, actual.SecurityCredentials.CredentialType);
209+
Assert.Equal(TimeSpan.FromMilliseconds(7), actual.ConnectTimeout);
210+
Assert.Equal(17, actual.MaxMessageSize);
211+
}
212+
finally
213+
{
214+
TraceContextEnrichedServiceRemotingProviderAttribute.RemotingSettingsLoader = originalLoader;
215+
}
216+
}
217+
}
218+
147219
private ServiceRemotingRequestMessageHeaderMock CreateServiceRemotingRequestMessageHeader(Type interfaceType, string methodName)
148220
{
149221
var interfaceId = ServiceFabricUtils.GetInterfaceId(interfaceType);

0 commit comments

Comments
 (0)