Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
e2b40d5
Add tests that we _can't_ duck type invalidly defined types
andrewlock Oct 7, 2021
c907cab
Add checks for method resolution that Duck attribute works with gener…
andrewlock Oct 14, 2021
2896662
Add some error tests for reverse duck typing
andrewlock Oct 8, 2021
04691e4
Add failing test for reverse proxy where virtual methods already have…
andrewlock Oct 4, 2021
d29380a
Add test that reverse-proxy properties work correctly (they don't cur…
andrewlock Oct 6, 2021
59d5f05
Fix uninitialized fields and properties in reverse proxy
tonyredondo Oct 6, 2021
df4e2ef
Change type returned by DuckType.Create() to an object
andrewlock Oct 12, 2021
e8dccf2
Fix cases in field and property duck typing that were being too lenient
andrewlock Oct 12, 2021
8f3d53f
Add additional guard clauses for method duck typing
andrewlock Oct 12, 2021
2b77e49
Skip tests for which we can't (or won't) detect failures
andrewlock Oct 12, 2021
1448252
Split reverse-duck-typing from forward-duck-typing
andrewlock Oct 13, 2021
b8c7218
Make [DuckReverseMethod] a mirror of [Duck] attribute
andrewlock Oct 14, 2021
c5f4c96
Fix [DuckReverseMethod] usages
andrewlock Oct 14, 2021
1da3e90
Update SelectTargetMethod implementation
andrewlock Oct 14, 2021
71061f3
Handle additional incorrect usage case and add additional exception t…
andrewlock Oct 15, 2021
e4122f1
Ensure IntegrationOptions catches DuckType exceptions when using Duck…
andrewlock Oct 15, 2021
6dece05
Refactor DuckType.Methods.cs to reduce duplication
andrewlock Oct 15, 2021
eac62e3
Update Public API tests
andrewlock Oct 14, 2021
bb61b30
Update ExceptionsTests
andrewlock Oct 20, 2021
f0d69ac
Update incorrect comments
andrewlock Oct 20, 2021
fb45c4d
Fix public API
andrewlock Oct 20, 2021
a6fc6ff
Comments from code review
andrewlock Oct 22, 2021
f5e4521
Update reverse proxy properties implementation
andrewlock Oct 22, 2021
75d9291
Handle case where [DuckReverseMethod] attribute has different number …
andrewlock Oct 25, 2021
3e3106a
Fix incorrect arguments in test
andrewlock Oct 25, 2021
2487327
Use public types so we can test net452 in reverse proxy error tests
andrewlock Oct 25, 2021
eef7151
Fix incorrectly ignored `WrongArgumentTypeInterfaceImplementations.Du…
andrewlock Oct 25, 2021
d87eb44
Fix Public API tests
andrewlock Oct 25, 2021
6cc1616
Ensure we catch _all_ exceptions when creating a duck type
andrewlock Oct 25, 2021
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 @@ -4,6 +4,7 @@
// </copyright>

using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using Datadog.Trace.DuckTyping;
using Datadog.Trace.Logging;
Expand All @@ -26,7 +27,7 @@ internal static void LogException(Exception exception, string message = null)
{
// ReSharper disable twice ExplicitCallerInfoArgument
Log.Error(exception, message ?? exception?.Message);
if (exception is DuckTypeException)
if (exception is DuckTypeException or TargetInvocationException { InnerException: DuckTypeException })
{
Log.Warning($"DuckTypeException has been detected, the integration <{typeof(TIntegration)}, {typeof(TTarget)}> will be disabled.");
_disableIntegration = true;
Expand Down
28 changes: 1 addition & 27 deletions tracer/src/Datadog.Trace/DuckTyping/DuckAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,42 +27,16 @@ public enum DuckKind
/// <summary>
/// Duck attribute
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Method | AttributeTargets.Field, AllowMultiple = false)]
public class DuckAttribute : Attribute
public class DuckAttribute : DuckAttributeBase
{
/// <summary>
/// Default BindingFlags
/// </summary>
public const BindingFlags DefaultFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance | BindingFlags.FlattenHierarchy;

/// <summary>
/// Gets or sets the underlying type member name
/// </summary>
public string Name { get; set; }

/// <summary>
/// Gets or sets duck kind
/// </summary>
public DuckKind Kind { get; set; } = DuckKind.Property;

/// <summary>
/// Gets or sets the binding flags
/// </summary>
public BindingFlags BindingFlags { get; set; } = DefaultFlags;

/// <summary>
/// Gets or sets the generic parameter type names definition for a generic method call (required when calling generic methods and instance type is non public)
/// </summary>
public string[] GenericParameterTypeNames { get; set; }

/// <summary>
/// Gets or sets the parameter type names of the target method (optional / used to disambiguation)
/// </summary>
public string[] ParameterTypeNames { get; set; }

/// <summary>
/// Gets or sets the explicit interface type name
/// </summary>
public string ExplicitInterfaceTypeName { get; set; }
}
}
43 changes: 43 additions & 0 deletions tracer/src/Datadog.Trace/DuckTyping/DuckAttributeBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// <copyright file="DuckAttributeBase.cs" company="Datadog">
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
// </copyright>

using System;
using System.ComponentModel;
using System.Reflection;

namespace Datadog.Trace.DuckTyping
{
/// <summary>
/// Duck attribute
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Method | AttributeTargets.Field, AllowMultiple = false)]
public abstract class DuckAttributeBase : Attribute
{
/// <summary>
/// Gets or sets the underlying type member name
/// </summary>
public string Name { get; set; }

/// <summary>
/// Gets or sets the binding flags
/// </summary>
public BindingFlags BindingFlags { get; set; } = DuckAttribute.DefaultFlags;

/// <summary>
/// Gets or sets the generic parameter type names definition for a generic method call (required when calling generic methods and instance type is non public)
/// </summary>
public string[] GenericParameterTypeNames { get; set; }

/// <summary>
/// Gets or sets the parameter type names of the target method (optional / used to disambiguation)
/// </summary>
public string[] ParameterTypeNames { get; set; }

/// <summary>
/// Gets or sets the explicit interface type name
/// </summary>
public string ExplicitInterfaceTypeName { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,7 @@ namespace Datadog.Trace.DuckTyping
/// <summary>
/// Duck reverse method attribute
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class DuckReverseMethodAttribute : Attribute
public class DuckReverseMethodAttribute : DuckAttributeBase
{
/// <summary>
/// Initializes a new instance of the <see cref="DuckReverseMethodAttribute"/> class.
/// </summary>
public DuckReverseMethodAttribute()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="DuckReverseMethodAttribute"/> class.
/// </summary>
/// <param name="arguments">Methods arguments</param>
public DuckReverseMethodAttribute(params string[] arguments)
{
Arguments = arguments;
}

/// <summary>
/// Gets the methods arguments
/// </summary>
public string[] Arguments { get; private set; }
}
}
2 changes: 2 additions & 0 deletions tracer/src/Datadog.Trace/DuckTyping/DuckType.Fields.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ private static MethodBuilder GetFieldGetMethod(TypeBuilder proxyTypeBuilder, Typ
il.Emit(OpCodes.Box, targetField.FieldType);
}

// WARNING: If targetField.FieldType cannot be duck cast to proxyMemberReturnType
// this will throw an exception at runtime when accessing the member
// We call DuckType.CreateCache<>.Create()
MethodInfo getProxyMethodInfo = typeof(CreateCache<>)
.MakeGenericType(proxyMemberReturnType).GetMethod("Create");
Expand Down
Loading