Skip to content

Experimental SqlLite integration #4274

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
6 changes: 4 additions & 2 deletions Sentry-CI-Build-macOS.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@
"samples\\Sentry.Samples.OpenTelemetry.AspNetCore\\Sentry.Samples.OpenTelemetry.AspNetCore.csproj",
"samples\\Sentry.Samples.OpenTelemetry.Console\\Sentry.Samples.OpenTelemetry.Console.csproj",
"samples\\Sentry.Samples.Serilog\\Sentry.Samples.Serilog.csproj",
"samples\\Sentry.Samples.Sqlite\\Sentry.Samples.Sqlite.csproj",
"src\\Sentry.Analyzers\\Sentry.Analyzers.csproj",
"src\\Sentry.Android.AssemblyReader\\Sentry.Android.AssemblyReader.csproj",
"src\\Sentry.AspNet\\Sentry.AspNet.csproj",
"src\\Sentry.AspNetCore.Blazor.WebAssembly\\Sentry.AspNetCore.Blazor.WebAssembly.csproj",
"src\\Sentry.AspNetCore.Grpc\\Sentry.AspNetCore.Grpc.csproj",
"src\\Sentry.AspNetCore\\Sentry.AspNetCore.csproj",
"src\\Sentry.AspNet\\Sentry.AspNet.csproj",
"src\\Sentry.Azure.Functions.Worker\\Sentry.Azure.Functions.Worker.csproj",
"src\\Sentry.Bindings.Android\\Sentry.Bindings.Android.csproj",
"src\\Sentry.Bindings.Cocoa\\Sentry.Bindings.Cocoa.csproj",
Expand All @@ -58,6 +59,7 @@
"src\\Sentry.Profiling\\Sentry.Profiling.csproj",
"src\\Sentry.Serilog\\Sentry.Serilog.csproj",
"src\\Sentry.SourceGenerators\\Sentry.SourceGenerators.csproj",
"src\\Sentry.Sqlite\\Sentry.Sqlite.csproj",
"src\\Sentry\\Sentry.csproj",
"test\\Sentry.Analyzers.Tests\\Sentry.Analyzers.Tests.csproj",
"test\\Sentry.Android.AssemblyReader.Tests\\Sentry.Android.AssemblyReader.Tests.csproj",
Expand Down Expand Up @@ -87,4 +89,4 @@
"test\\SingleFileTestApp\\SingleFileTestApp.csproj"
]
}
}
}
30 changes: 30 additions & 0 deletions Sentry.sln
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sentry.SourceGenerators.Tes
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sentry.Maui.CommunityToolkit.Mvvm.Tests", "test\Sentry.Maui.CommunityToolkit.Mvvm.Tests\Sentry.Maui.CommunityToolkit.Mvvm.Tests.csproj", "{ADC91A84-6054-42EC-8241-0D717E4C7194}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sentry.Sqlite", "src\Sentry.Sqlite\Sentry.Sqlite.csproj", "{CDCCB2EC-9DA8-4CBE-94D6-00EAB990D912}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sentry.Samples.Sqlite", "samples\Sentry.Samples.Sqlite\Sentry.Samples.Sqlite.csproj", "{BB4FF3AE-C835-41AB-8F75-1611977BCF71}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -1252,6 +1256,30 @@ Global
{ADC91A84-6054-42EC-8241-0D717E4C7194}.Release|x64.Build.0 = Release|Any CPU
{ADC91A84-6054-42EC-8241-0D717E4C7194}.Release|x86.ActiveCfg = Release|Any CPU
{ADC91A84-6054-42EC-8241-0D717E4C7194}.Release|x86.Build.0 = Release|Any CPU
{CDCCB2EC-9DA8-4CBE-94D6-00EAB990D912}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CDCCB2EC-9DA8-4CBE-94D6-00EAB990D912}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CDCCB2EC-9DA8-4CBE-94D6-00EAB990D912}.Debug|x64.ActiveCfg = Debug|Any CPU
{CDCCB2EC-9DA8-4CBE-94D6-00EAB990D912}.Debug|x64.Build.0 = Debug|Any CPU
{CDCCB2EC-9DA8-4CBE-94D6-00EAB990D912}.Debug|x86.ActiveCfg = Debug|Any CPU
{CDCCB2EC-9DA8-4CBE-94D6-00EAB990D912}.Debug|x86.Build.0 = Debug|Any CPU
{CDCCB2EC-9DA8-4CBE-94D6-00EAB990D912}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CDCCB2EC-9DA8-4CBE-94D6-00EAB990D912}.Release|Any CPU.Build.0 = Release|Any CPU
{CDCCB2EC-9DA8-4CBE-94D6-00EAB990D912}.Release|x64.ActiveCfg = Release|Any CPU
{CDCCB2EC-9DA8-4CBE-94D6-00EAB990D912}.Release|x64.Build.0 = Release|Any CPU
{CDCCB2EC-9DA8-4CBE-94D6-00EAB990D912}.Release|x86.ActiveCfg = Release|Any CPU
{CDCCB2EC-9DA8-4CBE-94D6-00EAB990D912}.Release|x86.Build.0 = Release|Any CPU
{BB4FF3AE-C835-41AB-8F75-1611977BCF71}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BB4FF3AE-C835-41AB-8F75-1611977BCF71}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BB4FF3AE-C835-41AB-8F75-1611977BCF71}.Debug|x64.ActiveCfg = Debug|Any CPU
{BB4FF3AE-C835-41AB-8F75-1611977BCF71}.Debug|x64.Build.0 = Debug|Any CPU
{BB4FF3AE-C835-41AB-8F75-1611977BCF71}.Debug|x86.ActiveCfg = Debug|Any CPU
{BB4FF3AE-C835-41AB-8F75-1611977BCF71}.Debug|x86.Build.0 = Debug|Any CPU
{BB4FF3AE-C835-41AB-8F75-1611977BCF71}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BB4FF3AE-C835-41AB-8F75-1611977BCF71}.Release|Any CPU.Build.0 = Release|Any CPU
{BB4FF3AE-C835-41AB-8F75-1611977BCF71}.Release|x64.ActiveCfg = Release|Any CPU
{BB4FF3AE-C835-41AB-8F75-1611977BCF71}.Release|x64.Build.0 = Release|Any CPU
{BB4FF3AE-C835-41AB-8F75-1611977BCF71}.Release|x86.ActiveCfg = Release|Any CPU
{BB4FF3AE-C835-41AB-8F75-1611977BCF71}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -1343,5 +1371,7 @@ Global
{C3CDF61C-3E28-441C-A9CE-011C89D11719} = {230B9384-90FD-4551-A5DE-1A5C197F25B6}
{3A76FF7D-2F32-4EA5-8999-2FFE3C7CB893} = {6987A1CC-608E-4868-A02C-09D30C8B7B2D}
{ADC91A84-6054-42EC-8241-0D717E4C7194} = {6987A1CC-608E-4868-A02C-09D30C8B7B2D}
{CDCCB2EC-9DA8-4CBE-94D6-00EAB990D912} = {230B9384-90FD-4551-A5DE-1A5C197F25B6}
{BB4FF3AE-C835-41AB-8F75-1611977BCF71} = {21B42F60-5802-404E-90F0-AEBCC56760C0}
EndGlobalSection
EndGlobal
89 changes: 89 additions & 0 deletions samples/Sentry.Samples.Sqlite/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System;
using System.IO;
using Microsoft.Data.Sqlite;
using Sentry.Sqlite;

