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

Aligning metrics with OTel semantic conventions #4482

Merged
merged 27 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
151488a
Aligning metrics with OTel semantic convention
xakep139 Sep 27, 2023
0e76509
Merge branch 'release/8.0' into xakep139/4432-metric-names-convention
xakep139 Oct 4, 2023
eb3189c
fix the build
xakep139 Oct 4, 2023
3ca7aa2
convert WindowsCounters
xakep139 Oct 4, 2023
bd7a57c
resilience enrichment tags
xakep139 Oct 4, 2023
7cab4c8
again Windows counters
xakep139 Oct 4, 2023
98e3f0e
Update src/Libraries/Microsoft.AspNetCore.HeaderParsing/Metric.cs
xakep139 Oct 5, 2023
2967ec9
fixes + improve mutation score
xakep139 Oct 5, 2023
2fc604f
CR + fixes
xakep139 Oct 6, 2023
fa01223
remove redundant dimension
xakep139 Oct 6, 2023
3a66a5d
CR
xakep139 Oct 6, 2023
fa88043
Update src/Libraries/Microsoft.Extensions.Diagnostics.Testing/Metrics…
xakep139 Oct 6, 2023
3613c4d
fixes
xakep139 Oct 6, 2023
717678c
minor fix
xakep139 Oct 6, 2023
bca9575
Update src/Libraries/Microsoft.Extensions.Diagnostics.ResourceMonitor…
xakep139 Oct 9, 2023
1887075
CR (Reiley)
xakep139 Oct 9, 2023
d036ccc
Merge branch 'release/8.0' into xakep139/4432-metric-names-convention
xakep139 Oct 10, 2023
43a73fe
CR (James)
xakep139 Oct 10, 2023
e10ccda
fixes in tests
xakep139 Oct 10, 2023
a8af544
fix tests
xakep139 Oct 10, 2023
fc8c3e0
CR
xakep139 Oct 17, 2023
fac0bb4
Update src/Libraries/Microsoft.Extensions.Diagnostics.ResourceMonitor…
xakep139 Oct 17, 2023
ddd86c0
CR
xakep139 Oct 18, 2023
dde8e31
Merge branch 'release/8.0' into xakep139/4432-metric-names-convention
xakep139 Oct 23, 2023
c61c8d3
align Resilience attribute names
xakep139 Oct 23, 2023
0d244ba
fix test coverage
xakep139 Oct 23, 2023
42ac60e
Update src/Libraries/Microsoft.Extensions.Diagnostics.ResourceMonitor…
xakep139 Oct 23, 2023
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 src/Generators/Microsoft.Gen.Metrics/Emitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ private void GenTagList(MetricMethod metricMethod)
foreach (var tagName in metricMethod.TagKeys)
{
var paramName = GetSanitizedParamName(tagName);
OutLn($"new global::System.Collections.Generic.KeyValuePair<string, object?>(\"{paramName}\", {paramName}),");
OutLn($"new global::System.Collections.Generic.KeyValuePair<string, object?>(\"{tagName}\", {paramName}),");
xakep139 marked this conversation as resolved.
Show resolved Hide resolved
}
}
else
Expand Down
4 changes: 2 additions & 2 deletions src/Libraries/Microsoft.AspNetCore.HeaderParsing/Metric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ namespace Microsoft.AspNetCore.HeaderParsing;

