有一个 example如何查询
- LogAnalytics 工作区日志或
- 单个资源的指标 使用 Node.Js:
但我找不到是否有从 AppInsights 或直接从资源查询日志的选项。
我需要它来自动报告性能,因此我计划查询 requests
表(我们使用 https://github.com/microsoft/ApplicationInsights-Java 发送日志)。目前,报告是使用 AppInsights 的性能 Blade 手动完成的 - 检查 URL 上具有特定过滤器的请求的平均值和 99%
最佳答案
您可以使用诊断设置将日志从应用程序引入到 Log Analytics 工作区。例如,如果您在 WebApp 中托管应用程序,则可以发送 AppServiceHTTPLogs
。然后在 Node.JS 应用程序中,您可以使用 @azure/monitor-query
包进行类似的查询:
let dataset=AppServiceHTTPLogs
| where CsHost == 'PUT_YOUR_HOSTNAME_HERE'
| where ScStatus == 200
| where CsUriStem contains 'FILTER_BY_URL_IF_YOU_NEED_IT';
dataset
| summarize arg_max(TimeTaken, CsUriStem)
| union(dataset
| summarize avg(TimeTaken), percentiles(TimeTaken, 99)
| extend CsUriStem='Overall')
这个非常接近应用洞察中的性能 Blade 。 然后你的整个应用程序可能是
const azureLogAnalyticsWorkspaceId = "WORKSPACE_ID";
const logsQueryClient = new LogsQueryClient(new DefaultAzureCredential());
export async function runWebAppPerformance(startDate: Date, endDate: Date) {
const query = "PUT_YOUR_QUERY_HERE";
const result = await logsQueryClient.queryWorkspace(
azureLogAnalyticsWorkspaceId,
query,
{
startTime: startDate, endTime: endDate
}
);
if (result.status === LogsQueryResultStatus.Success) {
const tablesFromResult: LogsTable[] = result.tables;
if (tablesFromResult.length === 0) {
console.log(`No results for query`);
return;
}
processTables(tablesFromResult);
} else {
console.log(`Error processing the query - ${result.partialError}`);
}
}
async function processTables(tablesFromResult: LogsTable[]) {
const table = tablesFromResult[0];
const urlIndex = table.columnDescriptors.findIndex(c => c.name === "CsUriStem");
const timeTakenIndex = table.columnDescriptors.findIndex(c => c.name === "TimeTaken");
const avgIndex = table.columnDescriptors.findIndex(c => c.name === "avg_TimeTaken");
const ninetyNineindex = table.columnDescriptors.findIndex(c => c.name === "percentile_TimeTaken_99");
for (const row of table.rows) {
if (row[urlIndex] === "Overall"){
console.log(`${row[urlIndex]} (ms):`);
console.log(`Average: ${row[avgIndex]}; \t 99%: ${row[ninetyNineindex]}`);
}
else {
console.log(`MAX (ms)`);
console.log(`${row[urlIndex]}: \t ${row[timeTakenIndex]}`);
}
}
}
关于node.js - 如何使用 Node.JS 查询 Azure App Insights 日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72561612/