SentrySdk.Init(options =>
{
#if !SENTRY_DSN_DEFINED_IN_ENV
// A DSN is required. You can set here in code, or you can set it in the SENTRY_DSN environment variable.
// See https://docs.sentry.io/product/sentry-basics/dsn-explainer/
options.Dsn = SamplesShared.Dsn;
#endif
options.Debug = false;
options.TracesSampleRate = 1.0;
});

using (var connection = new SentrySqliteConnection("Data Source=hello.db"))
{
connection.Open();

var command = connection.CreateCommand();
command.CommandText =
@"
CREATE TABLE user (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL
);

INSERT INTO user
VALUES (1, 'Brice'),
(2, 'Alexander'),
(3, 'Nate');
";
command.ExecuteNonQuery();

Console.Write("Name: ");
var name = Console.ReadLine();

#region snippet_Parameter
command.CommandText =
@"
INSERT INTO user (name)
VALUES ($name)
";
command.Parameters.AddWithValue("$name", name);
#endregion
command.ExecuteNonQuery();

command.CommandText =
@"
SELECT last_insert_rowid()
";
var newId = (long)command.ExecuteScalar()!;

Console.WriteLine($"Your new user ID is {newId}.");
}

Console.Write("User ID: ");
var id = int.Parse(Console.ReadLine()!);

