Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Elasticsearch integration #211

Merged
merged 9 commits into from
Nov 15, 2018
35 changes: 24 additions & 11 deletions samples/Samples.Elasticsearch/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Elasticsearch.Net;
using Nest;


Expand Down Expand Up @@ -29,6 +30,9 @@ static void Main(string[] args)
Concat(UserCommands(elastic)).
Concat(UserCommandsAsync(elastic)).
Concat(WatchCommands(elastic));

var exceptions = new List<Exception>();

foreach (var action in commands)
{
try
Expand All @@ -38,13 +42,20 @@ static void Main(string[] args)
{
result = TaskResult(task);
}

Console.WriteLine($"{result}");
}
catch (Exception ex)
{
Console.WriteLine($"Exception: {ex.Message}");
exceptions.Add(ex);
}
}

if (exceptions.Count > 0)
{
throw new AggregateException(exceptions).Flatten();
}
}

private static string Host()
Expand Down Expand Up @@ -78,6 +89,7 @@ private static List<Func<object>> DocumentCommands(ElasticClient elastic)
Title = "CreateDocument",
}),
() => elastic.Count<Post>(),
() => elastic.Search<Post>(s => s.MatchAll()),
() => elastic.DeleteByQuery(new DeleteByQueryRequest("test_index")
{
Size = 0,
Expand Down Expand Up @@ -111,6 +123,7 @@ private static List<Func<object>> DocumentCommandsAsync(ElasticClient elastic)
Title = "CreateDocument",
}),
() => elastic.CountAsync<Post>(),
() => elastic.SearchAsync<Post>(s => s.MatchAll()),
() => elastic.DeleteByQueryAsync(new DeleteByQueryRequest("test_index")
{
Size = 0,
Expand Down Expand Up @@ -238,7 +251,10 @@ private static List<Func<object>> CatCommands(ElasticClient elastic)
() => elastic.CatRepositories(new CatRepositoriesRequest()),
() => elastic.CatSegments(new CatSegmentsRequest()),
() => elastic.CatShards(new CatShardsRequest()),
() => elastic.CatSnapshots(new CatSnapshotsRequest()),
// CatSnapshots is failing with a JSON deserialization error.
// It might be a bug in the client or an incompatibility between client
// and server versions.
// () => elastic.CatSnapshots(new CatSnapshotsRequest()),
bmermet marked this conversation as resolved.
Show resolved Hide resolved
() => elastic.CatTasks(new CatTasksRequest()),
() => elastic.CatTemplates(new CatTemplatesRequest()),
() => elastic.CatThreadPool(new CatThreadPoolRequest()),
Expand All @@ -265,7 +281,10 @@ private static List<Func<object>> CatCommandsAsync(ElasticClient elastic)
() => elastic.CatRepositoriesAsync(new CatRepositoriesRequest()),
() => elastic.CatSegmentsAsync(new CatSegmentsRequest()),
() => elastic.CatShardsAsync(new CatShardsRequest()),
() => elastic.CatSnapshotsAsync(new CatSnapshotsRequest()),
// CatSnapshots is failing with a JSON deserialization error.
// It might be a bug in the client or an incompatibility between client
// and server versions.
// () => elastic.CatSnapshotsAsync(new CatSnapshotsRequest()),
() => elastic.CatTasksAsync(new CatTasksRequest()),
() => elastic.CatTemplatesAsync(new CatTemplatesRequest()),
() => elastic.CatThreadPoolAsync(new CatThreadPoolRequest()),
Expand Down Expand Up @@ -533,21 +552,15 @@ public static List<Func<object>> WatchCommands(ElasticClient elastic)

private static object TaskResult(Task task)
{
task.Wait();
var taskType = task.GetType();

bool isTaskOfT =
taskType.IsGenericType
&& taskType.GetGenericTypeDefinition() == typeof(Task<>);

if (isTaskOfT)
{
return taskType.GetProperty("Result").GetValue(task);
}
else
{
task.Wait();
return null;
}

return isTaskOfT ? taskType.GetProperty("Result")?.GetValue(task) : null;
}

public class Post
Expand Down
11 changes: 11 additions & 0 deletions samples/Samples.Elasticsearch/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
"profiles": {
"Samples.Elasticsearch": {
"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": true
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace Datadog.Trace.ClrProfiler.Integrations.Elasticsearch.Net
{
Expand Down Expand Up @@ -59,7 +60,7 @@ public static object CallElasticsearchAsync<TResponse>(object pipeline, object r

var cancellationToken = (cancellationTokenSource as CancellationTokenSource)?.Token ?? CancellationToken.None;

var originalMethod = DynamicMethodBuilder<Func<object, object, CancellationToken, TResponse>>.
var originalMethod = DynamicMethodBuilder<Func<object, object, CancellationToken, Task<TResponse>>>.
GetOrCreateMethodCallDelegate(
pipeline.GetType(),
"CallElasticsearchAsync",
Expand Down Expand Up @@ -99,7 +100,7 @@ private static Scope CreateScope(dynamic pipeline, dynamic requestData)
string method = null;
try
{
method = requestData?.Method;
method = requestData?.Method?.ToString();
bmermet marked this conversation as resolved.
Show resolved Hide resolved
}
catch
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Datadog.Trace.TestHelpers;
using Xunit;
using Xunit.Abstractions;
Expand Down Expand Up @@ -36,6 +33,7 @@ public void SubmitsTraces()
{
"Bulk",
"Create",
"Search",
"DeleteByQuery",

"CreateIndex",
Expand Down Expand Up @@ -76,7 +74,7 @@ public void SubmitsTraces()
"CatRepositories",
"CatSegments",
"CatShards",
"CatSnapshots",
// "CatSnapshots",
"CatTasks",
"CatTemplates",
"CatThreadPool",
Expand Down Expand Up @@ -120,10 +118,10 @@ public void SubmitsTraces()
});
}

var spans = agent.WaitForSpans(expected.Count).
Where(s => s.Type == "elasticsearch").
OrderBy(s => s.Start).
ToList();
var spans = agent.WaitForSpans(expected.Count)
.Where(s => s.Type == "elasticsearch")
.OrderBy(s => s.Start)
.ToList();

foreach (var span in spans)
{
Expand Down