ServiceLevelIndicators 0.1.0-alpha.10
See the version list below for details.
dotnet add package ServiceLevelIndicators --version 0.1.0-alpha.10
NuGet\Install-Package ServiceLevelIndicators -Version 0.1.0-alpha.10
<PackageReference Include="ServiceLevelIndicators" Version="0.1.0-alpha.10" />
paket add ServiceLevelIndicators --version 0.1.0-alpha.10
#r "nuget: ServiceLevelIndicators, 0.1.0-alpha.10"
// Install ServiceLevelIndicators as a Cake Addin #addin nuget:?package=ServiceLevelIndicators&version=0.1.0-alpha.10&prerelease // Install ServiceLevelIndicators as a Cake Tool #tool nuget:?package=ServiceLevelIndicators&version=0.1.0-alpha.10&prerelease
ArtifactType: nupkg. Language: csharp. Tags: SLI, OpenTelemetry, Metrics.
Service Level Indicators
Service Level Indicator library will help emit latency metrics for each API operation. The metrics is emitted via OpenTelemetry and can be used to monitor service level agreements.
By default an instrument named LatencySLI
is added to the service metrics and the metrics are emitted. The metrics are emitted with the following attributes.
- CustomerResourceId - The customer resource id.
- LocationId - The location id. Where is the service running? eg. Public cloud, West US 3 region.
- Operation - The name of the operation. Defaults to
AttributeRouteInfo.Template
information. - HttpStatusCode - The http status code.
- api_version - If API Versioning is used, the version of the API.
Prerequisites
The library targets .net core and requires the service to use OpenTelemetry https://opentelemetry.io/.
Create and register a metrics meter with the dependency injection.
Example.
public class SampleApiMeters { public const string MeterName = "SampleMeter"; public Meter Meter { get; } = new Meter(MeterName); } builder.Services.AddSingleton<SampleApiMeters>();
Add a class to configure SLI
Example.
internal sealed class ConfigureServiceLevelIndicatorOptions : IConfigureOptions<ServiceLevelIndicatorOptions> { private readonly SampleApiMeters meters; public ConfigureServiceLevelIndicatorOptions(SampleApiMeters meters) => this.meters = meters; public void Configure(ServiceLevelIndicatorOptions options) => options.Meter = meters.Meter; } builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IConfigureOptions<ServiceLevelIndicatorOptions>, ConfigureServiceLevelIndicatorOptions>());
Add ServiceLevelIndicator into the dependency injection.
Example.
builder.Services.AddServiceLevelIndicator(options => { Guid serviceTree = Guid.NewGuid(); options.CustomerResourceId = ServiceLevelIndicator.CreateCustomerResourceId(serviceTree); options.LocationId = ServiceLevelIndicator.CreateLocationId("public", "westus2"); });
Add the middleware to the pipeline.
If API versioning is used, Use app.UseServiceLevelIndicatorWithApiVersioning();
Otherwise, app.UseServiceLevelIndicator();
Usage
Once the Prerequisites are done, all controllers will emit SLI information. The default operation name is in the format HTTP Method_Controller_Action. eg GET WeatherForecast_Get_WeatherForecast/Action1
To override the default operation name add the attribute
[ServiceLevelIndicator]
on the method.Example.
[HttpGet("MyAction2")] [ServiceLevelIndicator(Operation = "MyOperation")] public IEnumerable<WeatherForecast> GetOperation() => GetWeather();
To set the CustomerResourceId within an API method, get the
IServiceLevelIndicatorFeature
and set it.[HttpGet("{customerResourceId}")] public IEnumerable<WeatherForecast> Get(string customerResourceId) { var sliFeature = HttpContext.Features.GetRequiredFeature<IServiceLevelIndicatorFeature>(); sliFeature.MeasureOperationLatency.CustomerResourceId = customerResourceId; return GetWeather(); }
Or use
GetMeasuredOperationLatency
extension method.[HttpGet("{customerResourceId}")] public IEnumerable<WeatherForecast> Get(string customerResourceId) { HttpContext.GetMeasuredOperationLatency().CustomerResourceId = customerResourceId; return GetWeather(); }
or mark the parameter with the attribute [CustomerResourceId]
[HttpGet("get-by-zip-code/{zipCode}")]
public IEnumerable<WeatherForecast> GetByZipcode([CustomerResourceId] string zipCode) => GetWeather();
To add custom Open Telemetry attributes.
HttpContext.GetMeasuredOperationLatency().AddAttribute(attribute, value);
To prevent automatically emitting SLI information on all controllers, set the option.
ServiceLevelIndicatorOptions.AutomaticallyEmitted = false;
In this case, add the attribute
[ServiceLevelIndicator]
on the controllers that should emit SLI.To measure a process, run it withing a
StartLatencyMeasureOperation
using block.Example.
public override async Task StoreItem(CaseEvent domainEvent, CancellationToken cancellationToken) { var attribute = new KeyValuePair<string, object?>("Event", domainEvent.GetType().Name); using var measuredOperation = _serviceLevelIndicator.StartLatencyMeasureOperation("StoreItem", attribute); DoTheWork();
Sample
Try out the sample weather forecast Web API.
To view the metrics locally.
- Run Docker Desktop
- Run sample\DockerOpenTelemetry\run.cmd to download and run zipkin and prometheus.
- Run the sample web API project and call the
GET WeatherForecast
using the Open API UI. - You should see the SLI metrics in prometheus under the meter
LatencySLI_bucket
where theOperation = "GET WeatherForeCase"
,HttpStatusCode = 200
,LocationId = "public_West US 3"
,Status = Ok
- If you run the sample with API Versioning, you will see something similar to the following.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
-
net7.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
- Microsoft.Extensions.Options (>= 7.0.1)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on ServiceLevelIndicators:
Package | Downloads |
---|---|
ServiceLevelIndicators.Asp
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
8.0.0-alpha.25 | 56 | 11/22/2024 |
8.0.0-alpha.24 | 52 | 11/20/2024 |
8.0.0-alpha.17 | 100 | 5/21/2024 |
8.0.0-alpha.12 | 101 | 3/29/2024 |
8.0.0-alpha.7 | 81 | 1/24/2024 |
8.0.0-alpha.6 | 65 | 1/23/2024 |
1.1.2 | 268 | 12/14/2023 |
1.1.1 | 223 | 11/10/2023 |
1.0.1 | 228 | 10/3/2023 |
0.1.0-alpha.20 | 101 | 9/27/2023 |
0.1.0-alpha.19 | 79 | 9/27/2023 |
0.1.0-alpha.18 | 130 | 9/14/2023 |
0.1.0-alpha.10 | 116 | 8/23/2023 |