Skip to content

Commit

Permalink
✨ log messages for connection, method and query events
Browse files Browse the repository at this point in the history
  • Loading branch information
Odonno committed Sep 18, 2024
1 parent 8a602a3 commit e71ec7f
Show file tree
Hide file tree
Showing 32 changed files with 1,468 additions and 1,094 deletions.
72 changes: 65 additions & 7 deletions SurrealDb.Embedded.InMemory/Internals/SurrealDbEngine.InMemory.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using System.Net;
using System.Diagnostics;
using System.Reactive;
using System.Runtime.InteropServices;
using System.Text.Json;
using System.Threading.Tasks;
using Dahomey.Cbor;
using Microsoft.Extensions.DependencyInjection;
using SurrealDb.Net.Exceptions;
using SurrealDb.Net.Extensions.DependencyInjection;
using SurrealDb.Net.Internals;
using SurrealDb.Net.Internals.Cbor;
using SurrealDb.Net.Internals.Constants;
using SurrealDb.Net.Internals.Extensions;
using SurrealDb.Net.Internals.Models;
using SurrealDb.Net.Internals.Models.LiveQuery;
using SurrealDb.Net.Internals.Stream;
using SurrealDb.Net.Models;
Expand All @@ -24,8 +23,10 @@ internal class SurrealDbInMemoryEngine : ISurrealDbInMemoryEngine
{
private static int _globalId;

private SurrealDbClientParams? _parameters;
private SurrealDbOptions? _parameters;
private Action<CborOptions>? _configureCborOptions;
private ISurrealDbLoggerFactory? _surrealDbLoggerFactory;

private readonly int _id;
private readonly SurrealDbEmbeddedEngineConfig _config = new();

Expand All @@ -43,12 +44,14 @@ public SurrealDbInMemoryEngine()
}

