Skip to content
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
5 changes: 5 additions & 0 deletions playground/Stress/Stress.ApiService/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@

app.Lifetime.ApplicationStarted.Register(ConsoleStresser.Stress);

app.Lifetime.ApplicationStarted.Register(() =>
{
_ = app.Services.GetRequiredService<TestMetrics>();
});

app.MapGet("/", () => "Hello world");

app.MapGet("/write-console", () =>
Expand Down
37 changes: 32 additions & 5 deletions playground/Stress/Stress.ApiService/TestMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,42 @@ public class TestMetrics : IDisposable

public TestMetrics()
{
_meter = new Meter(MeterName, "1.0.0", new[]
{
_meter = new Meter(MeterName, "1.0.0",
[
new KeyValuePair<string, object?>("meter-tag", Guid.NewGuid().ToString())
});
]);

_counter = _meter.CreateCounter<int>("test-counter", unit: null, description: null, tags: new[]
{
_counter = _meter.CreateCounter<int>("test-counter", unit: null, description: null, tags:
[
new KeyValuePair<string, object?>("instrument-tag", Guid.NewGuid().ToString())
]);

var uploadSpeed = new List<double>();

Task.Run(async () =>
{
while (true)
{
lock (uploadSpeed)
{
uploadSpeed.Add(Random.Shared.Next(5, 10));
}
await Task.Delay(1000);
}
});

_meter.CreateObservableGauge<double>("observable-gauge", () =>
{
lock (uploadSpeed)
{
var sum = 0d;
for (var i = 0; i < uploadSpeed.Count; i++)
{
sum += uploadSpeed[i];
}
return new Measurement<double>(sum / uploadSpeed.Count);
}
}, unit: "By/s");
}

public void IncrementCounter(int value, in TagList tags)
Expand Down
8 changes: 6 additions & 2 deletions src/Aspire.Dashboard/Model/DefaultInstrumentUnitResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@ public string ResolveDisplayedUnit(OtlpInstrumentSummary instrument, bool titleC
{
if (!string.IsNullOrEmpty(instrument.Unit))
{
var unit = OtlpUnits.GetUnit(instrument.Unit.TrimStart('{').TrimEnd('}'));
if (pluralize)
var (unit, isRateUnit) = OtlpUnits.GetUnit(instrument.Unit.TrimStart('{').TrimEnd('}'));

// Don't pluralize rate units, e.g. We want "Bytes per second", not "Bytes per seconds".
if (pluralize && !isRateUnit)
{
unit = unit.Pluralize();
}

if (titleCase)
{
unit = unit.Titleize();
}

return unit;
}

Expand Down
8 changes: 5 additions & 3 deletions src/Aspire.Dashboard/Otlp/Model/OtlpUnits.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ namespace Aspire.Dashboard.Otlp.Model;

public static class OtlpUnits
{
public static string GetUnit(string unit)
public static (string Unit, bool IsRateUnit) GetUnit(string unit)
{
// Dropping the portions of the Unit within brackets (e.g. {packet}). Brackets MUST NOT be included in the resulting unit. A "count of foo" is considered unitless in Prometheus.
// https://github.com/open-telemetry/opentelemetry-specification/blob/b2f923fb1650dde1f061507908b834035506a796/specification/compatibility/prometheus_and_openmetrics.md#L238
var updatedUnit = RemoveAnnotations(unit);

var isRateUnit = false;
// Converting "foo/bar" to "foo_per_bar".
// https://github.com/open-telemetry/opentelemetry-specification/blob/b2f923fb1650dde1f061507908b834035506a796/specification/compatibility/prometheus_and_openmetrics.md#L240C3-L240C41
if (TryProcessRateUnits(updatedUnit, out var updatedPerUnit))
{
updatedUnit = updatedPerUnit;
isRateUnit = true;
}
else
{
Expand All @@ -27,7 +29,7 @@ public static string GetUnit(string unit)
updatedUnit = MapUnit(updatedUnit.AsSpan());
}

return updatedUnit;
return (updatedUnit, isRateUnit);
}

private static bool TryProcessRateUnits(string updatedUnit, [NotNullWhen(true)] out string? updatedPerUnit)
Expand All @@ -44,7 +46,7 @@ private static bool TryProcessRateUnits(string updatedUnit, [NotNullWhen(true)]
return false;
}

updatedPerUnit = MapUnit(updatedUnit.AsSpan(0, i)) + " per" + MapPerUnit(updatedUnit.AsSpan(i + 1, updatedUnit.Length - i - 1));
updatedPerUnit = MapUnit(updatedUnit.AsSpan(0, i)) + " per " + MapPerUnit(updatedUnit.AsSpan(i + 1, updatedUnit.Length - i - 1));
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Aspire.Dashboard.Configuration;
using Aspire.Dashboard.Model;
using Aspire.Dashboard.Otlp.Model;
using Aspire.Dashboard.Resources;
using Microsoft.Extensions.Localization;
using OpenTelemetry.Proto.Common.V1;
using Xunit;

namespace Aspire.Dashboard.Tests.Model;

public sealed class DefaultInstrumentUnitResolverTests
{
[Theory]
[InlineData("By/s", "instrument_name", "Bytes Per Second")]
[InlineData("connection", "instrument_name", "Connections")]
[InlineData("{connection}", "instrument_name", "Connections")]
[InlineData("", "instrument_name", "Localized:PlotlyChartValue")]
[InlineData("", "instrument_name.count", "Localized:PlotlyChartCount")]
[InlineData("", "instrument_name.length", "Localized:PlotlyChartLength")]
public void ResolveDisplayedUnit(string unit, string name, string expected)
{
// Arrange
var localizer = new TestStringLocalizer<ControlsStrings>();
var resolver = new DefaultInstrumentUnitResolver(localizer);

var otlpInstrumentSummary = new OtlpInstrumentSummary
{
Description = "Description!",
Name = name,
Parent = new OtlpMeter(new InstrumentationScope { Name = "meter_name" }, new TelemetryLimitOptions()),
Type = OtlpInstrumentType.Gauge,
Unit = unit
};

// Act
var result = resolver.ResolveDisplayedUnit(otlpInstrumentSummary, titleCase: true, pluralize: true);

// Assert
Assert.Equal(expected, result);
}

private sealed class TestStringLocalizer<T> : IStringLocalizer<T>
{
public LocalizedString this[string name] => new LocalizedString(name, $"Localized:{name}");
public LocalizedString this[string name, params object[] arguments] => new LocalizedString(name, $"Localized:{name}");

public IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures) => [];
}
}