1.3.1 • Published 15 days ago

@azure/monitor-query v1.3.1

Weekly downloads
-
License
MIT
Repository
-
Last release
15 days ago

Azure Monitor Workspace query client library for JavaScript

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!--(https://docs.microsoft.com/javascript/api/@azure/monitor-query)-- | Product documentation Samples

Getting started

Install the package

npm install @azure/monitor-query

Prerequisites

Create an Azure Monitor resource

You can use the Azure Portal or the Azure CLI to create an Azure Monitor resource.

Instructions:

Authenticate the client

LogsClient and MetricsClient authenticate using a service principal.

Authenticating with a service principal

Authentication via service principal is done by:

  • Creating a credential using the @azure/identity package.
  • Setting appropriate RBAC rules on your Azure Monitor resource. More information on Azure Monitor roles can be found here.

Using DefaultAzureCredential

const { DefaultAzureCredential } = require("@azure/identity");
const { LogsClient, MetricsClient } = require("@azure/monitor-query");

const credential = new DefaultAzureCredential();

const logsClient = new LogsClient(credential);
// or
const metricsClient = new MetricsClient(credential);

More information about @azure/identity can be found here

Key concepts

The LogsClient 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 MetricsClient allows you to query metrics.

Examples

Querying logs

The LogsClient can be used to query a Monitor workspace using the Kusto Query language.

const { LogsClient } = require("@azure/monitor-query");
const { DefaultAzureCredential } = require("@azure/identity");

const azureLogAnalyticsWorkspaceId = "<the Workspace Id for your Azure Log Analytics resource>";
const logsClient = new LogsClient(new DefaultAzureCredential());

async function run() {
  const kustoQuery = "AppEvents | limit 1";
  const result = logsClient.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.

Troubleshooting

Logging

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.

Next steps

The following samples show you the various ways you can query your Log Analytics workspace:

More in-depth examples can be found in the samples folder on GitHub.

Contributing

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:

  1. rush update
  2. rush build -t @azure/monitor-query
  3. cd into sdk/monitor/monitor-query
  4. Copy the sample.env file to .env
  5. Open the .env file in an editor and fill in the values.
  6. npm run test.

View our tests folder for more details.

Related projects

Impressions

1.3.1

4 months ago

1.3.0

5 months ago

1.2.0

8 months ago

1.2.0-beta.3

12 months ago

1.2.0-beta.1

1 year ago

1.2.0-beta.2

1 year ago

1.1.1

1 year ago

1.1.0-beta.1

2 years ago

1.1.0

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

3 years ago

1.0.0

3 years ago

1.0.0-beta.5

3 years ago

1.0.0-beta.4

3 years ago

1.0.0-beta.3

3 years ago

1.0.0-beta.2

3 years ago

1.0.0-beta.1

3 years ago