diff --git a/src/OpenTelemetry.Instrumentation.AspNet/Implementation/PropertyFetcher.cs b/src/OpenTelemetry.Instrumentation.AspNet/Implementation/PropertyFetcher.cs deleted file mode 100644 index 1373f62695a..00000000000 --- a/src/OpenTelemetry.Instrumentation.AspNet/Implementation/PropertyFetcher.cs +++ /dev/null @@ -1,94 +0,0 @@ -// -// Copyright The OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -using System; -using System.Linq; -using System.Reflection; - -namespace OpenTelemetry.Instrumentation.AspNet.Implementation -{ - internal class PropertyFetcher - { - private readonly string propertyName; - private PropertyFetch innerFetcher; - - public PropertyFetcher(string propertyName) - { - this.propertyName = propertyName; - } - - public object Fetch(object obj) - { - if (this.innerFetcher == null) - { - var type = obj.GetType().GetTypeInfo(); - var property = type.DeclaredProperties.FirstOrDefault(p => string.Equals(p.Name, this.propertyName, StringComparison.InvariantCultureIgnoreCase)); - if (property == null) - { - property = type.GetProperty(this.propertyName); - } - - this.innerFetcher = PropertyFetch.FetcherForProperty(property); - } - - return this.innerFetcher?.Fetch(obj); - } - - // see https://github.com/dotnet/corefx/blob/master/src/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/DiagnosticSourceEventSource.cs - private class PropertyFetch - { - /// - /// Create a property fetcher from a .NET Reflection PropertyInfo class that - /// represents a property of a particular type. - /// - public static PropertyFetch FetcherForProperty(PropertyInfo propertyInfo) - { - if (propertyInfo == null) - { - // returns null on any fetch. - return new PropertyFetch(); - } - - var typedPropertyFetcher = typeof(TypedFetchProperty<,>); - var instantiatedTypedPropertyFetcher = typedPropertyFetcher.GetTypeInfo().MakeGenericType( - propertyInfo.DeclaringType, propertyInfo.PropertyType); - return (PropertyFetch)Activator.CreateInstance(instantiatedTypedPropertyFetcher, propertyInfo); - } - - /// - /// Given an object, fetch the property that this propertyFetch represents. - /// - public virtual object Fetch(object obj) - { - return null; - } - - private class TypedFetchProperty : PropertyFetch - { - private readonly Func propertyFetch; - - public TypedFetchProperty(PropertyInfo property) - { - this.propertyFetch = (Func)property.GetMethod.CreateDelegate(typeof(Func)); - } - - public override object Fetch(object obj) - { - return this.propertyFetch((TObject)obj); - } - } - } - } -} diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/PropertyFetcher.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/PropertyFetcher.cs deleted file mode 100644 index c1b980a8d27..00000000000 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/PropertyFetcher.cs +++ /dev/null @@ -1,94 +0,0 @@ -// -// Copyright The OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -using System; -using System.Linq; -using System.Reflection; - -namespace OpenTelemetry.Instrumentation.AspNetCore.Implementation -{ - internal class PropertyFetcher - { - private readonly string propertyName; - private PropertyFetch innerFetcher; - - public PropertyFetcher(string propertyName) - { - this.propertyName = propertyName; - } - - public object Fetch(object obj) - { - if (this.innerFetcher == null) - { - var type = obj.GetType().GetTypeInfo(); - var property = type.DeclaredProperties.FirstOrDefault(p => string.Equals(p.Name, this.propertyName, StringComparison.InvariantCultureIgnoreCase)); - if (property == null) - { - property = type.GetProperty(this.propertyName); - } - - this.innerFetcher = PropertyFetch.FetcherForProperty(property); - } - - return this.innerFetcher?.Fetch(obj); - } - - // see https://github.com/dotnet/corefx/blob/master/src/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/DiagnosticSourceEventSource.cs - private class PropertyFetch - { - /// - /// Create a property fetcher from a .NET Reflection PropertyInfo class that - /// represents a property of a particular type. - /// - public static PropertyFetch FetcherForProperty(PropertyInfo propertyInfo) - { - if (propertyInfo == null) - { - // returns null on any fetch. - return new PropertyFetch(); - } - - var typedPropertyFetcher = typeof(TypedFetchProperty<,>); - var instantiatedTypedPropertyFetcher = typedPropertyFetcher.GetTypeInfo().MakeGenericType( - propertyInfo.DeclaringType, propertyInfo.PropertyType); - return (PropertyFetch)Activator.CreateInstance(instantiatedTypedPropertyFetcher, propertyInfo); - } - - /// - /// Given an object, fetch the property that this propertyFetch represents. - /// - public virtual object Fetch(object obj) - { - return null; - } - - private class TypedFetchProperty : PropertyFetch - { - private readonly Func propertyFetch; - - public TypedFetchProperty(PropertyInfo property) - { - this.propertyFetch = (Func)property.GetMethod.CreateDelegate(typeof(Func)); - } - - public override object Fetch(object obj) - { - return this.propertyFetch((TObject)obj); - } - } - } - } -} diff --git a/src/OpenTelemetry.Instrumentation.GrpcNetClient/Implementation/PropertyFetcher.cs b/src/OpenTelemetry.Instrumentation.GrpcNetClient/Implementation/PropertyFetcher.cs deleted file mode 100644 index dab85e0874d..00000000000 --- a/src/OpenTelemetry.Instrumentation.GrpcNetClient/Implementation/PropertyFetcher.cs +++ /dev/null @@ -1,99 +0,0 @@ -// -// Copyright The OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -using System; -using System.Linq; -using System.Reflection; - -namespace OpenTelemetry.Instrumentation.GrpcNetClient.Implementation -{ - internal class PropertyFetcher - { - private readonly string propertyName; - private PropertyFetch innerFetcher; - - public PropertyFetcher(string propertyName) - { - this.propertyName = propertyName; - } - - public object Fetch(object obj) - { - if (this.innerFetcher == null) - { - var type = obj.GetType().GetTypeInfo(); - var property = type.DeclaredProperties.FirstOrDefault(p => string.Equals(p.Name, this.propertyName, StringComparison.InvariantCultureIgnoreCase)); - if (property == null) - { - property = type.GetProperty(this.propertyName); - } - - this.innerFetcher = PropertyFetch.FetcherForProperty(property); - } - - return this.innerFetcher?.Fetch(obj); - } - - // see https://github.com/dotnet/corefx/blob/master/src/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/DiagnosticSourceEventSource.cs - private class PropertyFetch - { - /// - /// Create a property fetcher from a .NET Reflection PropertyInfo class that - /// represents a property of a particular type. - /// - public static PropertyFetch FetcherForProperty(PropertyInfo propertyInfo) - { - if (propertyInfo == null) - { - // returns null on any fetch. - return new PropertyFetch(); - } - - var typedPropertyFetcher = typeof(TypedFetchProperty<,>); - var instantiatedTypedPropertyFetcher = typedPropertyFetcher.GetTypeInfo().MakeGenericType( - propertyInfo.DeclaringType, propertyInfo.PropertyType); - return (PropertyFetch)Activator.CreateInstance(instantiatedTypedPropertyFetcher, propertyInfo); - } - - /// - /// Given an object, fetch the property that this propertyFetch represents. - /// - public virtual object Fetch(object obj) - { - return null; - } - - private class TypedFetchProperty : PropertyFetch - { - private readonly Func propertyFetch; - - public TypedFetchProperty(PropertyInfo property) - { - this.propertyFetch = (Func)property.GetMethod.CreateDelegate(typeof(Func)); - } - - public override object Fetch(object obj) - { - if (obj is TObject o) - { - return this.propertyFetch(o); - } - - return null; - } - } - } - } -} diff --git a/src/OpenTelemetry.Instrumentation.Http/Implementation/PropertyFetcher.cs b/src/OpenTelemetry.Instrumentation.Http/Implementation/PropertyFetcher.cs deleted file mode 100644 index f702d1f2a45..00000000000 --- a/src/OpenTelemetry.Instrumentation.Http/Implementation/PropertyFetcher.cs +++ /dev/null @@ -1,99 +0,0 @@ -// -// Copyright The OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -using System; -using System.Linq; -using System.Reflection; - -namespace OpenTelemetry.Instrumentation.Http.Implementation -{ - internal class PropertyFetcher - { - private readonly string propertyName; - private PropertyFetch innerFetcher; - - public PropertyFetcher(string propertyName) - { - this.propertyName = propertyName; - } - - public object Fetch(object obj) - { - if (this.innerFetcher == null) - { - var type = obj.GetType().GetTypeInfo(); - var property = type.DeclaredProperties.FirstOrDefault(p => string.Equals(p.Name, this.propertyName, StringComparison.InvariantCultureIgnoreCase)); - if (property == null) - { - property = type.GetProperty(this.propertyName); - } - - this.innerFetcher = PropertyFetch.FetcherForProperty(property); - } - - return this.innerFetcher?.Fetch(obj); - } - - // see https://github.com/dotnet/corefx/blob/master/src/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/DiagnosticSourceEventSource.cs - private class PropertyFetch - { - /// - /// Create a property fetcher from a .NET Reflection PropertyInfo class that - /// represents a property of a particular type. - /// - public static PropertyFetch FetcherForProperty(PropertyInfo propertyInfo) - { - if (propertyInfo == null) - { - // returns null on any fetch. - return new PropertyFetch(); - } - - var typedPropertyFetcher = typeof(TypedFetchProperty<,>); - var instantiatedTypedPropertyFetcher = typedPropertyFetcher.GetTypeInfo().MakeGenericType( - propertyInfo.DeclaringType, propertyInfo.PropertyType); - return (PropertyFetch)Activator.CreateInstance(instantiatedTypedPropertyFetcher, propertyInfo); - } - - /// - /// Given an object, fetch the property that this propertyFetch represents. - /// - public virtual object Fetch(object obj) - { - return null; - } - - private class TypedFetchProperty : PropertyFetch - { - private readonly Func propertyFetch; - - public TypedFetchProperty(PropertyInfo property) - { - this.propertyFetch = (Func)property.GetMethod.CreateDelegate(typeof(Func)); - } - - public override object Fetch(object obj) - { - if (obj is TObject o) - { - return this.propertyFetch(o); - } - - return null; - } - } - } - } -} diff --git a/src/OpenTelemetry/CHANGELOG.md b/src/OpenTelemetry/CHANGELOG.md index 7a60aace8aa..87e1c8f0268 100644 --- a/src/OpenTelemetry/CHANGELOG.md +++ b/src/OpenTelemetry/CHANGELOG.md @@ -3,7 +3,10 @@ ## Unreleased * Fixes [953](https://github.com/open-telemetry/opentelemetry-dotnet/issues/953) -* Changes arising from DiagnosticSource changes ([#1203](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1203/files)) +* Changes arising from `DiagnosticSource` changes + ([#1203](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1203)) +* `PropertyFetcher` is now public + ([#1232](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1232)) ## 0.5.0-beta.2 diff --git a/src/OpenTelemetry.Instrumentation.SqlClient/Implementation/PropertyFetcher.cs b/src/OpenTelemetry/Instrumentation/PropertyFetcher.cs similarity index 86% rename from src/OpenTelemetry.Instrumentation.SqlClient/Implementation/PropertyFetcher.cs rename to src/OpenTelemetry/Instrumentation/PropertyFetcher.cs index 93d00eb57f8..b31cb3d00d5 100644 --- a/src/OpenTelemetry.Instrumentation.SqlClient/Implementation/PropertyFetcher.cs +++ b/src/OpenTelemetry/Instrumentation/PropertyFetcher.cs @@ -13,22 +13,35 @@ // See the License for the specific language governing permissions and // limitations under the License. // + using System; using System.Linq; using System.Reflection; -namespace OpenTelemetry.Instrumentation.SqlClient.Implementation +namespace OpenTelemetry.Instrumentation { - internal class PropertyFetcher + /// + /// PropertyFetcher fetches a property from an object. + /// + public class PropertyFetcher { private readonly string propertyName; private PropertyFetch innerFetcher; + /// + /// Initializes a new instance of the class. + /// + /// Property name to fetch. public PropertyFetcher(string propertyName) { this.propertyName = propertyName; } + /// + /// Fetch the property from the object. + /// + /// Object to be fetched. + /// Property fetched. public object Fetch(object obj) { if (this.innerFetcher == null)