Commit e328c61a authored by Lemon's avatar Lemon Committed by Savorboard

Support corefx built-in SqlClient (#39)

* Update SkyWalking.Diagnostics.EntityFrameworkCore.csproj

* Add DbType tag for EntityFrameworkCoreDiagnostic

* Support corefx built-in SqlClient

* Fix SqlErrorExecuteCommand

* Add sqlClient support in SkyWalking.AspNetCore
parent 17099445
......@@ -48,6 +48,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SkyWalking.Diagnostics.Enti
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SkyWalking.Diagnostics.EntityFrameworkCore.Tests", "test\SkyWalking.Diagnostics.EntityFrameworkCore.Tests\SkyWalking.Diagnostics.EntityFrameworkCore.Tests.csproj", "{4CF91A8C-25C8-4BD8-A93D-3183AF0FE2E2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SkyWalking.Diagnostics.SqlClient", "src\SkyWalking.Diagnostics.SqlClient\SkyWalking.Diagnostics.SqlClient.csproj", "{44DFFDF7-5935-475A-825F-2C0298464117}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
......@@ -110,6 +112,10 @@ Global
{4CF91A8C-25C8-4BD8-A93D-3183AF0FE2E2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4CF91A8C-25C8-4BD8-A93D-3183AF0FE2E2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4CF91A8C-25C8-4BD8-A93D-3183AF0FE2E2}.Release|Any CPU.Build.0 = Release|Any CPU
{44DFFDF7-5935-475A-825F-2C0298464117}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{44DFFDF7-5935-475A-825F-2C0298464117}.Debug|Any CPU.Build.0 = Debug|Any CPU
{44DFFDF7-5935-475A-825F-2C0298464117}.Release|Any CPU.ActiveCfg = Release|Any CPU
{44DFFDF7-5935-475A-825F-2C0298464117}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
......@@ -131,6 +137,7 @@ Global
{05E533B1-473F-48C0-95F7-B87061BE9834} = {613F0980-91ED-4064-8E8C-168582EF4AD7}
{CD334460-8E61-41EB-9762-62C6A342CACB} = {79ED86A5-E9B9-49B2-9354-C911C079D03E}
{4CF91A8C-25C8-4BD8-A93D-3183AF0FE2E2} = {613F0980-91ED-4064-8E8C-168582EF4AD7}
{44DFFDF7-5935-475A-825F-2C0298464117} = {79ED86A5-E9B9-49B2-9354-C911C079D03E}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {94C0DA2C-CCCB-4314-93A2-9809B5DD0583}
......
......@@ -20,6 +20,7 @@ using System;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SkyWalking.Diagnostics.HttpClient;
using SkyWalking.Diagnostics.SqlClient;
using SkyWalking.Extensions.DependencyInjection;
namespace SkyWalking.AspNetCore
......@@ -52,7 +53,7 @@ namespace SkyWalking.AspNetCore
var builder = new SkyWalkingBuilder(services);
builder.AddHosting().AddDiagnostics().AddHttpClient();
builder.AddHosting().AddDiagnostics().AddHttpClient().AddSqlClient();
return builder;
}
......
......@@ -20,6 +20,7 @@
<ItemGroup>
<ProjectReference Include="..\SkyWalking.Core\SkyWalking.Core.csproj" />
<ProjectReference Include="..\SkyWalking.Diagnostics.HttpClient\SkyWalking.Diagnostics.HttpClient.csproj" />
<ProjectReference Include="..\SkyWalking.Diagnostics.SqlClient\SkyWalking.Diagnostics.SqlClient.csproj" />
<ProjectReference Include="..\SkyWalking.Extensions.DependencyInjection\SkyWalking.Extensions.DependencyInjection.csproj">
<Project>{BF5579ED-113C-4EE6-AE03-9A9CA590C924}</Project>
<Name>SkyWalking.Extensions.DependencyInjection</Name>
......
......@@ -54,6 +54,7 @@ namespace SkyWalking.Diagnostics.EntityFrameworkCore
var span = ContextManager.CreateLocalSpan(operationName);
span.SetComponent(ComponentsDefine.EntityFrameworkCore);
span.SetLayer(SpanLayer.DB);
Tags.DbType.Set(span, "Sql");
Tags.DbInstance.Set(span, eventData.Command.Connection.Database);
Tags.DbStatement.Set(span, eventData.Command.CommandText);
Tags.DbBindVariables.Set(span, BuildParameterVariables(eventData.Command.Parameters));
......
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\build\common.props" />
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
......@@ -7,16 +9,15 @@
<PropertyGroup>
<AssemblyName>SkyWalking.Diagnostics.EntityFrameworkCore</AssemblyName>
<AssemblyTitle>SkyWalking.Diagnostics.EntityFrameworkCore</AssemblyTitle>
<PackageId>SkyWalking.AspNetCore</PackageId>
<PackageId>SkyWalking.Diagnostics.EntityFrameworkCore</PackageId>
<PackageTags>SkyWalking;APM;EntityFrameworkCore;EF</PackageTags>
<Description>SkyWalking.Diagnostics.EntityFrameworkCore notifies of EF requests.</Description>
<RootNamespace>SkyWalking.Diagnostics.HttpClient</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="2.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="2.0.0" />
</ItemGroup>
<ItemGroup>
......
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\build\common.props" />
<PropertyGroup>
<Description>SkyWalking.Diagnostics.SqlClient notifies outgoing SqlClient requests.</Description>
<AssemblyTitle>SkyWalking.Diagnostics.SqlClient</AssemblyTitle>
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyName>SSkyWalking.Diagnostics.SqlClient</AssemblyName>
<PackageId>SkyWalking.Diagnostics.SqlClient</PackageId>
<PackageTags>SkyWalking;APM;Diagnostics;SqlClient</PackageTags>
<PackageReleaseNotes>
</PackageReleaseNotes>
<RootNamespace>SkyWalking.Diagnostics.SqlClient</RootNamespace>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\SkyWalking.Abstractions\SkyWalking.Abstractions.csproj" />
<ProjectReference Include="..\SkyWalking.Core\SkyWalking.Core.csproj" />
<ProjectReference Include="..\SkyWalking.Extensions.DependencyInjection\SkyWalking.Extensions.DependencyInjection.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.Data.SqlClient" Version="4.4.0" />
</ItemGroup>
</Project>
\ No newline at end of file
/*
* Licensed to the OpenSkywalking under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using System;
using Microsoft.Extensions.DependencyInjection;
using SkyWalking.Extensions.DependencyInjection;
namespace SkyWalking.Diagnostics.SqlClient
{
public static class SkyWalkingBuilderExtensions
{
public static SkyWalkingBuilder AddSqlClient(this SkyWalkingBuilder builder)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
builder.Services.AddSingleton<ITracingDiagnosticProcessor, SqlClientDiagnosticProcessor>();
return builder;
}
}
}
\ No newline at end of file
/*
* Licensed to the OpenSkywalking under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using System;
using System.Data.SqlClient;
using System.Linq;
using SkyWalking.Context;
using SkyWalking.Context.Tag;
using SkyWalking.Context.Trace;
using SkyWalking.NetworkProtocol.Trace;
namespace SkyWalking.Diagnostics.SqlClient
{
public class SqlClientDiagnosticProcessor : ITracingDiagnosticProcessor
{
public string ListenerName { get; } = SqlClientDiagnosticStrings.DiagnosticListenerName;
private static string ResolveOperationName(SqlCommand sqlCommand)
{
var commandType = sqlCommand.CommandText?.Split(' ');
return $"{SqlClientDiagnosticStrings.SqlClientPrefix}{commandType?.FirstOrDefault()}";
}
[DiagnosticName(SqlClientDiagnosticStrings.SqlBeforeExecuteCommand)]
public void BeforeExecuteCommand([Property(Name = "Command")] SqlCommand sqlCommand)
{
var peer = sqlCommand.Connection.DataSource;
var span = ContextManager.CreateExitSpan(ResolveOperationName(sqlCommand), peer);
span.SetLayer(SpanLayer.DB);
span.SetComponent(ComponentsDefine.SqlClient);
Tags.DbType.Set(span, "Sql");
Tags.DbInstance.Set(span, sqlCommand.Connection.Database);
Tags.DbStatement.Set(span, sqlCommand.CommandText);
//todo Tags.DbBindVariables
}
[DiagnosticName(SqlClientDiagnosticStrings.SqlAfterExecuteCommand)]
public void AfterExecuteCommand()
{
ContextManager.StopSpan();
}
[DiagnosticName(SqlClientDiagnosticStrings.SqlErrorExecuteCommand)]
public void ErrorExecuteCommand([Property(Name = "Exception")] Exception ex)
{
var span = ContextManager.ActiveSpan;
span?.ErrorOccurred();
span?.Log(ex);
ContextManager.StopSpan(span);
}
}
}
\ No newline at end of file
/*
* Licensed to the OpenSkywalking under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
namespace SkyWalking.Diagnostics.SqlClient
{
internal static class SqlClientDiagnosticStrings
{
public const string DiagnosticListenerName = "SqlClientDiagnosticListener";
public const string SqlClientPrefix = "sqlClient ";
public const string SqlBeforeExecuteCommand = "System.Data.SqlClient.WriteCommandBefore";
public const string SqlAfterExecuteCommand = "System.Data.SqlClient.WriteCommandAfter";
public const string SqlErrorExecuteCommand = "System.Data.SqlClient.WriteCommandError";
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment