Skip to content

Commit 273a5e5

Browse files
committed
Merge pull request #1267 from elastic/fix/trace-corrolation
Fix/trace corrolation
2 parents c1fefa1 + 2495ede commit 273a5e5

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

src/Elasticsearch.Net/Connection/RequestState/TransportRequestState.cs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,30 @@
44
using System.IO;
55
using System.Linq;
66
using System.Text;
7+
using System.Threading;
78
using Elasticsearch.Net.Connection.Configuration;
89
using Elasticsearch.Net.ConnectionPool;
910
using PurifyNet;
1011

1112
namespace Elasticsearch.Net.Connection.RequestState
1213
{
14+
internal static class TransportRequestState
15+
{
16+
private static long _requestId = 0;
17+
public static long RequestId {
18+
get
19+
{
20+
var r = Interlocked.Increment(ref _requestId);
21+
return r;
22+
}
23+
}
24+
}
25+
1326
public class TransportRequestState<T> : IDisposable, ITransportRequestState
1427
{
1528
private readonly bool _traceEnabled;
1629
private readonly bool _metricsEnabled;
30+
private readonly long _requestId;
1731

1832
private Stopwatch _stopwatch;
1933

@@ -87,6 +101,7 @@ public TransportRequestState(
87101
string method,
88102
string path)
89103
{
104+
this._requestId = TransportRequestState.RequestId;
90105
this.StartedOn = DateTime.UtcNow;
91106
this.SeenNodes = new List<Uri>();
92107
this.SeenExceptions = new List<Exception>();
@@ -95,7 +110,16 @@ public TransportRequestState(
95110
this._traceEnabled = settings.TraceEnabled;
96111
this._metricsEnabled = settings.MetricsEnabled;
97112
if (this._metricsEnabled || this._traceEnabled)
98-
this._stopwatch = Stopwatch.StartNew();
113+
this._stopwatch = Stopwatch.StartNew();
114+
115+
if (this._traceEnabled)
116+
{
117+
Trace.TraceInformation("NEST start:{0} {1} {2}"
118+
, this._requestId
119+
, _result.RequestMethod
120+
, _result.RequestUrl
121+
);
122+
}
99123

100124
this.Method = method;
101125
this.Path = path;
@@ -179,7 +203,8 @@ public void Dispose()
179203

180204
if (_result.Success)
181205
{
182-
Trace.TraceInformation("NEST {0} {1} ({2}):\r\n{3}"
206+
Trace.TraceInformation("NEST end:{0} {1} {2} ({3}):\r\n{4}"
207+
, this._requestId
183208
, _result.RequestMethod
184209
, _result.RequestUrl
185210
, _stopwatch.Elapsed
@@ -189,7 +214,8 @@ public void Dispose()
189214
else
190215
{
191216
Trace.TraceError(
192-
"NEST {0} {1} ({2}):\r\n{3}"
217+
"NEST end:{0} {1} {2} ({3}):\r\n{4}"
218+
, this._requestId
193219
, _result.RequestMethod
194220
, _result.RequestUrl
195221
, _stopwatch.Elapsed

0 commit comments

Comments
 (0)