Skip to content

Commit

Permalink
chore: Add logging of query and params (#28)
Browse files Browse the repository at this point in the history
* Add logging of query and params

* Update DatabricksSqlQueryBuilder.cs

* Update DatabricksSqlQueryBuilder.cs

* Update DatabricksSqlQueryBuilder.cs

* Added loggerfactory

* Log executed sql as information

* Update host.json
  • Loading branch information
FirestarJes authored Sep 19, 2024
1 parent 7c17ed0 commit e2ac4cf
Show file tree
Hide file tree
Showing 14 changed files with 79 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
"isEnabled": true,
"excludedTypes": "Request"
},
"logLevel": {
"default": "Information"
},
"enableLiveMetricsFilters": true
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace Energinet.DataHub.SettlementReport.Infrastructure.Experimental;
Expand All @@ -31,10 +32,11 @@ public abstract class DatabricksContextBase : IDisposable

protected DatabricksContextBase(
DatabricksSqlWarehouseQueryExecutor databricksSqlWarehouseQueryExecutor,
IOptions<DeltaTableOptions> options)
IOptions<DeltaTableOptions> options,
ILoggerFactory loggerFactory)
{
_dbContext = new DbContextCore(OnModelCreating);
_executor = new DatabricksSqlQueryExecutor(_dbContext, databricksSqlWarehouseQueryExecutor, options);
_executor = new DatabricksSqlQueryExecutor(_dbContext, databricksSqlWarehouseQueryExecutor, options, loggerFactory);
}

public void Dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace Energinet.DataHub.SettlementReport.Infrastructure.Experimental;
Expand All @@ -28,11 +29,16 @@ public sealed class DatabricksSqlQueryBuilder
{
private readonly DbContext _context;
private readonly IOptions<DeltaTableOptions> _options;
private readonly ILogger<DatabricksSqlQueryBuilder> _logger;

public DatabricksSqlQueryBuilder(DbContext context, IOptions<DeltaTableOptions> options)
public DatabricksSqlQueryBuilder(
DbContext context,
IOptions<DeltaTableOptions> options,
ILoggerFactory loggerFactory)
{
_context = context;
_options = options;
_logger = loggerFactory.CreateLogger<DatabricksSqlQueryBuilder>();
}

public DatabricksStatement Build(DatabricksSqlQueryable query)
Expand Down Expand Up @@ -101,7 +107,10 @@ private string PrepareSqlStatement(DatabricksSqlQueryable query, Func<string, st
}

sqlParameters = sqlParams;
return TranslateTransactToAnsi(sqlStatement);
var translated = TranslateTransactToAnsi(sqlStatement);
var logString = $"Translated SQL: {translated}" + Environment.NewLine + string.Join(Environment.NewLine, sqlParams.Select(x => $"Parameter Name: {x.Key} | Value: {x.Value}"));
_logger.LogInformation(logString);
return translated;
}

