private object OnBegin(object thisObj, bool isAsyncCall)
{
try
{
if (thisObj == null)
{
DependencyCollectorEventSource.Log.NotExpectedCallback(0, "OnBeginHttp", "thisObj == null");
return null;
}
WebRequest webRequest = thisObj as WebRequest;
if (webRequest == null)
{
DependencyCollectorEventSource.Log.UnexpectedCallbackParameter("WebRequest");
}
var url = this.GetUrl(webRequest);
if (url == null)
{
DependencyCollectorEventSource.Log.NotExpectedCallback(thisObj.GetHashCode(), "OnBeginHttp", "resourceName is empty");
return null;
}
string httMethod = webRequest.Method;
string resourceName = url.AbsolutePath;
if (!string.IsNullOrEmpty(httMethod))
{
resourceName = httMethod + " " + resourceName;
}
DependencyCollectorEventSource.Log.BeginCallbackCalled(thisObj.GetHashCode(), resourceName);
if (this.applicationInsightsUrlFilter.IsApplicationInsightsUrl(url.ToString()))
{
// Not logging as we will be logging for all outbound AI calls
return null;
}
// If the object already exists, don't add again. This happens because either GetResponse or GetRequestStream could
// be the starting point for the outbound call.
var telemetryTuple = this.TelemetryTable.Get(thisObj);
if (telemetryTuple != null)
{
if (telemetryTuple.Item1 != null)
{
DependencyCollectorEventSource.Log.TrackingAnExistingTelemetryItemVerbose();
return null;
}
}
bool isCustomCreated = false;
var telemetry = ClientServerDependencyTracker.BeginTracking(this.telemetryClient);
telemetry.Name = resourceName;
telemetry.Target = url.Host;
telemetry.Type = RemoteDependencyConstants.HTTP;
telemetry.Data = url.OriginalString;
this.TelemetryTable.Store(thisObj, new Tuple<DependencyTelemetry, bool>(telemetry, isCustomCreated));
if (string.IsNullOrEmpty(telemetry.Context.InstrumentationKey))
{
// Instrumentation key is probably empty, because the context has not yet had a chance to associate the requestTelemetry to the telemetry client yet.
// and get they instrumentation key from all possible sources in the process. Let's do that now.
this.telemetryClient.Initialize(telemetry);
}
// Add the source instrumentation key header if collection is enabled, the request host is not in the excluded list and the same header doesn't already exist
if (this.setCorrelationHeaders
&& !this.correlationDomainExclusionList.Contains(url.Host)
&& !string.IsNullOrEmpty(telemetry.Context.InstrumentationKey)
&& webRequest.Headers[RequestResponseHeaders.SourceInstrumentationKeyHeader] == null)
{
webRequest.Headers.Add(RequestResponseHeaders.SourceInstrumentationKeyHeader, InstrumentationKeyHashLookupHelper.GetInstrumentationKeyHash(telemetry.Context.InstrumentationKey));
}
// Add the root ID
var rootId = telemetry.Context.Operation.Id;
if (!string.IsNullOrEmpty(rootId) && webRequest.Headers[RequestResponseHeaders.StandardRootIdHeader] == null)
{
webRequest.Headers.Add(RequestResponseHeaders.StandardRootIdHeader, rootId);
}
// Add the parent ID
var parentId = telemetry.Id;
if (!string.IsNullOrEmpty(parentId) && webRequest.Headers[RequestResponseHeaders.StandardParentIdHeader] == null)
{
webRequest.Headers.Add(RequestResponseHeaders.StandardParentIdHeader, parentId);
}
}
catch (Exception exception)
{
DependencyCollectorEventSource.Log.CallbackError(thisObj == null ? 0 : thisObj.GetHashCode(), "OnBeginHttp", exception);
}
return null;
}