ServiceLevelIndicators 1.0.1
See the version list below for details.
dotnet add package ServiceLevelIndicators --version 1.0.1
NuGet\Install-Package ServiceLevelIndicators -Version 1.0.1
<PackageReference Include="ServiceLevelIndicators" Version="1.0.1" />
paket add ServiceLevelIndicators --version 1.0.1
#r "nuget: ServiceLevelIndicators, 1.0.1"
// Install ServiceLevelIndicators as a Cake Addin #addin nuget:?package=ServiceLevelIndicators&version=1.0.1 // Install ServiceLevelIndicators as a Cake Tool #tool nuget:?package=ServiceLevelIndicators&version=1.0.1
ArtifactType: nupkg. Language: csharp. Tags: SLI, OpenTelemetry, Metrics.
Service Level Indicators
Service level indicators (SLIs) are metrics used to measure the performance of a service. They are typically used in the context of service level agreements (SLAs), which are contracts between a service provider and its customers that define the expected level of service. SLIs are used to track the actual performance of the service against the agreed upon SLA.
There are many different types of SLIs that can be used to measure the performance of a service. Some common examples include:
- Availability: This measures the percentage of time that a service is available and functioning properly.
- Response time: This measures the amount of time it takes for a service to respond to a request.
- Throughput: This measures the amount of work that a service can handle in a given period of time.
- Error rate: This measures the percentage of requests that result in errors.
SLIs are important because they provide a way to objectively measure the performance of a service. By tracking SLIs over time, service providers can identify trends and make improvements to the service to ensure that it meets the needs of its customers.
Service Level Indicator Library
Service Level Indicator library will help emit latency metrics for each API operation to help monitor the service performance over time. The metrics is emitted via standard .NET Meter Class.
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 of where the service running. eg. Public cloud, West US 3 region.
Operation - The name of the operation. Defaults to
AttributeRouteInfo.Template
information likeGET Weatherforecast
.activity.status_code - The activity status code tells if the operation succeeded or failed. ActivityStatusCode.
If ServiceLevelIndicators.Asp is utilized, the activity status code will be designated as "Ok" when the http response status code is in the 2xx range, "Error" when the http response status code is in the 5xx range, and "Unset" for any other status code.
http.response.status_code - The http status code is added when ServiceLevelIndicators.Asp package is used.
http.request.method - The http request method (GET, POST, etc) is added when ServiceLevelIndicators.Asp package is used.
http.api.version - API Version is added when ServiceLevelIndicators.Asp.Versioning is used in conjuction with API Versioning package.
Usage
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 => { // Override with calling service id or customer Id. options.CustomerResourceId = ServiceLevelIndicator.CreateCustomerResourceId(serviceId); options.LocationId = ServiceLevelIndicator.CreateLocationId("public", "westus2"); });
Add the middleware to the pipeline.
If API versioning is used and want http.api.version as a SLI metric dimension, Use app.UseServiceLevelIndicatorWithApiVersioning();
Otherwise, app.UseServiceLevelIndicator();
Customizations
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/Action1
To override the default operation name add the attribute
[ServiceLevelIndicator]
and specify the operation name.Example.
[HttpGet("MyAction2")] [ServiceLevelIndicator(Operation = "MyOperation")] public IEnumerable<WeatherForecast> GetOperation() => GetWeather();
To override the
CustomerResourceId
within an API method, mark the parameter with the attribute[CustomerResourceId]
[HttpGet("get-by-zip-code/{zipCode}")] public IEnumerable<WeatherForecast> GetByZipcode([CustomerResourceId] string zipCode) => GetWeather();
Or use
GetMeasuredOperationLatency
extension method.[HttpGet("{customerResourceId}")] public IEnumerable<WeatherForecast> Get(string customerResourceId) { HttpContext.GetMeasuredOperationLatency().CustomerResourceId = customerResourceId; return 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 void StoreItem(MyDomainEvent domainEvent) { 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"
,http.response.status.code = 200
,LocationId = "ms-loc://az/public/westus2"
,activity.status_code = 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 |