Azure Monitor is a comprehensive solution for collecting, analyzing, and acting on telemetry from your cloud and on-premises environments.
Use the client library for Azure Monitor to:
Source code | Package (NPM) | API reference documentation| Product documentation Samples
npm install @azure/monitor-query
See our support policy for more details.
You can use the Azure Portal or the Azure CLI to create an Azure Monitor resource.
Instructions:
LogsQueryClient and MetricsQueryClient authenticate using a service principal.
Authentication via service principal is done by:
@azure/identity
package.Using DefaultAzureCredential
const { DefaultAzureCredential } = require("@azure/identity");
const { LogsQueryClient, MetricsQueryClient } = require("@azure/monitor-query");
const credential = new DefaultAzureCredential();
const logsQueryClient = new LogsQueryClient(credential);
// or
const metricsQueryClient = new MetricsQueryClient(credential);
More information about @azure/identity
can be found here
The LogsQueryClient
allows you to query logs, using the Kusto query language. This data can be queried in the
portal using tables like AppEvents
, AppDependencies
and others.
The MetricsQueryClient
allows you to query metrics.
The LogsQueryClient
can be used to query a Monitor workspace using the Kusto Query language.
const { LogsQueryClient } = require("@azure/monitor-query");
const { DefaultAzureCredential } = require("@azure/identity");
const azureLogAnalyticsWorkspaceId = "<the Workspace Id for your Azure Log Analytics resource>";
const logsQueryClient = new LogsQueryClient(new DefaultAzureCredential());
async function run() {
const kustoQuery = "AppEvents | limit 1";
const result = await logsQueryClient.queryLogs(azureLogAnalyticsWorkspaceId, kustoQuery);
const tablesFromResult = result.tables;
if (tablesFromResult == null) {
console.log(`No results for query '${kustoQuery}'`);
return;
}
console.log(`Results for query '${kustoQuery}'`);
for (const table of tablesFromResult) {
const columnHeaderString = table.columns
.map((column) => `${column.name}(${column.type}) `)
.join("| ");
console.log("| " + columnHeaderString);
for (const row of table.rows) {
const columnValuesString = row.map((columnValue) => `'${columnValue}' `).join("| ");
console.log("| " + columnValuesString);
}
}
}
run().catch((err) => console.log("ERROR:", err));
For more samples see here: samples.
Enabling logging may help uncover useful information about failures. In order to see a log of HTTP requests and responses, set the AZURE_LOG_LEVEL
environment variable to info
. Alternatively, logging can be enabled at runtime by calling setLogLevel
in the @azure/logger
:
import { setLogLevel } from "@azure/logger";
setLogLevel("info");
For more detailed instructions on how to enable logs, you can look at the @azure/logger package docs.
The following samples show you the various ways you can query your Log Analytics workspace:
logsQuery.ts
- Query logs in a Monitor workspacelogsQueryBatchSample.ts
- Run multiple queries, simultaneously, with a batch in a Monitor workspacemetricsQuerySample.ts
- Query metrics in a Monitor workspaceMore in-depth examples can be found in the samples folder on GitHub.
If you'd like to contribute to this library, please read the contributing guide to learn more about how to build and test the code.
This module's tests are a mixture of live and unit tests, which require you to have an Azure Monitor instance. To execute the tests you'll need to run:
rush update
rush build -t @azure/monitor-query
cd into sdk/monitor/monitor-query
sample.env
file to .env
.env
file in an editor and fill in the values.npm run test
.View our tests folder for more details.
Generated using TypeDoc