public void Initialize(
SurrealDbClientParams parameters,
Action<CborOptions>? configureCborOptions
SurrealDbOptions parameters,
Action<CborOptions>? configureCborOptions,
ISurrealDbLoggerFactory? surrealDbLoggerFactory
)
{
_parameters = parameters;
_configureCborOptions = configureCborOptions;
_surrealDbLoggerFactory = surrealDbLoggerFactory;

if (_parameters.Serialization?.ToLowerInvariant() == SerializationConstants.JSON)
{
Expand Down Expand Up @@ -81,6 +84,8 @@ public async Task Connect(CancellationToken cancellationToken)
{
if (!_isConnected)
{
_surrealDbLoggerFactory?.Connection?.LogConnectionAttempt(_parameters!.Endpoint!);

var taskCompletionSource = new TaskCompletionSource<bool>();

Action<ByteBuffer> success = (byteBuffer) =>
Expand Down Expand Up @@ -132,6 +137,18 @@ public async Task Connect(CancellationToken cancellationToken)
if (_config.Ns is not null)
{
await Use(_config.Ns, _config.Db!, cancellationToken).ConfigureAwait(false);

if (_config.Db is not null)
{
_surrealDbLoggerFactory?.Connection?.LogConnectionNamespaceAndDatabaseSet(
_config.Ns,
_config.Db
);
}
else
{
_surrealDbLoggerFactory?.Connection?.LogConnectionNamespaceSet(_config.Ns);
}
}

_isInitialized = true;
Expand Down Expand Up @@ -384,12 +401,31 @@ public async Task<SurrealDbResponse> RawQuery(
CancellationToken cancellationToken
)
{
long executionStartTime = Stopwatch.GetTimestamp();

var list = await SendRequestAsync<List<ISurrealDbResult>>(
Method.Query,
[query, parameters],
cancellationToken
)
.ConfigureAwait(false);

#if NET7_0_OR_GREATER
var executionTime = Stopwatch.GetElapsedTime(executionStartTime);
#else
long executionEndTime = Stopwatch.GetTimestamp();
var executionTime = TimeSpan.FromTicks(executionEndTime - executionStartTime);
#endif

_surrealDbLoggerFactory?.Query?.LogQuerySuccess(
query,
SurrealDbLoggerExtensions.FormatQueryParameters(
parameters,
_parameters!.Logging.SensitiveDataLoggingEnabled
),
SurrealDbLoggerExtensions.FormatExecutionTime(executionTime)
);

return new SurrealDbResponse(list);
}

Expand Down Expand Up @@ -610,6 +646,8 @@ private async Task<T> SendRequestAsync<T>(
CancellationToken cancellationToken
)
{
long executionStartTime = Stopwatch.GetTimestamp();

using var timeoutCts = new CancellationTokenSource();
var timeoutTask = Task.Delay(30_000, cancellationToken);
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
Expand All @@ -634,6 +672,10 @@ await CborSerializer
if (!canGetBuffer)
{
timeoutCts.Cancel();
_surrealDbLoggerFactory?.Method?.LogMethodFailed(
method.ToString(),
"Failed to retrieve serialized buffer."
); // TODO : Avoid ToString()
throw new SurrealDbException("Failed to retrieve serialized buffer.");
}

Expand Down Expand Up @@ -722,11 +764,27 @@ await CborSerializer

if (completedTask == taskCompletionSource.Task)
{
#if NET7_0_OR_GREATER
var executionTime = Stopwatch.GetElapsedTime(executionStartTime);
#else
long executionEndTime = Stopwatch.GetTimestamp();
var executionTime = TimeSpan.FromTicks(executionEndTime - executionStartTime);
#endif

timeoutCts.Cancel();
_surrealDbLoggerFactory?.Method?.LogMethodSuccess(
method.ToString(), // TODO : Avoid ToString()
SurrealDbLoggerExtensions.FormatRequestParameters(
parameters!,
_parameters!.Logging.SensitiveDataLoggingEnabled
),
SurrealDbLoggerExtensions.FormatExecutionTime(executionTime)
);
return await taskCompletionSource.Task.ConfigureAwait(false);
}

taskCompletionSource.TrySetCanceled(CancellationToken.None);
_surrealDbLoggerFactory?.Method?.LogMethodFailed(method.ToString(), "Timeout"); // TODO : Avoid ToString()
throw new TimeoutException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="..\SurrealDb.Net\Internals\Constants\LoggingConstants.cs" Link="Internals\Constants\LoggingConstants.cs" />
<Compile Include="..\SurrealDb.Net\Internals\Constants\SerializationConstants.cs" Link="Internals\Constants\SerializationConstants.cs" />
<Compile Include="..\SurrealDb.Net\Internals\Extensions\FormattableStringExtensions.cs" Link="Internals\Extensions\FormattableStringExtensions.cs" />
<Compile Include="..\SurrealDb.Net\Internals\Extensions\StringBuilderExtensions.cs" Link="Internals\Extensions\StringBuilderExtensions.cs" />
<Compile Include="..\SurrealDb.Net\Internals\Extensions\StringExtensions.cs" Link="Internals\Extensions\StringExtensions.cs" />
<Compile Include="..\SurrealDb.Net\Internals\Extensions\SurrealDbLoggerExtensions.cs" Link="Internals\Extensions\SurrealDbLoggerExtensions.cs" />
<Compile Include="..\SurrealDb.Net\Internals\Stream\MemoryStreamProvider.cs" Link="Internals\Stream\MemoryStreamProvider.cs" />
</ItemGroup>

Expand Down Expand Up @@ -55,7 +58,7 @@

<Content Include="../rust-embedded/target/release/libsurreal_memory.so" TargetPath="libsurreal_memory.so" CopyToOutputDirectory="Always" Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Linux)))'" />

<Content Include="../rust-embedded/target/release/libsurreal_memory.dylib" TargetPath="libsurreal_memory.dylib" CopyToOutputDirectory="Always" Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::OSX)))'" />
<Content Include="../rust-embedded/target/release/libsurreal_memory.dylib" TargetPath="libsurreal_memory.dylib" CopyToOutputDirectory="Always" Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::OSX)))'" />
</ItemGroup>

<ItemGroup Condition="'$(Configuration)' == 'Release' And !$(DefineConstants.Contains('BENCHMARK_MODE'))">
Expand Down
Loading

0 comments on commit e71ec7f

Please sign in to comment.