private string TranslateTransactToAnsi(StringBuilder transactSqlQuery)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Energinet.DataHub.Core.Databricks.SqlStatementExecution.Formats;
using Energinet.DataHub.SettlementReport.Common.Infrastructure.Options;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace Energinet.DataHub.SettlementReport.Infrastructure.Experimental;
Expand All @@ -30,9 +31,10 @@ public sealed class DatabricksSqlQueryExecutor
public DatabricksSqlQueryExecutor(
DbContext dbContext,
DatabricksSqlWarehouseQueryExecutor databricksSqlWarehouseQueryExecutor,
IOptions<DeltaTableOptions> options)
IOptions<DeltaTableOptions> options,
ILoggerFactory loggerFactory)
{
_sqlQueryBuilder = new DatabricksSqlQueryBuilder(dbContext, options);
_sqlQueryBuilder = new DatabricksSqlQueryBuilder(dbContext, options, loggerFactory);
_sqlRowHydrator = new DatabricksSqlRowHydrator();
_databricksSqlWarehouseQueryExecutor = databricksSqlWarehouseQueryExecutor;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Energinet.DataHub.SettlementReport.Common.Infrastructure.Options;
using Energinet.DataHub.SettlementReport.Infrastructure.Experimental;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace Energinet.DataHub.SettlementReport.Infrastructure.Persistence.Databricks;
Expand All @@ -26,8 +27,9 @@ public sealed class SettlementReportDatabricksContext : DatabricksContextBase, I

public SettlementReportDatabricksContext(
IOptions<DeltaTableOptions> deltaTableOptions,
DatabricksSqlWarehouseQueryExecutor sqlWarehouseQueryExecutor)
: base(sqlWarehouseQueryExecutor, deltaTableOptions)
DatabricksSqlWarehouseQueryExecutor sqlWarehouseQueryExecutor,
ILoggerFactory loggerFactory)
: base(sqlWarehouseQueryExecutor, deltaTableOptions, loggerFactory)
{
_deltaTableOptions = deltaTableOptions;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
using Energinet.DataHub.SettlementReport.Interfaces.Models;
using Energinet.DataHub.SettlementReport.Interfaces.SettlementReports_v2.Models;
using Energinet.DataHub.Wholesale.CalculationResults.IntegrationTests.Fixtures;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Moq;
using NodaTime;
Expand Down Expand Up @@ -58,31 +60,36 @@ public SettlementReportFileRequestHandlerIntegrationTests(
Fixture.Inject(mockedOptions);

var sqlWarehouseQueryExecutor = _databricksSqlStatementApiFixture.GetDatabricksExecutor();
var databricksContext = new SettlementReportDatabricksContext(mockedOptions.Object, sqlWarehouseQueryExecutor);
var databricksContext = new SettlementReportDatabricksContext(mockedOptions.Object, sqlWarehouseQueryExecutor, NullLoggerFactory.Instance);

var settlementReportDataRepository = new SettlementReportEnergyResultRepository(databricksContext);
var settlementReportWholesaleRepository = new SettlementReportWholesaleRepository(databricksContext);
var settlementReportChargeLinkPeriodsRepository = new SettlementReportChargeLinkPeriodsRepository(databricksContext);

var settlementReportMeteringPointMasterDataRepository = new SettlementReportMeteringPointMasterDataRepository(new SettlementReportDatabricksContext(
mockedOptions.Object,
sqlWarehouseQueryExecutor));
sqlWarehouseQueryExecutor,
NullLoggerFactory.Instance));

var settlementReportMeteringPointTimeSeriesResultRepository = new SettlementReportMeteringPointTimeSeriesResultRepository(new SettlementReportDatabricksContext(
mockedOptions.Object,
sqlWarehouseQueryExecutor));
sqlWarehouseQueryExecutor,
NullLoggerFactory.Instance));

var settlementReportMonthlyAmountRepository = new SettlementReportMonthlyAmountRepository(new SettlementReportDatabricksContext(
mockedOptions.Object,
sqlWarehouseQueryExecutor));
sqlWarehouseQueryExecutor,
NullLoggerFactory.Instance));

var settlementReportChargePriceRepository = new SettlementReportChargePriceRepository(new SettlementReportDatabricksContext(
mockedOptions.Object,
sqlWarehouseQueryExecutor));
sqlWarehouseQueryExecutor,
NullLoggerFactory.Instance));

var settlementReportMonthlyAmountTotalRepository = new SettlementReportMonthlyAmountTotalRepository(new SettlementReportDatabricksContext(
mockedOptions.Object,
sqlWarehouseQueryExecutor));
sqlWarehouseQueryExecutor,
NullLoggerFactory.Instance));

