diff --git a/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpWebRequestActivitySource.netfx.cs b/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpWebRequestActivitySource.netfx.cs index aab50d346b7..d7a1c821e82 100644 --- a/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpWebRequestActivitySource.netfx.cs +++ b/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpWebRequestActivitySource.netfx.cs @@ -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 CreateFieldGetter(string fieldName, BindingFlags flags) where TClass : class { @@ -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; } } @@ -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; } }