我尝试使用
@azure/identity
和 applicationinsights
在我的 Node js 应用程序中使用 DefaultAzureCredentials 进行应用程序洞察,这是我的代码
const { DefaultAzureCredential } = require('@azure/identity');
const appInsights = require("applicationinsights");
appInsights.setup(process.env.APPLICATIONINSIGHTS_CONNECTION_STRING || config.applicationInsights.connectionString)
.setAutoCollectConsole(true)
.setSendLiveMetrics(true)
.start();
const credential = new DefaultAzureCredential();
const client = appInsights.defaultClient;
client.config.aadTokenCredential = credential;
client.trackEvent({ name: 'testEvent', properties: { customProperty: 'customValue' } });
我已正确配置环境变量以使用 DefaultAzureCredential
$env:AZURE_CLIENT_ID="XXXX-XXXX-XXXX-XXXX-XXXXXXX"
$env:AZURE_TENANT_ID="XXXX-XXXX-XXXX-XXXX-XXXXXXX"
$env:AZURE_CLIENT_SECRET="XXXXXXXXXXXX"
我正在捕获 const credential = new DefaultAzureCredential();它看起来像
DefaultAzureCredential {
_sources: [
EnvironmentCredential { _credential: [ClientSecretCredential] },
UnavailableDefaultCredential {
credentialName: 'createDefaultWorkloadIdentityCredential',
credentialUnavailableErrorMessage: 'WorkloadIdentityCredential: is unavailable. federatedTokenFilePath is a required parameter. In DefaultAzureCredential and ManagedIdentityCredential, this can be provided as an environment variable - "AZURE_FEDERATED_TOKEN_FILE".\n' +
' See the troubleshooting guide for more information: https://aka.ms/azsdk/js/identity/workloadidentitycredential/troubleshoot'
},
ManagedIdentityCredential { implProvider: [MsalMsiProvider] },
AzureCliCredential {
additionallyAllowedTenantIds: [],
timeout: undefined
},
AzurePowerShellCredential {
additionallyAllowedTenantIds: [],
timeout: undefined
},
AzureDeveloperCliCredential {
additionallyAllowedTenantIds: [],
timeout: undefined
}
]
}
并且在天蓝色应用程序洞察中提供基于角色的访问权限,但我无法记录日志,并且失败时不显示应用程序洞察日志。
您在 Application Insights 中遇到的跟踪和遥测数据丢失的问题可能与您的 Azure AD 应用程序的角色分配有关。
Monitoring Metrics Publisher
。这将使您的应用程序无需单独的用户权限即可发送遥测数据。Monitoring Metrics Publisher
-> 选择将访问权限分配给 user,group,service principal
或 managed identity
-> 选择成员并点击Review+Assign
。通过添加上述角色,我可以在我的应用程序洞察中看到痕迹。