Skip to content
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

Cleanup diagnostic source #1529

Merged
merged 41 commits into from
Mar 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
b8689fd
use options log extension
SimonCropp Mar 16, 2022
2312c06
use fields instead of props
SimonCropp Mar 16, 2022
04ee7d3
simplify seealso
SimonCropp Mar 16, 2022
bc2abfc
prop to field
SimonCropp Mar 16, 2022
c47c365
simplify some ifs
SimonCropp Mar 16, 2022
4500d62
simplify branching
SimonCropp Mar 16, 2022
2a561b5
use some pattern matching
SimonCropp Mar 16, 2022
d2ee73e
pin DiagnosticSource.csproj at c#9
SimonCropp Mar 16, 2022
1282688
some pattern matching
SimonCropp Mar 16, 2022
6298661
ImplicitUsings>false
SimonCropp Mar 16, 2022
f65f42b
Update KeyValuePairExtensions.cs
SimonCropp Mar 16, 2022
ff167e6
Update CHANGELOG.md
SimonCropp Mar 16, 2022
9e6d5ce
remove some redundant return values
SimonCropp Mar 16, 2022
1af685b
GetParent => static
SimonCropp Mar 16, 2022
20c1f1f
remove redundant guid check
SimonCropp Mar 16, 2022
0a7de44
split out SentrySqlListenerExtensions
SimonCropp Mar 17, 2022
56f0ba8
some using statics
SimonCropp Mar 17, 2022
5c39fcb
file scoped namespaces
SimonCropp Mar 17, 2022
274005c
Update DiagnosticsSentryOptionsExtensionsTests.cs
SimonCropp Mar 17, 2022
73c816e
Update SentrySqlListenerTests.cs
SimonCropp Mar 17, 2022
57a37ae
readonly fields
SimonCropp Mar 17, 2022
52c6fc6
Update SentryEFCoreListenerTests.cs
SimonCropp Mar 17, 2022
c96a7f3
Update SentrySqlListenerTests.cs
SimonCropp Mar 17, 2022
fe3a722
Update SentrySqlListener.cs
SimonCropp Mar 17, 2022
b2384f7
redundant qualifiers
SimonCropp Mar 17, 2022
67213da
Update Database.cs
SimonCropp Mar 17, 2022
96ea334
Update SentryEFCoreListenerTests.cs
SimonCropp Mar 17, 2022
5c25967
Update SentrySqlListenerTests.cs
SimonCropp Mar 17, 2022
46d08d9
Update SentrySqlListenerTests.cs
SimonCropp Mar 17, 2022
19a3c99
.
SimonCropp Mar 17, 2022
8cdaafb
Update SentrySqlListener.cs
SimonCropp Mar 17, 2022
10b2b53
.
SimonCropp Mar 17, 2022
49a0e98
Revert "."
SimonCropp Mar 17, 2022
d9dc5e0
Revert "Update SentrySqlListener.cs"
SimonCropp Mar 17, 2022
8370147
Revert "."
SimonCropp Mar 17, 2022
a6581b7
Update SentryEFCoreListener.cs
SimonCropp Mar 17, 2022
7d4c992
Update SentrySqlListener.cs
SimonCropp Mar 17, 2022
fa7f9d8
Update SentrySqlListener.cs
SimonCropp Mar 17, 2022
2070a95
Update SentryEFCoreListenerTests.cs
SimonCropp Mar 17, 2022
b15974a
Update SentryEFCoreListener.cs
SimonCropp Mar 19, 2022
27a9071
Merge branch 'main' into cleanup-DiagnosticSource
SimonCropp Mar 19, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Fixes

