Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.NET Framework HttpClient Instrumentation: Fix missing events on instances created before TracerProvider #2364

Merged
merged 2 commits into from
Sep 17, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -453,9 +453,49 @@ private static void PerformInjection()
Hashtable originalTable = servicePointTableField.GetValue(null) as Hashtable;
ServicePointHashtable newTable = new ServicePointHashtable(originalTable ?? new Hashtable());

foreach (DictionaryEntry existingServicePoint in originalTable)
{
HookServicePoint(existingServicePoint.Value);
}

servicePointTableField.SetValue(null, newTable);
}

private static void HookServicePoint(object value)
{
if (value is WeakReference weakRef
&& weakRef.IsAlive
&& weakRef.Target is ServicePoint servicePoint)
{
// Replace the ConnectionGroup hashtable inside this ServicePoint object,
// which allows us to intercept each new ConnectionGroup object added under
// this ServicePoint.
Hashtable originalTable = connectionGroupListField.GetValue(servicePoint) as Hashtable;
ConnectionGroupHashtable newTable = new ConnectionGroupHashtable(originalTable ?? new Hashtable());

foreach (DictionaryEntry existingConnectionGroup in originalTable)
{
HookConnectionGroup(existingConnectionGroup.Value);
}

connectionGroupListField.SetValue(servicePoint, newTable);
}
}

private static void HookConnectionGroup(object value)
{
if (connectionGroupType.IsInstanceOfType(value))
{
// Replace the Connection arraylist inside this ConnectionGroup object,
// which allows us to intercept each new Connection object added under
// this ConnectionGroup.
ArrayList originalArrayList = connectionListField.GetValue(value) as ArrayList;
ConnectionArrayList newArrayList = new ConnectionArrayList(originalArrayList ?? new ArrayList());

connectionListField.SetValue(value, newArrayList);
}
}

private static Func<TClass, TField> CreateFieldGetter<TClass, TField>(string fieldName, BindingFlags flags)
where TClass : class
{
Expand Down Expand Up @@ -675,20 +715,7 @@ public override object this[object key]
get => base[key];
set
{
if (value is WeakReference weakRef && weakRef.IsAlive)
{
if (weakRef.Target is ServicePoint servicePoint)
{
// Replace the ConnectionGroup hashtable inside this ServicePoint object,
// which allows us to intercept each new ConnectionGroup object added under
// this ServicePoint.
Hashtable originalTable = connectionGroupListField.GetValue(servicePoint) as Hashtable;
ConnectionGroupHashtable newTable = new ConnectionGroupHashtable(originalTable ?? new Hashtable());

connectionGroupListField.SetValue(servicePoint, newTable);
}
}

HookServicePoint(value);
base[key] = value;
}
}
Expand All @@ -711,17 +738,7 @@ public override object this[object key]
get => base[key];
set
{
if (connectionGroupType.IsInstanceOfType(value))
{
// Replace the Connection arraylist inside this ConnectionGroup object,
// which allows us to intercept each new Connection object added under
// this ConnectionGroup.
ArrayList originalArrayList = connectionListField.GetValue(value) as ArrayList;
ConnectionArrayList newArrayList = new ConnectionArrayList(originalArrayList ?? new ArrayList());

connectionListField.SetValue(value, newArrayList);
}

HookConnectionGroup(value);
base[key] = value;
}
}
Expand Down