-
Notifications
You must be signed in to change notification settings - Fork 263
Add logic to send logs through File Sink #2825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
15b7f39
Add main logic for file sink
RubenCerna2079 b2bedda
Add tests for file sink
RubenCerna2079 3236ceb
Merge branch 'main' into dev/rubencerna/add-file-sink-logic
RubenCerna2079 815cb84
Fix library imports ordering
RubenCerna2079 ac18425
Fix tests and add changes based on comments
RubenCerna2079 a8d3246
Fix library ordering
RubenCerna2079 f6a2153
Fix failing sink file tests
RubenCerna2079 eb78dd7
Changes based on comments
RubenCerna2079 28db2ab
Fix file sink tests
RubenCerna2079 1dccfcb
Remove Unecessary Library
RubenCerna2079 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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
163 changes: 163 additions & 0 deletions
163
src/Service.Tests/Configuration/Telemetry/FileSinkTests.cs
This file contains hidden or 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,163 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.IO; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using Azure.DataApiBuilder.Config.ObjectModel; | ||
using Microsoft.AspNetCore.TestHost; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Serilog; | ||
using Serilog.Core; | ||
using static Azure.DataApiBuilder.Service.Tests.Configuration.ConfigurationTests; | ||
|
||
namespace Azure.DataApiBuilder.Service.Tests.Configuration.Telemetry; | ||
|
||
/// <summary> | ||
/// Contains tests for File Sink functionality. | ||
/// </summary> | ||
[TestClass, TestCategory(TestCategory.MSSQL)] | ||
public class FileSinkTests | ||
{ | ||
public TestContext TestContext { get; set; } | ||
|
||
private const string CONFIG_WITH_TELEMETRY = "dab-file-sink-test-config.json"; | ||
private const string CONFIG_WITHOUT_TELEMETRY = "dab-no-file-sink-test-config.json"; | ||
private static RuntimeConfig _configuration; | ||
|
||
/// <summary> | ||
/// This is a helper function that creates runtime config file with specified telemetry options. | ||
/// </summary> | ||
/// <param name="configFileName">Name of the config file to be created.</param> | ||
/// <param name="isFileSinkEnabled">Whether File Sink is enabled or not.</param> | ||
/// <param name="fileSinkPath">Path where logs will be sent to.</param> | ||
/// <param name="rollingInterval">Time it takes for logs to roll over to next file.</param> | ||
private static void SetUpTelemetryInConfig(string configFileName, bool isFileSinkEnabled, string fileSinkPath, RollingInterval? rollingInterval = null) | ||
{ | ||
DataSource dataSource = new(DatabaseType.MSSQL, | ||
GetConnectionStringFromEnvironmentConfig(environment: TestCategory.MSSQL), Options: null); | ||
|
||
_configuration = InitMinimalRuntimeConfig(dataSource, graphqlOptions: new(), restOptions: new()); | ||
|
||
TelemetryOptions _testTelemetryOptions = new(File: new FileSinkOptions(isFileSinkEnabled, fileSinkPath, rollingInterval)); | ||
_configuration = _configuration with { Runtime = _configuration.Runtime with { Telemetry = _testTelemetryOptions } }; | ||
|
||
File.WriteAllText(configFileName, _configuration.ToJson()); | ||
} | ||
|
||
/// <summary> | ||
/// Cleans up the test environment by deleting the runtime config with telemetry options. | ||
/// </summary> | ||
[TestCleanup] | ||
public void CleanUpTelemetryConfig() | ||
{ | ||
if (File.Exists(CONFIG_WITH_TELEMETRY)) | ||
{ | ||
File.Delete(CONFIG_WITH_TELEMETRY); | ||
} | ||
|
||
if (File.Exists(CONFIG_WITHOUT_TELEMETRY)) | ||
{ | ||
File.Delete(CONFIG_WITHOUT_TELEMETRY); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Tests if the services are correctly enabled for File Sink. | ||
/// </summary> | ||
[TestMethod] | ||
public void TestFileSinkServicesEnabled() | ||
{ | ||
// Arrange | ||
SetUpTelemetryInConfig(CONFIG_WITH_TELEMETRY, true, "/dab-log-test/file-sink-file.txt"); | ||
|
||
string[] args = new[] | ||
{ | ||
$"--ConfigFileName={CONFIG_WITH_TELEMETRY}" | ||
}; | ||
using TestServer server = new(Program.CreateWebHostBuilder(args)); | ||
|
||
// Additional assertions to check if File Sink is enabled correctly in services | ||
IServiceProvider serviceProvider = server.Services; | ||
LoggerConfiguration serilogLoggerConfiguration = serviceProvider.GetService<LoggerConfiguration>(); | ||
Logger serilogLogger = serviceProvider.GetService<Logger>(); | ||
|
||
// If serilogLoggerConfiguration and serilogLogger are not null, File Sink is enabled | ||
Assert.IsNotNull(serilogLoggerConfiguration, "LoggerConfiguration for Serilog should be registered."); | ||
Assert.IsNotNull(serilogLogger, "Logger for Serilog should be registered."); | ||
} | ||
|
||
/// <summary> | ||
/// Tests if the logs are flushed to the proper path when File Sink is enabled. | ||
/// </summary> | ||
/// <summary> | ||
/// Tests if the logs are flushed to the proper path when File Sink is enabled. | ||
/// </summary> | ||
[DataTestMethod] | ||
[DataRow("file-sink-test-file.txt")] | ||
[DataRow("file-sink-test-file.log")] | ||
[DataRow("file-sink-test-file.csv")] | ||
public async Task TestFileSinkSucceed(string fileName) | ||
RubenCerna2079 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
// Arrange | ||
SetUpTelemetryInConfig(CONFIG_WITH_TELEMETRY, true, fileName, RollingInterval.Infinite); | ||
|
||
string[] args = new[] | ||
{ | ||
$"--ConfigFileName={CONFIG_WITH_TELEMETRY}" | ||
}; | ||
using TestServer server = new(Program.CreateWebHostBuilder(args)); | ||
|
||
// Act | ||
using (HttpClient client = server.CreateClient()) | ||
{ | ||
HttpRequestMessage restRequest = new(HttpMethod.Get, "/api/Book"); | ||
await client.SendAsync(restRequest); | ||
} | ||
|
||
server.Dispose(); | ||
|
||
// Assert | ||
Assert.IsTrue(File.Exists(fileName)); | ||
RubenCerna2079 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
bool containsInfo = false; | ||
string[] allLines = File.ReadAllLines(fileName); | ||
foreach (string line in allLines) | ||
{ | ||
containsInfo = line.Contains("INF"); | ||
if (containsInfo) | ||
{ | ||
break; | ||
} | ||
} | ||
|
||
Assert.IsTrue(containsInfo); | ||
} | ||
|
||
/// <summary> | ||
/// Tests if the services are correctly disabled for File Sink. | ||
/// </summary> | ||
[TestMethod] | ||
public void TestFileSinkServicesDisabled() | ||
{ | ||
// Arrange | ||
SetUpTelemetryInConfig(CONFIG_WITHOUT_TELEMETRY, false, null); | ||
|
||
string[] args = new[] | ||
{ | ||
$"--ConfigFileName={CONFIG_WITHOUT_TELEMETRY}" | ||
}; | ||
using TestServer server = new(Program.CreateWebHostBuilder(args)); | ||
|
||
// Additional assertions to check if File Sink is enabled correctly in services | ||
IServiceProvider serviceProvider = server.Services; | ||
LoggerConfiguration serilogLoggerConfiguration = serviceProvider.GetService<LoggerConfiguration>(); | ||
Logger serilogLogger = serviceProvider.GetService<Logger>(); | ||
|
||
// If serilogLoggerConfiguration and serilogLogger are null, File Sink is disabled | ||
Assert.IsNull(serilogLoggerConfiguration, "LoggerConfiguration for Serilog should not be registered."); | ||
Assert.IsNull(serilogLogger, "Logger for Serilog should not be registered."); | ||
} | ||
} |
This file contains hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.