- Ignore zero properties for MemoryInfo ([#1531](https://github.com/getsentry/sentry-dotnet/pull/1531))
- Cleanup diagnostic source ([#1529](https://github.com/getsentry/sentry-dotnet/pull/1529))

## 3.15.0

Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
using System;
using System.Diagnostics;
using Sentry.Extensibility;
using Sentry.Internal;

namespace Sentry.Internals.DiagnosticSource
{
internal class SentryDiagnosticListenerIntegration : IInternalSdkIntegration
{
private SentryDiagnosticSubscriber? _subscriber;
private IDisposable? _diagnosticListener { get; set; }
private IDisposable? _diagnosticListener;

public void Register(IHub hub, SentryOptions options)
{
if (options.TracesSampleRate == 0)
{
options.DiagnosticLogger?.Log(SentryLevel.Info, "DiagnosticSource Integration is now disabled due to TracesSampleRate being set to zero.");
options.Log(SentryLevel.Info, "DiagnosticSource Integration is now disabled due to TracesSampleRate being set to zero.");
options.DisableDiagnosticSourceIntegration();
return;
}
else
{
_subscriber = new SentryDiagnosticSubscriber(hub, options);
_diagnosticListener = DiagnosticListener.AllListeners.Subscribe(_subscriber);
}

_subscriber = new SentryDiagnosticSubscriber(hub, options);
_diagnosticListener = DiagnosticListener.AllListeners.Subscribe(_subscriber);
}

public void Unregister(IHub hub)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ namespace Sentry.Internals.DiagnosticSource
/// </summary>
internal class SentryDiagnosticSubscriber : IObserver<DiagnosticListener>, IDisposable
{
private SentryEFCoreListener? _efInterceptor { get; set; }
private SentrySqlListener? _sqlListener { get; set; }
private ConcurrentBag<IDisposable> _disposableListeners = new();
private IHub _hub { get; }
private SentryOptions _options { get; }
private SentryEFCoreListener? _efInterceptor;
private SentrySqlListener? _sqlListener;
private readonly ConcurrentBag<IDisposable> _disposableListeners = new();
private readonly IHub _hub;
private readonly SentryOptions _options;

public SentryDiagnosticSubscriber(IHub hub, SentryOptions options)
{
Expand All @@ -33,8 +33,10 @@ public void OnNext(DiagnosticListener listener)
_efInterceptor = new(_hub, _options);
_disposableListeners.Add(listener.Subscribe(_efInterceptor));
_options.Log(SentryLevel.Debug, "Registered integration with EF Core.");
return;
}
else if (listener.Name == "SqlClientDiagnosticListener")

if (listener.Name == "SqlClientDiagnosticListener")
{
_sqlListener = new(_hub, _options);
_disposableListeners.Add(listener.Subscribe(_sqlListener));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#if NETCOREAPP3_0_OR_GREATER
using System;
using System.Collections.Generic;
using System.Threading;
#endif
using System.Collections.Generic;
using System;
using Sentry.Extensibility;

namespace Sentry.Internals.DiagnosticSource
Expand All @@ -27,22 +25,22 @@ private enum SentryEFSpanType

/// <summary>
/// Used for EF Core 2.X and 3.X.
/// <seealso href="https://docs.microsoft.com/dotnet/api/microsoft.entityframeworkcore.diagnostics.coreeventid.querymodelcompiling?view=efcore-3.1"></seealso>
/// <seealso href="https://docs.microsoft.com/dotnet/api/microsoft.entityframeworkcore.diagnostics.coreeventid.querymodelcompiling?view=efcore-3.1"/>
/// </summary>
internal const string EFQueryStartCompiling = "Microsoft.EntityFrameworkCore.Query.QueryCompilationStarting";
/// <summary>
/// Used for EF Core 2.X and 3.X.
/// <seealso href="https://docs.microsoft.com/dotnet/api/microsoft.entityframeworkcore.diagnostics.coreeventid.querymodelcompiling?view=efcore-3.1"></seealso>
/// <seealso href="https://docs.microsoft.com/dotnet/api/microsoft.entityframeworkcore.diagnostics.coreeventid.querymodelcompiling?view=efcore-3.1"/>
/// </summary>
internal const string EFQueryCompiling = "Microsoft.EntityFrameworkCore.Query.QueryModelCompiling";
internal const string EFQueryCompiled = "Microsoft.EntityFrameworkCore.Query.QueryExecutionPlanned";

private IHub _hub { get; }
private SentryOptions _options { get; }
private readonly IHub _hub;
private readonly SentryOptions _options;

private AsyncLocal<WeakReference<ISpan>> _spansCompilerLocal = new();
private AsyncLocal<WeakReference<ISpan>> _spansQueryLocal = new();
private AsyncLocal<WeakReference<ISpan>> _spansConnectionLocal = new();
private readonly AsyncLocal<WeakReference<ISpan>> _spansCompilerLocal = new();
private readonly AsyncLocal<WeakReference<ISpan>> _spansQueryLocal = new();
private readonly AsyncLocal<WeakReference<ISpan>> _spansConnectionLocal = new();

private bool _logConnectionEnabled = true;
private bool _logQueryEnabled = true;
Expand All @@ -55,14 +53,20 @@ public SentryEFCoreListener(IHub hub, SentryOptions options)

internal void DisableConnectionSpan() => _logConnectionEnabled = false;

internal bool DisableQuerySpan() => _logQueryEnabled = false;
internal void DisableQuerySpan() => _logQueryEnabled = false;

private ISpan? GetParent(SentryEFSpanType type, Scope scope)
=> type == SentryEFSpanType.QueryExecution ? scope.GetSpan() : scope.Transaction;
private static ISpan? GetParent(SentryEFSpanType type, Scope scope)
{
if (type == SentryEFSpanType.QueryExecution)
{
return scope.GetSpan();
}

return scope.Transaction;
}

private ISpan? AddSpan(SentryEFSpanType type, string operation, string? description)
private void AddSpan(SentryEFSpanType type, string operation, string? description)
{
ISpan? span = null;
_hub.ConfigureScope(scope =>
{
if (scope.Transaction?.IsSampled != true)
Expand All @@ -75,45 +79,40 @@ public SentryEFCoreListener(IHub hub, SentryOptions options)
return;
}

if (GetSpanBucket(type) is not { } asyncLocalSpan)
{
return;
}

var asyncLocalSpan = GetSpanBucket(type);
asyncLocalSpan.Value = new WeakReference<ISpan>(startedChild);
span = startedChild;
});
return span;
}

private ISpan? TakeSpan(SentryEFSpanType type)
{
ISpan? span = null;
_hub.ConfigureScope(scope =>
{
if (scope.Transaction?.IsSampled == true)
if (scope.Transaction?.IsSampled != true)
{
if (GetSpanBucket(type)?.Value is { } reference &&
reference.TryGetTarget(out var startedSpan))
{
span = startedSpan;
}
else
{
_options.LogWarning("Trying to close a span that was already garbage collected. {0}", type);
}
return;
}

if (GetSpanBucket(type).Value is { } reference &&
reference.TryGetTarget(out var startedSpan))
{
span = startedSpan;
return;
}

_options.LogWarning("Trying to close a span that was already garbage collected. {0}", type);
});
return span;
}

private AsyncLocal<WeakReference<ISpan>>? GetSpanBucket(SentryEFSpanType type)
private AsyncLocal<WeakReference<ISpan>> GetSpanBucket(SentryEFSpanType type)
=> type switch
{
SentryEFSpanType.QueryCompiler => _spansCompilerLocal,
SentryEFSpanType.QueryExecution => _spansQueryLocal,
SentryEFSpanType.Connection => _spansConnectionLocal,
_ => null
_ => throw new ($"Unknown SentryEFSpanType: {type}")
};

public void OnCompleted() { }
Expand All @@ -125,41 +124,54 @@ public void OnNext(KeyValuePair<string, object?> value)
try
{
//Query compiler Span
if (value.Key == EFQueryStartCompiling || value.Key == EFQueryCompiling)
if (value.Key is EFQueryStartCompiling or EFQueryCompiling)
SimonCropp marked this conversation as resolved.
Show resolved Hide resolved
{
AddSpan(SentryEFSpanType.QueryCompiler, "db.query.compile", FilterNewLineValue(value.Value));
return;
}
else if (value.Key == EFQueryCompiled)

if (value.Key == EFQueryCompiled)
{
TakeSpan(SentryEFSpanType.QueryCompiler)?.Finish(SpanStatus.Ok);
return;
}

//Connection Span
//A transaction may or may not show a connection with it.
else if (_logConnectionEnabled && value.Key == EFConnectionOpening)

if (_logConnectionEnabled && value.Key == EFConnectionOpening)
{
AddSpan(SentryEFSpanType.Connection, "db.connection", null);
return;
}
else if (_logConnectionEnabled && value.Key == EFConnectionClosed)

if (_logConnectionEnabled && value.Key == EFConnectionClosed)
{
TakeSpan(SentryEFSpanType.Connection)?.Finish(SpanStatus.Ok);
return;
}

// return if not Query Execution Span
if (!_logQueryEnabled)
{
return;
}

if (value.Key == EFCommandExecuting)
{
AddSpan(SentryEFSpanType.QueryExecution, "db.query", FilterNewLineValue(value.Value));
return;
}

if (value.Key == EFCommandFailed)
{
TakeSpan(SentryEFSpanType.QueryExecution)?.Finish(SpanStatus.InternalError);
return;
}

//Query Execution Span
else if (_logQueryEnabled)
if (value.Key == EFCommandExecuted)
{
if (value.Key == EFCommandExecuting)
{
AddSpan(SentryEFSpanType.QueryExecution, "db.query", FilterNewLineValue(value.Value));
}
else if (value.Key == EFCommandFailed)
{
TakeSpan(SentryEFSpanType.QueryExecution)?.Finish(SpanStatus.InternalError);
}
else if (value.Key == EFCommandExecuted)
{
TakeSpan(SentryEFSpanType.QueryExecution)?.Finish(SpanStatus.Ok);
}
TakeSpan(SentryEFSpanType.QueryExecution)?.Finish(SpanStatus.Ok);
}
}
catch (Exception ex)
Expand Down
Loading