提交 98f84fd4 编写于 作者: A Ahoo Wang 提交者: Lemon

add SkyApm.Diagnostics.SmartSql (#167)

* add SkyApm.Diagnostics.SmartSql project

* add SmartSqlTracingDiagnosticProcessor

* add SkyApm.Diagnostics.SmartSql : DbSession - Open & BeginTransaction & CommitTransaction & Rollback & Dispose & Invoke

* 1. u SmartSql-version
2. add SmartSqlController Test

* 1. update PackageReference to PrivateAssets="All".
2. add description information to SkyApm.Diagnostics.SmartSql.csproj
上级 5bbe3125
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
......@@ -12,8 +17,10 @@
<AssemblyName>SkyApm.Sample.AspNet</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<FileUpgradeFlags>40</FileUpgradeFlags>
<UpgradeBackupLocation>D:\Work Space\skywalking-netcore\Backup1\</UpgradeBackupLocation>
<FileUpgradeFlags>
</FileUpgradeFlags>
<UpgradeBackupLocation>
</UpgradeBackupLocation>
<OldToolsVersion>4.0</OldToolsVersion>
<UseIISExpress>true</UseIISExpress>
<Use64BitIISExpress />
......@@ -193,7 +200,8 @@
</PropertyGroup>
<Error Condition="!Exists('..\..\packages\Grpc.Core.1.12.0\build\net45\Grpc.Core.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Grpc.Core.1.12.0\build\net45\Grpc.Core.targets'))" />
</Target>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v15.0\WebApplications\Microsoft.WebApplication.targets" />
<Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" Condition="false" />
<ProjectExtensions>
<VisualStudio>
<FlavorProperties GUID="{349C5851-65DF-11DA-9384-00065B846F21}">
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SmartSql;
namespace SkyApm.Sample.Backend.Controllers
{
[Route("[controller]/[action]")]
[ApiController]
public class SmartSqlController : ControllerBase
{
private readonly ISqlMapper _sqlMapper;
public SmartSqlController(ISqlMapper sqlMapper)
{
_sqlMapper = sqlMapper;
}
[HttpGet]
public Guid QueryError()
{
return _sqlMapper.QuerySingle<Guid>(new RequestContext
{
RealSql = "Error Sql"
});
}
[HttpGet]
public async Task<IEnumerable<dynamic>> QueryAsync()
{
return await _sqlMapper.QueryAsync<dynamic>(new RequestContext
{
RealSql = "SELECT Top (1000) T.* From T_AllPrimitive T With(NoLock)"
});
}
[HttpGet]
public IEnumerable<dynamic> Query()
{
return _sqlMapper.Query<dynamic>(new RequestContext
{
RealSql = "SELECT Top (1000) T.* From T_AllPrimitive T With(NoLock)"
});
}
[HttpGet]
public IEnumerable<dynamic> Transaction()
{
try
{
_sqlMapper.BeginTransaction();
var list = _sqlMapper.Query<dynamic>(new RequestContext
{
RealSql = "SELECT Top (1000) T.* From T_AllPrimitive T With(NoLock)"
});
_sqlMapper.CommitTransaction();
return list;
}
catch (Exception ex)
{
_sqlMapper.RollbackTransaction();
throw;
}
}
[HttpGet]
public IEnumerable<dynamic> TransactionError()
{
try
{
_sqlMapper.BeginTransaction();
var list = _sqlMapper.Query<dynamic>(new RequestContext
{
RealSql = "Error Sql"
});
_sqlMapper.CommitTransaction();
return list;
}
catch (Exception ex)
{
_sqlMapper.RollbackTransaction();
throw;
}
}
}
}
\ No newline at end of file
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\SkyApm.Agent.AspNetCore\SkyApm.Agent.AspNetCore.csproj" />
<ProjectReference Include="..\..\src\SkyApm.Diagnostics.EntityFrameworkCore.Sqlite\SkyApm.Diagnostics.EntityFrameworkCore.Sqlite.csproj" />
</ItemGroup>
......@@ -14,6 +15,9 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite">
<Version>2.1.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.0" />
<PackageReference Include="SmartSql.DIExtension" Version="4.0.0-rc1" />
</ItemGroup>
<ItemGroup>
<Content Update="appsettings.json">
......
......@@ -4,9 +4,12 @@ using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using SkyApm.Sample.Backend.Models;
using SkyApm.Sample.Backend.Sampling;
using SkyApm.Tracing;
using SmartSql;
using SmartSql.DataSource;
namespace SkyApm.Sample.Backend
{
......@@ -23,13 +26,21 @@ namespace SkyApm.Sample.Backend
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
var sqliteConnection = new SqliteConnection("DataSource=:memory:");
sqliteConnection.Open();
services.AddEntityFrameworkSqlite().AddDbContext<SampleDbContext>(c => c.UseSqlite(sqliteConnection));
services.AddSingleton<ISamplingInterceptor, CustomSamplingInterceptor>();
services.AddSmartSql(sp =>
{
return SmartSqlBuilder
.AddDataSource(DbProvider.SQLSERVER, "Data Source=.;Initial Catalog=SmartSqlTestDB;Integrated Security=True")
.UseLoggerFactory(sp.GetService<ILoggerFactory>())
.UseCache(false)
.Build();
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
......@@ -45,7 +56,7 @@ namespace SkyApm.Sample.Backend
using (var sampleDbContext = scope.ServiceProvider.GetService<SampleDbContext>())
{
sampleDbContext.Database.EnsureCreated();
}
}
}
app.UseMvc();
......
......@@ -8,6 +8,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.2.0" />
</ItemGroup>
<ItemGroup>
<Content Update="appsettings.json">
......
......@@ -34,6 +34,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{F0B43114
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Agent", "Agent", "{EF6194B2-9ACB-49B9-8049-DD6AFAEB0399}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Diagnostics", "Diagnostics", "{B5E677CF-2920-4B0A-A056-E73F6B2CF2BC}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "CLI", "CLI", "{1B0865AF-369E-493C-AA6F-C56D3E520A22}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkyApm.Abstractions", "src\SkyApm.Abstractions\SkyApm.Abstractions.csproj", "{C3860B5F-21D0-429F-8A00-B0C0F4573DF7}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkyApm.Sample.Backend", "sample\SkyApm.Sample.Backend\SkyApm.Sample.Backend.csproj", "{80A67B09-83F2-477F-907F-95FFF5B8FEAF}"
......@@ -68,17 +72,15 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkyApm.Utilities.Logging",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkyApm.Agent.AspNetCore", "src\SkyApm.Agent.AspNetCore\SkyApm.Agent.AspNetCore.csproj", "{5CB2889E-33DD-4326-9403-4977849E065D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SkyApm.Transport.Grpc.Protocol", "src\SkyApm.Transport.Grpc.Protocol\SkyApm.Transport.Grpc.Protocol.csproj", "{BAF5C1ED-09C4-4B32-8190-47D16265F01E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Diagnostics", "Diagnostics", "{B5E677CF-2920-4B0A-A056-E73F6B2CF2BC}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkyApm.Transport.Grpc.Protocol", "src\SkyApm.Transport.Grpc.Protocol\SkyApm.Transport.Grpc.Protocol.csproj", "{BAF5C1ED-09C4-4B32-8190-47D16265F01E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SkyApm.Core", "src\SkyApm.Core\SkyApm.Core.csproj", "{60D34708-45A2-4374-9500-5B3FF80C0AA9}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkyApm.Core", "src\SkyApm.Core\SkyApm.Core.csproj", "{60D34708-45A2-4374-9500-5B3FF80C0AA9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SkyApm.Diagnostics.EntityFrameworkCore.Pomelo.MySql", "src\SkyApm.Diagnostics.EntityFrameworkCore.Pomelo.MySql\SkyApm.Diagnostics.EntityFrameworkCore.Pomelo.MySql.csproj", "{2F7EEC69-35F4-4D31-B52A-7465D65E9DE0}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkyApm.Diagnostics.EntityFrameworkCore.Pomelo.MySql", "src\SkyApm.Diagnostics.EntityFrameworkCore.Pomelo.MySql\SkyApm.Diagnostics.EntityFrameworkCore.Pomelo.MySql.csproj", "{2F7EEC69-35F4-4D31-B52A-7465D65E9DE0}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "CLI", "CLI", "{1B0865AF-369E-493C-AA6F-C56D3E520A22}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkyApm.DotNet.CLI", "src\SkyApm.DotNet.CLI\SkyApm.DotNet.CLI.csproj", "{E909EEF5-5EBF-43F3-AB4A-6B2FB56EF89B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SkyApm.DotNet.CLI", "src\SkyApm.DotNet.CLI\SkyApm.DotNet.CLI.csproj", "{E909EEF5-5EBF-43F3-AB4A-6B2FB56EF89B}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkyApm.Diagnostics.SmartSql", "src\SkyApm.Diagnostics.SmartSql\SkyApm.Diagnostics.SmartSql.csproj", "{B43160C8-C9E3-43F6-83E9-A9A45B32F022}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
......@@ -170,40 +172,45 @@ Global
{E909EEF5-5EBF-43F3-AB4A-6B2FB56EF89B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E909EEF5-5EBF-43F3-AB4A-6B2FB56EF89B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E909EEF5-5EBF-43F3-AB4A-6B2FB56EF89B}.Release|Any CPU.Build.0 = Release|Any CPU
{B43160C8-C9E3-43F6-83E9-A9A45B32F022}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B43160C8-C9E3-43F6-83E9-A9A45B32F022}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B43160C8-C9E3-43F6-83E9-A9A45B32F022}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B43160C8-C9E3-43F6-83E9-A9A45B32F022}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{CBFF7EE0-69D7-4D6A-9BBD-8E567FF4D810} = {05BF0D4E-C824-4EC8-8330-36C1FC49910E}
{A4E67E09-3156-4D30-B451-F24F706E96C4} = {B5E677CF-2920-4B0A-A056-E73F6B2CF2BC}
{88AEFE60-F743-4AF3-8F55-B4B7B8079D95} = {05BF0D4E-C824-4EC8-8330-36C1FC49910E}
{4BD917BC-D579-4C75-9939-41BF012A4EE2} = {05BF0D4E-C824-4EC8-8330-36C1FC49910E}
{F0B43114-3BA9-4098-952D-0FB2241691A9} = {E38F0F6E-6E10-491D-8786-650F6A4B6698}
{EF6194B2-9ACB-49B9-8049-DD6AFAEB0399} = {05BF0D4E-C824-4EC8-8330-36C1FC49910E}
{B5E677CF-2920-4B0A-A056-E73F6B2CF2BC} = {05BF0D4E-C824-4EC8-8330-36C1FC49910E}
{1B0865AF-369E-493C-AA6F-C56D3E520A22} = {05BF0D4E-C824-4EC8-8330-36C1FC49910E}
{C3860B5F-21D0-429F-8A00-B0C0F4573DF7} = {CBFF7EE0-69D7-4D6A-9BBD-8E567FF4D810}
{80A67B09-83F2-477F-907F-95FFF5B8FEAF} = {844CEACD-4C85-4B15-9E2B-892B01FDA4BB}
{2B4E350E-A1E5-4B4A-A642-BCA6D3887E5A} = {844CEACD-4C85-4B15-9E2B-892B01FDA4BB}
{49DEFCA8-4289-4875-B6A5-35D84B3D2290} = {B5E677CF-2920-4B0A-A056-E73F6B2CF2BC}
{55621423-19C3-4928-8B45-666FA87BF6A2} = {B5E677CF-2920-4B0A-A056-E73F6B2CF2BC}
{CD334460-8E61-41EB-9762-62C6A342CACB} = {A4E67E09-3156-4D30-B451-F24F706E96C4}
{44DFFDF7-5935-475A-825F-2C0298464117} = {B5E677CF-2920-4B0A-A056-E73F6B2CF2BC}
{F5C529C9-23C4-46B7-84FF-E86C6D17EB3E} = {A4E67E09-3156-4D30-B451-F24F706E96C4}
{F8D96C30-369C-4FCB-B5B2-02EAA74199C9} = {A4E67E09-3156-4D30-B451-F24F706E96C4}
{3A454A8A-A1B7-4C6D-BE08-52D956F95CC1} = {EF6194B2-9ACB-49B9-8049-DD6AFAEB0399}
{C3649845-E458-448D-B97D-C7E231E1D040} = {844CEACD-4C85-4B15-9E2B-892B01FDA4BB}
{FF2FC3FB-D112-45C1-AF34-1FE37D55F682} = {CBFF7EE0-69D7-4D6A-9BBD-8E567FF4D810}
{CFBB522D-495F-40F9-B68F-DA25E48D9F6A} = {B5E677CF-2920-4B0A-A056-E73F6B2CF2BC}
{0B87C4B8-3F4F-456A-B588-28D8E9E3395A} = {4BD917BC-D579-4C75-9939-41BF012A4EE2}
{1812A19C-729C-410B-A90D-D5AF1404DD60} = {4BD917BC-D579-4C75-9939-41BF012A4EE2}
{AEB61F19-3DBB-411E-A67E-93007B96E160} = {4BD917BC-D579-4C75-9939-41BF012A4EE2}
{5CB2889E-33DD-4326-9403-4977849E065D} = {EF6194B2-9ACB-49B9-8049-DD6AFAEB0399}
{BAF5C1ED-09C4-4B32-8190-47D16265F01E} = {CBFF7EE0-69D7-4D6A-9BBD-8E567FF4D810}
{3A454A8A-A1B7-4C6D-BE08-52D956F95CC1} = {EF6194B2-9ACB-49B9-8049-DD6AFAEB0399}
{B5E677CF-2920-4B0A-A056-E73F6B2CF2BC} = {05BF0D4E-C824-4EC8-8330-36C1FC49910E}
{A4E67E09-3156-4D30-B451-F24F706E96C4} = {B5E677CF-2920-4B0A-A056-E73F6B2CF2BC}
{CFBB522D-495F-40F9-B68F-DA25E48D9F6A} = {B5E677CF-2920-4B0A-A056-E73F6B2CF2BC}
{55621423-19C3-4928-8B45-666FA87BF6A2} = {B5E677CF-2920-4B0A-A056-E73F6B2CF2BC}
{49DEFCA8-4289-4875-B6A5-35D84B3D2290} = {B5E677CF-2920-4B0A-A056-E73F6B2CF2BC}
{44DFFDF7-5935-475A-825F-2C0298464117} = {B5E677CF-2920-4B0A-A056-E73F6B2CF2BC}
{60D34708-45A2-4374-9500-5B3FF80C0AA9} = {CBFF7EE0-69D7-4D6A-9BBD-8E567FF4D810}
{2F7EEC69-35F4-4D31-B52A-7465D65E9DE0} = {A4E67E09-3156-4D30-B451-F24F706E96C4}
{1B0865AF-369E-493C-AA6F-C56D3E520A22} = {05BF0D4E-C824-4EC8-8330-36C1FC49910E}
{E909EEF5-5EBF-43F3-AB4A-6B2FB56EF89B} = {1B0865AF-369E-493C-AA6F-C56D3E520A22}
{B43160C8-C9E3-43F6-83E9-A9A45B32F022} = {B5E677CF-2920-4B0A-A056-E73F6B2CF2BC}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {94C0DA2C-CCCB-4314-93A2-9809B5DD0583}
......
......@@ -37,5 +37,7 @@ namespace SkyApm.Common
public static readonly StringOrIntValue NPGSQL_ENTITYFRAMEWORKCORE_POSTGRESQL = new StringOrIntValue("Npgsql.EntityFrameworkCore.PostgreSQL");
public static readonly StringOrIntValue ASPNET = new StringOrIntValue("AspNet");
public static readonly StringOrIntValue SMART_SQL = new StringOrIntValue("SmartSql");
}
}
\ No newline at end of file
......@@ -36,6 +36,7 @@ using SkyApm.Diagnostics.EntityFrameworkCore;
using SkyApm.Diagnostics.HttpClient;
using SkyApm.Diagnostics.SqlClient;
using SkyApm.Utilities.DependencyInjection;
using SkyApm.Diagnostics.SmartSql;
namespace SkyApm.Agent.AspNetCore
{
......@@ -62,7 +63,8 @@ namespace SkyApm.Agent.AspNetCore
services.AddSingleton<IEnvironmentProvider, HostingEnvironmentProvider>();
services.AddTracing().AddSampling().AddGrpcTransport().AddLogging();
services.AddSkyApmExtensions().AddAspNetCoreHosting().AddHttpClient().AddSqlClient()
.AddEntityFrameworkCore(c => c.AddPomeloMysql().AddNpgsql().AddSqlite());
.AddEntityFrameworkCore(c => c.AddPomeloMysql().AddNpgsql().AddSqlite())
.AddSmartSql();
return services;
}
......
......@@ -23,6 +23,7 @@
<ProjectReference Include="..\SkyApm.Diagnostics.EntityFrameworkCore\SkyApm.Diagnostics.EntityFrameworkCore.csproj" />
<ProjectReference Include="..\SkyApm.Core\SkyApm.Core.csproj" />
<ProjectReference Include="..\SkyApm.Diagnostics.HttpClient\SkyApm.Diagnostics.HttpClient.csproj" />
<ProjectReference Include="..\SkyApm.Diagnostics.SmartSql\SkyApm.Diagnostics.SmartSql.csproj" />
<ProjectReference Include="..\SkyApm.Diagnostics.SqlClient\SkyApm.Diagnostics.SqlClient.csproj" />
<ProjectReference Include="..\SkyApm.Transport.Grpc\SkyApm.Transport.Grpc.csproj" />
<ProjectReference Include="..\SkyApm.Utilities.Configuration\SkyApm.Utilities.Configuration.csproj" />
......
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\build\common.props" />
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyName>$(PackagePrefix).Diagnostics.SmartSql</AssemblyName>
<AssemblyTitle>$(PackagePrefix).Diagnostics.SmartSql</AssemblyTitle>
<PackageId>$(PackagePrefix).Diagnostics.SmartSql</PackageId>
<PackageTags>SkyWalking;APM;Diagnostics;SmartSql</PackageTags>
<Description>SkyApm.Diagnostics.SmartSql notifies of SmartSql requests.</Description>
<RootNamespace>SkyApm.Diagnostics.SmartSql</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="SmartSql" Version="4.0.0-rc3" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SkyApm.Abstractions\SkyApm.Abstractions.csproj" />
<ProjectReference Include="..\SkyApm.Core\SkyApm.Core.csproj" />
<ProjectReference Include="..\SkyApm.Utilities.DependencyInjection\SkyApm.Utilities.DependencyInjection.csproj" />
</ItemGroup>
</Project>
using Microsoft.Extensions.DependencyInjection;
using SkyApm.Utilities.DependencyInjection;
using System;
namespace SkyApm.Diagnostics.SmartSql
{
public static class SkyWalkingBuilderExtensions
{
public static SkyApmExtensions AddSmartSql(this SkyApmExtensions extensions)
{
if (extensions == null)
{
throw new ArgumentNullException(nameof(extensions));
}
extensions.Services.AddSingleton<ITracingDiagnosticProcessor, SmartSqlTracingDiagnosticProcessor>();
return extensions;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using SkyApm.Tracing;
using SmartSql;
using SmartSql.Diagnostics;
namespace SkyApm.Diagnostics.SmartSql
{
public class SmartSqlTracingDiagnosticProcessor : ITracingDiagnosticProcessor
{
public string ListenerName => SmartSqlDiagnosticListenerExtensions.SMART_SQL_DIAGNOSTIC_LISTENER;
private readonly ITracingContext _tracingContext;
private readonly ILocalSegmentContextAccessor _localSegmentContextAccessor;
public SmartSqlTracingDiagnosticProcessor(ITracingContext tracingContext,
ILocalSegmentContextAccessor localSegmentContextAccessor)
{
_tracingContext = tracingContext;
_localSegmentContextAccessor = localSegmentContextAccessor;
}
#region BeginTransaction
[DiagnosticName(SmartSqlDiagnosticListenerExtensions.SMART_SQL_BEFORE_DB_SESSION_BEGINTRANSACTION)]
public void BeforeDbSessionBeginTransaction([Object]DbSessionBeginTransactionBeforeEventData eventData)
{
var context = _tracingContext.CreateLocalSegmentContext("BeginTransaction");
context.Span.SpanLayer = Tracing.Segments.SpanLayer.DB;
context.Span.Component = Common.Components.SMART_SQL;
context.Span.AddTag(Common.Tags.DB_TYPE, "Sql");
}
[DiagnosticName(SmartSqlDiagnosticListenerExtensions.SMART_SQL_AFTER_DB_SESSION_BEGINTRANSACTION)]
public void AfterDbSessionBeginTransaction([Object]DbSessionBeginTransactionAfterEventData eventData)
{
var context = _localSegmentContextAccessor.Context;
if (context != null)
{
context.Span.Peer = new Common.StringOrIntValue(eventData.DbSession.Connection?.DataSource);
context.Span.AddTag(Common.Tags.DB_INSTANCE, eventData.DbSession.Connection?.Database);
_tracingContext.Release(context);
}
}
[DiagnosticName(SmartSqlDiagnosticListenerExtensions.SMART_SQL_ERROR_DB_SESSION_BEGINTRANSACTION)]
public void ErrorDbSessionBeginTransaction([Object]DbSessionBeginTransactionErrorEventData eventData)
{
var context = _localSegmentContextAccessor.Context;
if (context != null)
{
context.Span.ErrorOccurred(eventData.Exception);
_tracingContext.Release(context);
}
}
#endregion
#region Commit
[DiagnosticName(SmartSqlDiagnosticListenerExtensions.SMART_SQL_BEFORE_DB_SESSION_COMMIT)]
public void BeforeDbSessionCommit([Object]DbSessionCommitBeforeEventData eventData)
{
var context = _tracingContext.CreateLocalSegmentContext(eventData.Operation);
context.Span.SpanLayer = Tracing.Segments.SpanLayer.DB;
context.Span.Component = Common.Components.SMART_SQL;
context.Span.Peer = new Common.StringOrIntValue(eventData.DbSession.Connection?.DataSource);
context.Span.AddTag(Common.Tags.DB_INSTANCE, eventData.DbSession.Connection?.Database);
context.Span.AddTag(Common.Tags.DB_TYPE, "Sql");
}
[DiagnosticName(SmartSqlDiagnosticListenerExtensions.SMART_SQL_AFTER_DB_SESSION_COMMIT)]
public void AfterDbSessionCommit([Object]DbSessionCommitAfterEventData eventData)
{
var context = _localSegmentContextAccessor.Context;
if (context != null)
{
_tracingContext.Release(context);
}
}
[DiagnosticName(SmartSqlDiagnosticListenerExtensions.SMART_SQL_ERROR_DB_SESSION_COMMIT)]
public void ErrorDbSessionCommit([Object]DbSessionCommitErrorEventData eventData)
{
var context = _localSegmentContextAccessor.Context;
if (context != null)
{
context.Span.ErrorOccurred(eventData.Exception);
_tracingContext.Release(context);
}
}
#endregion
#region Rollback
[DiagnosticName(SmartSqlDiagnosticListenerExtensions.SMART_SQL_BEFORE_DB_SESSION_ROLLBACK)]
public void BeforeDbSessionRollback([Object]DbSessionRollbackBeforeEventData eventData)
{
var context = _tracingContext.CreateLocalSegmentContext(eventData.Operation);
context.Span.SpanLayer = Tracing.Segments.SpanLayer.DB;
context.Span.Component = Common.Components.SMART_SQL;
context.Span.Peer = new Common.StringOrIntValue(eventData.DbSession.Connection?.DataSource);
context.Span.AddTag(Common.Tags.DB_INSTANCE, eventData.DbSession.Connection?.Database);
context.Span.AddTag(Common.Tags.DB_TYPE, "Sql");
}
[DiagnosticName(SmartSqlDiagnosticListenerExtensions.SMART_SQL_AFTER_DB_SESSION_ROLLBACK)]
public void AfterDbSessionRollback([Object]DbSessionRollbackAfterEventData eventData)
{
var context = _localSegmentContextAccessor.Context;
if (context != null)
{
_tracingContext.Release(context);
}
}
[DiagnosticName(SmartSqlDiagnosticListenerExtensions.SMART_SQL_ERROR_DB_SESSION_ROLLBACK)]
public void ErrorDbSessionRollback([Object]DbSessionRollbackErrorEventData eventData)
{
var context = _localSegmentContextAccessor.Context;
if (context != null)
{
context.Span.ErrorOccurred(eventData.Exception);
_tracingContext.Release(context);
}
}
#endregion
#region Dispose
[DiagnosticName(SmartSqlDiagnosticListenerExtensions.SMART_SQL_BEFORE_DB_SESSION_DISPOSE)]
public void BeforeDbSessionDispose([Object]DbSessionDisposeBeforeEventData eventData)
{
var context = _tracingContext.CreateLocalSegmentContext(eventData.Operation);
context.Span.SpanLayer = Tracing.Segments.SpanLayer.DB;
context.Span.Component = Common.Components.SMART_SQL;
context.Span.Peer = new Common.StringOrIntValue(eventData.DbSession.Connection?.DataSource);
context.Span.AddTag(Common.Tags.DB_INSTANCE, eventData.DbSession.Connection?.Database);
context.Span.AddTag(Common.Tags.DB_TYPE, "Sql");
}
[DiagnosticName(SmartSqlDiagnosticListenerExtensions.SMART_SQL_AFTER_DB_SESSION_DISPOSE)]
public void AfterDbSessionDispose([Object]DbSessionDisposeAfterEventData eventData)
{
var context = _localSegmentContextAccessor.Context;
if (context != null)
{
_tracingContext.Release(context);
}
}
[DiagnosticName(SmartSqlDiagnosticListenerExtensions.SMART_SQL_ERROR_DB_SESSION_DISPOSE)]
public void ErrorDbSessionDispose([Object]DbSessionDisposeErrorEventData eventData)
{
var context = _localSegmentContextAccessor.Context;
if (context != null)
{
context.Span.ErrorOccurred(eventData.Exception);
_tracingContext.Release(context);
}
}
#endregion
#region Open
[DiagnosticName(SmartSqlDiagnosticListenerExtensions.SMART_SQL_BEFORE_DB_SESSION_OPEN)]
public void BeforeDbSessionOpen([Object]DbSessionOpenBeforeEventData eventData)
{
var context = _tracingContext.CreateLocalSegmentContext(eventData.Operation);
context.Span.SpanLayer = Tracing.Segments.SpanLayer.DB;
context.Span.Component = Common.Components.SMART_SQL;
context.Span.AddTag(Common.Tags.DB_TYPE, "Sql");
}
[DiagnosticName(SmartSqlDiagnosticListenerExtensions.SMART_SQL_AFTER_DB_SESSION_OPEN)]
public void AfterDbSessionOpen([Object]DbSessionOpenAfterEventData eventData)
{
var context = _localSegmentContextAccessor.Context;
if (context != null)
{
context.Span.Peer = new Common.StringOrIntValue(eventData.DbSession.Connection?.DataSource);
context.Span.AddTag(Common.Tags.DB_INSTANCE, eventData.DbSession.Connection?.Database);
_tracingContext.Release(context);
}
}
[DiagnosticName(SmartSqlDiagnosticListenerExtensions.SMART_SQL_ERROR_DB_SESSION_OPEN)]
public void ErrorDbSessionOpen([Object]DbSessionOpenErrorEventData eventData)
{
var context = _localSegmentContextAccessor.Context;
if (context != null)
{
context.Span.ErrorOccurred(eventData.Exception);
_tracingContext.Release(context);
}
}
#endregion
#region Invoke
private static string ResolveOperationName(ExecutionContext executionContext)
{
return executionContext.Request.FullSqlId != "." ?
executionContext.Request.FullSqlId : executionContext.Request.ExecutionType.ToString();
}
[DiagnosticName(SmartSqlDiagnosticListenerExtensions.SMART_SQL_BEFORE_DB_SESSION_INVOKE)]
public void BeforeDbSessionInvoke([Object]DbSessionInvokeBeforeEventData eventData)
{
var context = _tracingContext.CreateLocalSegmentContext(ResolveOperationName(eventData.ExecutionContext));
context.Span.SpanLayer = Tracing.Segments.SpanLayer.DB;
context.Span.Component = Common.Components.SMART_SQL;
context.Span.AddTag(Common.Tags.DB_TYPE, "Sql");
}
[DiagnosticName(SmartSqlDiagnosticListenerExtensions.SMART_SQL_AFTER_DB_SESSION_INVOKE)]
public void AfterDbSessionInvoke([Object]DbSessionInvokeAfterEventData eventData)
{
var context = _localSegmentContextAccessor.Context;
if (context != null)
{
context.Span.AddTag("from_cache", eventData.ExecutionContext.Result.FromCache);
context.Span.Peer = new Common.StringOrIntValue(eventData.DbSession.Connection?.DataSource);
context.Span.AddTag(Common.Tags.DB_INSTANCE, eventData.DbSession.Connection?.Database);
context.Span.AddTag(Common.Tags.DB_STATEMENT, eventData.ExecutionContext.Request.RealSql);
_tracingContext.Release(context);
}
}
[DiagnosticName(SmartSqlDiagnosticListenerExtensions.SMART_SQL_ERROR_DB_SESSION_INVOKE)]
public void ErrorDbSessionInvoke([Object]DbSessionInvokeErrorEventData eventData)
{
var context = _localSegmentContextAccessor.Context;
if (context != null)
{
context.Span.ErrorOccurred(eventData.Exception);
_tracingContext.Release(context);
}
}
#endregion
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册