Skip to content

Commit 0478999

Browse files
committed
refactor(telemetry): update duration metrics to seconds
1 parent 067b733 commit 0478999

File tree

6 files changed

+19
-17
lines changed

6 files changed

+19
-17
lines changed

src/HttpUserAgentParser/HttpUserAgentParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static HttpUserAgentInformation Parse(string userAgent)
4444

4545
if (measureDuration)
4646
{
47-
HttpUserAgentParserTelemetry.ParseDuration(Stopwatch.GetElapsedTime(startTimestamp).TotalMilliseconds);
47+
HttpUserAgentParserTelemetry.ParseDuration(Stopwatch.GetElapsedTime(startTimestamp));
4848
}
4949

5050
return result;

src/HttpUserAgentParser/Telemetry/HttpUserAgentParserEventSource.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public sealed class HttpUserAgentParserEventSource : EventSource
2828
internal static HttpUserAgentParserEventSource Log { get; } = new();
2929

3030
private readonly IncrementingEventCounter _parseRequests;
31-
private readonly EventCounter? _parseDurationMs;
31+
private readonly EventCounter? _parseDurationSeconds;
3232

3333
private readonly IncrementingEventCounter _concurrentCacheHit;
3434
private readonly IncrementingEventCounter _concurrentCacheMiss;
@@ -46,10 +46,10 @@ private HttpUserAgentParserEventSource()
4646
DisplayUnits = "calls",
4747
};
4848

49-
_parseDurationMs = new EventCounter("parse-duration", this)
49+
_parseDurationSeconds = new EventCounter("parse-duration", this)
5050
{
5151
DisplayName = "Parse duration",
52-
DisplayUnits = "ms",
52+
DisplayUnits = "s",
5353
};
5454

5555
// Providers (cache)
@@ -92,16 +92,16 @@ internal void ParseRequest()
9292
/// <summary>
9393
/// Records the duration of a User-Agent parse operation.
9494
/// </summary>
95-
/// <param name="milliseconds">Elapsed parse time in milliseconds.</param>
95+
/// <param name="seconds">Elapsed parse time in seconds.</param>
9696
[NonEvent]
97-
internal void ParseDuration(double milliseconds)
97+
internal void ParseDuration(double seconds)
9898
{
9999
if (!IsEnabled())
100100
{
101101
return;
102102
}
103103

104-
_parseDurationMs?.WriteMetric(milliseconds);
104+
_parseDurationSeconds?.WriteMetric(seconds);
105105
}
106106

107107
/// <summary>
@@ -155,7 +155,7 @@ protected override void Dispose(bool disposing)
155155
if (disposing)
156156
{
157157
_parseRequests?.Dispose();
158-
_parseDurationMs?.Dispose();
158+
_parseDurationSeconds?.Dispose();
159159

160160
_concurrentCacheHit?.Dispose();
161161
_concurrentCacheMiss?.Dispose();

src/HttpUserAgentParser/Telemetry/HttpUserAgentParserMeters.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,9 @@ public static void Enable(Meter? meter = null)
6363
unit: "{call}",
6464
description: "User-Agent parse requests");
6565

66-
// Using "ms" instead of "s" because parse times are sub-millisecond,
67-
// making milliseconds more readable than fractional seconds.
6866
s_parseDuration = s_meter.CreateHistogram<double>(
6967
name: "parse.duration",
70-
unit: "ms",
68+
unit: "s",
7169
description: "Parse duration");
7270

7371
s_concurrentCacheHit = s_meter.CreateCounter<long>(
@@ -94,10 +92,10 @@ public static void Enable(Meter? meter = null)
9492
public static void ParseRequest() => s_parseRequests?.Add(1);
9593

9694
/// <summary>
97-
/// Records the parse duration in milliseconds.
95+
/// Records the parse duration in seconds.
9896
/// </summary>
9997
[MethodImpl(MethodImplOptions.AggressiveInlining)]
100-
public static void ParseDuration(double milliseconds) => s_parseDuration?.Record(milliseconds);
98+
public static void ParseDuration(double seconds) => s_parseDuration?.Record(seconds);
10199

102100
/// <summary>
103101
/// Emits a counter increment for a concurrent dictionary cache hit.

src/HttpUserAgentParser/Telemetry/HttpUserAgentParserTelemetry.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,19 @@ public static void ParseRequest()
9292
/// <summary>
9393
/// Records the duration of a parse request.
9494
/// </summary>
95-
/// <param name="milliseconds">The duration in milliseconds.</param>
95+
/// <param name="duration">The elapsed duration.</param>
9696
[MethodImpl(MethodImplOptions.AggressiveInlining)]
97-
public static void ParseDuration(double milliseconds)
97+
public static void ParseDuration(TimeSpan duration)
9898
{
9999
int flags = Volatile.Read(ref s_enabledFlags);
100100
if ((flags & EventCountersFlag) != 0)
101101
{
102-
HttpUserAgentParserEventSource.Log.ParseDuration(milliseconds);
102+
HttpUserAgentParserEventSource.Log.ParseDuration(duration.TotalSeconds);
103103
}
104104

105105
if ((flags & MetersFlag) != 0)
106106
{
107-
HttpUserAgentParserMeters.ParseDuration(milliseconds);
107+
HttpUserAgentParserMeters.ParseDuration(duration.TotalSeconds);
108108
}
109109
}
110110

tests/HttpUserAgentParser.UnitTests/Telemetry/HttpUserAgentParserMetersTelemetryTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,7 @@ public void Meters_Emit_WhenEnabled()
4848
Assert.Contains("cache.concurrent_dictionary.hit", listener.InstrumentNames);
4949
Assert.Contains("cache.concurrent_dictionary.miss", listener.InstrumentNames);
5050
Assert.Contains("cache.concurrent_dictionary.size", listener.InstrumentNames);
51+
52+
Assert.Equal("s", listener.InstrumentUnits["parse.duration"]);
5153
}
5254
}

tests/HttpUserAgentParser.UnitTests/Telemetry/MeterTestListener.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ internal sealed class MeterTestListener : IDisposable
1111
private readonly MeterListener _listener;
1212

1313
public ConcurrentBag<string> InstrumentNames { get; } = [];
14+
public ConcurrentDictionary<string, string?> InstrumentUnits { get; } = new(StringComparer.Ordinal);
1415

1516
public MeterTestListener(string meterName)
1617
{
@@ -24,6 +25,7 @@ public MeterTestListener(string meterName)
2425
return;
2526
}
2627

28+
InstrumentUnits[instrument.Name] = instrument.Unit;
2729
listener.EnableMeasurementEvents(instrument);
2830
};
2931

0 commit comments

Comments
 (0)