Fixture.Inject<ISettlementReportFileGeneratorFactory>(new SettlementReportFileGeneratorFactory(
settlementReportDataRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
using Energinet.DataHub.SettlementReport.Interfaces.SettlementReports_v2.Models;
using Energinet.DataHub.Wholesale.CalculationResults.IntegrationTests.Fixtures;
using FluentAssertions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using Xunit;

namespace Energinet.DataHub.Wholesale.CalculationResults.IntegrationTests.Infrastructure.SettlementReports_v2;
Expand All @@ -39,7 +42,8 @@ public SettlementReportChargeLinkPeriodsRepositoryTests(MigrationsFreeDatabricks

Fixture.Inject<ISettlementReportDatabricksContext>(new SettlementReportDatabricksContext(
_databricksSqlStatementApiFixture.DatabricksSchemaManager.DeltaTableOptions,
_databricksSqlStatementApiFixture.GetDatabricksExecutor()));
_databricksSqlStatementApiFixture.GetDatabricksExecutor(),
NullLoggerFactory.Instance));
}

[Fact(Skip = "Performance testing")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
using Energinet.DataHub.SettlementReport.Interfaces.Models;
using Energinet.DataHub.SettlementReport.Interfaces.SettlementReports_v2.Models;
using Energinet.DataHub.Wholesale.CalculationResults.IntegrationTests.Fixtures;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using Xunit;

namespace Energinet.DataHub.Wholesale.CalculationResults.IntegrationTests.Infrastructure.SettlementReports_v2;
Expand Down Expand Up @@ -50,7 +53,8 @@ public SettlementReportChargePricesRepositoryTests(MigrationsFreeDatabricksSqlSt

Fixture.Inject<ISettlementReportDatabricksContext>(new SettlementReportDatabricksContext(
_databricksSqlStatementApiFixture.DatabricksSchemaManager.DeltaTableOptions,
_databricksSqlStatementApiFixture.GetDatabricksExecutor()));
_databricksSqlStatementApiFixture.GetDatabricksExecutor(),
NullLoggerFactory.Instance));
}

[Fact(Skip = "Performance testing")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
using Energinet.DataHub.SettlementReport.Interfaces.Models;
using Energinet.DataHub.SettlementReport.Interfaces.SettlementReports_v2.Models;
using Energinet.DataHub.Wholesale.CalculationResults.IntegrationTests.Fixtures;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using Xunit;

namespace Energinet.DataHub.Wholesale.CalculationResults.IntegrationTests.Infrastructure.SettlementReports_v2;
Expand All @@ -38,7 +41,8 @@ public SettlementReportEnergyResultRepositoryTests(MigrationsFreeDatabricksSqlSt

Fixture.Inject<ISettlementReportDatabricksContext>(new SettlementReportDatabricksContext(
_databricksSqlStatementApiFixture.DatabricksSchemaManager.DeltaTableOptions,
_databricksSqlStatementApiFixture.GetDatabricksExecutor()));
_databricksSqlStatementApiFixture.GetDatabricksExecutor(),
NullLoggerFactory.Instance));
}

[Fact(Skip = "Performance testing")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
using Energinet.DataHub.SettlementReport.Interfaces.SettlementReports_v2.Models;
using Energinet.DataHub.Wholesale.CalculationResults.IntegrationTests.Fixtures;
using FluentAssertions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using Xunit;

namespace Energinet.DataHub.Wholesale.CalculationResults.IntegrationTests.Infrastructure.SettlementReports_v2;
Expand All @@ -39,7 +42,8 @@ public SettlementReportMeteringPointMasterDataRepositoryTests(MigrationsFreeData

Fixture.Inject<ISettlementReportDatabricksContext>(new SettlementReportDatabricksContext(
_databricksSqlStatementApiFixture.DatabricksSchemaManager.DeltaTableOptions,
_databricksSqlStatementApiFixture.GetDatabricksExecutor()));
_databricksSqlStatementApiFixture.GetDatabricksExecutor(),
NullLoggerFactory.Instance));
}

