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

HostBuilderExtensions.UseElasticApm: subscribe to subscribers params #1060

Merged
merged 4 commits into from
Nov 24, 2020
Merged

HostBuilderExtensions.UseElasticApm: subscribe to subscribers params #1060

merged 4 commits into from
Nov 24, 2020

Conversation

gregkalapos
Copy link
Contributor

@gregkalapos gregkalapos commented Nov 23, 2020

Fixes #1059

In Elastic.Apm.Extensions.Hosting.HostBuilderExtensions.UseElasticApm(this IHostBuilder builder, params IDiagnosticsSubscriber[] subscribers) the params IDiagnosticsSubscriber[] subscribers were never activated. This was because of the following wrong check:

if(Agent.IsConfigured && Agent.Config.Enabled)
  if (subscribers != null && subscribers.Any() && Agent.IsConfigured) Agent.Subscribe(subscribers);

Problem here was that Agent.IsConfigured remained false until IApmAgent got resolved first - but that always runs after the check.

Proposed fix: we force the resolution directly within the UseElasticApm method:

var serviceProvider = services.BuildServiceProvider();
var agent = serviceProvider.GetService<IApmAgent>();

And do a similar check after that.

I feel doing "lazy agent loading" here makes no sense, the agent should be immediately enabled, otherwise it would e.g. not collect metrics, plus there is no guarantee that a user forces to resolve IApmAgent.

@gregkalapos gregkalapos self-assigned this Nov 23, 2020
@gregkalapos gregkalapos requested a review from russcam November 23, 2020 20:03
@apmmachine
Copy link
Contributor

apmmachine commented Nov 23, 2020

💚 Build Succeeded

the below badges are clickable and redirect to their specific view in the CI or DOCS
Pipeline View Test View Changes Artifacts preview

Expand to view the summary

Build stats

  • Build Cause: [Pull request #1060 updated]

  • Start Time: 2020-11-24T13:56:55.216+0000

  • Duration: 58 min 8 sec

Test stats 🧪

Test Results
Failed 0
Passed 13021
Skipped 0
Total 13021

Steps errors 1

Expand to view the steps failures

Build

  • Took 0 min 24 sec . View more details on here
  • Description: .ci/windows/dotnet.bat

@codecov-io
Copy link

Codecov Report

Merging #1060 (4e22ddb) into master (5b45e67) will increase coverage by 45.83%.
The diff coverage is 94.73%.

Impacted file tree graph

@@             Coverage Diff             @@
##           master    #1060       +/-   ##
===========================================
+ Coverage   33.08%   78.92%   +45.83%     
===========================================
  Files         120      148       +28     
  Lines        6190     7259     +1069     
===========================================
+ Hits         2048     5729     +3681     
+ Misses       4142     1530     -2612     
Impacted Files Coverage Δ
...ic.Apm.Extensions.Hosting/HostBuilderExtensions.cs 84.28% <84.61%> (ø)
...tic.Apm.SqlClient/SqlClientDiagnosticSubscriber.cs 86.66% <100.00%> (ø)
src/Elastic.Apm/Logging/ConsoleLogger.cs 96.72% <100.00%> (+30.05%) ⬆️
src/Elastic.Apm/Report/PayloadSenderV2.cs 88.62% <100.00%> (+88.62%) ⬆️
...rc/Elastic.Apm.Extensions.Hosting/NetCoreLogger.cs 85.71% <0.00%> (ø)
...Elasticsearch/HttpConnectionDiagnosticsListener.cs 0.00% <0.00%> (ø)
...astic.Apm.SqlClient/SqlClientDiagnosticListener.cs 76.08% <0.00%> (ø)
...icListener/AspNetCoreErrorDiagnosticsSubscriber.cs 90.90% <0.00%> (ø)
...c.Apm.GrpcClient/GrpcClientDiagnosticSubscriber.cs 90.90% <0.00%> (ø)
...DiagnosticListener/AspNetCoreDiagnosticListener.cs 85.71% <0.00%> (ø)
... and 131 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update dbad2d5...4e22ddb. Read the comment docs.

@russcam russcam added the bug Something isn't working label Nov 24, 2020
Copy link
Contributor

@russcam russcam left a comment

Choose a reason for hiding this comment

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

LGTM 👍

Agent.Setup(sp.GetService<AgentComponents>());
return apmAgent;
return newAgentInstance;
Copy link
Contributor

Choose a reason for hiding this comment

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

can agent components be resolved once, and passed to both agent instance and Agent.Setup(...) i.e.

var components = sp.GetService<AgentComponents>();
var newAgentInstance = new ApmAgent(components );
Agent.Setup(components);					
return newAgentInstance;

Also, does this mean there are potentially two instances of an agent in the application - a singleton instance created by this method, and the lazy instance on Agent that's used internally?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also, does this mean there are potentially two instances of an agent in the application - a singleton instance created by this method, and the lazy instance on Agent that's used internally?

Hahh - that's a good point - we should not new up an ApmAgent instance here. Technically they were 2 instances, but they were sharing the AgentComponents, but still - best is to just return Agent.Instance. I changed this part.

can agent components be resolved once, and passed to both agent instance and Agent.Setup(...) i.e.

With the previous change this is not needed. Otherwise I'd agree.

.Build();

fakeSubscriber.IsSubscribed.Should().BeTrue();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Should there also be a test to assert that subscribers are not subscribed when the agent is not enabled?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed - I added a test.

Add test to cover `enabled=false` case.
Resolve AgentComponents only once.
@gregkalapos gregkalapos merged commit 997d924 into elastic:master Nov 24, 2020
@gregkalapos gregkalapos deleted the ExtensionsHostingFix branch November 24, 2020 14:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
agent-dotnet bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

HostBuilderExtensions.UseElasticApm doesn't register DiagnosticsSubscribers
4 participants