Glimpse / Glimpse.Prototype

Glimpse v2 prototype
MIT License
185 stars 42 forks source link

Inspector module #31

Closed nikmd23 closed 9 years ago

nikmd23 commented 9 years ago

High Level

Glimpse inspects requests with the usage of inspectors, which typically publish messages to the message broker for storage and consumption by a client.

From a high level, inspectors in Glimpse 2.0 will be pieces of middleware.

There are two API's available:

  1. A more succinct high level API which pushes implements into "a pit of success"
  2. A more robust low level API which could be used as an "escape hatch" for advanced scenarios.

    Detailed Breakdown

    High Level

To use the high level API, users must implement IInspector

public interface IInspector
{
    Task Before(HttpContext context);

    Task After(HttpContext context);
}

An example implementation:

public class UserInspector : IInspector
{
    private IMessageBus _messageBus;

    public UserInspector(IMessageBus messageBus)
    {
        _messageBus = messageBus;
    }
    public async Task Before(HttpContext context)
    {
        context.Response.Headers.Append("inspected", "true");
    }

    public async Task After(HttpContext context)
    {
        _messageBus.Publish(context.User);
    }
}

Users can alternatively extend the Inspector class to override just Before or After if they prefer. Like all ASP.NET 5 components, dependency injection is fully supported, as shown with the injected IMessageBroker in the example.

Low Level

The low level API tracks much more closely with standard middleware, and in fact, standard middleware can be used as an inspector.

The interface to be implemented is IInspectorStartup:

public interface IInspectorStartup
{
    void Configure(IInspectorBuilder inspectorBuilder);
}

A sample implementation:

public class UserInspector : IInspectorStartup
{
    private IMessageBus _messageBus;

    public UserInspector(IMessageBus messageBus)
    {
        _messageBus = messageBus;
    }

    public void Configure(IInspectorBuilder inspectorBuilder)
    {
        inspectorBuilder.Use(async (context, next) =>
        {
            await next(context);
            _messageBus.Publish(context.User);
        });
    }
}

The low level API allows implementors to register multiple inspectors at once via IInspectorBuilder.Use(). Implementors can also access the IApplicationBuilder via IInspectorBuilder.Application.

The full IInspectorBuilder is defined as follows:

public interface IInspectorBuilder
{
    IApplicationBuilder AppBuilder { get; }
    //TODO: Add all Use overloads
    IInspectorBuilder Use( Func<HttpContext, Func<Task>, Task> middleware);
}

Other Considerations

Need To Implement

avanderhoorn commented 9 years ago

Initial work has been done here. Still need to write in IInspectors to actually be executed. This involves creating a IInspectorStartup which processes/runs all the registered IInspectors that are registered with the IInspectorProvider.

avanderhoorn commented 9 years ago

Work finished, pulling it in.