Skip to content

Commit

Permalink
ADO.NET integration (#248)
Browse files Browse the repository at this point in the history
* add sample app for Postgres client (Npgsql)

* disable inlining in profiler

* remove manual build dependencies between sample apps the native profiler

* replace SQL Server integration with generic ADO.NET integration

* use explicit types where possible instead of `object`

* add debug logs, but keep commented in repo until we add debug mode

* also look for "Host" in the connection string

* fix compiler error

* remove commented code

* uncomment INSERT and DELETE queries

* remove target signatures
  • Loading branch information
lucaspimentel authored Jan 17, 2019
1 parent 8c15495 commit 426bfca
Show file tree
Hide file tree
Showing 11 changed files with 337 additions and 156 deletions.
29 changes: 17 additions & 12 deletions Datadog.Trace.sln
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2027
# Visual Studio Version 16
VisualStudioVersion = 16.0.28407.52
MinimumVisualStudioVersion = 15.0.26124.0
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Datadog.Trace", "src\Datadog.Trace\Datadog.Trace.csproj", "{5DFDF781-F24C-45B1-82EF-9125875A80A4}"
EndProject
Expand Down Expand Up @@ -62,7 +62,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Samples.AspNetMvc5", "sampl
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Samples.AspNetCoreMvc2", "samples\Samples.AspNetCoreMvc2\Samples.AspNetCoreMvc2.csproj", "{9951A03A-AD1B-48D8-A96D-B85532BEDADD}"
ProjectSection(ProjectDependencies) = postProject
{C0C8D381-D6B9-4C76-9428-F40F2FA93A9A} = {C0C8D381-D6B9-4C76-9428-F40F2FA93A9A}
{85F35AAF-D102-4960-8B41-3BD9CBD0E77F} = {85F35AAF-D102-4960-8B41-3BD9CBD0E77F}
EndProjectSection
EndProject
Expand All @@ -86,14 +85,8 @@ EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Samples.ExampleLibraryTracer", "samples\Samples.ExampleLibraryTracer\Samples.ExampleLibraryTracer.csproj", "{4B243CF1-4269-45C6-A238-1A9BFA58B8CC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Samples.ConsoleFramework", "samples\Samples.ConsoleFramework\Samples.ConsoleFramework.csproj", "{F40F73F0-95CA-43B5-9881-DC58F0AE8BC3}"
ProjectSection(ProjectDependencies) = postProject
{C0C8D381-D6B9-4C76-9428-F40F2FA93A9A} = {C0C8D381-D6B9-4C76-9428-F40F2FA93A9A}
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Samples.ConsoleCore", "samples\Samples.ConsoleCore\Samples.ConsoleCore.csproj", "{BC7B3216-9C37-4570-8A4A-5D716AB98E9C}"
ProjectSection(ProjectDependencies) = postProject
{C0C8D381-D6B9-4C76-9428-F40F2FA93A9A} = {C0C8D381-D6B9-4C76-9428-F40F2FA93A9A}
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Datadog.Trace.ClrProfiler.IntegrationTests", "test\Datadog.Trace.ClrProfiler.IntegrationTests\Datadog.Trace.ClrProfiler.IntegrationTests.csproj", "{0D546118-B70A-44D0-B675-39EDB99FCEEE}"
ProjectSection(ProjectDependencies) = postProject
Expand All @@ -106,9 +99,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Datadog.Trace.ClrProfiler.I
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Samples.SqlServer", "samples\Samples.SqlServer\Samples.SqlServer.csproj", "{086FF8A0-9CEE-470A-9751-78B0F1340649}"
ProjectSection(ProjectDependencies) = postProject
{C0C8D381-D6B9-4C76-9428-F40F2FA93A9A} = {C0C8D381-D6B9-4C76-9428-F40F2FA93A9A}
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Samples.RedisCore", "samples\Samples.RedisCore\Samples.RedisCore.csproj", "{F5B27CC4-1DF6-4ECD-A4FD-8200152F9A5A}"
EndProject
Expand Down Expand Up @@ -137,6 +127,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Datadog.Trace.ClrProfiler.M
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Samples.MongoDB", "samples\Samples.MongoDB\Samples.MongoDB.csproj", "{3493346B-44F6-4F50-8FB4-51D0090DF544}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Samples.Npgsql", "samples\Samples.Npgsql\Samples.Npgsql.csproj", "{DD3E8ED8-A0E4-482E-A5ED-115E21D543C0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -447,6 +439,18 @@ Global
{3493346B-44F6-4F50-8FB4-51D0090DF544}.Release|x64.Build.0 = Release|Any CPU
{3493346B-44F6-4F50-8FB4-51D0090DF544}.Release|x86.ActiveCfg = Release|Any CPU
{3493346B-44F6-4F50-8FB4-51D0090DF544}.Release|x86.Build.0 = Release|Any CPU
{DD3E8ED8-A0E4-482E-A5ED-115E21D543C0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DD3E8ED8-A0E4-482E-A5ED-115E21D543C0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DD3E8ED8-A0E4-482E-A5ED-115E21D543C0}.Debug|x64.ActiveCfg = Debug|x64
{DD3E8ED8-A0E4-482E-A5ED-115E21D543C0}.Debug|x64.Build.0 = Debug|x64
{DD3E8ED8-A0E4-482E-A5ED-115E21D543C0}.Debug|x86.ActiveCfg = Debug|x86
{DD3E8ED8-A0E4-482E-A5ED-115E21D543C0}.Debug|x86.Build.0 = Debug|x86
{DD3E8ED8-A0E4-482E-A5ED-115E21D543C0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DD3E8ED8-A0E4-482E-A5ED-115E21D543C0}.Release|Any CPU.Build.0 = Release|Any CPU
{DD3E8ED8-A0E4-482E-A5ED-115E21D543C0}.Release|x64.ActiveCfg = Release|x64
{DD3E8ED8-A0E4-482E-A5ED-115E21D543C0}.Release|x64.Build.0 = Release|x64
{DD3E8ED8-A0E4-482E-A5ED-115E21D543C0}.Release|x86.ActiveCfg = Release|x86
{DD3E8ED8-A0E4-482E-A5ED-115E21D543C0}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -478,6 +482,7 @@ Global
{5B844A49-CA3C-42CB-AD31-E44CD73FD1A3} = {5D8E1F81-B820-4736-B797-271B0FE787EE}
{5B52C0C0-A554-4E53-9D17-B121E78FF919} = {8CEC2042-F11C-49F5-A674-2355793B600A}
{3493346B-44F6-4F50-8FB4-51D0090DF544} = {AA6F5582-3B71-49AC-AA39-8F7815AC46BE}
{DD3E8ED8-A0E4-482E-A5ED-115E21D543C0} = {AA6F5582-3B71-49AC-AA39-8F7815AC46BE}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {160A1D00-1F5B-40F8-A155-621B4459D78F}
Expand Down
99 changes: 61 additions & 38 deletions integrations.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,65 @@
[
{
"name": "AdoNet",
"method_replacements": [
{
"caller": {},
"target": {
"assembly": "System.Data",
"type": "System.Data.Common.DbCommand",
"method": "ExecuteDbDataReader"
},
"wrapper": {
"assembly": "Datadog.Trace.ClrProfiler.Managed, Version=0.7.0.0, Culture=neutral, PublicKeyToken=def86d061d0d2eeb",
"type": "Datadog.Trace.ClrProfiler.Integrations.AdoNetIntegration",
"method": "ExecuteDbDataReader",
"signature": "00 02 1C 1C 08"
}
},
{
"caller": {},
"target": {
"assembly": "System.Data.Common",
"type": "System.Data.Common.DbCommand",
"method": "ExecuteDbDataReader"
},
"wrapper": {
"assembly": "Datadog.Trace.ClrProfiler.Managed, Version=0.7.0.0, Culture=neutral, PublicKeyToken=def86d061d0d2eeb",
"type": "Datadog.Trace.ClrProfiler.Integrations.AdoNetIntegration",
"method": "ExecuteDbDataReader",
"signature": "00 02 1C 1C 08"
}
},
{
"caller": {},
"target": {
"assembly": "System.Data",
"type": "System.Data.Common.DbCommand",
"method": "ExecuteDbDataReaderAsync"
},
"wrapper": {
"assembly": "Datadog.Trace.ClrProfiler.Managed, Version=0.7.0.0, Culture=neutral, PublicKeyToken=def86d061d0d2eeb",
"type": "Datadog.Trace.ClrProfiler.Integrations.AdoNetIntegration",
"method": "ExecuteDbDataReaderAsync",
"signature": "00 03 1C 1C 08 1C"
}
},
{
"caller": {},
"target": {
"assembly": "System.Data.Common",
"type": "System.Data.Common.DbCommand",
"method": "ExecuteDbDataReaderAsync"
},
"wrapper": {
"assembly": "Datadog.Trace.ClrProfiler.Managed, Version=0.7.0.0, Culture=neutral, PublicKeyToken=def86d061d0d2eeb",
"type": "Datadog.Trace.ClrProfiler.Integrations.AdoNetIntegration",
"method": "ExecuteDbDataReaderAsync",
"signature": "00 03 1C 1C 08 1C"
}
}
]
},
{
"name": "AspNetCoreMvc2",
"method_replacements": [
Expand Down Expand Up @@ -211,44 +272,6 @@
}
]
},
{
"name": "SqlServer",
"method_replacements": [
{
"caller": {
"assembly": "System.Data",
"type": "System.Data.SqlClient.SqlCommand"
},
"target": {
"assembly": "System.Data",
"type": "System.Data.SqlClient.SqlCommand",
"method": "ExecuteReader",
"signature": "20 02 0C 52 08 0B 52 5B 0E"
},
"wrapper": {
"assembly": "Datadog.Trace.ClrProfiler.Managed, Version=0.7.0.0, Culture=neutral, PublicKeyToken=def86d061d0d2eeb",
"type": "Datadog.Trace.ClrProfiler.Integrations.SqlServerIntegration",
"method": "ExecuteReaderWithMethod",
"signature": "00 03 1C 1C 08 0E"
}
},
{
"caller": {},
"target": {
"assembly": "System.Data.SqlClient",
"type": "System.Data.SqlClient.SqlCommand",
"method": "ExecuteReader",
"signature": "20 01 0C 57 0C 0B 5C"
},
"wrapper": {
"assembly": "Datadog.Trace.ClrProfiler.Managed, Version=0.7.0.0, Culture=neutral, PublicKeyToken=def86d061d0d2eeb",
"type": "Datadog.Trace.ClrProfiler.Integrations.SqlServerIntegration",
"method": "ExecuteReader",
"signature": "00 02 1C 1C 08"
}
}
]
},
{
"name": "StackExchangeRedis",
"method_replacements": [
Expand Down
80 changes: 80 additions & 0 deletions samples/Samples.Npgsql/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System;
using System.Data.Common;
using System.Linq;
using Datadog.Trace.ClrProfiler;
using Npgsql;

namespace Samples.Npgsql
{
public static class Program
{
public static void Main(string[] args)
{
Console.WriteLine($"Profiler attached: {Instrumentation.ProfilerAttached}");
Console.WriteLine($"Platform: {(Environment.Is64BitProcess ? "x64" : "x32")}");
Console.WriteLine();

using (var conn = new NpgsqlConnection("Host=localhost;Username=postgres;Password=postgres;Database=postgres"))
{
conn.Open();

// Insert some data
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "INSERT INTO employees (name, birth_date) VALUES (@name, @birth_date);";
cmd.Parameters.AddWithValue("name", "Jane Smith");
cmd.Parameters.AddWithValue("@birth_date", new DateTime(1980, 2, 3));

int count = cmd.ExecuteNonQuery();
Console.WriteLine($"{count} rows inserted.");
}

// Retrieve all rows
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "SELECT * FROM employees sync;";
int rows = 0;

using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
var values = new object[10];
int count = reader.GetValues(values);
Console.WriteLine(string.Join(", ", values.Take(count)));
rows++;
}
}

Console.WriteLine($"{rows:N0} rows returned from sync query.");
Console.WriteLine();

cmd.CommandText = "SELECT * FROM employees async;";
rows = 0;

using (var reader = cmd.ExecuteReaderAsync().GetAwaiter().GetResult())
{
while (reader.Read())
{
var values = new object[10];
int count = reader.GetValues(values);
Console.WriteLine(string.Join(", ", values.Take(count)));
rows++;
}
}

Console.WriteLine($"{rows:N0} rows returned from async query.");
}

// Delete all data
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "DELETE FROM employees;";

int count = cmd.ExecuteNonQuery();
Console.WriteLine($"{count} rows deleted.");
}
}
}
}
}
19 changes: 19 additions & 0 deletions samples/Samples.Npgsql/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"profiles": {
"Samples.Npgsql": {
"commandName": "Project",
"environmentVariables": {
"COR_ENABLE_PROFILING": "1",
"COR_PROFILER": "{846F5F1C-F9AE-4B07-969E-05C26BC060D8}",
"COR_PROFILER_PATH": "%UserProfile%\\source\\repos\\dd-trace-csharp\\src\\Datadog.Trace.ClrProfiler.Native\\bin\\Debug\\x64\\Datadog.Trace.ClrProfiler.Native.dll",

"CORECLR_ENABLE_PROFILING": "1",
"CORECLR_PROFILER": "{846F5F1C-F9AE-4B07-969E-05C26BC060D8}",
"CORECLR_PROFILER_PATH": "%UserProfile%\\source\\repos\\dd-trace-csharp\\src\\Datadog.Trace.ClrProfiler.Native\\bin\\Debug\\x64\\Datadog.Trace.ClrProfiler.Native.dll",

"DD_INTEGRATIONS": "%UserProfile%\\source\\repos\\dd-trace-csharp\\integrations.json"
},
"nativeDebugging": false
}
}
}
26 changes: 26 additions & 0 deletions samples/Samples.Npgsql/Samples.Npgsql.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net461;netcoreapp2.1</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<RuntimeIdentifiers>win-x64;win-x86;linux-x64</RuntimeIdentifiers>
</PropertyGroup>

<PropertyGroup Condition=" '$(OS)' != 'Windows_NT' ">
<TargetFrameworks>netcoreapp2.1</TargetFrameworks>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)' != 'netcoreapp2.1'">
<RuntimeIdentifier>win-$(Platform)</RuntimeIdentifier>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Npgsql" Version="4.0.4" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Datadog.Trace.ClrProfiler.Managed\Datadog.Trace.ClrProfiler.Managed.csproj" />
</ItemGroup>

</Project>
Loading

0 comments on commit 426bfca

Please sign in to comment.