From 664e701f708975fa36f3e5fee3dd05a4b2c474a2 Mon Sep 17 00:00:00 2001 From: Xavier Date: Fri, 17 May 2024 15:20:36 -0700 Subject: [PATCH] Rename StartLatencyMeasureOperation to StartMeasuring RecordLatency to Record --- README.md | 2 +- .../src/HttpContextExtensions.cs | 22 +++++++++---------- .../src/IServiceLevelIndicatorFeature.cs | 2 +- .../src/ServiceLevelIndicatorFeature.cs | 4 ++-- .../src/ServiceLevelIndicatorMiddleware.cs | 8 +++---- .../src/WebEnrichmentContext.cs | 4 ++-- .../tests/ServiceLevelIndicatorAspTests.cs | 6 ++--- .../tests/TestController.cs | 6 ++--- ...erationLatency.cs => MeasuredOperation.cs} | 8 +++---- .../src/ServiceLevelIndicator.cs | 8 +++---- .../tests/ServiceLevelIndicatorTests.cs | 8 +++---- 11 files changed, 39 insertions(+), 39 deletions(-) rename ServiceLevelIndicators/src/{MeasuredOperationLatency.cs => MeasuredOperation.cs} (79%) diff --git a/README.md b/README.md index 2bfa15f..cd25a75 100644 --- a/README.md +++ b/README.md @@ -195,7 +195,7 @@ Example. async Task MeasureCodeBlock(ServiceLevelIndicator serviceLevelIndicator) { - using var measuredOperation = serviceLevelIndicator.StartLatencyMeasureOperation("OperationName"); + using var measuredOperation = serviceLevelIndicator.StartMeasuring("OperationName"); // Do Work. measuredOperation.SetActivityStatusCode(System.Diagnostics.ActivityStatusCode.Ok); } diff --git a/ServiceLevelIndicators.Asp/src/HttpContextExtensions.cs b/ServiceLevelIndicators.Asp/src/HttpContextExtensions.cs index 8ac0a1c..2220c44 100644 --- a/ServiceLevelIndicators.Asp/src/HttpContextExtensions.cs +++ b/ServiceLevelIndicators.Asp/src/HttpContextExtensions.cs @@ -7,38 +7,38 @@ public static class HttpContextExtensions { /// - /// Gets the MeasuredOperationLatency from the IServiceLevelIndicatorFeature. + /// Gets the MeasuredOperation from the IServiceLevelIndicatorFeature. /// The method will throw an exception if the route is not configured to emit SLI metrics. /// /// - /// MeasuredOperationLatency for the current API method. + /// MeasuredOperation for the current API method. /// - /// If the route does not emit SLI information and therefore MeasuredOperationLatency does not exist. - public static MeasuredOperationLatency GetMeasuredOperationLatency(this HttpContext context) + /// If the route does not emit SLI information and therefore MeasuredOperation does not exist. + public static MeasuredOperation GetMeasuredOperation(this HttpContext context) { ArgumentNullException.ThrowIfNull(context); - return context.Features.GetRequiredFeature().MeasuredOperationLatency; + return context.Features.GetRequiredFeature().MeasuredOperation; } /// - /// Gets the MeasuredOperationLatency from the IServiceLevelIndicatorFeature. + /// Gets the MeasuredOperation from the IServiceLevelIndicatorFeature. /// /// - /// - /// true if MeasuredOperationLatency exists. + /// + /// true if MeasuredOperation exists. /// - public static bool TryGetMeasuredOperationLatency(this HttpContext context, [MaybeNullWhen(false)] out MeasuredOperationLatency measuredOperationLatency) + public static bool TryGetMeasuredOperation(this HttpContext context, [MaybeNullWhen(false)] out MeasuredOperation measuredOperation) { ArgumentNullException.ThrowIfNull(context); if (context.Features.Get() is IServiceLevelIndicatorFeature feature) { - measuredOperationLatency = feature.MeasuredOperationLatency; + measuredOperation = feature.MeasuredOperation; return true; } - measuredOperationLatency = null; + measuredOperation = null; return false; } } diff --git a/ServiceLevelIndicators.Asp/src/IServiceLevelIndicatorFeature.cs b/ServiceLevelIndicators.Asp/src/IServiceLevelIndicatorFeature.cs index 625f2c0..3151336 100644 --- a/ServiceLevelIndicators.Asp/src/IServiceLevelIndicatorFeature.cs +++ b/ServiceLevelIndicators.Asp/src/IServiceLevelIndicatorFeature.cs @@ -4,5 +4,5 @@ /// public interface IServiceLevelIndicatorFeature { - MeasuredOperationLatency MeasuredOperationLatency { get; } + MeasuredOperation MeasuredOperation { get; } } diff --git a/ServiceLevelIndicators.Asp/src/ServiceLevelIndicatorFeature.cs b/ServiceLevelIndicators.Asp/src/ServiceLevelIndicatorFeature.cs index 5589855..7c3533f 100644 --- a/ServiceLevelIndicators.Asp/src/ServiceLevelIndicatorFeature.cs +++ b/ServiceLevelIndicators.Asp/src/ServiceLevelIndicatorFeature.cs @@ -2,7 +2,7 @@ internal sealed class ServiceLevelIndicatorFeature : IServiceLevelIndicatorFeature { - public ServiceLevelIndicatorFeature(MeasuredOperationLatency measureOperationLatency) => MeasuredOperationLatency = measureOperationLatency; + public ServiceLevelIndicatorFeature(MeasuredOperation measureOperation) => MeasuredOperation = measureOperation; - public MeasuredOperationLatency MeasuredOperationLatency { get; } + public MeasuredOperation MeasuredOperation { get; } } diff --git a/ServiceLevelIndicators.Asp/src/ServiceLevelIndicatorMiddleware.cs b/ServiceLevelIndicators.Asp/src/ServiceLevelIndicatorMiddleware.cs index 1fdac3e..d20ade5 100644 --- a/ServiceLevelIndicators.Asp/src/ServiceLevelIndicatorMiddleware.cs +++ b/ServiceLevelIndicators.Asp/src/ServiceLevelIndicatorMiddleware.cs @@ -32,7 +32,7 @@ public async Task InvokeAsync(HttpContext context) var operation = GetOperation(context, metadata); var attributes = GetMeasuredAttributes(context, metadata); - using var measuredOperation = _serviceLevelIndicator.StartLatencyMeasureOperation(operation, attributes); + using var measuredOperation = _serviceLevelIndicator.StartMeasuring(operation, attributes); SetCustomerResourceIdFromAttribute(context, metadata, measuredOperation); AddSliFeatureToHttpContext(context, measuredOperation); await _next(context); @@ -47,14 +47,14 @@ public async Task InvokeAsync(HttpContext context) } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void SetCustomerResourceIdFromAttribute(HttpContext context, EndpointMetadataCollection metadata, MeasuredOperationLatency measuredOperation) + private static void SetCustomerResourceIdFromAttribute(HttpContext context, EndpointMetadataCollection metadata, MeasuredOperation measuredOperation) { var customerResourceId = GetCustomerResourceIdAttributes(context, metadata); if (customerResourceId is not null) measuredOperation.CustomerResourceId = customerResourceId; } - private static void UpdateOperationWithResponseStatus(HttpContext context, MeasuredOperationLatency measuredOperation) + private static void UpdateOperationWithResponseStatus(HttpContext context, MeasuredOperation measuredOperation) { var statusCode = context.Response.StatusCode; measuredOperation.AddAttribute("http.response.status.code", statusCode); @@ -129,7 +129,7 @@ private static string GetOperation(HttpContext context, EndpointMetadataCollecti return attributes; } - private void AddSliFeatureToHttpContext(HttpContext context, MeasuredOperationLatency measuredOperation) + private void AddSliFeatureToHttpContext(HttpContext context, MeasuredOperation measuredOperation) { if (context.Features.Get() != null) throw new InvalidOperationException($"Another instance of {nameof(ServiceLevelIndicatorFeature)} already exists. Only one instance of {nameof(ServiceLevelIndicatorMiddleware)} can be configured for an application."); diff --git a/ServiceLevelIndicators.Asp/src/WebEnrichmentContext.cs b/ServiceLevelIndicators.Asp/src/WebEnrichmentContext.cs index 239c44e..973e7e1 100644 --- a/ServiceLevelIndicators.Asp/src/WebEnrichmentContext.cs +++ b/ServiceLevelIndicators.Asp/src/WebEnrichmentContext.cs @@ -3,10 +3,10 @@ public class WebEnrichmentContext : IEnrichmentContext { - private readonly MeasuredOperationLatency _operation; + private readonly MeasuredOperation _operation; public HttpContext HttpContext { get; } - public WebEnrichmentContext(MeasuredOperationLatency operation, HttpContext httpContext) + public WebEnrichmentContext(MeasuredOperation operation, HttpContext httpContext) { _operation = operation; HttpContext = httpContext; diff --git a/ServiceLevelIndicators.Asp/tests/ServiceLevelIndicatorAspTests.cs b/ServiceLevelIndicators.Asp/tests/ServiceLevelIndicatorAspTests.cs index c5fe4c4..5361ce9 100644 --- a/ServiceLevelIndicators.Asp/tests/ServiceLevelIndicatorAspTests.cs +++ b/ServiceLevelIndicators.Asp/tests/ServiceLevelIndicatorAspTests.cs @@ -285,7 +285,7 @@ void OnMeasurementRecorded(Instrument instrument, long measurement, ReadOnlySpan } [Fact] - public async Task GetMeasuredOperationLatency_will_throw_if_route_does_not_emit_SLI() + public async Task GetMeasuredOperation_will_throw_if_route_does_not_emit_SLI() { using var host = await TestHostBuilder.CreateHostWithoutSli(); @@ -298,7 +298,7 @@ public async Task GetMeasuredOperationLatency_will_throw_if_route_does_not_emit_ } [Fact] - public async Task TryGetMeasuredOperationLatency_will_return_false_if_route_does_not_emit_SLI() + public async Task TryGetMeasuredOperation_will_return_false_if_route_does_not_emit_SLI() { using var host = await TestHostBuilder.CreateHostWithoutSli(); @@ -310,7 +310,7 @@ public async Task TryGetMeasuredOperationLatency_will_return_false_if_route_does } [Fact] - public async Task TryGetMeasuredOperationLatency_will_return_true_if_route_emits_SLI() + public async Task TryGetMeasuredOperation_will_return_true_if_route_emits_SLI() { _meterListener.SetMeasurementEventCallback(OnMeasurementRecorded); _meterListener.Start(); diff --git a/ServiceLevelIndicators.Asp/tests/TestController.cs b/ServiceLevelIndicators.Asp/tests/TestController.cs index 1c0afd7..5ce9d99 100644 --- a/ServiceLevelIndicators.Asp/tests/TestController.cs +++ b/ServiceLevelIndicators.Asp/tests/TestController.cs @@ -24,16 +24,16 @@ public class TestController : ControllerBase [HttpGet("custom_attribute/{value}")] public IActionResult AddCustomAttribute(string value) { - HttpContext.GetMeasuredOperationLatency().AddAttribute("CustomAttribute", value); + HttpContext.GetMeasuredOperation().AddAttribute("CustomAttribute", value); return Ok(value); } [HttpGet("try_get_measured_operation_latency/{value}")] public IActionResult TryGetMeasuredOperationLatency(string value) { - if (HttpContext.TryGetMeasuredOperationLatency(out var measuredOperationLatency)) + if (HttpContext.TryGetMeasuredOperation(out var measuredOperation)) { - measuredOperationLatency.AddAttribute("CustomAttribute", value); + measuredOperation.AddAttribute("CustomAttribute", value); return Ok(true); } return Ok(false); diff --git a/ServiceLevelIndicators/src/MeasuredOperationLatency.cs b/ServiceLevelIndicators/src/MeasuredOperation.cs similarity index 79% rename from ServiceLevelIndicators/src/MeasuredOperationLatency.cs rename to ServiceLevelIndicators/src/MeasuredOperation.cs index a5b71e8..1f3304b 100644 --- a/ServiceLevelIndicators/src/MeasuredOperationLatency.cs +++ b/ServiceLevelIndicators/src/MeasuredOperation.cs @@ -4,7 +4,7 @@ using System.Collections.Generic; using System.Diagnostics; -public class MeasuredOperationLatency : IDisposable +public class MeasuredOperation : IDisposable { private bool _disposed; private readonly ServiceLevelIndicator _serviceLevelIndicator; @@ -12,11 +12,11 @@ public class MeasuredOperationLatency : IDisposable private ActivityStatusCode _activityStatusCode = ActivityStatusCode.Unset; private readonly object _disposeLock = new(); - public MeasuredOperationLatency(ServiceLevelIndicator serviceLevelIndicator, string operation, params KeyValuePair[] attributes) : + public MeasuredOperation(ServiceLevelIndicator serviceLevelIndicator, string operation, params KeyValuePair[] attributes) : this(serviceLevelIndicator, operation, serviceLevelIndicator.ServiceLevelIndicatorOptions.CustomerResourceId, attributes) { } - public MeasuredOperationLatency(ServiceLevelIndicator serviceLevelIndicator, string operation, string customerResourceId, params KeyValuePair[] attributes) + public MeasuredOperation(ServiceLevelIndicator serviceLevelIndicator, string operation, string customerResourceId, params KeyValuePair[] attributes) { _serviceLevelIndicator = serviceLevelIndicator; Operation = operation; @@ -46,7 +46,7 @@ protected virtual void Dispose(bool disposing) _stopWatch.Stop(); var elapsedTime = _stopWatch.ElapsedMilliseconds; Attributes.Add(new KeyValuePair(_serviceLevelIndicator.ServiceLevelIndicatorOptions.ActivityStatusCodeAttributeName, _activityStatusCode.ToString())); - _serviceLevelIndicator.RecordLatency(Operation, CustomerResourceId, elapsedTime, Attributes.ToArray()); + _serviceLevelIndicator.Record(Operation, CustomerResourceId, elapsedTime, Attributes.ToArray()); } _disposed = true; diff --git a/ServiceLevelIndicators/src/ServiceLevelIndicator.cs b/ServiceLevelIndicators/src/ServiceLevelIndicator.cs index 2d9795d..1bb18b7 100644 --- a/ServiceLevelIndicators/src/ServiceLevelIndicator.cs +++ b/ServiceLevelIndicators/src/ServiceLevelIndicator.cs @@ -20,10 +20,10 @@ public ServiceLevelIndicator(IOptions options) _responseLatencyHistogram = ServiceLevelIndicatorOptions.Meter.CreateHistogram(ServiceLevelIndicatorOptions.InstrumentName, "ms"); } - public void RecordLatency(string operation, long elapsedTime, params KeyValuePair[] attributes) => - RecordLatency(operation, ServiceLevelIndicatorOptions.CustomerResourceId, elapsedTime, attributes); + public void Record(string operation, long elapsedTime, params KeyValuePair[] attributes) => + Record(operation, ServiceLevelIndicatorOptions.CustomerResourceId, elapsedTime, attributes); - public void RecordLatency(string operation, string customerResourseId, long elapsedTime, params KeyValuePair[] attributes) + public void Record(string operation, string customerResourseId, long elapsedTime, params KeyValuePair[] attributes) { var tagList = new TagList { @@ -38,7 +38,7 @@ public void RecordLatency(string operation, string customerResourseId, long elap _responseLatencyHistogram.Record(elapsedTime, tagList); } - public MeasuredOperationLatency StartLatencyMeasureOperation(string operation, params KeyValuePair[] attributes) => new(this, operation, attributes); + public MeasuredOperation StartMeasuring(string operation, params KeyValuePair[] attributes) => new(this, operation, attributes); public static string CreateCustomerResourceId(Guid serviceId) { diff --git a/ServiceLevelIndicators/tests/ServiceLevelIndicatorTests.cs b/ServiceLevelIndicators/tests/ServiceLevelIndicatorTests.cs index 3fcbde8..c0c3bf2 100644 --- a/ServiceLevelIndicators/tests/ServiceLevelIndicatorTests.cs +++ b/ServiceLevelIndicators/tests/ServiceLevelIndicatorTests.cs @@ -39,7 +39,7 @@ public ServiceLevelIndicatorTests(ITestOutputHelper output) [Fact] - public void Record_latency() + public void Record() { // Arrange var customerResourceId = "TestResourceId"; @@ -62,7 +62,7 @@ public void Record_latency() }; // Act - serviceLevelIndicator.RecordLatency(operation, elapsedTime, attributes); + serviceLevelIndicator.Record(operation, elapsedTime, attributes); // Assert _expectedTags = @@ -111,7 +111,7 @@ public async Task Will_measure_code_block() async Task MeasureCodeBlock(ServiceLevelIndicator serviceLevelIndicator) { - using var measuredOperation = serviceLevelIndicator.StartLatencyMeasureOperation("SleepWorker"); + using var measuredOperation = serviceLevelIndicator.StartMeasuring("SleepWorker"); await Task.Delay(sleepTime); measuredOperation.SetActivityStatusCode(System.Diagnostics.ActivityStatusCode.Ok); } @@ -139,7 +139,7 @@ public void Customize_instrument_name() var elapsedTime = 30; // Act - serviceLevelIndicator.RecordLatency(operation, elapsedTime); + serviceLevelIndicator.Record(operation, elapsedTime); // Assert _expectedTags =