You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Move docs to _docs_ folder
Fix links
* Clarify return types in instrumentation docs
* Mention that integration methods are optional
* Add small section on constraints duck-typing.
Copy file name to clipboardExpand all lines: docs/development/AutomaticInstrumentation.md
+40-8
Original file line number
Diff line number
Diff line change
@@ -8,12 +8,12 @@ Creating a new instrumentation implementation typically uses the following proce
8
8
9
9
1. Identify the operation of interest that we want to measure. Also gather the tags, resource names that we will need to set. Don't forget to check what has been implemented by other tracers.
10
10
2. Find an appropriate instrumentation point in the target library. You may need to use multiple instrumentation points, and you may need to use different targets for different _versions_ of the library
11
-
3. Create an instrumentation class using one of the standard "shapes" (described below), and place it in the [ClrProfiler/AutoInstrumentation folder](./AutoInstrumentation). If the methods you need to instrument have different prototypes (especially the number of parameters), you will need multiple class to instrument them.
11
+
3. Create an instrumentation class using one of the standard "shapes" (described below), and place it in the [ClrProfiler/AutoInstrumentation folder](../../tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation). If the methods you need to instrument have different prototypes (especially the number of parameters), you will need multiple class to instrument them.
12
12
4. Add an `[InstrumentMethod]` attribute to the instrumentation class, as described below. Alternatively, add an assembly-level `[AdoNetClientInstrumentMethods]` attribute
13
13
5. (Optional) Create duck-typing unit tests in _Datadog.Trace.Tests_ to confirm any duck types are valid. This can make the feedback cycle much faster than relying on integration tests
14
14
6. Create integration tests for your instrumentation
15
15
1. Create (or reuse) a sample application that uses the target library, which ideally exercises all the code paths in your new instrumentation. Use an `$(ApiVersion)` MSBuild variables to allow testing against multiple package versions in CI.
16
-
2. Add an entry in [tracer/build/PackageVersionsGeneratorDefinitions.json](../../../build/PackageVersionsGeneratorDefinitions.json) defining the range of all supported versions. See the existing definitions for examples
16
+
2. Add an entry in [tracer/build/PackageVersionsGeneratorDefinitions.json](../../tracer/build/PackageVersionsGeneratorDefinitions.json) defining the range of all supported versions. See the existing definitions for examples
17
17
3. Run `./tracer/build.ps1 GeneratePackageVersions`. This generates the xunit test data for package versions in the `TestData` that you can use as `[MemberData]` for your `[Theory]` tests.
18
18
4. Use the `MockTracerAgent` to confirm your instrumentation is working as expected.
19
19
7. After testing locally, push to GitHub, and do a manual run in Azure Devops for your branch
@@ -40,14 +40,16 @@ public class ClientQueryIteratorsIntegrations
40
40
41
41
// Include ONE of the following:
42
42
43
-
// 👇 Async method
43
+
// 👇 Async method, with different behavior based on the return type
44
+
// - Task<TReturn>: returnValue is set to the Result
45
+
// - Task: TReturn is set to typeof(object) and returnValue is set to null
@@ -61,12 +63,42 @@ public class ClientQueryIteratorsIntegrations
61
63
}
62
64
```
63
65
66
+
> Note that both `OnMethodBegin` and `OnMethodEnd` are optional. If you only need one of the integration points, you can omit the others
67
+
68
+
The first parameter passed to the method is the instance on which the method is called (for `static` methods, this parameter should be omitted), and should be a _generic parameter_ type.
69
+
70
+
For parameters that are well-known types like `string`, `object`, or `Exception`, you can use the type directly in the `OnMethodBegin` or `OnMethodEnd` methods. For other types that can't be directly referenced, such as types in the target-library, you should use generic parameters. If you need to manipulate the generic parameters, for example to access values, use the duck-typing approach described below.
71
+
72
+
### Duck-typing, instrumentation classes, and constraints
73
+
74
+
When creating instrumentation classes you often need to work with `Type`s in the target library that you can't reference directly. Rather than using reflection directly to manipulate these types, the .NET Tracer has an optimised solution for working with them called _Duck Typing_.
75
+
76
+
> See [the Duck Typing document](./DuckTyping.md) for a detailed description of duck-typing, use cases, best practices, and benchmarks.
77
+
78
+
You can use duck-typing imperatively at runtime to "cast" any object to a type you can manipulate directly, but if you know at call time that you need to work with one of the generic parameters passed to an `OnMethodBegin` or `OnMethodEnd`, you can use a more performant approach leveraging _constraints_.
79
+
80
+
Add a constraint to your method that the generic type implements your duck type. The value passed to your method will then be pre-duck-typed, and will have better performance than using duck-typing manually at a later point. For example, the integration below [(A GraphQL integration)](../../tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/GraphQL/ExecuteAsyncIntegration.cs) takes a single parameter which is duck-typed using constraints to implement the type IExecutionContext
> ⚠ Note that pre duck-typed parameters that use constraints will never be `null`. If you need to check the parameter for `null`, add the `IDuckType` constrainttoo, andcheckthevalueof `IDuckType.Instance`.
93
+
94
+
Formoreinformationonduck-typing, see [thedocumentation](./DuckTyping.md).
@@ -88,7 +120,7 @@ public class HttpClientHandlerIntegration
88
120
}
89
121
```
90
122
91
-
We also have a special case for ADO.NET method instrumentation, as this is generally more convoluted, and requires a lot of duplication. All new ADO.NET implementations will likely reuse existing instrumentation classes, such as [`CommandExecuteReaderIntegration`](./AutoInstrumentation/AdoNet/CommandExecuteReaderIntegration.cs) for example. To save having to specify many `[InstrumentMethod]` attributes, you can instead use the `[AdoNetClientInstrumentMethods]`_assembly_ attribute, to define some standard types, as well as which of the standard ADO.NET signatures to implement. For example:
@@ -114,6 +146,6 @@ We also have a special case for ADO.NET method instrumentation, as this is gener
114
146
})]
115
147
```
116
148
117
-
The above attribute shows how to select which signatures to implement, via the `TargetMethodAttributes` property. These attributes are nested types defined inside [`AdoNetClientInstrumentMethodsAttribute`](./AutoInstrumentation/AdoNet/AdoNetClientInstrumentMethodsAttribute.cs), each of which are associated with a given signature + instrumentation class (via the `[AdoNetClientInstrumentMethodsAttribute.AdoNetTargetSignature]` attribute)
Copy file name to clipboardExpand all lines: tracer/README.MD
+3-3
Original file line number
Diff line number
Diff line change
@@ -37,7 +37,7 @@ You can develop the tracer on various environments.
37
37
- Optional: [nuget.exe CLI](https://www.nuget.org/downloads) v5.3 or newer
38
38
- Optional: [WiX Toolset 3.11.1](http://wixtoolset.org/releases/) or newer to build Windows installer (msi)
39
39
-[WiX Toolset Visual Studio Extension](https://wixtoolset.org/releases/) to build installer from Visual Studio
40
-
- Optional: [Docker for Windows](https://docs.docker.com/docker-for-windows/) to build Linux binaries and run integration tests on Linux containers. See [section on Docker Compose](#building-and-running-tests-with-docker-compose).
40
+
- Optional: [Docker for Windows](https://docs.docker.com/docker-for-windows/) to build Linux binaries and run integration tests on Linux containers.
41
41
- Requires Windows 10 (1607 Anniversary Update, Build 14393 or newer)
42
42
43
43
Microsoft provides [evaluation developer VMs](https://developer.microsoft.com/en-us/windows/downloads/virtual-machines) with Windows and Visual Studio pre-installed.
@@ -101,8 +101,8 @@ You can use Rider and CLion to develop on macOS. Building and testing can be don
101
101
102
102
## Additional Technical Documentation
103
103
104
-
*[Implementing an automatic instrumentation](./src/Datadog.Trace/ClrProfiler/README.md)
105
-
*[Duck typing: usages, best practices, and benchmarks](./src/Datadog.Trace/DuckTyping/README.md)
104
+
*[Implementing an automatic instrumentation](../docs/development/AutomaticInstrumentation.md)
105
+
*[Duck typing: usages, best practices, and benchmarks](../docs/development/DuckTyping.md)
0 commit comments