-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for app insights in CLI (#1952)
## Why make this change? - Closes #1745 - This change will allow users to add application insights to config through CLI ## What is this change? - Added a new cli command `add-telemetry` to add telemetry resource where the data will be sent. Currently DAB supports sending telemetry to Application Insights. - this command has two flags/options: `--app-insights-enabled`: A boolean flag that specifies whether Application Insights telemetry should be enabled. This flag is optional and default value is true. `--app-insights-conn-string`: A string that specifies the connection string for the Application Insights resource to which telemetry data should be sent. This flag is required must be set to a valid connection string. - This command will add application insights telemetry resource details in the config, when dab engine starts with this config it will send telemetry details to applications insights. ## How was this tested? - [X] Unit Tests --------- Co-authored-by: Aniruddh Munde <anmunde@microsoft.com> Co-authored-by: Shyam Sundar J <shyamsundarj@microsoft.com>
- Loading branch information
1 parent
542ac92
commit 1f3f176
Showing
9 changed files
with
363 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
namespace Cli.Tests | ||
{ | ||
/// <summary> | ||
/// Tests for verifying the functionality of adding telemetry to the config file. | ||
/// </summary> | ||
[TestClass] | ||
public class AddTelemetryTests | ||
: VerifyBase | ||
{ | ||
public string RUNTIME_SECTION_WITH_APP_INSIGHTS_TELEMETRY_SECTION = GenerateRuntimeSection(TELEMETRY_SECTION_WITH_APP_INSIGHTS); | ||
public string RUNTIME_SECTION_WITH_EMPTY_TELEMETRY_SECTION = GenerateRuntimeSection(EMPTY_TELEMETRY_SECTION); | ||
|
||
[TestInitialize] | ||
public void TestInitialize() | ||
{ | ||
ILoggerFactory loggerFactory = TestLoggerSupport.ProvisionLoggerFactory(); | ||
|
||
SetLoggerForCliConfigGenerator(loggerFactory.CreateLogger<ConfigGenerator>()); | ||
SetCliUtilsLogger(loggerFactory.CreateLogger<Utils>()); | ||
} | ||
|
||
/// <summary> | ||
/// Testing to check telemetry options are correctly added to the config. | ||
/// Verifying scenarios such as enabling/disabling telemetry and providing a valid/empty connection string. | ||
/// </summary> | ||
[DataTestMethod] | ||
[DataRow(CliBool.True, "", false, DisplayName = "Fail to add telemetry with empty app-insights connection string.")] | ||
[DataRow(CliBool.True, "InstrumentationKey=00000000-0000-0000-0000-000000000000", true, DisplayName = "Successfully adds telemetry with valid connection string")] | ||
[DataRow(CliBool.False, "InstrumentationKey=00000000-0000-0000-0000-000000000000", true, DisplayName = "Successfully adds telemetry but disabled")] | ||
public void TestAddApplicationInsightsTelemetry(CliBool isTelemetryEnabled, string appInsightsConnString, bool expectSuccess) | ||
{ | ||
MockFileSystem fileSystem = FileSystemUtils.ProvisionMockFileSystem(); | ||
string configPath = "test-app-insights-config.json"; | ||
fileSystem.AddFile(configPath, new MockFileData(INITIAL_CONFIG)); | ||
|
||
// Initial State | ||
Assert.IsTrue(fileSystem.FileExists(configPath)); | ||
Assert.IsTrue(RuntimeConfigLoader.TryParseConfig(fileSystem.File.ReadAllText(configPath), out RuntimeConfig? config)); | ||
Assert.IsNotNull(config); | ||
Assert.IsNotNull(config.Runtime); | ||
Assert.IsNull(config.Runtime.Telemetry); | ||
|
||
// Add Telemetry | ||
bool isSuccess = ConfigGenerator.TryAddTelemetry( | ||
new AddTelemetryOptions(appInsightsConnString, isTelemetryEnabled, configPath), | ||
new FileSystemRuntimeConfigLoader(fileSystem), | ||
fileSystem); | ||
|
||
// Assert after adding telemetry | ||
Assert.AreEqual(expectSuccess, isSuccess); | ||
if (expectSuccess) | ||
{ | ||
Assert.IsTrue(fileSystem.FileExists(configPath)); | ||
Assert.IsTrue(RuntimeConfigLoader.TryParseConfig(fileSystem.File.ReadAllText(configPath), out config)); | ||
Assert.IsNotNull(config); | ||
Assert.IsNotNull(config.Runtime); | ||
Assert.IsNotNull(config.Runtime.Telemetry); | ||
TelemetryOptions telemetryOptions = config.Runtime.Telemetry; | ||
Assert.IsNotNull(telemetryOptions.ApplicationInsights); | ||
Assert.AreEqual(isTelemetryEnabled is CliBool.True ? true : false, telemetryOptions.ApplicationInsights.Enabled); | ||
Assert.AreEqual(appInsightsConnString, telemetryOptions.ApplicationInsights.ConnectionString); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Test to verify when Telemetry section is present in the config | ||
/// It should add application insights telemetry if telemetry section is empty | ||
/// or overwrite the existing app insights telemetry with the given app insights telemetry options. | ||
/// </summary> | ||
[DataTestMethod] | ||
[DataRow(true, DisplayName = "Add AppInsights Telemetry when telemetry section is empty.")] | ||
[DataRow(false, DisplayName = "Overwrite AppInsights Telemetry when telemetry section already exists.")] | ||
public void TestAddAppInsightsTelemetryWhenTelemetryAlreadyExists(bool isEmptyTelemetry) | ||
{ | ||
MockFileSystem fileSystem = FileSystemUtils.ProvisionMockFileSystem(); | ||
string configPath = "test-app-insights-config.json"; | ||
string runtimeSection = isEmptyTelemetry ? RUNTIME_SECTION_WITH_EMPTY_TELEMETRY_SECTION : RUNTIME_SECTION_WITH_APP_INSIGHTS_TELEMETRY_SECTION; | ||
string configData = $"{{{SAMPLE_SCHEMA_DATA_SOURCE},{runtimeSection}}}"; | ||
fileSystem.AddFile(configPath, new MockFileData(configData)); | ||
|
||
// Initial State | ||
Assert.IsTrue(fileSystem.FileExists(configPath)); | ||
Assert.IsTrue(RuntimeConfigLoader.TryParseConfig(fileSystem.File.ReadAllText(configPath), out RuntimeConfig? config)); | ||
Assert.IsNotNull(config); | ||
Assert.IsNotNull(config.Runtime); | ||
Assert.IsNotNull(config.Runtime.Telemetry); | ||
|
||
if (isEmptyTelemetry) | ||
{ | ||
Assert.IsNull(config.Runtime.Telemetry.ApplicationInsights); | ||
} | ||
else | ||
{ | ||
Assert.IsNotNull(config.Runtime.Telemetry.ApplicationInsights); | ||
Assert.AreEqual(true, config.Runtime.Telemetry.ApplicationInsights.Enabled); | ||
Assert.AreEqual("InstrumentationKey=00000000-0000-0000-0000-000000000000", config.Runtime.Telemetry.ApplicationInsights.ConnectionString); | ||
} | ||
|
||
// Add Telemetry | ||
bool isSuccess = ConfigGenerator.TryAddTelemetry( | ||
new AddTelemetryOptions("InstrumentationKey=11111-1111-111-11-1", CliBool.False, configPath), | ||
new FileSystemRuntimeConfigLoader(fileSystem), | ||
fileSystem); | ||
|
||
// Assert after adding telemetry | ||
Assert.IsTrue(isSuccess); | ||
Assert.IsTrue(fileSystem.FileExists(configPath)); | ||
Assert.IsTrue(RuntimeConfigLoader.TryParseConfig(fileSystem.File.ReadAllText(configPath), out config)); | ||
Assert.IsNotNull(config); | ||
Assert.IsNotNull(config.Runtime); | ||
Assert.IsNotNull(config.Runtime.Telemetry); | ||
Assert.IsNotNull(config.Runtime.Telemetry.ApplicationInsights); | ||
Assert.IsFalse(config.Runtime.Telemetry.ApplicationInsights.Enabled); | ||
Assert.AreEqual("InstrumentationKey=11111-1111-111-11-1", config.Runtime.Telemetry.ApplicationInsights.ConnectionString); | ||
} | ||
|
||
/// <summary> | ||
/// Generates a JSON string representing a runtime section of the config, with a customizable telemetry section. | ||
/// </summary> | ||
private static string GenerateRuntimeSection(string telemetrySection) | ||
{ | ||
return $@" | ||
""runtime"": {{ | ||
""rest"": {{ | ||
""path"": ""/api"", | ||
""enabled"": false | ||
}}, | ||
""graphql"": {{ | ||
""path"": ""/graphql"", | ||
""enabled"": false, | ||
""allow-introspection"": true | ||
}}, | ||
""host"": {{ | ||
""mode"": ""development"", | ||
""cors"": {{ | ||
""origins"": [], | ||
""allow-credentials"": false | ||
}}, | ||
""authentication"": {{ | ||
""provider"": ""StaticWebApps"" | ||
}} | ||
}}, | ||
{telemetrySection} | ||
}}, | ||
""entities"": {{}}"; | ||
} | ||
|
||
/// <summary> | ||
/// Represents a JSON string for the telemetry section of the config, with Application Insights enabled and a specified connection string. | ||
/// </summary> | ||
private const string TELEMETRY_SECTION_WITH_APP_INSIGHTS = @" | ||
""telemetry"": { | ||
""application-insights"": { | ||
""enabled"": true, | ||
""connection-string"": ""InstrumentationKey=00000000-0000-0000-0000-000000000000"" | ||
} | ||
}"; | ||
|
||
/// <summary> | ||
/// Represents a JSON string for the empty telemetry section of the config. | ||
/// </summary> | ||
private const string EMPTY_TELEMETRY_SECTION = @" | ||
""telemetry"": {}"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System.IO.Abstractions; | ||
using Azure.DataApiBuilder.Config; | ||
using Azure.DataApiBuilder.Config.ObjectModel; | ||
using Azure.DataApiBuilder.Product; | ||
using CommandLine; | ||
using Microsoft.Extensions.Logging; | ||
using static Cli.Utils; | ||
|
||
namespace Cli.Commands | ||
{ | ||
/// <summary> | ||
/// Telemetry command options | ||
/// </summary> | ||
[Verb("add-telemetry", isDefault: false, HelpText = "Add telemetry for Data Api builder Application", Hidden = false)] | ||
public class AddTelemetryOptions : Options | ||
{ | ||
public AddTelemetryOptions(string appInsightsConnString, CliBool appInsightsEnabled, string? config) : base(config) | ||
{ | ||
AppInsightsConnString = appInsightsConnString; | ||
AppInsightsEnabled = appInsightsEnabled; | ||
} | ||
|
||
// Connection string for the Application Insights resource to which telemetry data should be sent. | ||
// This option is required and must be provided with a valid connection string. | ||
[Option("app-insights-conn-string", Required = true, HelpText = "Connection string for the Application Insights resource for telemetry data")] | ||
public string AppInsightsConnString { get; } | ||
|
||
// To specify whether Application Insights telemetry should be enabled. This flag is optional and default value is true. | ||
[Option("app-insights-enabled", Default = CliBool.True, Required = false, HelpText = "(Default: true) Enable/Disable Application Insights")] | ||
public CliBool AppInsightsEnabled { get; } | ||
|
||
public void Handler(ILogger logger, FileSystemRuntimeConfigLoader loader, IFileSystem fileSystem) | ||
{ | ||
logger.LogInformation("{productName} {version}", PRODUCT_NAME, ProductInfo.GetProductVersion()); | ||
|
||
bool isSuccess = ConfigGenerator.TryAddTelemetry(this, loader, fileSystem); | ||
|
||
if (isSuccess) | ||
{ | ||
logger.LogInformation("Successfully added telemetry to the configuration file."); | ||
} | ||
else | ||
{ | ||
logger.LogError("Failed to add telemetry to the configuration file."); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.