Tracer.Fody plugin for instrumenting code with OpenTracing
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
.gitignore
LICENSE
README.md

README.md

NuGet

Tracer.OpenTracing.Fody

Fody plugin based off of tracer for instrumenting code with OpenTracing

Functionality

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()
   {
-    using (GlobalTracer.Instance
-      .BuildSpan(nameof(Class) + "." + nameof(Method1))
-      .StartActive())
-    {
       // Do stuff
       this.MethodImpl();
-    }
   }

   public void Method2()
   {
-    using (GlobalTracer.Instance
-      .BuildSpan(nameof(Class) + "." + nameof(Method2))
-      .StartActive())
-    {
       // Do stuff
       this.MethodImpl();
-    }
   }
   
   private void MethodImpl()
   {
     // Do stuff
   }
 }

Setup

  1. Make sure your startup logic is configuring GlobalTracer.Instance via GlobalTracer.Register. If you don't have one ready presently, you can use EventHookTracer as follows.

    EventHookTracer example

    var eventHookTracer = new EventHookTracer(new MockTracer());
    eventHookTracer.SpanActivated += (sender, eventArgs) => Console.WriteLine("+" + eventArgs.OperationName);
    eventHookTracer.SpanFinished += (sender, eventArgs) => Console.WriteLine("-" + eventArgs.OperationName);
    GlobalTracer.Register(eventHookTracer);
  2. Import Tracer.OpenTracing.Fody from nuget

    This will import Tracer.Fody and Tracer, which are needed to manipulate the assembly with tracing instrumentation.

  3. 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>
  4. 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.

    Example 1 - FodyWeavers.xml

    <?xml version="1.0" encoding="utf-8" ?>
    <Weavers>   
      <Tracer adapterAssembly="Tracer.OpenTracing" 
              logManager="Tracer.OpenTracing.LogManagerAdapter" 
              logger="Tracer.OpenTracing.LoggerAdapter" >
        <!-- 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] -->
        <TraceOn class="public" method="all" />
      </Tracer>
    </Weavers>

    Example 2 - MyClass.cs

    [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
    }