Fody plugin based off of tracer for instrumenting code with OpenTracing
After setup, you can configure application wide tracing with a simple FodyWeavers.xml file, or you can individually select methods and classes to trace with the included [TraceOn] and [NoTrace] attributes.
It can reduce your code as follows
+[TraceOn(TraceTarget.Public)]
class Class
{
public void Method1()
{
- IScope scope = GlobalTracer.Instance
- .BuildSpan(nameof(Class) + "." + nameof(Method1))
- .StartActive();
- try
- {
// Do stuff
this.MethodImpl();
- }
- finally
- {
- /* Some error logging on the Scope */
- scope.Dispose();
- }
}
public void Method2()
{
- IScope scope = GlobalTracer.Instance
- .BuildSpan(nameof(Class) + "." + nameof(Method2))
- .StartActive();
- try
- {
// Do stuff
this.MethodImpl();
- }
- finally
- {
- /* Some error logging on the Scope */
- scope.Dispose();
- }
}
private void MethodImpl()
{
// Do stuff
}
}
-
Make sure your startup logic is configuring
GlobalTracer.Instance
viaGlobalTracer.Register
. If you don't have one ready presently, you can useEventHookTracer
as follows. ^ Old news, use https://github.com/opentracing-contrib/csharp-decorators nowvar eventHookTracer = new EventHookTracer(new MockTracer()); eventHookTracer.SpanActivated += (sender, eventArgs) => Console.WriteLine("+" + eventArgs.OperationName); eventHookTracer.SpanFinished += (sender, eventArgs) => Console.WriteLine("-" + eventArgs.OperationName); GlobalTracer.Register(eventHookTracer);
-
Import Tracer.OpenTracing.Fody from nuget
This will import Tracer.Fody and Tracer, which are needed to manipulate the assembly with tracing instrumentation.
-
Configure Fody to use this tracer Add a FodyWeavers.xml file with the following contents. (This file does not need CopyToOutputDirectory)
<?xml version="1.0" encoding="utf-8" ?> <Weavers> <Tracer adapterAssembly="Tracer.OpenTracing" logManager="Tracer.OpenTracing.LogManagerAdapter" logger="Tracer.OpenTracing.LoggerAdapter" > <TraceOn class="none" method="none" /> </Tracer> </Weavers>
-
Configure code to be traced
Use a combination of the FodyWeavers.xml configuration and Attributes on Classes, Assembly, or individual Methods to signal what elements should be traced. See tracer/basics wiki for details.
<?xml version="1.0" encoding="utf-8" ?> <Weavers> <Tracer adapterAssembly="Tracer.OpenTracing" logManager="Tracer.OpenTracing.LogManagerAdapter" logger="Tracer.OpenTracing.LoggerAdapter" staticLogger="" > <!-- Trace all methods on public classes --> <!-- Avoid tracing the startup method - e.g. don't trace before you've configured the GlobalTracer.Instance so in this case, make sure Main and SetupTracer are marked with [NoTrace] --> <!-- ^ not necessary anymore, now it will just Trace.Write that you tried to do a trace without a tracer registered --> <TraceOn class="public" method="all" /> </Tracer> </Weavers>
[TraceOn] class Foo { [NoTrace] public static void A() { B(); B(); } public static void B() { // BuildSpan().StartActive() is injected on this line } // ActiveSpan.Finish() is injected on this line }