#region snippet_HelloWorld
using (var connection = new SqliteConnection("Data Source=hello.db"))
{
connection.Open();

var command = connection.CreateCommand();
command.CommandText =
@"
SELECT name
FROM user
WHERE id = $id
";
command.Parameters.AddWithValue("$id", id);

using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
var name = reader.GetString(0);

Console.WriteLine($"Hello, {name}!");
}
}
}
#endregion

// Clean up
SqliteConnection.ClearAllPools();
File.Delete("hello.db");
19 changes: 19 additions & 0 deletions samples/Sentry.Samples.Sqlite/Sentry.Samples.Sqlite.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Data.Sqlite" Version="9.0.6" />
<PackageReference Include="SQLitePCLRaw.core" Version="2.1.11" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Sentry.Sqlite\Sentry.Sqlite.csproj" />
</ItemGroup>

</Project>
20 changes: 20 additions & 0 deletions src/Sentry.Sqlite/Sentry.Sqlite.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Description>Experimental Microsoft.Data.Sqlite integration for Sentry - Open-source error tracking that helps developers monitor and fix crashes in real time.</Description>
<PackageTags>$(PackageTags);Sqlite</PackageTags>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Sentry\Sentry.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Data.Sqlite" Version="9.0.6" />
<PackageReference Include="SQLitePCLRaw.core" Version="2.1.11" />
</ItemGroup>

</Project>
37 changes: 37 additions & 0 deletions src/Sentry.Sqlite/SentrySqliteCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Microsoft.Data.Sqlite;

namespace Sentry.Sqlite;

/// <inheritdoc cref="SqliteCommand"/>
public class SentrySqliteCommand : SqliteCommand
{
internal ITransactionTracer? TransactionTracer { get; set; } = null;

/// <inheritdoc cref="SqliteCommand.ExecuteNonQuery"/>
public override int ExecuteNonQuery()
{
var span = TransactionTracer?.StartChild("db.query", CommandText);
try
{
return base.ExecuteNonQuery();
}
finally
{
span?.Finish();
}
}

/// <inheritdoc cref="SqliteCommand.ExecuteScalar"/>
public override object? ExecuteScalar()
{
var span = TransactionTracer?.StartChild("db.query", CommandText);
try
{
return base.ExecuteScalar();
}
finally
{
span?.Finish();
}
}
}
52 changes: 52 additions & 0 deletions src/Sentry.Sqlite/SentrySqliteConnection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System.Data.Common;
using Microsoft.Data.Sqlite;

namespace Sentry.Sqlite;

/// <inheritdoc cref="SqliteConnection"/>
public class SentrySqliteConnection : SqliteConnection
{
private ITransactionTracer? TransactionTracer { get; set; } = null;

/// <summary>
/// Initializes a new instance of the <see cref="SentrySqliteConnection" /> class.
/// </summary>
/// <param name="connectionString">The string used to open the connection.</param>
/// <seealso href="https://docs.microsoft.com/dotnet/standard/data/sqlite/connection-strings">Connection Strings</seealso>
/// <seealso cref="SqliteConnectionStringBuilder" />
public SentrySqliteConnection(string? connectionString) : base(connectionString)
{
}

/// <inheritdoc cref="SqliteConnection.Open"/>
public override void Open()
{
TransactionTracer = SentrySdk.StartTransaction(
"Open SQLite Connection",
"db.sqlite.open"
);
base.Open();
}

/// <inheritdoc cref="SqliteConnection.CreateCommand"/>
public new virtual SentrySqliteCommand CreateCommand()
=> new()
{
Connection = this,
CommandTimeout = DefaultTimeout,
Transaction = Transaction,
TransactionTracer = TransactionTracer
};

/// <inheritdoc cref="SqliteConnection.CreateDbCommand"/>
protected override DbCommand CreateDbCommand()
=> CreateCommand();

/// <inheritdoc cref="SqliteConnection.Close"/>
public override void Close()
{
base.Close();
TransactionTracer?.Finish();
TransactionTracer = null;
}
}