Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .aspire/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"appHostPath": "../src/apphost/apphost.csproj"
}
3 changes: 2 additions & 1 deletion Nuget.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
<configuration>
<packageSources>
<clear />
<add key="data_api_builder_build_packages" value="https://pkgs.dev.azure.com/sqldab/fcb212b3-b288-4c9e-b55a-5842a268b16d/_packaging/data_api_builder_build_packages/nuget/v3/index.json" />
<!-- <add key="data_api_builder_build_packages" value="https://pkgs.dev.azure.com/sqldab/fcb212b3-b288-4c9e-b55a-5842a268b16d/_packaging/data_api_builder_build_packages/nuget/v3/index.json" /> -->
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
</packageSources>
<disabledPackageSources>
<clear />
Expand Down
6 changes: 3 additions & 3 deletions src/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ dotnet_style_allow_statement_immediately_after_block_experimental = false:error
#### C# Coding Conventions ####

# var preferences
csharp_style_var_elsewhere = false:error
csharp_style_var_for_built_in_types = false:error
csharp_style_var_when_type_is_apparent = false:error
# csharp_style_var_elsewhere = false:error
# csharp_style_var_for_built_in_types = false:error
# csharp_style_var_when_type_is_apparent = false:error

# Modifier preferences
csharp_prefer_static_local_function = true:suggestion
Expand Down
8 changes: 7 additions & 1 deletion src/Azure.DataApiBuilder.sln
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.3.32405.409
MinimumVisualStudioVersion = 10.0.40219.1
Expand Down Expand Up @@ -31,6 +31,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.DataApiBuilder.Core",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.DataApiBuilder.Product", "Product\Azure.DataApiBuilder.Product.csproj", "{E3D2076C-EE49-43A0-8F92-5FC41EC99DA7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "apphost", "apphost\apphost.csproj", "{87B53030-EB52-4EB1-870D-72540DA4724E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -73,6 +75,10 @@ Global
{E3D2076C-EE49-43A0-8F92-5FC41EC99DA7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E3D2076C-EE49-43A0-8F92-5FC41EC99DA7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E3D2076C-EE49-43A0-8F92-5FC41EC99DA7}.Release|Any CPU.Build.0 = Release|Any CPU
{87B53030-EB52-4EB1-870D-72540DA4724E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{87B53030-EB52-4EB1-870D-72540DA4724E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{87B53030-EB52-4EB1-870D-72540DA4724E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{87B53030-EB52-4EB1-870D-72540DA4724E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
3 changes: 3 additions & 0 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Aspire.Hosting.AppHost" Version="9.4.0" />
<PackageVersion Include="Aspire.Hosting.SqlServer" Version="9.4.0" />
<PackageVersion Include="Aspire.Hosting.PostgreSQL" Version="9.4.0" />
<PackageVersion Include="Azure.Identity" Version="1.11.4" />
<PackageVersion Include="CommandLineParser" Version="2.9.1" />
<PackageVersion Include="coverlet.msbuild" Version="6.0.2" />
Expand Down
11 changes: 6 additions & 5 deletions src/Service/HealthCheck/HealthCheckHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data.Common;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -44,7 +45,7 @@ public HealthCheckHelper(ILogger<HealthCheckHelper> logger, HttpUtilities httpUt

/// <summary>
/// GetHealthCheckResponse is the main function which fetches the HttpContext and then creates the comprehensive health check report.
/// Serializes the report to JSON and returns the response.
/// Serializes the report to JSON and returns the response.
/// </summary>
/// <param name="runtimeConfig">RuntimeConfig</param>
/// <returns>This function returns the comprehensive health report after calculating the response time of each datasource, rest and graphql health queries.</returns>
Expand Down Expand Up @@ -160,7 +161,7 @@ private async Task UpdateDataSourceHealthCheckResultsAsync(ComprehensiveHealthCh
if (ComprehensiveHealthCheckReport.Checks != null && runtimeConfig.DataSource.IsDatasourceHealthEnabled)
{
string query = Utilities.GetDatSourceQuery(runtimeConfig.DataSource.DatabaseType);
(int, string?) response = await ExecuteDatasourceQueryCheckAsync(query, runtimeConfig.DataSource.ConnectionString);
(int, string?) response = await ExecuteDatasourceQueryCheckAsync(query, runtimeConfig.DataSource.ConnectionString, Utilities.GetDbProviderFactory(runtimeConfig.DataSource.DatabaseType));
bool isResponseTimeWithinThreshold = response.Item1 >= 0 && response.Item1 < runtimeConfig.DataSource.DatasourceThresholdMs;

// Add DataSource Health Check Results
Expand All @@ -180,22 +181,22 @@ private async Task UpdateDataSourceHealthCheckResultsAsync(ComprehensiveHealthCh
}

// Executes the DB Query and keeps track of the response time and error message.
private async Task<(int, string?)> ExecuteDatasourceQueryCheckAsync(string query, string connectionString)
private async Task<(int, string?)> ExecuteDatasourceQueryCheckAsync(string query, string connectionString, DbProviderFactory dbProviderFactory)
{
string? errorMessage = null;
if (!string.IsNullOrEmpty(query) && !string.IsNullOrEmpty(connectionString))
{
Stopwatch stopwatch = new();
stopwatch.Start();
errorMessage = await _httpUtility.ExecuteDbQueryAsync(query, connectionString);
errorMessage = await _httpUtility.ExecuteDbQueryAsync(query, connectionString, dbProviderFactory);
stopwatch.Stop();
return string.IsNullOrEmpty(errorMessage) ? ((int)stopwatch.ElapsedMilliseconds, errorMessage) : (HealthCheckConstants.ERROR_RESPONSE_TIME_MS, errorMessage);
}

return (HealthCheckConstants.ERROR_RESPONSE_TIME_MS, errorMessage);
}

// Updates the Entity Health Check Results in the response.
// Updates the Entity Health Check Results in the response.
// Goes through the entities one by one and executes the rest and graphql checks (if enabled).
private async Task UpdateEntityHealthCheckResultsAsync(ComprehensiveHealthCheckReport report, RuntimeConfig runtimeConfig)
{
Expand Down
30 changes: 21 additions & 9 deletions src/Service/HealthCheck/HttpUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Net.Http;
using System.Text;
Expand All @@ -14,7 +15,6 @@
using Azure.DataApiBuilder.Core.Services;
using Azure.DataApiBuilder.Core.Services.MetadataProviders;
using Azure.DataApiBuilder.Service.GraphQLBuilder;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Logging;

namespace Azure.DataApiBuilder.Service.HealthCheck
Expand Down Expand Up @@ -49,19 +49,31 @@ public HttpUtilities(
}

// Executes the DB query by establishing a connection to the DB.
public async Task<string?> ExecuteDbQueryAsync(string query, string connectionString)
public async Task<string?> ExecuteDbQueryAsync(string query, string connectionString, DbProviderFactory providerFactory)
{
string? errorMessage = null;
// Execute the query on DB and return the response time.
using (SqlConnection connection = new(connectionString))
using (DbConnection? connection = providerFactory.CreateConnection())
{
if (connection == null)
{
errorMessage = "Failed to create database connection.";
_logger.LogError(errorMessage);
return errorMessage;
}

try
{
SqlCommand command = new(query, connection);
connection.Open();
SqlDataReader reader = await command.ExecuteReaderAsync();
_logger.LogTrace("The health check query for datasource executed successfully.");
reader.Close();
connection.ConnectionString = connectionString;
using (DbCommand command = connection.CreateCommand())
{
command.CommandText = query;
await connection.OpenAsync();
using (DbDataReader reader = await command.ExecuteReaderAsync())
{
_logger.LogTrace("The health check query for datasource executed successfully.");
}
}
}
catch (Exception ex)
{
Expand Down Expand Up @@ -145,7 +157,7 @@ public HttpUtilities(
List<string> columnNames = dbObject.SourceDefinition.Columns.Keys.ToList();

// In case of GraphQL API, use the plural value specified in [entity.graphql.type.plural].
// Further, we need to camel case this plural value to match the GraphQL object name.
// Further, we need to camel case this plural value to match the GraphQL object name.
string graphqlObjectName = GraphQLNaming.GenerateListQueryName(entityName, entity);

// In case any primitive column names are present, execute the query
Expand Down
18 changes: 18 additions & 0 deletions src/Service/HealthCheck/Utilities.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Text.Json;
using Azure.DataApiBuilder.Config.ObjectModel;
using Microsoft.Data.SqlClient;
using Npgsql;

namespace Azure.DataApiBuilder.Service.HealthCheck
{
Expand Down Expand Up @@ -32,6 +36,20 @@ public static string GetDatSourceQuery(DatabaseType dbType)
}
}

public static DbProviderFactory GetDbProviderFactory(DatabaseType dbType)
{
switch (dbType)
{
case DatabaseType.PostgreSQL:
return NpgsqlFactory.Instance;
case DatabaseType.MSSQL:
case DatabaseType.DWSQL:
return SqlClientFactory.Instance;
default:
throw new NotSupportedException($"Database type '{dbType}' is not supported.");
}
}

public static string CreateHttpGraphQLQuery(string entityName, List<string> columnNames, int first)
{
var payload = new
Expand Down
26 changes: 5 additions & 21 deletions src/Service/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,14 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:35704",
"sslPort": 44353
}
},
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "graphql",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Azure.DataApiBuilder.Service": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "graphql",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "MsSql"
},
"dotnetRunMessages": "true",
"dotnetRunMessages": true,
"applicationUrl": "https://localhost:5001;http://localhost:5000"
},
"Development": {
Expand All @@ -34,7 +18,7 @@
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": "true",
"dotnetRunMessages": true,
"applicationUrl": "https://localhost:5001;http://localhost:5000"
},
"PostgreSql": {
Expand All @@ -44,7 +28,7 @@
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "PostgreSql"
},
"dotnetRunMessages": "true",
"dotnetRunMessages": true,
"applicationUrl": "https://localhost:5001;http://localhost:5000"
},
"MsSql": {
Expand All @@ -54,7 +38,7 @@
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "MsSql"
},
"dotnetRunMessages": "true",
"dotnetRunMessages": true,
"applicationUrl": "https://localhost:5001;http://localhost:5000"
},
"MySql": {
Expand All @@ -64,7 +48,7 @@
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "MySql"
},
"dotnetRunMessages": "true",
"dotnetRunMessages": true,
"applicationUrl": "https://localhost:5001;http://localhost:5000"
},
"CosmosDb_NoSql": {
Expand Down
Loading
Loading