Metrics Advisor is a part of Azure Cognitive Services that uses AI perform data monitoring and anomaly detection in time series data. The service automates the process of applying models to your data, and provides a set of APIs web-based workspace for data ingestion, anomaly detection, and diagnostics - without needing to know machine learning. Use Metrics Advisor to:
Source code | Package (NPM) | API reference documentation | Product documentation | Samples
If you use the Azure CLI, replace <your-resource-group-name>
and <your-resource-name>
with your own unique names:
az cognitiveservices account create --kind MetricsAdvisor --resource-group <your-resource-group-name> --name <your-resource-name> --sku <sku level> --location <location>
@azure/ai-metrics-advisor
packageInstall the Azure Metrics Advisor client library for JavaScript with npm
:
npm install @azure/ai-metrics-advisor
MetricsAdvisorClient
or MetricsAdvisorAdministrationClient
To create a client object to access the Metrics Advisor API, you will need the endpoint
of your Metrics Advisor resource and a credential
. The Metrics Advisor clients use a Metrics Advisor key credential to authenticate.
You can find the endpoint for your Metrics Advisor resource either in the Azure Portal or by using the Azure CLI snippet below:
az cognitiveservices account show --name <your-resource-name> --resource-group <your-resource-group-name> --query "endpoint"
You will need two keys to authenticate the client:
Use the Azure Portal to browse to your Metrics Advisor resource and retrieve an subscription key, or use the Azure CLI snippet below:
az cognitiveservices account keys list --resource-group <your-resource-group-name> --name <your-resource-name>
In addition, you will also need the per-user api key from your Metrics Advisor web portal.
Once you have the two keys and endpoint, you can use the MetricsAdvisorKeyCredential
class to authenticate the clients as follows:
const {
MetricsAdvisorKeyCredential,
MetricsAdvisorClient,
MetricsAdvisorAdministrationClient
} = require("@azure/ai-metrics-advisor");
const credential = new MetricsAdvisorKeyCredential("<subscription Key>", "<API key>");
const client = new MetricsAdvisorClient("<endpoint>", credential);
const adminClient = new MetricsAdvisorAdministrationClient("<endpoint>", credential);
MetricsAdvisorClient
is the primary querying interface for developers using the Metrics Advisor client library. It provides asynchronous methods to access a specific use of Metrics Advisor, such as listing incidents, retrive root causes of incidents, retrieving original time series data and time series data enriched by the service.
MetricsAdvisorAdministrationClient
is the interface responsible for managing entities in the Metrics Advisor resources, such as managing data feeds, anomaly detection configurations, anomaly alerting configurations.
A DataFeed
is what Metrics Advisor ingests from your data source, such as Cosmos DB or a SQL server. A data feed contains rows of:
A Metric
is a quantifiable measure that is used to monitor and assess the status of a specific business process. It can be a combination of multiple time series values divided into dimensions. For example a web health metric might contain dimensions for user count and the en-us market.
AnomalyDetectionConfiguration
is required for every time series, and determines whether a point in the time series is an anomaly.
After a detection configuration is applied to metrics, Incident
s are generated whenever any series within it has an Anomaly
.
You can configure which anomalies should trigger an Alert
. You can set multiple alerts with different settings. For example, you could create an alert for anomalies with lower business impact, and another for more important alerts.
Metrics Advisor lets you create and subscribe to real-time alerts. These alerts are sent over the internet, using a Hook
.
Please refer to the Metrics Advisory Glossary documentation page for a comprehensive list of concepts.
The following section provides several JavaScript code snippets illustrating common patterns used in the Metrics Advisor client libraries.
Metrics Advisor supports connecting different types of data sources. Here is a sample to ingest data from SQL Server.
const {
MetricsAdvisorKeyCredential,
MetricsAdvisorAdministrationClient
} = require("@azure/ai-metrics-advisor");
async function main() {
// You will need to set these environment variables or edit the following values
const endpoint = process.env["METRICS_ADVISOR_ENDPOINT"] || "<service endpoint>";
const subscriptionKey = process.env["METRICS_ADVISOR_SUBSCRIPTION_KEY"] || "<subscription key>";
const apiKey = process.env["METRICS_ADVISOR_API_KEY"] || "<api key>";
const sqlServerConnectionString =
process.env["METRICS_ADVISOR_SQL_SERVER_CONNECTION_STRING"] ||
"<connection string to SQL Server>";
const sqlServerQuery =
process.env["METRICS_ADVISOR_AZURE_SQL_SERVER_QUERY"] || "<SQL Server query to retrive data>";
const credential = new MetricsAdvisorKeyCredential(subscriptionKey, apiKey);
const adminClient = new MetricsAdvisorAdministrationClient(endpoint, credential);
const created = await createDataFeed(adminClient, sqlServerConnectionString, sqlServerQuery);
console.log(`Data feed created: ${created.id}`);
}
async function createDataFeed(adminClient, sqlServerConnectionString, sqlServerQuery) {
const metric = [
{
name: "revenue",
displayName: "revenue",
description: "Metric1 description"
},
{
name: "cost",
displayName: "cost",
description: "Metric2 description"
}
];
const dimension = [
{ name: "city", displayName: "city display" },
{ name: "category", displayName: "category display" }
];
const dataFeedSchema = {
metrics: metric,
dimensions: dimension,
timestampColumn: null
};
const dataFeedIngestion = {
ingestionStartTime: new Date(Date.UTC(2020, 5, 1)),
ingestionStartOffsetInSeconds: 0,
dataSourceRequestConcurrency: -1,
ingestionRetryDelayInSeconds: -1,
stopRetryAfterInSeconds: -1
};
const granualarity = {
granularityType: "Daily"
};
const source = {
dataSourceType: "SqlServer",
dataSourceParameter: {
connectionString: sqlServerConnectionString,
query: sqlServerQuery
}
};
const options = {
rollupSettings: {
rollupType: "AutoRollup",
rollupMethod: "Sum",
rollupIdentificationValue: "__CUSTOM_SUM__"
},
missingDataPointFillSettings: {
fillType: "SmartFilling"
},
accessMode: "Private",
admins: ["xyz@example.com"]
};
console.log("Creating Datafeed...");
const result = await adminClient.createDataFeed({
name: "test_datafeed_" + new Date().getTime().toFixed(),
source,
granularity,
schema: dataFeedSchema,
ingestionSettings: dataFeedIngestion,
options
});
return result;
}
After we start the data ingestion, we can check the ingestion status.
const {
MetricsAdvisorKeyCredential,
MetricsAdvisorAdministrationClient
} = require("@azure/ai-metrics-advisor");
async function main() {
// You will need to set these environment variables or edit the following values
const endpoint = process.env["METRICS_ADVISOR_ENDPOINT"] || "<service endpoint>";
const subscriptionKey = process.env["METRICS_ADVISOR_SUBSCRIPTION_KEY"] || "<subscription key>";
const apiKey = process.env["METRICS_ADVISOR_API_KEY"] || "<api key>";
const dataFeedId = process.env["METRICS_DATAFEED_ID"] || "<data feed id>";
const credential = new MetricsAdvisorKeyCredential(subscriptionKey, apiKey);
const adminClient = new MetricsAdvisorAdministrationClient(endpoint, credential);
await checkIngestionStatus(
adminClient,
dataFeedId,
new Date(Date.UTC(2020, 8, 1)),
new Date(Date.UTC(2020, 8, 12))
);
}
async function checkIngestionStatus(adminClient, datafeedId, startTime, endTime) {
// This shows how to use for-await-of syntax to list status
console.log("Checking ingestion status...");
for await (const status of adminClient.listDataFeedIngestionStatus(
datafeedId,
startTime,
endTime
)) {
console.log(` [${status.timestamp}] ${status.status} - ${status.message}`);
}
}
We need an anomaly detection configuration to determine whether a point in the time series is an anomaly. While a default detection configuration is automatically applied to each metric, you can tune the detection modes used on your data by creating a customized anomaly detection configuration.
const {
MetricsAdvisorKeyCredential,
MetricsAdvisorAdministrationClient
} = require("@azure/ai-metrics-advisor");
async function main() {
// You will need to set these environment variables or edit the following values
const endpoint = process.env["METRICS_ADVISOR_ENDPOINT"] || "<service endpoint>";
const subscriptionKey = process.env["METRICS_ADVISOR_SUBSCRIPTION_KEY"] || "<subscription key>";
const apiKey = process.env["METRICS_ADVISOR_API_KEY"] || "<api key>";
const metricId = process.env["METRICS_ADVISOR_METRIC_ID"] || "<metric id>";
const credential = new MetricsAdvisorKeyCredential(subscriptionKey, apiKey);
const adminClient = new MetricsAdvisorAdministrationClient(endpoint, credential);
const detectionConfig = await configureAnomalyDetectionConfiguration(adminClient, metricId);
console.log(`Detection configuration created: ${detectionConfig.id}`);
}
async function configureAnomalyDetectionConfiguration(adminClient, metricId) {
console.log(`Creating an anomaly detection configuration on metric '${metricId}'...`);
return await adminClient.createMetricAnomalyDetectionConfiguration({
name: "test_detection_configuration" + new Date().getTime().toString(),
metricId,
wholeSeriesDetectionCondition: {
smartDetectionCondition: {
sensitivity: 100,
anomalyDetectorDirection: "Both",
suppressCondition: {
minNumber: 1,
minRatio: 1
}
}
},
description: "Detection configuration description"
});
}
We use hooks subscribe to real-time alerts. In this example, we create a webhook for the Metrics Advisor service to POST the alert to.
const {
MetricsAdvisorKeyCredential,
MetricsAdvisorAdministrationClient
} = require("@azure/ai-metrics-advisor");
async function main() {
// You will need to set these environment variables or edit the following values
const endpoint = process.env["METRICS_ADVISOR_ENDPOINT"] || "<service endpoint>";
const subscriptionKey = process.env["METRICS_ADVISOR_SUBSCRIPTION_KEY"] || "<subscription key>";
const apiKey = process.env["METRICS_ADVISOR_API_KEY"] || "<api key>";
const credential = new MetricsAdvisorKeyCredential(subscriptionKey, apiKey);
const adminClient = new MetricsAdvisorAdministrationClient(endpoint, credential);
const hook = await createWebhookHook(adminClient);
console.log(`Webhook hook created: ${hook.id}`);
}
async function createWebhookHook(adminClient) {
console.log("Creating a webhook hook");
const hook = {
hookType: "Webhook",
name: "web hook " + new Date().getTime().toFixed(),
description: "description",
hookParameter: {
endpoint: "https://example.com/handleAlerts",
username: "username",
password: "password"
// certificateKey: "certificate key",
// certificatePassword: "certificate password"
}
};
return await adminClient.createHook(hook);
}
Then let's configure in which conditions an alert needs to be triggered and which hooks to send the alert.
const {
MetricsAdvisorKeyCredential,
MetricsAdvisorAdministrationClient
} = require("@azure/ai-metrics-advisor");
async function main() {
// You will need to set these environment variables or edit the following values
const endpoint = process.env["METRICS_ADVISOR_ENDPOINT"] || "<service endpoint>";
const subscriptionKey = process.env["METRICS_ADVISOR_SUBSCRIPTION_KEY"] || "<subscription key>";
const apiKey = process.env["METRICS_ADVISOR_API_KEY"] || "<api key>";
const detectionConfigId = process.env["METRICS_ADVISOR_DETECTION_CONFIG_ID"] || "<detection id>";
const hookId = process.env["METRICS_ADVISOR_HOOK_ID"] || "<hook id>";
const credential = new MetricsAdvisorKeyCredential(subscriptionKey, apiKey);
const adminClient = new MetricsAdvisorAdministrationClient(endpoint, credential);
const alertConfig = await configureAlertConfiguration(adminClient, detectionConfigId, [hookId]);
console.log(`Alert configuration created: ${alertConfig.id}`);
}
async function configureAlertConfiguration(adminClient, detectionConfigId, hookIds) {
console.log("Creating a new alerting configuration...");
const metricAlertingConfig = {
detectionConfigurationId: detectionConfigId,
alertScope: {
scopeType: "All"
},
alertConditions: {
severityCondition: { minAlertSeverity: "Medium", maxAlertSeverity: "High" }
},
snoozeCondition: {
autoSnooze: 0,
snoozeScope: "Metric",
onlyForSuccessive: true
}
};
return await adminClient.createAnomalyAlertConfiguration({
name: "test_alert_config_" + new Date().getTime().toString(),
crossMetricsOperator: "AND",
metricAlertConfigurations: [metricAlertingConfig],
hookIds,
description: "Alerting config description"
});
}
We can query the alerts and anomalies.
const { MetricsAdvisorKeyCredential, MetricsAdvisorClient } = require("@azure/ai-metrics-advisor");
async function main() {
// You will need to set these environment variables or edit the following values
const endpoint = process.env["METRICS_ADVISOR_ENDPOINT"] || "<service endpoint>";
const subscriptionKey = process.env["METRICS_ADVISOR_SUBSCRIPTION_KEY"] || "<subscription key>";
const apiKey = process.env["METRICS_ADVISOR_API_KEY"] || "<api key>";
const alertConfigId = process.env["METRICS_ADVISOR_ALERT_CONFIG_ID"] || "<alert config id>";
const credential = new MetricsAdvisorKeyCredential(subscriptionKey, apiKey);
const client = new MetricsAdvisorClient(endpoint, credential);
const alertIds = await queryAlerts(
client,
alertConfigId,
new Date(Date.UTC(2020, 8, 1)),
new Date(Date.UTC(2020, 8, 12))
);
if (alertIds.length > 1) {
// query anomalies using an alert id.
await queryAnomaliesByAlert(client, alertConfigId, alertIds[0]);
} else {
console.log("No alerts during the time period");
}
}
async function queryAlerts(client, alertConfigId, startTime, endTime) {
let alertIds = [];
for await (const alert of client.listAlertsForAlertConfiguration(
alertConfigId,
startTime,
endTime,
"AnomalyTime"
)) {
alertIds.push(alert.id);
}
return alertIds;
}
async function queryAnomaliesByAlert(client, alertConfigId, alertId) {
console.log(
`Listing anomalies for alert configuration '${alertConfigId}' and alert '${alertId}'`
);
for await (const anomaly of client.listAnomaliesForAlert(alertConfigId, alertId)) {
console.log(
` Anomaly ${anomaly.severity} ${anomaly.status} ${anomaly.dimension} ${anomaly.timestamp}`
);
}
}
You can set the following environment variable to see debug logs when using this library.
export DEBUG=azure*
Please take a look at the samples directory for detailed examples on how to use this library.
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.
Generated using TypeDoc