question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

HttpClient calls from .NET Framework not always auto wrapped

See original GitHub issue

Describe the bug .NET Framework calls made using HttpClient are not automatically wrapped in a span within a ASP.NET Web API web app.

To Reproduce Steps to reproduce the behavior:

  1. Clone this repo
  2. Add a ASP.NET Web Application (.NET Framework) project to the sample folder (.NET 4.6.1). Select Web API template, uncheck configure for HTTPS.
  3. Add the following existing projects as references: Elastic.AP, Elastic.Apm.AspNetFullFramework
  4. Add to the <system.webserver> section in the Web.config:
<modules>
    <add name="ElasticApmModule" type="Elastic.Apm.AspNetFullFramework.ElasticApmModule, Elastic.Apm.AspNetFullFramework"/>
  </modules>
  1. Edit Web.config to have a ElasticApm:ServerUrls appSetting pointing at your instance of APM server.
  2. Edit ValuesController.Get() to contain:
// GET api/values
public IEnumerable<string> Get()
{
  var client = new HttpClient();
  var x = client.GetAsync("http://www.google.com").Result.Content.ReadAsStringAsync().Result;
  return new [] { $"Length: {x.Length}" };
}
  1. Run service and send a request: GET /api/values

Expected behavior When the /api/values transaction is logged to the APM server, it should contain a span from the HttpClient call to google. A transaction for /api/values is logged just fine, it just has no spans.

I have only seen this work out of the box with the AspNetFullFrameworkSampleApp and haven’t been able to get it to work in any of my existing .Net framework services by just adding the module reference in the web.configs (in addition to installing the nuget packages). There is so much extra going on in AspNetFullFrameworkSampleApp that it is hard for me to compare what might be causing the issue.

I have put breakpoints on various methods in HttpDiagnosticListenerImplBase like OnNext, ProcessStartEvent, etc, with none getting hit.

The following breakpoints are hit while testing:

  • ElasticApmModule.InitOnceForAllInstancesUnderLock (line 312)
  • HttpDiagnosticListener.New (line 23)

I’m guessing I’m missing something simple. Please advise.

Thanks!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
sluebbertcommented, Feb 7, 2020

So you were right about version issues, but for me it was the System.Diagnostics.DiagnosticSource dll.

If I added System.Memory.dll, added binding redirects into my web.config for System.memory and System.Diagnostics.DiagnosticSource, and then swapped out the version of System.Diagnostics.DiagnosticSource from AspNetFullFrameworkSampleApp to my test app after building, it works!

I just need to figure out how to gracefully do that without my hackory.

It started working without adding System.Memory, it just threw exceptions complaining about it missing later on.

0reactions
vhatsuracommented, Mar 19, 2020

@gregkalapos, I did fast check, and yes it seems System.Diagnostics.DiagnosticSource of 4.0.0 version is enough for netstandard2.0 for getting event from HttpClient.

Read more comments on GitHub >

github_iconTop Results From Across the Web

You're using HttpClient wrong and it is destabilizing your ...
Do not dispose of or wrap your HttpClient in a using unless you explicitly are looking for a particular behaviour (such as causing...
Read more >
Using .NET's HttpClient without following redirects
To do this in .NET we're using as usual an HttpClient , but as its default behavior is to follow redirects, a little...
Read more >
c# - Should I use the Using statement with HttpClient?
All of this happens automatically. Do not use sync calls. Make your action method async, and await on async methods of http client....
Read more >
Use the IHttpClientFactory - .NET
In this article, you'll learn how to use the IHttpClientFactory to create HttpClient types with various .NET fundamentals, such as ...
Read more >
Make HTTP requests using IHttpClientFactory in ASP.NET ...
Learn about using the IHttpClientFactory interface to manage logical HttpClient instances in ASP.NET Core.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found