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

Current transaction is null for ASP.NET Web API #602

Closed
SergeyKleyman opened this issue Nov 14, 2019 · 8 comments
Closed

Current transaction is null for ASP.NET Web API #602

SergeyKleyman opened this issue Nov 14, 2019 · 8 comments
Labels
bug Something isn't working from a real user This issue is from a real user (i.e., not an issue we found during internal testing) from discuss.elastic.co The issue is from a user at discuss.elastic.co .NET Framework

Comments

@SergeyKleyman
Copy link
Contributor

Mentioned for the first time - #83 (comment).
Brought up by another user at discuss.

@SergeyKleyman SergeyKleyman added bug Something isn't working .NET Framework from discuss.elastic.co The issue is from a user at discuss.elastic.co from a real user This issue is from a real user (i.e., not an issue we found during internal testing) labels Nov 14, 2019
@kodigo35
Copy link
Contributor

Hello, i have this issue with WCF support on 1.2 release.

CurrentTransaction is null when calling in WCF services, due to AsyncLocal

i fixed it temporarly using httpcontext.current to store CurrentTransaction and CurrentSpan.

Is there a workaround for this issue ?

@SergeyKleyman
Copy link
Contributor Author

i fixed it temporarly using httpcontext.current to store CurrentTransaction and CurrentSpan.

Is there a workaround for this issue ?

It sounds like you have a workaround (using HttpContext.Current) or did I misunderstand what you meant by "i fixed it temporarly"?

Unfortunately, at the moment (as of agent version 1.2) there is no fix for this issue available out of the box. We accept community PRs if you would like to give it a try.

BTW I have (albeit not 100% verified) understanding why CurrentTransaction using AsyncLocal doesn't work for WCF but are you sure CurrentSpan doesn't work neither?

@kodigo35
Copy link
Contributor

thx for your reply

i have a workaround for WCF but i am not sure it is convenient for .net api calls.

i have to make more tests on basic http api calls using async tasks.

i guess asynclocal use has been made for a particular reason

@sluebbert
Copy link

@kodigo35,

Is your work around a short one that can be shared here as a snippet or is it lengthy? We are bumping up against this too for our WCF services. I'm suspicious this is also causing auto wrapping of outgoing HTTP calls to not be collected in these services as well.

Thanks!

@kodigo35
Copy link
Contributor

@sluebbert sorry for delay, my workaround is pretty simple.

on which branch do you want me to PR ?

@sluebbert
Copy link

@kodigo35 thanks for following up. I think we would rather live with the issue for now if it involves a patch to this repo vs making a work around outside of it within our code. We are trying to stick with the official releases.

@kodigo35
Copy link
Contributor

ok no prb, here's what i did if you want to integrate it later
CurrentExecutionSegmentsContainer.cs

               public Span CurrentSpan
		{
			get
			{
				if (HttpContext.Current.Items["Elastic.Apm.CurrentSpan"] != null)
				{
					return HttpContext.Current.Items["Elastic.Apm.CurrentSpan"] as Span;
				}

				return null;
			}

			set => HttpContext.Current.Items["Elastic.Apm.CurrentSpan"] = value;
		}

		public Transaction CurrentTransaction
		{
			get
			{
				if (HttpContext.Current.Items["Elastic.Apm.CurrentTransaction"] != null)
				{
					return HttpContext.Current.Items["Elastic.Apm.CurrentTransaction"] as Transaction;
				}

				return null;
			}

			set => HttpContext.Current.Items["Elastic.Apm.CurrentTransaction"] = value;
		}

@russcam
Copy link
Contributor

russcam commented Dec 17, 2020

@sluebbert, @kodigo35

#992 introduced an ICurrentExecutionSegmentsContainer implementation that uses both HttpContext and AsyncLocal<T> to get/set the current transaction/span. This is in version 1.7.0 onwards. If you're running Web API or WCF in IIS and using the ElasticApmModule, this will address the issue.

Closing this as I believe this is now addressed.

@russcam russcam closed this as completed Dec 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working from a real user This issue is from a real user (i.e., not an issue we found during internal testing) from discuss.elastic.co The issue is from a user at discuss.elastic.co .NET Framework
Projects
None yet
Development

No branches or pull requests

4 participants