public void Start_ActivityOperationRootSpanChecks() { // Create an activity var activity = new System.Diagnostics.Activity("foo") .SetIdFormat(System.Diagnostics.ActivityIdFormat.W3C) .Start(); try { // matching root operation name var tracerMock = GetDefaultTracerMock(); var shim = new SpanBuilderShim(tracerMock.Object, "foo", new List <string> { "foo" }); shim.Start(); tracerMock.Verify(o => o.StartSpanFromActivity("foo", activity, 0, It.Is <IEnumerable <Link> >(links => !links.Any())), Times.Once); // mis-matched root operation name tracerMock = GetDefaultTracerMock(); shim = new SpanBuilderShim(tracerMock.Object, "foo", new List <string> { "bar" }); shim.Start(); tracerMock.Verify(o => o.StartSpan("foo", It.IsAny <ISpan>(), 0, It.Is <SpanCreationOptions>(so => so.StartTimestamp == default && !so.Links.Any())), Times.Once); } finally { activity.Stop(); } }
// [Fact] public async void AddDistributedTracing() { // TraceActivity.DefaultIdFormat = ActivityIdFormat.W3C; var activity = new TraceActivity("CallToTimetable").Start(); try { var url = $"{_host}/api/Timetable/departures/LDS/{_testDate}?to=BHM&fullday=true&includeStops=false&toc=VT&toc=GW&toc=GR&toc=XC"; var client = new HttpClient(); client.DefaultRequestHeaders.Add("Accept", "application/json"); client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate"); client.DefaultRequestHeaders.Add("Origin", _host); var response = await client.GetStringAsync(url); var departures = JsonConvert.DeserializeObject <Model.FoundSummaryResponse>(response); Assert.True(departures.Services.Any()); } finally { activity.Stop(); } }
private void RaiseRequestEvent(HttpWebRequest request) { if (request.Headers.Get(RequestIdHeaderName) != null) { // this request was instrumented by previous RaiseRequestEvent return; } if (this.IsEnabled(ActivityName, request)) { var activity = new Activity(ActivityName); // Only send start event to users who subscribed for it, but start activity anyway if (this.IsEnabled(RequestStartName)) { this.StartActivity(activity, new { Request = request }); } else { activity.Start(); } if (activity.IdFormat == ActivityIdFormat.W3C) { // do not inject header if it was injected already // perhaps tracing systems wants to override it if (request.Headers.Get(TraceParentHeaderName) == null) { request.Headers.Add(TraceParentHeaderName, activity.Id); var traceState = activity.TraceStateString; if (traceState != null) { request.Headers.Add(TraceStateHeaderName, traceState); } } } else { // do not inject header if it was injected already // perhaps tracing systems wants to override it if (request.Headers.Get(RequestIdHeaderName) == null) { request.Headers.Add(RequestIdHeaderName, activity.Id); } } if (request.Headers.Get(CorrelationContextHeaderName) == null) { // we expect baggage to be empty or contain a few items using (IEnumerator <KeyValuePair <string, string> > e = activity.Baggage.GetEnumerator()) { if (e.MoveNext()) { StringBuilder baggage = new StringBuilder(); do { KeyValuePair <string, string> item = e.Current; baggage.Append(WebUtility.UrlEncode(item.Key)).Append('=').Append(WebUtility.UrlEncode(item.Value)).Append(','); }while (e.MoveNext()); baggage.Remove(baggage.Length - 1, 1); request.Headers.Add(CorrelationContextHeaderName, baggage.ToString()); } } } // There is no guarantee that Activity.Current will flow to the Response, so let's stop it here activity.Stop(); } }