internal static partial class Metric
{
[Counter("HeaderName", "Kind", Name = @"HeaderParsing.ParsingErrors")]
[Counter("aspnetcore.header_parsing.header.name", "error.type", Name = "aspnetcore.header_parsing.parse_errors")]
public static partial ParsingErrorCounter CreateParsingErrorCounter(Meter meter);

[Counter("HeaderName", "Type", Name = @"HeaderParsing.CacheAccess")]
[Counter("aspnetcore.header_parsing.header.name", "aspnetcore.header_parsing.cache_access.type", Name = "aspnetcore.header_parsing.cache_accesses")]
public static partial CacheAccessCounter CreateCacheAccessCounter(Meter meter);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.Metrics;
using System.Globalization;
using Microsoft.Extensions.Diagnostics.Metrics;
using Microsoft.Extensions.EnumStrings;

Expand All @@ -12,14 +11,14 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks;

internal static partial class Metric
{
[Counter("healthy", "status", Name = @"R9\\HealthCheck\\Report")]
[Counter("dotnet.health_check.status", Name = "dotnet.health_check.reports")]
public static partial HealthCheckReportCounter CreateHealthCheckReportCounter(Meter meter);

[Counter("name", "status", Name = @"R9\\HealthCheck\\UnhealthyHealthCheck")]
[Counter("dotnet.health_check.name", "dotnet.health_check.status", Name = "dotnet.health_check.unhealthy_checks")]
public static partial UnhealthyHealthCheckCounter CreateUnhealthyHealthCheckCounter(Meter meter);

public static void RecordMetric(this HealthCheckReportCounter counterMetric, bool isHealthy, HealthStatus status)
=> counterMetric.Add(1, isHealthy.ToString(CultureInfo.InvariantCulture), status.ToInvariantString());
public static void RecordMetric(this HealthCheckReportCounter counterMetric, HealthStatus status)
=> counterMetric.Add(1, status.ToInvariantString());

public static void RecordMetric(this UnhealthyHealthCheckCounter counterMetric, string name, HealthStatus status)
=> counterMetric.Add(1, name, status.ToInvariantString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ public Task PublishAsync(HealthReport report, CancellationToken cancellationToke
{
Log.Healthy(_logger, report.Status);
}

_metrics.HealthCheckReportCounter.RecordMetric(true, report.Status);
}
else
{
Expand Down Expand Up @@ -79,10 +77,10 @@ public Task PublishAsync(HealthReport report, CancellationToken cancellationToke

Log.Unhealthy(_logger, report.Status, stringBuilder);
PoolFactory.SharedStringBuilderPool.Return(stringBuilder);

_metrics.HealthCheckReportCounter.RecordMetric(false, report.Status);
}

_metrics.HealthCheckReportCounter.RecordMetric(report.Status);

return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ namespace Microsoft.Extensions.Diagnostics.ResourceMonitoring.Linux;

internal sealed class LinuxUtilizationProvider : ISnapshotProvider
{
private const float Hundred = 100.0f;
private const double One = 1.0;
private const long Hundred = 100L;

private readonly object _cpuLocker = new();
private readonly object _memoryLocker = new();
private readonly LinuxUtilizationParser _parser;
Expand Down Expand Up @@ -48,7 +50,7 @@ public LinuxUtilizationProvider(IOptions<ResourceMonitoringOptions> options, Lin
var hostCpus = _parser.GetHostCpuCount();
var availableCpus = _parser.GetCgroupLimitedCpus();

_scale = hostCpus * Hundred / availableCpus;
_scale = hostCpus / availableCpus;
_scaleForTrackerApi = hostCpus / availableCpus;

#pragma warning disable CA2000 // Dispose objects before losing scope
Expand All @@ -58,13 +60,13 @@ public LinuxUtilizationProvider(IOptions<ResourceMonitoringOptions> options, Lin
var meter = meterFactory.Create("Microsoft.Extensions.Diagnostics.ResourceMonitoring");
#pragma warning restore CA2000 // Dispose objects before losing scope

_ = meter.CreateObservableGauge(name: ResourceUtilizationCounters.CpuConsumptionPercentage, observeValue: CpuPercentage);
_ = meter.CreateObservableGauge(name: ResourceUtilizationCounters.MemoryConsumptionPercentage, observeValue: MemoryPercentage);
_ = meter.CreateObservableGauge(name: ResourceUtilizationInstruments.CpuUtilization, observeValue: CpuUtilization, unit: "1");
_ = meter.CreateObservableGauge(name: ResourceUtilizationInstruments.MemoryUtilization, observeValue: MemoryUtilization, unit: "1");
xakep139 marked this conversation as resolved.
Show resolved Hide resolved

Resources = new SystemResources(1, hostCpus, _totalMemoryInBytes, hostMemory);
}

public double CpuPercentage()
public double CpuUtilization()
{
var now = _timeProvider.GetUtcNow();
bool needUpdate = false;
Expand All @@ -91,7 +93,7 @@ public double CpuPercentage()

if (deltaHost > 0 && deltaCgroup > 0)
{
var percentage = Math.Min(Hundred, deltaCgroup / deltaHost * _scale);
var percentage = Math.Min(One, deltaCgroup / deltaHost * _scale);

_cpuPercentage = percentage;
_refreshAfterCpu = now.Add(_cpuRefreshInterval);
Expand All @@ -105,7 +107,7 @@ public double CpuPercentage()
return _cpuPercentage;
}

public double MemoryPercentage()
public double MemoryUtilization()
{
var now = _timeProvider.GetUtcNow();
bool needUpdate = false;
Expand All @@ -126,7 +128,7 @@ public double MemoryPercentage()
{
if (now >= _refreshAfterMemory)
{
var memoryPercentage = Math.Min(Hundred, (double)memoryUsed / _totalMemoryInBytes * Hundred);
var memoryPercentage = Math.Min(One, (double)memoryUsed / _totalMemoryInBytes);

_memoryPercentage = memoryPercentage;
_refreshAfterMemory = now.Add(_memoryRefreshInterval);
Expand All @@ -149,9 +151,9 @@ public Snapshot GetSnapshot()
var memoryUsed = _parser.GetMemoryUsageInBytes();

return new Snapshot(
totalTimeSinceStart: TimeSpan.FromTicks(hostTime / (long)Hundred),
totalTimeSinceStart: TimeSpan.FromTicks(hostTime / Hundred),
kernelTimeSinceStart: TimeSpan.Zero,
userTimeSinceStart: TimeSpan.FromTicks((long)(cgroupTime / (long)Hundred * _scaleForTrackerApi)),
userTimeSinceStart: TimeSpan.FromTicks((long)(cgroupTime / Hundred * _scaleForTrackerApi)),
memoryUsageInBytes: memoryUsed);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@

<ItemGroup>
<InternalsVisibleToDynamicProxyGenAssembly2 Include="*" />
<InternalsVisibleToDynamicProxyGenAssembly2 Remove="ResourceMonitoringBuilderExtensions.cs" />
<InternalsVisibleToDynamicProxyGenAssembly2 Remove="ResourceMonitoringServiceCollectionExtensions.cs" />
<InternalsVisibleToTest Include="$(AssemblyName).Tests" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -133,20 +133,6 @@
}
]
},
{
"Type": "static class Microsoft.Extensions.Diagnostics.ResourceMonitoring.ResourceUtilizationCounters",
"Stage": "Stable",
"Properties": [
{
"Member": "static string Microsoft.Extensions.Diagnostics.ResourceMonitoring.ResourceUtilizationCounters.CpuConsumptionPercentage { get; }",
"Stage": "Stable"
},
{
"Member": "static string Microsoft.Extensions.Diagnostics.ResourceMonitoring.ResourceUtilizationCounters.MemoryConsumptionPercentage { get; }",
"Stage": "Stable"
}
]
},
{
"Type": "readonly struct Microsoft.Extensions.Diagnostics.ResourceMonitoring.SystemResources",
"Stage": "Stable",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public partial class ResourceMonitoringOptions
internal static readonly TimeSpan DefaultRefreshInterval = TimeSpan.FromSeconds(5);

/// <summary>
/// Gets or sets the default interval used for refreshing values reported by <see cref="ResourceUtilizationCounters.CpuConsumptionPercentage"/>.
/// Gets or sets the default interval used for refreshing values reported by <c>"process.cpu.utilization"</c> metrics.
/// </summary>
/// <value>
/// The default value is 5 seconds.
Expand All @@ -26,7 +26,7 @@ public partial class ResourceMonitoringOptions
public TimeSpan CpuConsumptionRefreshInterval { get; set; } = DefaultRefreshInterval;

/// <summary>
/// Gets or sets the default interval used for refreshing values reported by <see cref="ResourceUtilizationCounters.MemoryConsumptionPercentage"/>.
/// Gets or sets the default interval used for refreshing values reported by <c>"dotnet.process.memory.virtual.utilization"</c> metrics.
/// </summary>
/// <value>
/// The default value is 5 seconds.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ namespace Microsoft.Extensions.Diagnostics.ResourceMonitoring;
/// Represents the names of instruments published by this package.
/// </summary>
/// <remarks>
/// These counters are currently only published on Linux.
/// These metrics are currently only published on Linux.
/// </remarks>
/// <seealso cref="System.Diagnostics.Metrics.Instrument"/>
public static class ResourceUtilizationCounters
internal static class ResourceUtilizationInstruments
{
/// <summary>
/// Gets the CPU consumption of the running application in percentages.
/// Gets the CPU consumption of the running application in range <c>[0, 1]</c>.
/// </summary>
/// <remarks>
/// The type of an instrument is <see cref="System.Diagnostics.Metrics.ObservableGauge{T}"/>.
/// </remarks>
public static string CpuConsumptionPercentage => "cpu_consumption_percentage";
public const string CpuUtilization = "process.cpu.utilization";

/// <summary>
/// Gets the memory consumption of the running application in percentages.
/// Gets the memory consumption of the running application in range <c>[0, 1]</c>.
/// </summary>
/// <remarks>
/// The type of an instrument is <see cref="System.Diagnostics.Metrics.ObservableGauge{T}"/>.
/// </remarks>
public static string MemoryConsumptionPercentage => "memory_consumption_percentage";
public const string MemoryUtilization = "dotnet.process.memory.virtual.utilization";
}
Loading