Automatically generate activity by defined [Activity] on runtime.
$ dotnet add package PW.OpenTelemetry.DynamicProxyGenerate an activity to wrap the method, modify name of the inner activity or discard inner activity.
[ActivitySource] all methods defined in the interface or virtual methods defined in the class will automatically generate activity, unless defined [NonActivity] on method.
IncludeNonAsyncStateMachineMethod: default include all async methods of defined in an interface, or all [AsyncStateMachine] public or protected virtual method of class (except for async void method). If true, all methods defined in the interface will be included and all public or protected virtual methods of the class.
[ActivitySource] all methods will automatically generate activity, except for those that are defined [NonActivity] on method.
IncludeNonAsyncStateMachineMethod: public methods defined in the class that are marked with the [AsyncStateMachine] attribute (except for async void method). If true, will include all methods of class.
Needs to be installed in each working project, and install
Metalama.CompilerorFody(Fodyneed some additional file), recommendMetalama.Compiler.
[Activity] can be defined on method only.
If defined [NonActivity] on method, call method will not generate an activity. if SuppressInstrumentation is true, the inner activity will be discarded.
To modify the DisplayName of an inner activity, you must invoke TracerProviderBuilder.AddActivityNameProcessor(). If a type is defined with the [ActivitySource] attribute or a method is defined with the [Activity] or [NonActivity] attribute, the [ActivityName] attribute will not take effect.
Priority: [NonActivity] > [Activity] > [ActivityName] (method) > [ActivitySource] > [ActivityName] (class)
To add tag to activity, it defined on parameter or return value.
To add tags to activity, it defined on type or method.
| DynamicProxy | StaticProxy | |
|---|---|---|
| AOT | ❌ | ✔️ |
| Work in | Runtime | Compiling |
| Support scenario | interface or virtual method | Any method with a body of type. |
| Work order (ASC) | 2 | 1 |
| Performance | ⭐⭐⭐ | ⭐⭐⭐⭐⭐ |
| Expression | Default: public property, field or parameterless method.<br />DynamicExpressionParser: see DynamicExpresso.Core | Any valid code. |
Must start with $ on [ActivityTag]
Parse expression on runtime.
Public property, field or parameterless method only.
Parse expression on compiling.
Any valid code.
ActivitySourceAttribute.GetActivitySourceName(typeof(YourType))
See demo
ProxyGenerator generator = ...
IActivityInvokerFactory invokerFactory = ...
var proxyType = generator.CreateClassProxy<YourType>(new ActivityInterceptor(invokerFactory));See demo.