如何为DependencyTelemetry设置Application Insights AuthenticatedUserId?

时间:2018-07-11 15:06:13

标签: c# azure azure-functions azure-application-insights

在Azure功能中,我正在提交自定义遥测。

//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();
}

可以在Application Insights中成功跟踪事件和相关性,但是DependencyTelemetry缺少IdAuthenticatedUserId信息。

user_iduser_authenticatedid是否受依赖关系支持?


回答问题:

  

您是否有任何TelemetryInitializer或TelemetryProcessor是   修改这些字段?

不。就是上面代码中显示的内容。

  

如何创建TelemetryClient实例?

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

我认为您必须为实例设置上下文

我也尝试过但结果相同,在这种情况下,我的代码看起来更像这样:

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

还请分享一个示例,说明Application Insights的分析视图中显示的事件和相关性

在这些示例中,我并没有尝试设置“ user_id”,但确实适用于事件。

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

依赖项

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 个答案:

答案 0 :(得分:1)

UserIdUserAuthenticatedUserId支持所有类型,包括DependencyTelemetry。因此,如果PortalUser.Id.ToString();返回非空值,则上面的代码也应显示依赖项的userid等。

您是否也正在修改这些字段的TelemetryInitializerTelemetryProcessor

答案 1 :(得分:0)

对于您在Azure Functions中使用Application Insights的特定方案:

  • 支持user_authenticatedid
  • 默认情况下,user_id设置为“ Azure”

如何创建TelemetryClient实例? 我认为您必须为实例设置上下文:

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";