Repository navigation
IAnalysable
Martin Halliday edited this page Jul 25, 2016
·
2 revisions
This is an optional capability that you can add to your middleware to make it more useful. If you don't choose to implement this interface your middleware will still provide all of its intended functionality to the application, but it will be harder for the operations team to support your middleware.
The IAnalysable interface defines these properties and methods:
| Method or Property | Description |
|---|---|
IList<IStatisticInformation> AvailableStatistics { get; } |
Returns a list of the values that this middleware measures about its internal operation |
IStatistic GetStatistic(string id); |
Returns a reference to a specific measurement |
The IStatistic interface defines these properties and methods:
| Method or Property | Description |
|---|---|
float Value { get; } |
Returns the current value of this statistic. This is the value that should be displayed, graphed, averaged, trended etc |
float Denominator { get; } |
For statistics that represent a ratio of two values, this is the denominator of that ratio. You can multiply the Denominator by the Value to calculate the numerator of the original ratio. This is useful when combining multiple ratios into an average value. |
string Formatted { get; } |
Returns the Value property scaled and with units suitable for display to the user |
IStatistic Refresh(); |
Statistics can use this as a hint that the Value property should be updated. Consumers of this interface should assume that the Value of the statistic might not change unless they call the Refresh() method, but should also assume that the Value can change even when Refresh() is not called. |
Below is an example useage of this interface:
private Task RenderXml(IOwinContext context, IEnumerable<AnalysableInfo> analysisData)
{
var document = new XDocument(new XDeclaration("1.0", "utf-8", "true"));
var rootElement = new XElement("Analytics");
document.Add(rootElement);
foreach (var analysable in analysisData)
{
var middlewareElement = new XElement(
"Middleware",
new XAttribute("name", analysable.Name),
new XAttribute("type", analysable.Type),
new XElement("Description", analysable.Description)
);
rootElement.Add(middlewareElement);
var statisticsElement = new XElement("Statistics");
middlewareElement.Add(statisticsElement);
foreach (var statistic in analysable.Statistics)
{
statistic.Statistic.Refresh();
statisticsElement.Add(
new XElement(
"Statistic",
new XAttribute("name", statistic.Name),
new XElement("units", statistic.Units ?? ""),
new XElement("value", statistic.Statistic.Value),
new XElement("denominator", statistic.Statistic.Denominator),
new XElement("formatted", statistic.Statistic.Formatted),
new XElement("description", statistic.Description)));
}
}
return context.Response.WriteAsync(document.ToString(SaveOptions.None));
}
Below is an example implementation of this interface:
public class RouteVisualizerMiddleware: IMiddleware<object>, IAnalysable
{
private volatile int _requestCount;
IList<IStatisticInformation> IAnalysable.AvailableStatistics
{
get
{
return new List<IStatisticInformation>
{
new StatisticInformation
{
Id = "request.count",
Name = "Total requests",
Description = "The total number of requests processed by the " +
"visualazer midldeware since the application was restarted",
}
};
}
}
IStatistic IAnalysable.GetStatistic(string id)
{
switch (id)
{
case "request.count":
return new RequestCountStatistic(this).Refresh();
}
return null;
}
private class StatisticInformation: IStatisticInformation
{
public string Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Explanation { get; set; }
public string Units { get; set; }
}
private abstract class Statistic: IStatistic
{
public float Value { get; protected set; }
public float Denominator { get; protected set; }
public string Formatted { get; protected set; }
public abstract IStatistic Refresh();
}
private class RequestCountStatistic: Statistic
{
private readonly RouteVisualizerMiddleware _routeVisualizerMiddleware;
public RequestCountStatistic(RouteVisualizerMiddleware routeVisualizerMiddleware)
{
_routeVisualizerMiddleware = routeVisualizerMiddleware;
}
public override IStatistic Refresh()
{
Value = _routeVisualizerMiddleware._requestCount;
Denominator = 1;
if (Value < 2000)
Formatted = Value.ToString(CultureInfo.InvariantCulture);
else if (Value < 2000000)
Formatted = (Value / 1000f).ToString("f1", CultureInfo.InvariantCulture) + "K";
else
Formatted = (Value / 1000000f).ToString("f1", CultureInfo.InvariantCulture) + "M";
return this;
}
}
}