Azure / azure-functions-host

The host/runtime that powers Azure Functions
https://functions.azure.com
MIT License
1.92k stars 441 forks source link

Application Insights -- add custom dimensions to 'requests' telemetry items generated by the host #2617

Open johnib opened 6 years ago

johnib commented 6 years ago

Hi,

I'm really missing the ability to decorate each request with custom dimensions. I'm using the native functions integration with AppInsights, and I use ILogger -- I wouldn't want to install the AppInsights nuget and do all the initializations myself.

Optimal solution for me is to have a Dictionary as an optional input in the Function's signature.

Is this by any chance possible today? If not, can you consider this as a new feature/improvement?

Many thanks ! Jonathan

madhu2a commented 6 years ago

I am exactly looking for such feature. Thanx Johnib for posting 👍

ishepherd commented 5 years ago

I have read that this is coming, or already here?, like a few other improvements to App Insights integration that have gone in recently. (Just searching for the source I saw a month or two back.)

ishepherd commented 5 years ago

@jeffhollan Please have (whoever recently worked on app insights / functions integration) respond to this issue? I suspect it may be now "Done", but I can't remember where I read it.

mayank1495 commented 4 years ago

Can someone please let us know if we can use ILogger's LogInformation method to log CustomeDimensions in Application insights.

ishepherd commented 4 years ago

@mayank1495 you cannuse that method and they will go through to app insights as traces.

And that's fine but if we could add attributes directly to requests (as customdimensions) they would be much more easily discoverable. Presumably also reduces our app insights bills if we send fewer objects.

ishepherd commented 4 years ago

I'm really missing the ability to decorate each request with custom dimensions.

@johnib @madhu2a I found this old comment from @lmolkova ... does it work?

https://github.com/Azure/azure-webjobs-sdk/pull/1965#issuecomment-430364439

jeffhollan commented 4 years ago

@ahmedelnably in case he has an answer as well

xstof commented 4 years ago

Activity.Current.Tag(...) seems to work and translates into a custom property on the App Insights side. Would be good to have confirmation this is a supported way of working. Similarly, Activity.Current.AddBagage equally seems to work for custom tracking data which you want to pass onto any dependencies (and dependencies of those... all the way down)l. This is compatible with at least Service Bus and .NET Core Http.

fabiocav commented 4 years ago

@brettsam, this issue fell through the cracks. Can you comment?

mheskandari commented 3 years ago

Can someone please let us know if we can use ILogger's LogInformation method to log CustomeDimensions in Application insights.

if you use logger like this it automatically add it as custome property in your logs: var property = new KeyValuePair<string, string>("name","value"); _logger.LogInformation("request name value ===> {name}", property);

rjk commented 2 years ago

The code from @mheskandari works for me to add to traces table, but that doesn't include it in requests table. This makes sense: logs written from ILogger just go to traces table.

To add to customDimensions within the requests table you can do this:

httpContext.Features.Get<RequestTelemetry>().Properties["SomePropertyName"] = someData;

e.g. as described here in more detail https://www.davepaquette.com/archive/2020/03/07/enhancing-application-insights-request-telemetry.aspx

@xstof suggests above that you could instead use System.Diagnostics.Activity.Current.AddTag() to add custom dimensions to requests but that doesn't work for me now (Application Insights v2.19.0). The tags don't seem to go anywhere. Seems like it would be nice, perhaps I'm just doing it wrong.

If you want to add custom properties that are automatically added to dependencies (e.g. SQL) and traces tables you can use System.Diagnostics.Activity.Current.AddBaggage(), e.g. within a middleware class so this gets done at the start of every request. In my testing this does not get added to requests table though, so I need to do both this and the RequestTelemetry Properties as above.

string theTenantId = // get something from httpContext
System.Diagnostics.Activity.Current.AddBaggage("MyTenantId", theTenantId);