diff --git a/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/README.md b/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/README.md index 5dbe8596de41a..2d97d2e67a414 100644 --- a/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/README.md +++ b/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/README.md @@ -16,7 +16,7 @@ Azure Cognitive Services Metrics Advisor is a cloud service that uses machine le Install the Azure Metrics Advisor client library for .NET with [NuGet][nuget]: ```PowerShell -dotnet add package Azure.AI.MetricsAdvisor --version 1.0.0 +dotnet add package Azure.AI.MetricsAdvisor ``` ### Prerequisites @@ -131,41 +131,59 @@ var adminClient = new MetricsAdvisorAdministrationClient(new Uri(endpoint), new ### MetricsAdvisorAdministrationClient -`MetricsAdvisorAdministrationClient` is the interface responsible for managing entities in the Metrics Advisor resource. It provides synchronous and asynchronous methods for tasks such as creating and updating data feeds, anomaly detection configurations, and anomaly alerting configurations. +`MetricsAdvisorAdministrationClient` is the interface responsible for managing entities in the Metrics Advisor resource. It provides synchronous and asynchronous methods for tasks such as creating and updating data feeds, anomaly detection configurations, and anomaly alert configurations. ### Data Feed -A `DataFeed` ingests data from your data source, such as CosmosDB or a SQL server, and makes it available for the Metrics Advisor service. It's the entry point of data, and therefore, the first required agent to be set before anomaly detection can take place. See the sample [Create a data feed from a data source](#create-a-data-feed-from-a-data-source) below for more information. +A `DataFeed` periodically ingests tables of aggregated data from your data source, such as CosmosDB or a SQL server, and makes it available for the Metrics Advisor service. It's the entry point of data, and therefore, the first required agent to be set before anomaly detection can take place. See the sample [Create a data feed from a data source](#create-a-data-feed-from-a-data-source) below for more information. ### Data Feed Metric -A `DataFeedMetric`, or simply "metric", is a quantifiable measure to be monitored by the Metrics Advisor service. It could be the cost of a product over the months, or even a daily measure of temperature. The service will monitor how this value varies over time in search of any anomalous behavior. A [data feed](#data-feed) can ingest multiple metrics from the same data source. +A `DataFeedMetric`, or simply "metric", is a quantifiable measure used to monitor an assess the status of a specific business process. It could be the cost of a product over the months, or even a daily measure of temperature. The service will monitor how this value varies over time in search of any anomalous behavior. A [data feed](#data-feed) can ingest multiple metrics from the same data source. ### Data Feed Dimension -A `DataFeedDimension`, or simply "dimension", is a set of categorical values that characterize a [metric](#data-feed-metric). For instance, if a metric represents the cost of a product, the type of product (e.g., shoes, hats) and the city in which these values were measured (e.g., New York, Tokyo) could be used as a dimension. Possible dimension values would include: `(shoes, New York)`, `(shoes, Tokyo)`, `(hats, New York)`, and `(hats, Tokyo)`. +A `DataFeedDimension`, or simply "dimension", is a categorical value that characterize a [metric](#data-feed-metric). For instance, if a metric represents the cost of a product, the type of product (e.g., shoes, hats) and the city in which these values were measured (e.g., New York, Tokyo) could be used as a dimension. The combination of multiple dimensions identify a particular univariate [time series](#time-series). ### Time Series -A time series is a series of data points indexed in time order. These data points describe the variation of the value of a [metric](#data-feed-metric) over time. +A time series is a sequence of data points indexed chronologically. These data points describe the variation of the value of a [metric](#data-feed-metric) over time. -Given a metric, the Metrics Advisor service creates one series for every possible [dimension](#data-feed-dimension) value, which means that multiple time series can be monitored for the same metric. +Given a metric, the Metrics Advisor service creates one series for every possible combination of [dimension](#data-feed-dimension) values, which means that multiple time series can be monitored for the same metric. + +For example, suppose the following columns of data are returned by your data source: + +| City | Category | Cost | Revenue | +| -------- | -------- | ------- | ------- | +| New York | Shoes | 1045.00 | 1345.00 | +| New York | Hats | 670.00 | 502.00 | +| Delhi | Shoes | 991.00 | 1009.00 | +| Delhi | Hats | 623.00 | 711.00 | + +Cost and revenue are the metrics you want the service to monitor, while city and category are the dimensions that characterize those metrics. There are 4 possible dimension combinations in this data: + +- City = New York, Category = Shoes +- City = New York, Category = Hats +- City = Delhi, Category = Shoes +- City = Delhi, Category = Hats + +For each metric, the service will create 4 time series to monitor data, each one representing one possible dimension combination. Every time a data source ingestion happens, these series will be updated with a new data point, if available in the newly ingested data. ### Data Point Anomaly -A `DataPointAnomaly`, or simply "anomaly", occurs when a data point in a [time series](#time-series) behaves unexpectedly. It may occur when a data point value is too high or too low, or when its value changes abruptly between close points. You can specify the conditions a data point must satisfy to be considered an anomaly with an `AnomalyDetectionConfiguration`. See the sample [Create an anomaly detection configuration](#create-an-anomaly-detection-configuration) below for more information. +A `DataPointAnomaly`, or simply "anomaly", occurs when a data point in a [time series](#time-series) behaves unexpectedly. It may occur when a data point value is too high or too low, or when its value changes abruptly between close points. You can specify the conditions a data point must satisfy to be considered an anomaly with an `AnomalyDetectionConfiguration`. After data ingestion happens, the service applies all existing configurations to the set of new points in search of anomalies. See the sample [Create an anomaly detection configuration](#create-an-anomaly-detection-configuration) below for more information. ### Anomaly Incident -Detected [anomalies](#data-point-anomaly) within the same [time series](#time-series) can be grouped into an `AnomalyIncident`, or simply "incident". The service looks for patterns across anomalies to determine which ones are likely to have the same cause, grouping them together. +When there are [anomalies](#data-point-anomaly) detected on multiple [time series](#time-series) within one [metric](#data-feed-metric) at a particular timestamp, the Metrics Advisor service will automatically group anomalies that share the same root cause into one `AnomalyIncident`, or simply "incident". This will significantly remove the effort to check each individual anomaly and quickly finds the most important contributing factor to an issue. ### Anomaly Alert -An `AnomalyAlert`, or simply "alert", is triggered when a detected [anomaly](#data-point-anomaly) meets a specified criteria. For instance, an alert could be triggered every time an anomaly with high severity is detected. You can specify the conditions an anomaly must satisfy to trigger an alert with an `AnomalyAlertConfiguration`, which make use of [hooks](#notification-hook) to send notifications to the concerned parties every time an alert is triggered. These configurations are not set by default, so you need to create one in order to start triggering and receiving alerts. See the sample [Create an anomaly alert configuration](#create-an-anomaly-alert-configuration) below for more information. +An `AnomalyAlert`, or simply "alert", is triggered when a detected [anomaly](#data-point-anomaly) meets a specified criteria. For instance, an alert could be triggered every time an anomaly with high severity is detected. You can specify the conditions an anomaly must satisfy to trigger an alert with an `AnomalyAlertConfiguration`. After anomaly detection is performed over newly ingested data points, the service applies all existing configurations to the new anomalies, and each configuration fires a single alert for the set of points satisfying the specified criteria. Alert configurations are not set by default, so you need to create one in order to start triggering alerts. See the sample [Create an anomaly alert configuration](#create-an-anomaly-alert-configuration) below for more information. ### Notification Hook -A `NotificationHook`, or simply "hook", is a means of subscribing to [alerts](#anomaly-alert) notifications. You can pass a hook to an `AnomalyAlertConfiguration` and start getting notifications for every alert it creates. See the sample [Create a hook for receiving anomaly alerts](#create-a-hook-for-receiving-anomaly-alerts) below for more information. +A `NotificationHook`, or simply "hook", is a means of subscribing to [alert](#anomaly-alert) notifications. You can pass a hook to an `AnomalyAlertConfiguration` and start getting notifications for every alert it creates. See the sample [Create a hook for receiving anomaly alerts](#create-a-hook-for-receiving-anomaly-alerts) below for more information. ### Thread safety We guarantee that all client instance methods are thread-safe and independent of each other ([guideline](https://azure.github.io/azure-sdk/dotnet_introduction.html#dotnet-service-methods-thread-safety)). This ensures that the recommendation of reusing client instances is always safe, even across threads. @@ -185,6 +203,7 @@ We guarantee that all client instance methods are thread-safe and independent of The following section provides several code snippets illustrating common patterns used in the Metrics Advisor .NET API. The snippets below make use of asynchronous service calls, but note that the Azure.AI.MetricsAdvisor package supports both synchronous and asynchronous APIs. * [Create a data feed from a data source](#create-a-data-feed-from-a-data-source) +* [Other data source authentication alternatives](#other-data-source-authentication-alternatives) * [Check the ingestion status of a data feed](#check-the-ingestion-status-of-a-data-feed) * [Create an anomaly detection configuration](#create-an-anomaly-detection-configuration) * [Create a hook for receiving anomaly alerts](#create-a-hook-for-receiving-anomaly-alerts) @@ -240,6 +259,43 @@ foreach (DataFeedDimension dimension in createdDataFeed.Schema.DimensionColumns) } ``` +### Other data source authentication alternatives + +Some data sources support multiple types of authentication. For example, a `SqlServerDataFeedSource` supports connection string, Service Principal, and managed identity. You can check the complete list of data sources and their types of authentication [here][metricsadv_authentication]. + +Once you've made sure that your data source supports the authentication you want to use, you need to set the `Authentication` property when creating or updating the data source: + +```C# Snippet:SettingAuthentication +var dataSoure = new SqlServerDataFeedSource("", "") +{ + Authentication = SqlServerDataFeedSource.AuthenticationType.ManagedIdentity +}; +``` + +Be aware that, except for the `Basic` and `ManagedIdentity` types of authentication, you also need to have the ID of a corresponding `DataSourceCredentialEntity` in the service. In order to create a credential entity, you need to do: + +```C# Snippet:CreateDataSourceCredentialAsync +string credentialName = ""; + +var credentialEntity = new ServicePrincipalCredentialEntity(credentialName, "", "", ""); + +Response response = await adminClient.CreateDataSourceCredentialAsync(credentialEntity); + +DataSourceCredentialEntity createdCredentialEntity = response.Value; + +Console.WriteLine($"Credential entity ID: {createdCredentialEntity.Id}"); +``` + +Once you have the ID, add it to the `DataSourceCredentialId` property when setting up your data source: + +```C# Snippet:SettingCredentialAuthentication +var dataSoure = new SqlServerDataFeedSource("", "") +{ + Authentication = SqlServerDataFeedSource.AuthenticationType.ServicePrincipal, + DataSourceCredentialId = "" +}; +``` + ### Check the ingestion status of a data feed Check the ingestion status of a previously created [`DataFeed`](#data-feed). @@ -311,7 +367,7 @@ Console.WriteLine($"Anomaly detection configuration ID: {createdDetectionConfigu ### Create a hook for receiving anomaly alerts -Metrics Advisor supports the [`EmailNotificationHook`](#notification-hook) and the [`WebNotificationHook`](#notification-hook) classes as means of subscribing to [alerts](#anomaly-alert) notifications. In this example we'll illustrate how to create an `EmailNotificationHook`. Note that you need to pass the hook to an anomaly alert configuration to start getting notifications. See the sample [Create an anomaly alert configuration](#create-an-anomaly-alert-configuration) below for more information. +Metrics Advisor supports the [`EmailNotificationHook`](#notification-hook) and the [`WebNotificationHook`](#notification-hook) classes as means of subscribing to [alert](#anomaly-alert) notifications. In this example we'll illustrate how to create an `EmailNotificationHook`. Note that you need to pass the hook to an anomaly alert configuration to start getting notifications. See the sample [Create an anomaly alert configuration](#create-an-anomaly-alert-configuration) below for more information. ```C# Snippet:CreateHookAsync string hookName = ""; @@ -489,15 +545,16 @@ To learn more about other logging mechanisms see [Diagnostics Samples][logging]. Samples showing how to use the Cognitive Services Metrics Advisor library are available in this GitHub repository. Samples are provided for each main functional area: - [Data feed CRUD operations][metricsadv-sample1] -- [Data feed ingestion operations][metricsadv-sample2] -- [Anomaly detection configuration CRUD operations][metricsadv-sample3] -- [Hook CRUD operations][metricsadv-sample4] -- [Anomaly alert configuration CRUD operations][metricsadv-sample5] -- [Query triggered alerts][metricsadv-sample6] -- [Query detected anomalies][metricsadv-sample7] -- [Query incidents and their root causes][metricsadv-sample8] -- [Query time series information][metricsadv-sample9] -- [Feedback CRUD operations][metricsadv-sample10] +- [Credential entity CRUD operations][metricsadv-sample2] +- [Data feed ingestion operations][metricsadv-sample3] +- [Anomaly detection configuration CRUD operations][metricsadv-sample4] +- [Hook CRUD operations][metricsadv-sample5] +- [Anomaly alert configuration CRUD operations][metricsadv-sample6] +- [Query triggered alerts][metricsadv-sample7] +- [Query detected anomalies][metricsadv-sample8] +- [Query incidents and their root causes][metricsadv-sample9] +- [Query time series information][metricsadv-sample10] +- [Feedback CRUD operations][metricsadv-sample11] ## Contributing @@ -522,15 +579,16 @@ This project has adopted the [Microsoft Open Source Code of Conduct][code_of_con [metrics_advisor_client_class]: https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/src/MetricsAdvisorClient.cs [metricsadv-sample1]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample01_DataFeedCrudOperations.cs -[metricsadv-sample2]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample02_DataFeedIngestionOperations.cs -[metricsadv-sample3]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample03_DetectionConfigurationCrudOperations.cs -[metricsadv-sample4]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample04_HookCrudOperations.cs -[metricsadv-sample5]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample05_AlertConfigurationCrudOperations.cs -[metricsadv-sample6]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample06_QueryTriggeredAlerts.cs -[metricsadv-sample7]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample07_QueryDetectedAnomalies.cs -[metricsadv-sample8]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample08_QueryIncidentsAndRootCauses.cs -[metricsadv-sample9]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample09_QueryTimeSeriesInformation.cs -[metricsadv-sample10]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample10_FeedbackCrudOperations.cs +[metricsadv-sample2]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample02_CredentialEntityCrudOperations.cs +[metricsadv-sample3]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample03_DataFeedIngestionOperations.cs +[metricsadv-sample4]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample04_DetectionConfigurationCrudOperations.cs +[metricsadv-sample5]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample05_HookCrudOperations.cs +[metricsadv-sample6]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample06_AlertConfigurationCrudOperations.cs +[metricsadv-sample7]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample07_QueryTriggeredAlerts.cs +[metricsadv-sample8]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample08_QueryDetectedAnomalies.cs +[metricsadv-sample9]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample09_QueryIncidentsAndRootCauses.cs +[metricsadv-sample10]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample10_QueryTimeSeriesInformation.cs +[metricsadv-sample11]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample11_FeedbackCrudOperations.cs [aad_grant_access]: https://docs.microsoft.com/azure/cognitive-services/authentication#assign-a-role-to-a-service-principal [azure_identity]: https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/identity/Azure.Identity/README.md @@ -539,6 +597,8 @@ This project has adopted the [Microsoft Open Source Code of Conduct][code_of_con [DefaultAzureCredential]: https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/identity/Azure.Identity/README.md#defaultazurecredential [register_aad_app]: https://docs.microsoft.com/azure/cognitive-services/authentication#assign-a-role-to-a-service-principal +[metricsadv_authentication]: https://aka.ms/metricsadvisor/authentication + [logging]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/core/Azure.Core/samples/Diagnostics.md [azure_cli]: https://docs.microsoft.com/cli/azure diff --git a/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/samples/README.md b/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/samples/README.md index bb3c1f948952b..254061f66691b 100644 --- a/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/samples/README.md +++ b/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/samples/README.md @@ -24,24 +24,26 @@ Azure Cognitive Services Metrics Advisor is a cloud service that uses machine le |**Sample**|**Description**| |---|---| |[Data feed CRUD operations][metricsadv-sample1]|Create, get, update, list, and delete data feeds| -|[Data feed ingestion operations][metricsadv-sample2]|Check and refresh a data feed's ingestion status| -|[Anomaly detection configuration CRUD operations][metricsadv-sample3]|Create, get, update, list, and delete anomaly detection configurations| -|[Hook CRUD operations][metricsadv-sample4]|Create, get, update, list, and delete hooks| -|[Anomaly alert configuration CRUD operations][metricsadv-sample5]|Create, get, update, list, and delete anomaly alert configurations| -|[Query triggered alerts][metricsadv-sample6]|Get the alerts triggered by the service| -|[Query detected anomalies][metricsadv-sample7]|Get the anomalies detected by the service| -|[Query incidents and their root causes][metricsadv-sample8]|Get the incidents created by the service and root cause analysis| -|[Query time series information][metricsadv-sample9]|Get information about the time series monitored by the service| -|[Feedback CRUD operations][metricsadv-sample10]|Create, get, and list feedback| +|[Credential entity CRUD operations][metricsadv-sample2]|Create, get, update, list, and delete credential entities| +|[Data feed ingestion operations][metricsadv-sample3]|Check and refresh a data feed's ingestion status| +|[Anomaly detection configuration CRUD operations][metricsadv-sample4]|Create, get, update, list, and delete anomaly detection configurations| +|[Hook CRUD operations][metricsadv-sample5]|Create, get, update, list, and delete hooks| +|[Anomaly alert configuration CRUD operations][metricsadv-sample6]|Create, get, update, list, and delete anomaly alert configurations| +|[Query triggered alerts][metricsadv-sample7]|Get the alerts triggered by the service| +|[Query detected anomalies][metricsadv-sample8]|Get the anomalies detected by the service| +|[Query incidents and their root causes][metricsadv-sample9]|Get the incidents created by the service and root cause analysis| +|[Query time series information][metricsadv-sample10]|Get information about the time series monitored by the service| +|[Feedback CRUD operations][metricsadv-sample11]|Create, get, and list feedback| [metricsadv-sample1]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample01_DataFeedCrudOperations.cs -[metricsadv-sample2]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample02_DataFeedIngestionOperations.cs -[metricsadv-sample3]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample03_DetectionConfigurationCrudOperations.cs -[metricsadv-sample4]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample04_HookCrudOperations.cs -[metricsadv-sample5]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample05_AlertConfigurationCrudOperations.cs -[metricsadv-sample6]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample06_QueryTriggeredAlerts.cs -[metricsadv-sample7]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample07_QueryDetectedAnomalies.cs -[metricsadv-sample8]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample08_QueryIncidentsAndRootCauses.cs -[metricsadv-sample9]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample09_QueryTimeSeriesInformation.cs -[metricsadv-sample10]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample10_FeedbackCrudOperations.cs \ No newline at end of file +[metricsadv-sample2]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample02_CredentialEntityCrudOperations.cs +[metricsadv-sample3]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample03_DataFeedIngestionOperations.cs +[metricsadv-sample4]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample04_DetectionConfigurationCrudOperations.cs +[metricsadv-sample5]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample05_HookCrudOperations.cs +[metricsadv-sample6]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample06_AlertConfigurationCrudOperations.cs +[metricsadv-sample7]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample07_QueryTriggeredAlerts.cs +[metricsadv-sample8]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample08_QueryDetectedAnomalies.cs +[metricsadv-sample9]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample09_QueryIncidentsAndRootCauses.cs +[metricsadv-sample10]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample10_QueryTimeSeriesInformation.cs +[metricsadv-sample11]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample11_FeedbackCrudOperations.cs \ No newline at end of file diff --git a/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/MetricsAdvisorTestEnvironment.cs b/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/MetricsAdvisorTestEnvironment.cs index ebd7a7008b291..fbaed6dc5c99d 100644 --- a/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/MetricsAdvisorTestEnvironment.cs +++ b/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/MetricsAdvisorTestEnvironment.cs @@ -25,6 +25,7 @@ public class MetricsAdvisorTestEnvironment : TestEnvironment public string HookId => Environment.GetEnvironmentVariable("METRICSADVISOR_HOOK_ID"); public string DetectionConfigurationId => Environment.GetEnvironmentVariable("METRICSADVISOR_DETECTION_CONFIGURATION_ID"); public string AlertConfigurationId => Environment.GetEnvironmentVariable("METRICSADVISOR_ALERT_CONFIGURATION_ID"); + public string CredentialEntityId => Environment.GetEnvironmentVariable("METRICSADVISOR_CREDENTIAL_ENTITY_ID"); public string AlertId => Environment.GetEnvironmentVariable("METRICSADVISOR_ALERT_ID"); public string IncidentId => Environment.GetEnvironmentVariable("METRICSADVISOR_INCIDENT_ID"); public string FeedbackId => Environment.GetEnvironmentVariable("METRICSADVISOR_FEEDBACK_ID"); diff --git a/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample02_CredentialEntityCrudOperations.cs b/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample02_CredentialEntityCrudOperations.cs new file mode 100644 index 0000000000000..c186f17a6f6ab --- /dev/null +++ b/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample02_CredentialEntityCrudOperations.cs @@ -0,0 +1,157 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.Threading.Tasks; +using Azure.AI.MetricsAdvisor.Administration; +using Azure.AI.MetricsAdvisor.Models; +using Azure.AI.MetricsAdvisor.Tests; +using NUnit.Framework; + +namespace Azure.AI.MetricsAdvisor.Samples +{ + public partial class MetricsAdvisorSamples : MetricsAdvisorTestEnvironment + { + [Test] + public async Task CreateAndDeleteDataSourceCredentialAsync() + { + string endpoint = MetricsAdvisorUri; + string subscriptionKey = MetricsAdvisorSubscriptionKey; + string apiKey = MetricsAdvisorApiKey; + var credential = new MetricsAdvisorKeyCredential(subscriptionKey, apiKey); + + var adminClient = new MetricsAdvisorAdministrationClient(new Uri(endpoint), credential); + + #region Snippet:CreateDataSourceCredentialAsync +#if SNIPPET + string credentialName = ""; +#else + string credentialName = GetUniqueName(); +#endif + + var credentialEntity = new ServicePrincipalCredentialEntity(credentialName, "", "", ""); + + Response response = await adminClient.CreateDataSourceCredentialAsync(credentialEntity); + + DataSourceCredentialEntity createdCredentialEntity = response.Value; + + Console.WriteLine($"Credential entity ID: {createdCredentialEntity.Id}"); + #endregion + + // Delete the created credential to clean up the Metrics Advisor resource. Do not perform this + // step if you intend to keep using the credential. + + await adminClient.DeleteDataSourceCredentialAsync(createdCredentialEntity.Id); + } + + [Test] + public async Task GetDataSourceCredentialAsync() + { + string endpoint = MetricsAdvisorUri; + string subscriptionKey = MetricsAdvisorSubscriptionKey; + string apiKey = MetricsAdvisorApiKey; + var credential = new MetricsAdvisorKeyCredential(subscriptionKey, apiKey); + + var adminClient = new MetricsAdvisorAdministrationClient(new Uri(endpoint), credential); + + string credentialEntityId = CredentialEntityId; + + Response response = await adminClient.GetDataSourceCredentialAsync(credentialEntityId); + + DataSourceCredentialEntity credentialEntity = response.Value; + + Console.WriteLine($"Credential name: {credentialEntity.Name}"); + Console.WriteLine($"Credential description: {credentialEntity.Description}"); + + // You can access specific properties of your credential entity depending on its kind. + + if (credentialEntity.CredentialKind == DataSourceCredentialKind.ServicePrincipal) + { + Console.WriteLine("Credential of kind Service Principal:"); + + var servicePrincipalEntity = credentialEntity as ServicePrincipalCredentialEntity; + + Console.WriteLine($" Client ID: {servicePrincipalEntity.ClientId}"); + Console.WriteLine($" Tenant ID: {servicePrincipalEntity.TenantId}"); + } + else if (credentialEntity.CredentialKind == DataSourceCredentialKind.ServicePrincipalInKeyVault) + { + Console.WriteLine("Credential of kind Service Principal in Key Vault:"); + + var servicePrincipalEntity = credentialEntity as ServicePrincipalInKeyVaultCredentialEntity; + + Console.WriteLine($"Name of secret for client ID: {servicePrincipalEntity.SecretNameForClientId}"); + Console.WriteLine($"Name of secret for client secret: {servicePrincipalEntity.SecretNameForClientSecret}"); + } + } + + [Test] + public async Task UpdateDataSourceCredentialAsync() + { + string endpoint = MetricsAdvisorUri; + string subscriptionKey = MetricsAdvisorSubscriptionKey; + string apiKey = MetricsAdvisorApiKey; + var credential = new MetricsAdvisorKeyCredential(subscriptionKey, apiKey); + + var adminClient = new MetricsAdvisorAdministrationClient(new Uri(endpoint), credential); + + string credentialEntityId = CredentialEntityId; + + Response response = await adminClient.GetDataSourceCredentialAsync(credentialEntityId); + DataSourceCredentialEntity credentialEntity = response.Value; + + credentialEntity.Description = "This description was generated by a sample."; + + // You can update specific properties of your credential entity depending on its kind. + + if (credentialEntity.CredentialKind == DataSourceCredentialKind.ServicePrincipal) + { + var servicePrincipalEntity = credentialEntity as ServicePrincipalCredentialEntity; + + servicePrincipalEntity.ClientId = ""; + + // Secrets can't be read and must be updated through specific methods. + + servicePrincipalEntity.UpdateClientSecret(""); + } + + response = await adminClient.UpdateDataSourceCredentialAsync(credentialEntity); + DataSourceCredentialEntity updatedCredentialEntity = response.Value; + + Console.WriteLine($"Updated description: {updatedCredentialEntity.Description}"); + } + + [Test] + public async Task GetDataSourceCredentialsAsync() + { + string endpoint = MetricsAdvisorUri; + string subscriptionKey = MetricsAdvisorSubscriptionKey; + string apiKey = MetricsAdvisorApiKey; + var credential = new MetricsAdvisorKeyCredential(subscriptionKey, apiKey); + + var adminClient = new MetricsAdvisorAdministrationClient(new Uri(endpoint), credential); + + var options = new GetDataSourceCredentialsOptions() + { + MaxPageSize = 5 + }; + + int credentialCount = 0; + + await foreach (DataSourceCredentialEntity credentialEntity in adminClient.GetDataSourceCredentialsAsync(options)) + { + Console.WriteLine($"Credential ID: {credentialEntity.Id}"); + Console.WriteLine($"Name: {credentialEntity.Name}"); + Console.WriteLine($"Description: {credentialEntity.Description}"); + Console.WriteLine($"Kind: {credentialEntity.CredentialKind}"); + Console.WriteLine(); + + // Print at most 5 credentials. + if (++credentialCount >= 5) + { + break; + } + } + } + } +} diff --git a/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample02_DataFeedIngestionOperations.cs b/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample03_DataFeedIngestionOperations.cs similarity index 100% rename from sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample02_DataFeedIngestionOperations.cs rename to sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample03_DataFeedIngestionOperations.cs diff --git a/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample03_DetectionConfigurationCrudOperations.cs b/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample04_DetectionConfigurationCrudOperations.cs similarity index 100% rename from sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample03_DetectionConfigurationCrudOperations.cs rename to sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample04_DetectionConfigurationCrudOperations.cs diff --git a/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample04_HookCrudOperations.cs b/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample05_HookCrudOperations.cs similarity index 100% rename from sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample04_HookCrudOperations.cs rename to sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample05_HookCrudOperations.cs diff --git a/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample05_AlertConfigurationCrudOperations.cs b/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample06_AlertConfigurationCrudOperations.cs similarity index 100% rename from sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample05_AlertConfigurationCrudOperations.cs rename to sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample06_AlertConfigurationCrudOperations.cs diff --git a/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample06_QueryTriggeredAlerts.cs b/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample07_QueryTriggeredAlerts.cs similarity index 100% rename from sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample06_QueryTriggeredAlerts.cs rename to sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample07_QueryTriggeredAlerts.cs diff --git a/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample07_QueryDetectedAnomalies.cs b/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample08_QueryDetectedAnomalies.cs similarity index 100% rename from sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample07_QueryDetectedAnomalies.cs rename to sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample08_QueryDetectedAnomalies.cs diff --git a/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample08_QueryIncidentsAndRootCauses.cs b/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample09_QueryIncidentsAndRootCauses.cs similarity index 100% rename from sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample08_QueryIncidentsAndRootCauses.cs rename to sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample09_QueryIncidentsAndRootCauses.cs diff --git a/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample09_QueryTimeSeriesInformation.cs b/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample10_QueryTimeSeriesInformation.cs similarity index 100% rename from sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample09_QueryTimeSeriesInformation.cs rename to sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample10_QueryTimeSeriesInformation.cs diff --git a/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample10_FeedbackCrudOperations.cs b/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample11_FeedbackCrudOperations.cs similarity index 100% rename from sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample10_FeedbackCrudOperations.cs rename to sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Sample11_FeedbackCrudOperations.cs diff --git a/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Snippets.cs b/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Snippets.cs index b7304a2335201..36de176eae448 100644 --- a/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Snippets.cs +++ b/sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/Samples/Snippets.cs @@ -103,5 +103,30 @@ public async Task MetricsAdvisorNotFound() } #endregion } + + [Test] + + public void SettingAuthentication() + { + #region Snippet:SettingAuthentication + var dataSoure = new SqlServerDataFeedSource("", "") + { + Authentication = SqlServerDataFeedSource.AuthenticationType.ManagedIdentity + }; + #endregion + } + + [Test] + + public void SettingCredentialAuthentication() + { + #region Snippet:SettingCredentialAuthentication + var dataSoure = new SqlServerDataFeedSource("", "") + { + Authentication = SqlServerDataFeedSource.AuthenticationType.ServicePrincipal, + DataSourceCredentialId = "" + }; + #endregion + } } } diff --git a/sdk/metricsadvisor/tests.yml b/sdk/metricsadvisor/tests.yml index ec6f00c1c73a7..79f6e51643925 100644 --- a/sdk/metricsadvisor/tests.yml +++ b/sdk/metricsadvisor/tests.yml @@ -12,6 +12,7 @@ extends: METRICSADVISOR_METRIC_ID: "27e3015f-04fd-44ba-a20b-bc529a0aebae" METRICSADVISOR_DETECTION_CONFIGURATION_ID: "fb5a6ed6-2b9e-4b72-8b0c-0046ead1c15c" METRICSADVISOR_ALERT_CONFIGURATION_ID: "204a211a-c5f4-45f3-a30e-512fb25d1d2c" + METRICSADVISOR_CREDENTIAL_ENTITY_ID: "c4b3985c-5112-4516-9417-d655dfcacf99" METRICSADVISOR_HOOK_ID: "ffe4e97d-dbc4-4b60-a96a-03d22ca87f00" METRICSADVISOR_ALERT_ID: "17548749000" METRICSADVISOR_INCIDENT_ID: "2c8a72ba0e8eb41643cc0e5cff368412-17548749000"