1
votes

Within an Azure Function I am submitting custom telemetry.

//First an event
EventTelemetry eventTelemetry = new EventTelemetry()
{
    Name = $"Authenticated user: {portalUser.FullName} - ({portalUser.Id})"
};

ConfigureTelemetry(eventTelemetry);

TelemetryClient.TrackEvent(eventTelemetry);

//Then a dependency
DependencyTelemetry dependencyTelemetry = new DependencyTelemetry()
{
    Type = "SOAP",
    Name = name,
    Target = target,
    Data = data,
    Timestamp = DateTime.UtcNow,
    Success = success
};

ConfigureTelemetry(dependencyTelemetry);

TelemetryClient.TrackDependency(dependencyTelemetry);

...

//Set shared properties
void ConfigureTelemetry(ITelemetry telemetry)
{
    telemetry.Context.Operation.Id = FunctionSettings.InvocationId;
    telemetry.Context.Operation.ParentId = FunctionSettings.InvocationId;
    telemetry.Context.Operation.Name = FunctionSettings.FunctionName;

    telemetry.Context.User.Id = PortalUser.Id.ToString();
    telemetry.Context.User.AuthenticatedUserId = PortalUser.Id.ToString();
}

The event and dependency are succesfully tracked in Application Insights, however the DependencyTelemetry is missing the Id and AuthenticatedUserId information.

Is user_id and user_authenticatedid supported for dependencies?


In response to questions:

Do you any TelemetryInitializer or TelemetryProcessor which is modifying these fields as well?

No. Just what is shown in the code above.

How do you create the instance of TelemetryClient?

return new TelemetryClient()
{
    InstrumentationKey = ApplicationSettings.ApplicationInsightsKey
};

In my opinion you have to set the context for the instance

I have also tried that but same result, in which case my code looks more like this:

void ConfigureTelemetry()
{
    ...
    TelemetryClient.Context.User.Id = PortalUser.Id.ToString();
    TelemetryClient.Context.User.AuthenticatedUserId = PortalUser.Id.ToString();
}

please also share an example for the event and dependency shown in Application Insights's analytics view

In these examples I wasn't trying to set `user_id' but that does work for events.

customEvents

timestamp [UTC] | 2018-07-11T16:14:15.48Z   
name | Authenticated user: Cenajog2 Cenajog2 - (127a897f-d16f-e811-810a-3863bb343b78)   
itemType | customEvent  
operation_Name | TestOperation
operation_Id | c03d657f-6b0b-483e-9a32-c592fd2af701 
operation_ParentId | c03d657f-6b0b-483e-9a32-c592fd2af701   
user_AuthenticatedId | 127a897f-d16f-e811-810a-3863bb343b78 
client_Type | PC    
client_IP | 0.0.0.0 
client_City | London    
client_StateOrProvince | England    
client_CountryOrRegion | United Kingdom 
cloud_RoleInstance | RD00155D8C9B54 
appId | 824c86f8-29be-4a01-b242-44bfe1915520    
appName | dev
iKey | 9921a9e7-0c07-49fd-bd71-a9e76b9906bc 
sdkVersion | dotnet:2.5.1-172   
itemId | 7b2708c1-8525-11e8-a89a-2fa60245d417   
itemCount | 1

dependencies

timestamp [UTC] | 2018-07-11T16:14:15.48Z   
id | yP475Jc3ZwY=   
target | Development    
type | SOAP 
name | RetrieveMultiple 
success | True  
performanceBucket | >=5min  
itemType | dependency   
operation_Name | TestOperation
operation_Id | c03d657f-6b0b-483e-9a32-c592fd2af701 
operation_ParentId | c03d657f-6b0b-483e-9a32-c592fd2af701
client_Type | PC
client_IP | 0.0.0.0
client_City | London
client_StateOrProvince | England
client_CountryOrRegion | United Kingdom
cloud_RoleInstance | RD00155D8C9B54
appId | 824c86f8-29be-4a01-b242-44bfe1915520
appName | dev
iKey | 9921a9e7-0c07-49fd-bd71-a9e76b9906bc
sdkVersion | dotnet:2.5.1-172
itemId | 7b2708c0-8525-11e8-a89a-2fa60245d417
itemCount | 1
2

2 Answers

1
votes

UserId and UserAuthenticatedUserId are supported for all types, including DependencyTelemetry. So the above code should show userid etc for Dependencies as well, if PortalUser.Id.ToString(); returns non empty value.

Do you any TelemetryInitializer or TelemetryProcessor which is modifying these fields as well?

0
votes

for your specific scenario, where you are using Application Insights in Azure Functions:

  • user_authenticatedid is supported
  • user_id is set to "Azure" by default

How do you create the instance of TelemetryClient? In my opinion you have to set the context for the instance:

private static string key = TelemetryConfiguration.Active.InstrumentationKey = System.Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY", EnvironmentVariableTarget.Process);
private static TelemetryClient telemetry = new TelemetryClient() { InstrumentationKey = key };
telemetry.Context.User.AuthenticatedUserId = "whatEver";