Skip to content

Commit

Permalink
Aligning metrics with OTel semantic conventions (#4482)
Browse files Browse the repository at this point in the history
* Aligning metrics with OTel semantic convention
  • Loading branch information
xakep139 authored Oct 23, 2023
1 parent bfecbbb commit 02c8641
Show file tree
Hide file tree
Showing 21 changed files with 170 additions and 370 deletions.
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}),");
}
}
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");

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

0 comments on commit 02c8641

Please sign in to comment.