Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename MeasuredOperationLatency to MeasuredOperation #36

Merged
merged 1 commit into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
22 changes: 11 additions & 11 deletions ServiceLevelIndicators.Asp/src/HttpContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,38 @@
public static class HttpContextExtensions
{
/// <summary>
/// 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.
/// </summary>
/// <param name="context"></param>
/// <returns>MeasuredOperationLatency for the current API method.</returns>
/// <returns>MeasuredOperation for the current API method.</returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="InvalidOperationException">If the route does not emit SLI information and therefore MeasuredOperationLatency does not exist.</exception>
public static MeasuredOperationLatency GetMeasuredOperationLatency(this HttpContext context)
/// <exception cref="InvalidOperationException">If the route does not emit SLI information and therefore MeasuredOperation does not exist.</exception>
public static MeasuredOperation GetMeasuredOperation(this HttpContext context)
{
ArgumentNullException.ThrowIfNull(context);

return context.Features.GetRequiredFeature<IServiceLevelIndicatorFeature>().MeasuredOperationLatency;
return context.Features.GetRequiredFeature<IServiceLevelIndicatorFeature>().MeasuredOperation;
}

/// <summary>
/// Gets the MeasuredOperationLatency from the IServiceLevelIndicatorFeature.
/// Gets the MeasuredOperation from the IServiceLevelIndicatorFeature.
/// </summary>
/// <param name="context"></param>
/// <param name="measuredOperationLatency"></param>
/// <returns>true if MeasuredOperationLatency exists.</returns>
/// <param name="measuredOperation"></param>
/// <returns>true if MeasuredOperation exists.</returns>
/// <exception cref="ArgumentNullException"></exception>
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<IServiceLevelIndicatorFeature>() is IServiceLevelIndicatorFeature feature)
{
measuredOperationLatency = feature.MeasuredOperationLatency;
measuredOperation = feature.MeasuredOperation;
return true;
}

measuredOperationLatency = null;
measuredOperation = null;
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
/// </summary>
public interface IServiceLevelIndicatorFeature
{
MeasuredOperationLatency MeasuredOperationLatency { get; }
MeasuredOperation MeasuredOperation { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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<IServiceLevelIndicatorFeature>() != null)
throw new InvalidOperationException($"Another instance of {nameof(ServiceLevelIndicatorFeature)} already exists. Only one instance of {nameof(ServiceLevelIndicatorMiddleware)} can be configured for an application.");
Expand Down
4 changes: 2 additions & 2 deletions ServiceLevelIndicators.Asp/src/WebEnrichmentContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();

Expand All @@ -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<long>(OnMeasurementRecorded);
_meterListener.Start();
Expand Down
6 changes: 3 additions & 3 deletions ServiceLevelIndicators.Asp/tests/TestController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
using System.Collections.Generic;
using System.Diagnostics;

public class MeasuredOperationLatency : IDisposable
public class MeasuredOperation : IDisposable
{
private bool _disposed;
private readonly ServiceLevelIndicator _serviceLevelIndicator;
private readonly Stopwatch _stopWatch;
private ActivityStatusCode _activityStatusCode = ActivityStatusCode.Unset;
private readonly object _disposeLock = new();

public MeasuredOperationLatency(ServiceLevelIndicator serviceLevelIndicator, string operation, params KeyValuePair<string, object?>[] attributes) :
public MeasuredOperation(ServiceLevelIndicator serviceLevelIndicator, string operation, params KeyValuePair<string, object?>[] attributes) :
this(serviceLevelIndicator, operation, serviceLevelIndicator.ServiceLevelIndicatorOptions.CustomerResourceId, attributes)
{ }

public MeasuredOperationLatency(ServiceLevelIndicator serviceLevelIndicator, string operation, string customerResourceId, params KeyValuePair<string, object?>[] attributes)
public MeasuredOperation(ServiceLevelIndicator serviceLevelIndicator, string operation, string customerResourceId, params KeyValuePair<string, object?>[] attributes)
{
_serviceLevelIndicator = serviceLevelIndicator;
Operation = operation;
Expand Down Expand Up @@ -46,7 +46,7 @@ protected virtual void Dispose(bool disposing)
_stopWatch.Stop();
var elapsedTime = _stopWatch.ElapsedMilliseconds;
Attributes.Add(new KeyValuePair<string, object?>(_serviceLevelIndicator.ServiceLevelIndicatorOptions.ActivityStatusCodeAttributeName, _activityStatusCode.ToString()));
_serviceLevelIndicator.RecordLatency(Operation, CustomerResourceId, elapsedTime, Attributes.ToArray());
_serviceLevelIndicator.Record(Operation, CustomerResourceId, elapsedTime, Attributes.ToArray());
}

_disposed = true;
Expand Down
8 changes: 4 additions & 4 deletions ServiceLevelIndicators/src/ServiceLevelIndicator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public ServiceLevelIndicator(IOptions<ServiceLevelIndicatorOptions> options)
_responseLatencyHistogram = ServiceLevelIndicatorOptions.Meter.CreateHistogram<long>(ServiceLevelIndicatorOptions.InstrumentName, "ms");
}

public void RecordLatency(string operation, long elapsedTime, params KeyValuePair<string, object?>[] attributes) =>
RecordLatency(operation, ServiceLevelIndicatorOptions.CustomerResourceId, elapsedTime, attributes);
public void Record(string operation, long elapsedTime, params KeyValuePair<string, object?>[] attributes) =>
Record(operation, ServiceLevelIndicatorOptions.CustomerResourceId, elapsedTime, attributes);

public void RecordLatency(string operation, string customerResourseId, long elapsedTime, params KeyValuePair<string, object?>[] attributes)
public void Record(string operation, string customerResourseId, long elapsedTime, params KeyValuePair<string, object?>[] attributes)
{
var tagList = new TagList
{
Expand All @@ -38,7 +38,7 @@ public void RecordLatency(string operation, string customerResourseId, long elap
_responseLatencyHistogram.Record(elapsedTime, tagList);
}

public MeasuredOperationLatency StartLatencyMeasureOperation(string operation, params KeyValuePair<string, object?>[] attributes) => new(this, operation, attributes);
public MeasuredOperation StartMeasuring(string operation, params KeyValuePair<string, object?>[] attributes) => new(this, operation, attributes);

public static string CreateCustomerResourceId(Guid serviceId)
{
Expand Down
8 changes: 4 additions & 4 deletions ServiceLevelIndicators/tests/ServiceLevelIndicatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public ServiceLevelIndicatorTests(ITestOutputHelper output)


[Fact]
public void Record_latency()
public void Record()
{
// Arrange
var customerResourceId = "TestResourceId";
Expand All @@ -62,7 +62,7 @@ public void Record_latency()
};

// Act
serviceLevelIndicator.RecordLatency(operation, elapsedTime, attributes);
serviceLevelIndicator.Record(operation, elapsedTime, attributes);

// Assert
_expectedTags =
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -139,7 +139,7 @@ public void Customize_instrument_name()
var elapsedTime = 30;

// Act
serviceLevelIndicator.RecordLatency(operation, elapsedTime);
serviceLevelIndicator.Record(operation, elapsedTime);

// Assert
_expectedTags =
Expand Down
Loading