Skip to content

Commit 5e5c61c

Browse files
committed
added EnableTrace() on ConnectionSettings, great for debugging with i.e glimpse
1 parent 45366c3 commit 5e5c61c

File tree

5 files changed

+125
-28
lines changed

5 files changed

+125
-28
lines changed

src/Nest/Domain/Connection/Connection.cs

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.IO;
66
using System.Threading.Tasks;
77
using System.Collections.Generic;
8+
using Nest.Domain.Connection;
89

910
namespace Nest
1011
{
@@ -14,6 +15,7 @@ public class Connection : IConnection
1415

1516
private IConnectionSettings _ConnectionSettings { get; set; }
1617
private Semaphore _ResourceLock;
18+
private readonly bool _enableTrace;
1719

1820
public Connection(IConnectionSettings settings)
1921
{
@@ -22,6 +24,7 @@ public Connection(IConnectionSettings settings)
2224

2325
this._ConnectionSettings = settings;
2426
this._ResourceLock = new Semaphore(settings.MaximumAsyncConnections, settings.MaximumAsyncConnections);
27+
this._enableTrace = settings.TraceEnabled;
2528
}
2629

2730
public ConnectionStatus GetSync(string path)
@@ -162,61 +165,81 @@ private HttpWebRequest CreateWebRequest(string path, string method)
162165

163166
protected virtual ConnectionStatus DoSynchronousRequest(HttpWebRequest request, string data = null)
164167
{
165-
var timeout = this._ConnectionSettings.Timeout;
166-
if (data != null)
168+
using (var tracer = new ConnectionStatusTracer(this._ConnectionSettings.TraceEnabled))
167169
{
168-
using (var r = request.GetRequestStream())
170+
ConnectionStatus cs = null;
171+
if (data != null)
169172
{
170-
byte[] buffer = Encoding.UTF8.GetBytes(data);
171-
r.Write(buffer, 0, buffer.Length);
173+
using (var r = request.GetRequestStream())
174+
{
175+
byte[] buffer = Encoding.UTF8.GetBytes(data);
176+
r.Write(buffer, 0, buffer.Length);
177+
}
172178
}
173-
}
174-
try
175-
{
176-
using (var response = (HttpWebResponse)request.GetResponse())
177-
using (var responseStream = response.GetResponseStream())
178-
using (var streamReader = new StreamReader(responseStream))
179+
try
180+
{
181+
using (var response = (HttpWebResponse)request.GetResponse())
182+
using (var responseStream = response.GetResponseStream())
183+
using (var streamReader = new StreamReader(responseStream))
184+
{
185+
string result = streamReader.ReadToEnd();
186+
cs = new ConnectionStatus(result)
187+
{
188+
Request = data,
189+
RequestUrl = request.RequestUri.ToString(),
190+
RequestMethod = request.Method
191+
};
192+
tracer.SetResult(cs);
193+
return cs;
194+
}
195+
}
196+
catch (WebException webException)
179197
{
180-
string result = streamReader.ReadToEnd();
181-
var cs = new ConnectionStatus(result)
198+
cs = new ConnectionStatus(webException)
182199
{
183200
Request = data,
184201
RequestUrl = request.RequestUri.ToString(),
185202
RequestMethod = request.Method
186203
};
204+
tracer.SetResult(cs);
187205
return cs;
188206
}
189207
}
190-
catch (WebException webException)
191-
{
192-
return new ConnectionStatus(webException) { Request = data, RequestUrl = request.RequestUri.ToString(), RequestMethod = request.Method };
193-
}
194208
}
195209

196210
protected virtual Task<ConnectionStatus> DoAsyncRequest(HttpWebRequest request, string data = null)
197211
{
198-
var timeout = this._ConnectionSettings.Timeout;
199-
200212
var tcs = new TaskCompletionSource<ConnectionStatus>();
213+
var timeout = this._ConnectionSettings.Timeout;
201214
if (!this._ResourceLock.WaitOne(timeout))
202215
{
203-
var m = "Could not start the operation before the timeout of " + timeout + "ms completed while waiting for the semaphore";
204-
tcs.SetResult(new ConnectionStatus(new TimeoutException(m)));
205-
return tcs.Task;
216+
using (var tracer = new ConnectionStatusTracer(this._ConnectionSettings.TraceEnabled))
217+
{
218+
var m = "Could not start the operation before the timeout of " + timeout +
219+
"ms completed while waiting for the semaphore";
220+
var cs = new ConnectionStatus(new TimeoutException(m));
221+
tcs.SetResult(cs);
222+
tracer.SetResult(cs);
223+
return tcs.Task;
224+
}
206225
}
207226
try
208227
{
209228
return Task.Factory.StartNew(() =>
210229
{
211-
this.Iterate(this._AsyncSteps(request, tcs, data), tcs);
212-
return tcs.Task.Result;
230+
using (var tracer = new ConnectionStatusTracer(this._ConnectionSettings.TraceEnabled))
231+
{
232+
this.Iterate(this._AsyncSteps(request, tcs, data), tcs);
233+
var cs = tcs.Task.Result;
234+
tracer.SetResult(cs);
235+
return cs;
236+
}
213237
}, TaskCreationOptions.LongRunning);
214238
}
215239
finally
216240
{
217241
this._ResourceLock.Release();
218242
}
219-
220243
}
221244

222245
private IEnumerable<Task> _AsyncSteps(HttpWebRequest request, TaskCompletionSource<ConnectionStatus> tcs, string data = null)
@@ -284,7 +307,7 @@ public void Iterate(IEnumerable<Task> asyncIterator, TaskCompletionSource<Connec
284307
//none of the individual steps in _AsyncSteps run in parallel for 1 request
285308
//as this would be impossible we can assume Aggregate Exception.InnerException
286309
var exception = completedTask.Exception.InnerException;
287-
310+
288311
//cleanly exit from exceptions in stages if the exception is a webexception
289312
if (exception is WebException)
290313
tcs.SetResult(new ConnectionStatus(exception));

src/Nest/Domain/Connection/ConnectionSettings.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public string DefaultIndex
2929

3030
public int MaximumAsyncConnections { get; private set; }
3131
public bool UsesPrettyResponses { get; private set; }
32-
32+
public bool TraceEnabled { get; private set; }
3333
public Func<Type, string> DefaultTypeNameInferrer { get; private set; }
3434
public FluentDictionary<Type, string> DefaultIndices { get; private set; }
3535
public FluentDictionary<Type, string> DefaultTypeNames { get; private set; }
@@ -90,6 +90,15 @@ public ConnectionSettings SetProxy(Uri proxyAdress, string username, string pass
9090
return this;
9191
}
9292

93+
/// <summary>
94+
/// Enable Trace signals to the IConnection that it should put debug information on the Trace.
95+
/// </summary>
96+
public ConnectionSettings EnableTrace(bool enabled = true)
97+
{
98+
this.TraceEnabled = enabled;
99+
return this;
100+
}
101+
93102
/// <summary>
94103
/// Timeout in milliseconds when the .NET webrquest should abort the request, note that you can set this to a high value here,
95104
/// and specify the timeout in various calls on Elasticsearch's side.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Linq;
5+
using System.Text;
6+
7+
namespace Nest.Domain.Connection
8+
{
9+
public class ConnectionStatusTracer : IDisposable
10+
{
11+
private readonly bool _enabled;
12+
private Stopwatch _stopwatch;
13+
14+
public ConnectionStatus _result { get; set; }
15+
16+
public ConnectionStatusTracer(bool enabled)
17+
{
18+
this._enabled = enabled;
19+
if (enabled)
20+
{
21+
this._stopwatch = Stopwatch.StartNew();
22+
}
23+
}
24+
25+
public void SetResult(ConnectionStatus status)
26+
{
27+
if (!_enabled)
28+
return;
29+
this._result = status;
30+
this._stopwatch.Stop();
31+
}
32+
33+
public void Dispose()
34+
{
35+
if (!_enabled || this._result == null)
36+
return;
37+
38+
if (_result.Success)
39+
{
40+
Trace.TraceInformation(
41+
"NEST {0} {1} ({2}):\r\n{3}"
42+
, _result.RequestMethod
43+
, _result.RequestUrl
44+
, _stopwatch.Elapsed.ToString()
45+
, _result.ToString()
46+
);
47+
}
48+
else
49+
{
50+
Trace.TraceError(
51+
"NEST {0} {1} ({2}):\r\n{3}"
52+
, _result.RequestMethod
53+
, _result.RequestUrl
54+
, _stopwatch.Elapsed.ToString()
55+
, _result.ToString()
56+
);
57+
}
58+
}
59+
60+
}
61+
}

src/Nest/Domain/Connection/IConnectionSettings.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ public interface IConnectionSettings
1818
string ProxyUsername { get; }
1919
string ProxyPassword { get; }
2020

21-
int MaximumAsyncConnections { get; }
21+
int MaximumAsyncConnections { get; }
22+
23+
bool TraceEnabled { get; }
24+
2225
bool UsesPrettyResponses { get; }
2326
Uri Uri { get; }
2427

src/Nest/Nest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
<Reference Include="System.ServiceModel" />
6868
</ItemGroup>
6969
<ItemGroup>
70+
<Compile Include="Domain\Connection\ConnectionStatusTracer.cs" />
7071
<Compile Include="Domain\PathAndData.cs" />
7172
<Compile Include="DSL\PercolatorDescriptor.cs" />
7273
<Compile Include="DSL\PercolateDescriptor.cs" />

0 commit comments

Comments
 (0)