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

[BUG] CultureNotFoundException on startup in global-invariant-mode #2422

Closed
ben-kiplot opened this issue Aug 16, 2024 · 1 comment · Fixed by #2423
Closed

[BUG] CultureNotFoundException on startup in global-invariant-mode #2422

ben-kiplot opened this issue Aug 16, 2024 · 1 comment · Fixed by #2423
Labels
bug Something isn't working community triage

Comments

@ben-kiplot
Copy link
Contributor

ben-kiplot commented Aug 16, 2024

APM Agent version

1.28.3

Environment

Operating system and version: Windows 11 / Alpine Linux

.NET Framework/Core name and version (e.g. .NET 4.6.2, NET Core 3.1.100) : .NET 8

Application Target Framework(s) (e.g. net462, netcoreapp3.1): net80

Describe the bug

The APM agent crashes on startup when running in global-invariant mode. This mode is necessary to run applications in docker using the Alpine Linux .NET runtime images such as https://mcr.microsoft.com/dotnet/aspnet:8.0.7-alpine3.20-amd64 as they don't have culture information included.

To Reproduce

Steps to reproduce the behavior:

  1. Enable global-invariant mode by adding the following to the .csproj file:
<PropertyGroup>
    <InvariantGlobalization>true</InvariantGlobalization>
</PropertyGroup>
  1. Add ElasticApm to the application with:
services.AddElasticApm(
    new HttpDiagnosticsSubscriber(), // Instruments outbound HTTP calls via HttpClient
    new EfCoreDiagnosticsSubscriber()
);
  1. Start the application, which crashes on startup

Expected behavior

The APM agent does not throw an exception when started as part of an application with global-invariant mode enabled.

Actual behavior

The APM agent throws the following exception when started as part of an application with global-invariant mode enabled

[15:48:47 ERR] [Elastic.Apm] Failed initializing agent.
System.Globalization.CultureNotFoundException: Only the invariant culture is supported in globalization-invariant mode. See https://aka.ms/GlobalizationInvariantMode for more information. (Parameter 'name')
ca is an invalid culture identifier.
   at System.Globalization.CultureInfo.GetCultureInfo(String name)
   at System.Reflection.RuntimeAssembly.GetLocale()
   at System.Reflection.RuntimeAssembly.GetName(Boolean copiedName)
   at Elastic.Apm.Api.Service.<>c__DisplayClass36_0.<SetAgentActivationMethod>b__1(Assembly n)
   at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
   at Elastic.Apm.Api.Service.<SetAgentActivationMethod>g__CheckForLoadedAssembly|36_0(String name)
   at Elastic.Apm.Api.Service.SetAgentActivationMethod(IApmLogger logger, Service service)
   at Elastic.Apm.Api.Service.GetDefaultService(IConfigurationReader configurationReader, IApmLogger loggerArg)
   at Elastic.Apm.AgentComponents..ctor(IApmLogger logger, IConfigurationReader configurationReader, IPayloadSender payloadSender, IMetricsCollector metricsCollector, ICurrentExecutionSegmentsContainer currentExecutionSegmentsContainer, ICentralConfigurationFetcher centralConfigurationFetcher, IApmServerInfo apmServerInfo, BreakdownMetricsProvider breakdownMetricsProvider, IHostNameDetector hostNameDetector)
@ben-kiplot ben-kiplot added the bug Something isn't working label Aug 16, 2024
ben-kiplot added a commit to ben-kiplot/apm-agent-dotnet that referenced this issue Aug 17, 2024
@ben-kiplot
Copy link
Contributor Author

After further investigation I've identified that this only occurs when the APM agent is initialized within an application that has assemblies with a culture other than neutral loaded into the AppDomain at the point of AddElasticApm() being called during application startup. As the call stack in the issue description shows, this occurs due to Assembly.GetName() ultimately calling CultureInfo.GetCultureInfo which can't be called in global-invariant mode. This only happens when retrieving the name of an assembly with a culture other than neutral.

I've raised #2423 to fix this which changes the call to Assembly.GetName() to directly extract the assembly name from Assembly.FullName (which is in the format Microsoft.AspNetCore.Mvc.Core, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60) which avoids the call to CultureInfo.GetCultureInfo.

This issue is basically the same as #1985 which was previously fixed in #1990 but was re-introduced in #1976 before #1990 merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working community triage
Projects
None yet
1 participant