Skip to content

Latest commit

 

History

History
124 lines (112 loc) · 4.44 KB

README.md

File metadata and controls

124 lines (112 loc) · 4.44 KB

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()
   {
-    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
   }
 }

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. ^ Old news, use https://github.com/opentracing-contrib/csharp-decorators now

    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" 
              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>

    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
    }