[Fact(Skip = "Performance testing")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
using Energinet.DataHub.SettlementReport.Interfaces.Models;
using Energinet.DataHub.SettlementReport.Interfaces.SettlementReports_v2.Models;
using Energinet.DataHub.Wholesale.CalculationResults.IntegrationTests.Fixtures;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using Xunit;

namespace Energinet.DataHub.Wholesale.CalculationResults.IntegrationTests.Infrastructure.SettlementReports_v2;
Expand All @@ -39,7 +42,8 @@ public SettlementReportMeteringPointTimeSeriesResultRepositoryTests(MigrationsFr

Fixture.Inject<ISettlementReportDatabricksContext>(new SettlementReportDatabricksContext(
_databricksSqlStatementApiFixture.DatabricksSchemaManager.DeltaTableOptions,
_databricksSqlStatementApiFixture.GetDatabricksExecutor()));
_databricksSqlStatementApiFixture.GetDatabricksExecutor(),
NullLoggerFactory.Instance));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
using Energinet.DataHub.SettlementReport.Interfaces.Models;
using Energinet.DataHub.SettlementReport.Interfaces.SettlementReports_v2.Models;
using Energinet.DataHub.Wholesale.CalculationResults.IntegrationTests.Fixtures;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using Xunit;

namespace Energinet.DataHub.Wholesale.CalculationResults.IntegrationTests.Infrastructure.SettlementReports_v2;
Expand All @@ -38,7 +41,8 @@ public SettlementReportMonthlyAmountRepositoryTests(MigrationsFreeDatabricksSqlS

Fixture.Inject<ISettlementReportDatabricksContext>(new SettlementReportDatabricksContext(
_databricksSqlStatementApiFixture.DatabricksSchemaManager.DeltaTableOptions,
_databricksSqlStatementApiFixture.GetDatabricksExecutor()));
_databricksSqlStatementApiFixture.GetDatabricksExecutor(),
NullLoggerFactory.Instance));
}

[Fact(Skip = "Performance testing")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
using Energinet.DataHub.SettlementReport.Interfaces.Models;
using Energinet.DataHub.SettlementReport.Interfaces.SettlementReports_v2.Models;
using Energinet.DataHub.Wholesale.CalculationResults.IntegrationTests.Fixtures;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using Xunit;

namespace Energinet.DataHub.Wholesale.CalculationResults.IntegrationTests.Infrastructure.SettlementReports_v2;
Expand All @@ -38,7 +41,8 @@ public SettlementReportMonthlyAmountTotalRepositoryTests(MigrationsFreeDatabrick

Fixture.Inject<ISettlementReportDatabricksContext>(new SettlementReportDatabricksContext(
_databricksSqlStatementApiFixture.DatabricksSchemaManager.DeltaTableOptions,
_databricksSqlStatementApiFixture.GetDatabricksExecutor()));
_databricksSqlStatementApiFixture.GetDatabricksExecutor(),
NullLoggerFactory.Instance));
}

[Fact(Skip = "Performance testing")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
using Energinet.DataHub.SettlementReport.Interfaces.Models;
using Energinet.DataHub.SettlementReport.Interfaces.SettlementReports_v2.Models;
using Energinet.DataHub.Wholesale.CalculationResults.IntegrationTests.Fixtures;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using Xunit;

namespace Energinet.DataHub.Wholesale.CalculationResults.IntegrationTests.Infrastructure.SettlementReports_v2;
Expand All @@ -38,7 +41,8 @@ public SettlementReportWholesaleRepositoryTests(MigrationsFreeDatabricksSqlState

Fixture.Inject<ISettlementReportDatabricksContext>(new SettlementReportDatabricksContext(
_databricksSqlStatementApiFixture.DatabricksSchemaManager.DeltaTableOptions,
_databricksSqlStatementApiFixture.GetDatabricksExecutor()));
_databricksSqlStatementApiFixture.GetDatabricksExecutor(),
NullLoggerFactory.Instance));
}

[Fact(Skip = "Performance testing")]
Expand Down

0 comments on commit e2ac4cf

Please sign in to comment.