Skip to content

Latest commit

 

History

History
78 lines (60 loc) · 2.46 KB

README.md

File metadata and controls

78 lines (60 loc) · 2.46 KB

NuGet

DynamicTracing

Uses Castle DynamicProxy to add OpenTracing instrumentation to an interface at runtime.

Example

Summary Example

IMyInterface instance = /* ... */;
IMyInterface tracedInstance = instance.WrapInterfaceWithTracingAdapter<ISomething>(/* settings... */);
tracedInstance.MethodCall(); // Will open a Span around the method call, and close it when the method is complete.

Full Example

using System;

using OpenTracing;
using OpenTracing.Contrib.Decorators;
using OpenTracing.Contrib.DynamicInstrumentation;
using OpenTracing.Mock;
using OpenTracing.Util;

internal static class Program
{
    private static void Main()
    {
        GlobalTracer.Register(BuildTracer());

        ISomething something = new FakeSomething();

        var tracedSomething = something.WrapInterfaceWithTracingAdapter<ISomething>(
            new DynamicTracingWrapper.TracingInterceptorOptions(
                formatArgumentForTag: null,
                formatResultForTag: null,
                wrapInterfaces: false,
                recursivelyWrapInterfaces: false,
                includeClassName: true));

        tracedSomething.DoNothing("str"); // Outputs "Span FakeSomething.DoNothing started"
    }

#region Boilerplate for demo

    private static ITracer BuildTracer()
    {
        // Using https://www.nuget.org/packages/OpenTracing.Contrib.Decorators/ for a dev box tracer
        return new TracerDecoratorBuilder(new MockTracer())
            .OnSpanActivated((span, name) => Console.WriteLine($"Span {name} started"))
            .Build();
    }
}

// Type must be accessible to Castle's DynamicProxy. You can also use InternalsVisibleTo if required.
public interface ISomething
{
    void DoNothing(string str);
    int Add(int a, int b);
}

internal sealed class FakeSomething : ISomething
{
    public void DoNothing(string str) { }

    public int Add(int a, int b) => a + b;
}

#endregion

TracingInterceptorOptions

wiki

Caveats/Notes