public Activity StartActivity(Activity activity, object?args) { activity.Start(); Write(activity.OperationName + ".Start", args); return(activity); }
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(); } }