diff --git a/sample/SkyApm.Sample.Backend/SkyApm.Sample.Backend.csproj b/sample/SkyApm.Sample.Backend/SkyApm.Sample.Backend.csproj index dc149f709739537c1858b082d1ba97157771b06e..9cd5cc412481e28f851da16bd7dfeed6293ff21f 100644 --- a/sample/SkyApm.Sample.Backend/SkyApm.Sample.Backend.csproj +++ b/sample/SkyApm.Sample.Backend/SkyApm.Sample.Backend.csproj @@ -6,7 +6,6 @@ - @@ -16,7 +15,6 @@ 2.1.0 - diff --git a/sample/SkyApm.Sample.Backend/Startup.cs b/sample/SkyApm.Sample.Backend/Startup.cs index dda4d9022079d43812139be1f2bfd00da0bce2c7..c2530c5f54d81eb1d946df0021ceef19b1fe1570 100644 --- a/sample/SkyApm.Sample.Backend/Startup.cs +++ b/sample/SkyApm.Sample.Backend/Startup.cs @@ -1,4 +1,6 @@ -using Microsoft.AspNetCore.Builder; +using System; +using System.Globalization; +using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; @@ -8,8 +10,6 @@ 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 { @@ -26,21 +26,13 @@ namespace SkyApm.Sample.Backend public void ConfigureServices(IServiceCollection services) { services.AddMvc(); - var sqliteConnection = new SqliteConnection("DataSource=:memory:"); sqliteConnection.Open(); services.AddEntityFrameworkSqlite().AddDbContext(c => c.UseSqlite(sqliteConnection)); services.AddSingleton(); - services.AddSmartSql(sp => - { - return SmartSqlBuilder - .AddDataSource(DbProvider.SQLSERVER, "Data Source=.;Initial Catalog=SmartSqlTestDB;Integrated Security=True") - .UseLoggerFactory(sp.GetService()) - .UseCache(false) - .Build(); - }); + } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. diff --git a/sample/SkyApm.Sample.Backend/Controllers/SmartSqlController.cs b/sample/SkyApm.Sample.SmartSql/Controllers/SmartSqlController.cs similarity index 87% rename from sample/SkyApm.Sample.Backend/Controllers/SmartSqlController.cs rename to sample/SkyApm.Sample.SmartSql/Controllers/SmartSqlController.cs index d41b48ea2b7fc97f7a0366415eebaf2259c39c34..b7b1ba1065f2729cbf633a1ec174c8eb3b218c7f 100644 --- a/sample/SkyApm.Sample.Backend/Controllers/SmartSqlController.cs +++ b/sample/SkyApm.Sample.SmartSql/Controllers/SmartSqlController.cs @@ -6,7 +6,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using SmartSql; -namespace SkyApm.Sample.Backend.Controllers +namespace SkyApm.Sample.SmartSql.Controllers { [Route("[controller]/[action]")] [ApiController] @@ -31,7 +31,7 @@ namespace SkyApm.Sample.Backend.Controllers { return await _sqlMapper.QueryAsync(new RequestContext { - RealSql = "SELECT Top (1000) T.* From T_AllPrimitive T With(NoLock)" + RealSql = "SELECT T.* From T_User T Limit 100" }); } [HttpGet] @@ -39,9 +39,10 @@ namespace SkyApm.Sample.Backend.Controllers { return _sqlMapper.Query(new RequestContext { - RealSql = "SELECT Top (1000) T.* From T_AllPrimitive T With(NoLock)" + RealSql = "SELECT T.* From T_User T Limit 100" }); } + [HttpGet] public IEnumerable Transaction() { @@ -51,7 +52,7 @@ namespace SkyApm.Sample.Backend.Controllers var list = _sqlMapper.Query(new RequestContext { - RealSql = "SELECT Top (1000) T.* From T_AllPrimitive T With(NoLock)" + RealSql = "SELECT T.* From T_User T Limit 100" }); _sqlMapper.CommitTransaction(); return list; diff --git a/sample/SkyApm.Sample.SmartSql/Program.cs b/sample/SkyApm.Sample.SmartSql/Program.cs new file mode 100644 index 0000000000000000000000000000000000000000..07ec393cc83721052ca8849c3fd98ff795ee0730 --- /dev/null +++ b/sample/SkyApm.Sample.SmartSql/Program.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; + +namespace SkyApm.Sample.SmartSql +{ + public class Program + { + public static void Main(string[] args) + { + CreateWebHostBuilder(args).Build().Run(); + } + + public static IWebHostBuilder CreateWebHostBuilder(string[] args) => + WebHost.CreateDefaultBuilder(args) + .UseStartup(); + } +} diff --git a/sample/SkyApm.Sample.SmartSql/Properties/launchSettings.json b/sample/SkyApm.Sample.SmartSql/Properties/launchSettings.json new file mode 100644 index 0000000000000000000000000000000000000000..9c555fe404b085c967f526285fe03655a0d7c01f --- /dev/null +++ b/sample/SkyApm.Sample.SmartSql/Properties/launchSettings.json @@ -0,0 +1,15 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "profiles": { + "SkyApm.Sample.SmartSql": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "api/values", + "applicationUrl": "http://localhost:5003", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "SkyAPM.Agent.AspNetCore" + } + } + } +} \ No newline at end of file diff --git a/sample/SkyApm.Sample.SmartSql/SkyApm.Sample.SmartSql.csproj b/sample/SkyApm.Sample.SmartSql/SkyApm.Sample.SmartSql.csproj new file mode 100644 index 0000000000000000000000000000000000000000..894f3a9cc98192b8c9a97332d91964de3fadca5d --- /dev/null +++ b/sample/SkyApm.Sample.SmartSql/SkyApm.Sample.SmartSql.csproj @@ -0,0 +1,27 @@ + + + + netcoreapp2.2 + + + + + + + + + + + + + + Always + + + + + Always + + + + diff --git a/sample/SkyApm.Sample.SmartSql/SmartSqlTestDB.db b/sample/SkyApm.Sample.SmartSql/SmartSqlTestDB.db new file mode 100644 index 0000000000000000000000000000000000000000..e93abfda25cb492bef4dfd118041a0ff507fbf2f Binary files /dev/null and b/sample/SkyApm.Sample.SmartSql/SmartSqlTestDB.db differ diff --git a/sample/SkyApm.Sample.SmartSql/Startup.cs b/sample/SkyApm.Sample.SmartSql/Startup.cs new file mode 100644 index 0000000000000000000000000000000000000000..9de749fa44523d0693223645c31da8b90181d4e8 --- /dev/null +++ b/sample/SkyApm.Sample.SmartSql/Startup.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using SmartSql; +using SmartSql.DataSource; + +namespace SkyApm.Sample.SmartSql +{ + public class Startup + { + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } + + public IConfiguration Configuration { get; } + + // This method gets called by the runtime. Use this method to add services to the container. + public void ConfigureServices(IServiceCollection services) + { + services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); + services.AddSmartSql(sp => new SmartSqlBuilder() + .UseLoggerFactory(sp.GetService()) + .UseDataSource(DbProvider.SQLITE, "Data Source=SmartSqlTestDB.db;Version=3;") + .UseCache(false) + .Build()); + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IHostingEnvironment env) + { + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + + app.UseMvc(); + } + } +} diff --git a/sample/SkyApm.Sample.SmartSql/appsettings.Development.json b/sample/SkyApm.Sample.SmartSql/appsettings.Development.json new file mode 100644 index 0000000000000000000000000000000000000000..e203e9407e74a6b9662aab8fde5d73ae64665f18 --- /dev/null +++ b/sample/SkyApm.Sample.SmartSql/appsettings.Development.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Debug", + "System": "Information", + "Microsoft": "Information" + } + } +} diff --git a/sample/SkyApm.Sample.SmartSql/appsettings.json b/sample/SkyApm.Sample.SmartSql/appsettings.json new file mode 100644 index 0000000000000000000000000000000000000000..def9159a7d9403c04a926f64e71ef3ee7c9e4c57 --- /dev/null +++ b/sample/SkyApm.Sample.SmartSql/appsettings.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/sample/SkyApm.Sample.SmartSql/skyapm.json b/sample/SkyApm.Sample.SmartSql/skyapm.json new file mode 100644 index 0000000000000000000000000000000000000000..39838d554ef47933ee752cd17e846bde40c17746 --- /dev/null +++ b/sample/SkyApm.Sample.SmartSql/skyapm.json @@ -0,0 +1,29 @@ +{ + "SkyWalking": { + "ServiceName": "asp-net-core-SmartSql", + "Namespace": "", + "HeaderVersions": [ + "sw6" + ], + "Sampling": { + "SamplePer3Secs": -1, + "Percentage": -1.0 + }, + "Logging": { + "Level": "Information", + "FilePath": "logs/skyapm-{Date}.log" + }, + "Transport": { + "Interval": 3000, + "ProtocolVersion": "v6", + "QueueSize": 30000, + "BatchSize": 3000, + "gRPC": { + "Servers": "localhost:11800", + "Timeout": 100000, + "ConnectTimeout": 100000, + "ReportTimeout": 600000 + } + } + } +} \ No newline at end of file diff --git a/skyapm-dotnet.sln b/skyapm-dotnet.sln index 3059e590fe2637070762a967e75ae253075a6317..a0ed237635ee4d4a73fe4d60d5be044a1ec812c0 100644 --- a/skyapm-dotnet.sln +++ b/skyapm-dotnet.sln @@ -82,6 +82,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkyApm.DotNet.CLI", "src\Sk EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkyApm.Diagnostics.SmartSql", "src\SkyApm.Diagnostics.SmartSql\SkyApm.Diagnostics.SmartSql.csproj", "{B43160C8-C9E3-43F6-83E9-A9A45B32F022}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SkyApm.Sample.SmartSql", "sample\SkyApm.Sample.SmartSql\SkyApm.Sample.SmartSql.csproj", "{8E2B336B-8138-4179-A754-A1D0C0471654}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -176,6 +178,10 @@ Global {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 + {8E2B336B-8138-4179-A754-A1D0C0471654}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8E2B336B-8138-4179-A754-A1D0C0471654}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8E2B336B-8138-4179-A754-A1D0C0471654}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8E2B336B-8138-4179-A754-A1D0C0471654}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -211,6 +217,7 @@ Global {2F7EEC69-35F4-4D31-B52A-7465D65E9DE0} = {A4E67E09-3156-4D30-B451-F24F706E96C4} {E909EEF5-5EBF-43F3-AB4A-6B2FB56EF89B} = {1B0865AF-369E-493C-AA6F-C56D3E520A22} {B43160C8-C9E3-43F6-83E9-A9A45B32F022} = {B5E677CF-2920-4B0A-A056-E73F6B2CF2BC} + {8E2B336B-8138-4179-A754-A1D0C0471654} = {844CEACD-4C85-4B15-9E2B-892B01FDA4BB} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {94C0DA2C-CCCB-4314-93A2-9809B5DD0583} diff --git a/src/SkyApm.Diagnostics.SmartSql/SkyApm.Diagnostics.SmartSql.csproj b/src/SkyApm.Diagnostics.SmartSql/SkyApm.Diagnostics.SmartSql.csproj index 00fa7055d48d9751c15bc7fa2c954d6248c49b09..a92db88874721be21f6ff9f977e7dab1e90b2022 100644 --- a/src/SkyApm.Diagnostics.SmartSql/SkyApm.Diagnostics.SmartSql.csproj +++ b/src/SkyApm.Diagnostics.SmartSql/SkyApm.Diagnostics.SmartSql.csproj @@ -12,7 +12,7 @@ - + diff --git a/src/SkyApm.Diagnostics.SmartSql/SmartSqlTracingDiagnosticProcessor.cs b/src/SkyApm.Diagnostics.SmartSql/SmartSqlTracingDiagnosticProcessor.cs index 6d46361fd91ed720da714fa3cfa5ef7e322708d1..ddaaa9805c17437be1b19703ffd136837aaf5b68 100644 --- a/src/SkyApm.Diagnostics.SmartSql/SmartSqlTracingDiagnosticProcessor.cs +++ b/src/SkyApm.Diagnostics.SmartSql/SmartSqlTracingDiagnosticProcessor.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Text; using SkyApm.Tracing; @@ -196,9 +197,10 @@ namespace SkyApm.Diagnostics.SmartSql 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); + var resultSize = eventData.ExecutionContext.Result.IsList + ? (eventData.ExecutionContext.Result.GetData() as ICollection)?.Count + : 1; + context.Span.AddTag("result_size", resultSize?.ToString()); _tracingContext.Release(context); } } @@ -213,5 +215,40 @@ namespace SkyApm.Diagnostics.SmartSql } } #endregion + + #region CommandExecuter + + [DiagnosticName(SmartSqlDiagnosticListenerExtensions.SMART_SQL_BEFORE_COMMAND_EXECUTER_EXECUTE)] + public void BeforeCommandExecuterExecute([Object]CommandExecuterExecuteBeforeEventData 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_COMMAND_EXECUTER_EXECUTE)] + public void AfterCommandExecuterExecute([Object]CommandExecuterExecuteAfterEventData eventData) + { + var context = _localSegmentContextAccessor.Context; + if (context != null) + { + context.Span.Peer = new Common.StringOrIntValue(eventData.ExecutionContext.DbSession.Connection?.DataSource); + context.Span.AddTag(Common.Tags.DB_INSTANCE, eventData.ExecutionContext.DbSession.Connection?.Database); + context.Span.AddTag(Common.Tags.DB_STATEMENT, eventData.ExecutionContext.Request.RealSql); + _tracingContext.Release(context); + } + } + [DiagnosticName(SmartSqlDiagnosticListenerExtensions.SMART_SQL_ERROR_COMMAND_EXECUTER_EXECUTE)] + public void ErrorCommandExecuterExecute([Object]CommandExecuterExecuteErrorEventData eventData) + { + var context = _localSegmentContextAccessor.Context; + if (context != null) + { + context.Span.ErrorOccurred(eventData.Exception); + _tracingContext.Release(context); + } + } + + #endregion } }