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

Activity.Current null in Azure Function when using the latest version of System.Diagnostics.DiagnosticSource #7278

Closed
johannesoenema opened this issue Apr 7, 2021 · 11 comments

Comments

@johannesoenema
Copy link

When using Activity.Current to add custom tags to request telemetry, we encounter the problem that Activity.Current is null. We have currently not a way to add custom telemetry to request. The following code will not work with the latest version of System.Diagnostics.DiagnosticSource:

[FunctionName(nameof(Function1))]
public static void Run([TimerTrigger("0 * * * * *")] TimerInfo myTimer)
{
    Activity.Current.AddTag("Test", "Test");
}

Information

  • TargetFramework: 3.1
  • Microsoft.NET.Sdk.Functions 3.0.11
  • System.Diagnostics.DiagnosticSource 4.7.1

Repro steps

We use EFCore.BulkExtensions which has a dependency on version 4.7.1 of System.Diagnostics.DiagnosticSource and because of that Activity.Current isn't working anymore. See repro here: https://github.com/johannesoenema/ActivityIssue

Also very weird, Activity.Current is null according to the code, but in the debug window it isn't null...
image

Expected behavior

Activity.Current is not null.

Actual behavior

Activity.Current is null.

Known workarounds

Downgrading to version 4.6 of System.Diagnostics.DiagnosticSource:

<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="4.6.0">
   <NoWarn>NU1605</NoWarn>
</PackageReference>

Of course we prefer to use latest version of packages.

Related information

This issue is already described in another issue #5014. Because it is a separate issue from the original issue, I open a new one as suggested.

@brettsam suggested that this change (removing System.Diagnostics.DiagnosticSource from the unification) could be related: #4941

@ghost ghost assigned yojagad Apr 7, 2021
@v-bbalaiagar v-bbalaiagar self-assigned this Apr 8, 2021
@v-bbalaiagar
Copy link

Tagging @brettsam / @fabiocav , for more inputs

@plamber
Copy link

plamber commented Apr 11, 2021

Hello,
we are experiencing the a similar issue when referencing the Microsoft.Graph package 3.29.0 which comes with the "System.Diagnostics.DiagnosticSource" version 4.7.1.

Similarly you will experience the same issue if you reference Microsoft.ApplicationInsights.AspNetCore 2.16.0 >= in any of your packages getting "System.Diagnostics.DiagnosticSource" version 5.0.0.
The last one might be required if you want to create your own TelemetryInitializers

br,
Patrick

@Gilligan128
Copy link

We are also getting this issue. This is a glaring hole in our ability to observe our functions.

@haktech
Copy link

haktech commented Jun 1, 2021

Getting this as well. Any workaround or update?

@kashimiz kashimiz modified the milestones: Traiged, Triaged Jun 8, 2021
@BlakeWills
Copy link

BlakeWills commented Jun 22, 2021

I think I've been running into this too, although the behaviour I'm experience is slightly different, especially around Activity.Current being null in code vs in the debug windows / tooltips.

For example, the screenshot below shows Activity.Current as null but somehow the traceid is included in the string.

image

On top of this, if you inspect an instance of System.Diagnostics.Activity, all of the new properties introduced in System.Diagnostics.DiagnosticSource v5.0.0 throw an ArgumentException:

image

It looks like a much older version of System.Diagnostics.DiagnosticSource is being loaded than the one directly referenced, which I can't work around.

Investigative information

Code:

  • TargetFramework: 3.1
  • AzureFunctionsVersion: 3
  • Microsoft.NET.Sdk.Functions 3.0.13
  • System.Diagnostics.DiagnosticSource: 5.0.0 and 5.0.1

Tools:

  • Visual Studio Version: 16.9.4
  • Core Tools Version: 3.0.3568 Commit hash: e30a0ede85fd498199c28ad699ab2548593f759b (64-bit)
  • Function Runtime Version: 3.0.15828.0

Please see the following repo:

https://github.com/BlakeWills/SystemDiagnosticsFunction/blob/master/SystemDiagnosticsFunction

Once cloned, start debugging and you should see that:

  1. Activity.Current is null in all VS windows and tooltips.
  2. Activity.Current.TraceId is included in responseMessage
  3. If you hover over activity you should see that all of the properties introduced in System.Diagnostics.DiagnosticSource v5.0.0 throw an ArgumentException.

@alefranz
Copy link

alefranz commented Oct 6, 2021

@brettsam @fabiocav Any chance this could be looked into?
While there is a working fix, it is a subtle bug and also not easy to identify (given the different behavior observed by the debugger)
I am sure many people has "wasted" quite some time due to this.

@Zenuka
Copy link

Zenuka commented Oct 7, 2021

The fix unfortunately doesn't work for everyone (or at least me). I'm also open to alternatives that add data to request telemetry?

Edit

Scratch that, after talking to @johannesoenema it seems that the workaround is working when deployed to Azure but not locally. So downgrading to a lower version (4.x) works in Azure.

@fhtino
Copy link

fhtino commented Dec 15, 2021

I cannot downgrade. My project references EntityFramework.SqlServer which requires 4.71

Error	NU1605	Detected package downgrade: System.Diagnostics.DiagnosticSource from 4.7.1 to 4.6.0. 
Reference the package directly from the project to select a different version.   
[SNIP....]  -> 
Microsoft.EntityFrameworkCore.SqlServer 3.1.21 -> 
Microsoft.EntityFrameworkCore.Relational 3.1.21 -> 
Microsoft.EntityFrameworkCore 3.1.21 -> 
System.Diagnostics.DiagnosticSource (>= 4.7.1) 

Is there any other solution?

Like other people, I use Activity.Current.AddTag to inject custom data into current Application Insights request data. Is there any other solution to inject that data into current request?

@dlebee
Copy link

dlebee commented Jan 4, 2022

I have the same issue I can't downgrade is there anyone finding a solution for this?

@johannesoenema
Copy link
Author

For the ones that experience this issue and can't downgrade, the issue seems to be fixed. I tried again when migrating to .NET 6 and now it works again (locally and in Azure). I did the following change:

image

So upgrading to .NET 6 and v4 of Azure Function and upgrading to latest version of System.Diagnostics.DiagnosticSource.

Result when logging locally to App insights:
image

Result when logging in Azure to App insights:
image

I will close this issue, since it is fixed and downgrading is no longer needed.

@fhtino
Copy link

fhtino commented Jan 4, 2022

In my opinion, requesting to upgrade to .NET 6 is not a perfect solution. There are cases where it's not possible to switch to .NET 6 or it requires time/effort not available in the mid term. /my2cents

@Azure Azure locked as resolved and limited conversation to collaborators Feb 3, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests