Skip to content

Uses Castle DynamicProxy to add OpenTracing instrumentation to an interface

License

Notifications You must be signed in to change notification settings

ndrwrbgs/DynamicTracing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Uses Castle DynamicProxy to add OpenTracing instrumentation to an interface

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published