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

Refine agent initialization within ElasticApmModule #2298

Merged
merged 1 commit into from
Feb 23, 2024

Conversation

stevejgordon
Copy link
Contributor

This PR simplifies the locking for agent initialization and ensures we only attempt the initialisation once, even if Init is called multiple times of from multiple instances of HttpApplication. The use of InitOnceHelper didn't seem to properly guard this and is overly complex for the goal here. The optimized flow it also logs some additional trace messages to improve diagnostics.

It removes the use of DbgInstanceNameGenerator which was not incrementing as expected. The new code prefers a simpler approach to define a scoped logger for each instance of the module.

Although this code no longer uses the InitOnceHelper, I haven't removed that in this PR to keep the scope of the change here quite tight. It's now only used (and likely not required) when initializing the HttpClient used to send data to the APM Server. I'll follow up with a PR to simplify that area and deal with a few other related element.

The updated logging results in the following:

[2024-02-22 16:17:47.888 +00:00][1][Debug] - {ElasticApmModule.#1} Found ASP.NET version: 4.8.9220.0
[2024-02-22 16:17:47.889 +00:00][1][Trace] - {ElasticApmModule.#1} Initializing singleton Agent.
[2024-02-22 16:17:47.891 +00:00][1][Trace] - Initialization - Agent.Setup called
[2024-02-22 16:17:47.894 +00:00][1][Trace] - Initialization - Agent instance initialized. Callstack:    at Elastic.Apm.Agent.<>c.<.cctor>b__21_0()
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.LazyInitValue()
   at Elastic.Apm.Agent.Setup(AgentComponents agentComponents)
   at Elastic.Apm.AspNetFullFramework.ElasticApmModule.AttemptAgentInitialization()
   at Elastic.Apm.AspNetFullFramework.ElasticApmModule.Init(HttpApplication application)
   at System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers)
   at System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context)
   at System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context)
   at System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext)

[2024-02-22 16:17:47.894 +00:00][1][Debug] - {ElasticApmModule.#1} Initialized Agent singleton. .NET runtime: .NET Framework 4.8.9181.0; IIS: 10.0
[2024-02-22 16:17:47.897 +00:00][1][Trace] - Agent.Subscribe(), Agent Enabled: True Subscriber count: 2, (SqlClientDiagnosticSubscriber, HttpDiagnosticsSubscriber)
...
[2024-02-22 16:16:03.131 +00:00][1][Trace] - {ElasticApmModule.#1} ElasticApmModule.Init was invoked and called AttemptAgentInitialization.
...
[2024-02-22 16:17:47.984 +00:00][6][Trace] - {ElasticApmModule.#2} ElasticApmModule.Init was invoked by an instance when the Agent has already been initialized.

Copy link
Member

@Mpdreamz Mpdreamz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM ❤️

@stevejgordon stevejgordon merged commit 2f7d592 into main Feb 23, 2024
12 of 13 checks passed
@stevejgordon stevejgordon deleted the enhancement/elasticapmmodule branch February 23, 2024 07:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants