Skip to content

Commit

Permalink
Convert AdoNet to MethodBuilder (DataDog#489)
Browse files Browse the repository at this point in the history
* Convert AdoNet to MethodBuilder, more sample async calls

* MySql sample
  • Loading branch information
colin-higgins authored Aug 27, 2019
1 parent 98efd8f commit c5894f5
Show file tree
Hide file tree
Showing 10 changed files with 249 additions and 42 deletions.
13 changes: 13 additions & 0 deletions Datadog.Trace.sln
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".azure-pipelines", ".azure-
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SynchronizeVersions", "tools\SynchronizeVersions\SynchronizeVersions.csproj", "{7F0BD409-FA74-4FB1-853F-D10D888E1542}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Samples.MySql", "samples\Samples.MySql\Samples.MySql.csproj", "{42FA33DD-AEA3-4FF3-8319-F30244A666A4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -744,6 +746,16 @@ Global
{7F0BD409-FA74-4FB1-853F-D10D888E1542}.Release|x64.Build.0 = Release|Any CPU
{7F0BD409-FA74-4FB1-853F-D10D888E1542}.Release|x86.ActiveCfg = Release|Any CPU
{7F0BD409-FA74-4FB1-853F-D10D888E1542}.Release|x86.Build.0 = Release|Any CPU
{42FA33DD-AEA3-4FF3-8319-F30244A666A4}.Debug|Any CPU.ActiveCfg = Debug|x86
{42FA33DD-AEA3-4FF3-8319-F30244A666A4}.Debug|x64.ActiveCfg = Debug|x64
{42FA33DD-AEA3-4FF3-8319-F30244A666A4}.Debug|x64.Build.0 = Debug|x64
{42FA33DD-AEA3-4FF3-8319-F30244A666A4}.Debug|x86.ActiveCfg = Debug|x86
{42FA33DD-AEA3-4FF3-8319-F30244A666A4}.Debug|x86.Build.0 = Debug|x86
{42FA33DD-AEA3-4FF3-8319-F30244A666A4}.Release|Any CPU.ActiveCfg = Release|x86
{42FA33DD-AEA3-4FF3-8319-F30244A666A4}.Release|x64.ActiveCfg = Release|x64
{42FA33DD-AEA3-4FF3-8319-F30244A666A4}.Release|x64.Build.0 = Release|x64
{42FA33DD-AEA3-4FF3-8319-F30244A666A4}.Release|x86.ActiveCfg = Release|x86
{42FA33DD-AEA3-4FF3-8319-F30244A666A4}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -797,6 +809,7 @@ Global
{6ABAD006-E206-488E-ACA1-1AA73F9B5146} = {FEBCE7DC-9FD1-48A6-B911-71ABB240A030}
{C52D6695-4E05-4930-88F8-0EFF8056A967} = {FEBCE7DC-9FD1-48A6-B911-71ABB240A030}
{7F0BD409-FA74-4FB1-853F-D10D888E1542} = {5D8E1F81-B820-4736-B797-271B0FE787EE}
{42FA33DD-AEA3-4FF3-8319-F30244A666A4} = {AA6F5582-3B71-49AC-AA39-8F7815AC46BE}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {160A1D00-1F5B-40F8-A155-621B4459D78F}
Expand Down
24 changes: 24 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ services:
ports:
- "127.0.0.1:5432:5432"

mysql:
image: mysql/mysql-server:5.7
environment:
- MYSQL_DATABASE=world
- MYSQL_ROOT_PASSWORD=mysqldb
- MYSQL_USER=mysqldb
- MYSQL_PASSWORD=mysqldb
ports:
- "127.0.0.1:3306:3306"

sqlserver:
image: microsoft/mssql-server-linux:latest
ports:
Expand Down Expand Up @@ -163,6 +173,19 @@ services:
- POSTGRES_HOST=postgres
depends_on:
- postgres

Samples.MySql:
build:
context: ./
dockerfile: ./docker/dotnet.dockerfile
image: datadog-dotnet
command: bash -c "/project/docker/with-profiler-logs.bash wait-for-it mysql:3306 -- /project/docker/with-profiler.bash dotnet /project/samples/Samples.MySql/bin/Release/netcoreapp2.1/publish/Samples.MySql.dll"
volumes:
- ./:/project
environment:
- MYSQL_HOST=mysql
depends_on:
- mysql

Samples.MongoDB:
build:
Expand Down Expand Up @@ -191,6 +214,7 @@ services:
- ELASTICSEARCH5_HOST=elasticsearch5:9200
- SQLSERVER_CONNECTION_STRING=Server=sqlserver;Database=BlogDatabase;User=sa;Password=Strong!Passw0rd
- POSTGRES_HOST=postgres
- MYSQL_HOST=mysql
depends_on:
- servicestackredis
- stackexchangeredis
Expand Down
83 changes: 83 additions & 0 deletions samples/Samples.MySql/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System;
using Datadog.Trace.ClrProfiler;
using MySql.Data.MySqlClient;

namespace Samples.MySql
{
class Program
{
private static string Host()
{
return Environment.GetEnvironmentVariable("MYSQL_HOST") ?? "localhost";
}

private static string ConnectionString(string database)
{
return $"server={Host()};user=mysqldb;password=mysqldb;port=3306;database={database}";
}

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

Console.WriteLine("Opening the connection.");

string connStr = ConnectionString("world");
var conn = new MySqlConnection(connStr);
conn.Open();

Console.WriteLine("Creating the table for the continents.");

// Create table
var tableCommand =
new MySqlCommand(
"DROP TABLE IF EXISTS `continent`; CREATE TABLE continent (continent_id INT AUTO_INCREMENT, name VARCHAR(255) NOT NULL, PRIMARY KEY(continent_id));",
conn);
tableCommand.ExecuteNonQuery();

Console.WriteLine("Creating the continents.");

// Create continents
MySqlCommand createContinent;
createContinent = new MySqlCommand("INSERT INTO continent (name) VALUES ('Africa');", conn);
createContinent.ExecuteNonQuery();
createContinent = new MySqlCommand("INSERT INTO continent (name) VALUES ('Antarctica');", conn);
createContinent.ExecuteNonQuery();
createContinent = new MySqlCommand("INSERT INTO continent (name) VALUES ('Asia');", conn);
createContinent.ExecuteNonQuery();
createContinent = new MySqlCommand("INSERT INTO continent (name) VALUES ('Australia');", conn);
createContinent.ExecuteNonQuery();
createContinent = new MySqlCommand("INSERT INTO continent (name) VALUES ('Europe');", conn);
createContinent.ExecuteNonQuery();
createContinent = new MySqlCommand("INSERT INTO continent (name) VALUES ('North America');", conn);
createContinent.ExecuteNonQuery();
createContinent = new MySqlCommand("INSERT INTO continent (name) VALUES ('South America');", conn);
createContinent.ExecuteNonQuery();

try
{
Console.WriteLine("Beginning to read the continents.");
string sql = "SELECT continent_id, name FROM continent";
var readContinents = new MySqlCommand(sql, conn);
using (var rdr = readContinents.ExecuteReader())
{
while (rdr.Read())
{
Console.WriteLine(rdr[0] + " -- " + rdr[1]);
}
rdr.Close();
}
Console.WriteLine("Done reading the continents.");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}

conn.Close();
Console.WriteLine("Done setting up, inserting, and reading from MySQL.");
}
}
}
19 changes: 19 additions & 0 deletions samples/Samples.MySql/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"profiles": {
"Samples.MySql": {
"commandName": "Project",
"environmentVariables": {
"COR_ENABLE_PROFILING": "1",
"COR_PROFILER": "{846F5F1C-F9AE-4B07-969E-05C26BC060D8}",
"COR_PROFILER_PATH": "$(ProjectDir)$(OutputPath)profiler-lib\\Datadog.Trace.ClrProfiler.Native.dll",

"CORECLR_ENABLE_PROFILING": "1",
"CORECLR_PROFILER": "{846F5F1C-F9AE-4B07-969E-05C26BC060D8}",
"CORECLR_PROFILER_PATH": "$(ProjectDir)$(OutputPath)profiler-lib\\Datadog.Trace.ClrProfiler.Native.dll",

"DD_INTEGRATIONS": "$(ProjectDir)$(OutputPath)profiler-lib\\integrations.json"
},
"nativeDebugging": true
}
}
}
13 changes: 13 additions & 0 deletions samples/Samples.MySql/Samples.MySql.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<ApiVersion Condition="'$(ApiVersion)' == ''">8.0.17</ApiVersion>
<!-- Required to build multiple projects with the same Configuration|Platform -->
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MySql.Data" Version="$(ApiVersion)" />
</ItemGroup>

</Project>
1 change: 0 additions & 1 deletion samples/Samples.Npgsql/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Data.Common;
using System.Linq;
using Datadog.Trace.ClrProfiler;
using Npgsql;
Expand Down
26 changes: 24 additions & 2 deletions samples/Samples.SqlServer/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using Microsoft.EntityFrameworkCore;

namespace Samples.SqlServer
{
Expand All @@ -22,16 +23,37 @@ static void Main(string[] args)
db.SaveChanges();
}

// Display all Blogs from the database
// Display all Blogs from the database synchronously
var query = from b in db.Blogs
orderby b.Name
select b;

Console.WriteLine("All blogs in the database:");
Console.WriteLine("All blogs in the database from the synchronous call:");
foreach (var item in query)
{
Console.WriteLine(item.Name);
}

var asyncName = "test-async";

var asyncBlog = (from b in db.Blogs where b.Name == asyncName select b).FirstOrDefaultAsync();
if (asyncBlog.Result == null)
{
blog = new Blog { Name = asyncName };
db.Blogs.Add(blog);
db.SaveChangesAsync().Wait();
}

// Display all Blogs from the database asynchronously
var asyncQueryTask = db.Blogs.Where(b => b.Name == asyncName).ToListAsync();

asyncQueryTask.Wait();

Console.WriteLine("All blogs in the database from the async call:");
foreach (var item in asyncQueryTask.Result)
{
Console.WriteLine(item.Name);
}
}
}
}
Expand Down
Loading

0 comments on commit c5894f5

Please sign in to comment.