Skip to content

Commit 055ed40

Browse files
committed
feat(telemetry): enhance telemetry documentation and add meters support
1 parent 081727e commit 055ed40

3 files changed

Lines changed: 45 additions & 17 deletions

File tree

src/HttpUserAgentParser/Telemetry/HttpUserAgentParserMeters.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ namespace MyCSharp.HttpUserAgentParser.Telemetry;
1010
/// System.Diagnostics.Metrics instruments emitted by MyCSharp.HttpUserAgentParser.
1111
/// This is opt-in and designed to keep overhead negligible unless a listener is enabled.
1212
/// </summary>
13+
/// <remarks>
14+
/// Instruments are created once on first enablement and emit no data unless observed
15+
/// by an active listener.
16+
/// </remarks>
1317
[ExcludeFromCodeCoverage]
1418
internal static class HttpUserAgentParserMeters
1519
{
@@ -40,8 +44,11 @@ internal static class HttpUserAgentParserMeters
4044
public static bool IsParseDurationEnabled => s_parseDurationMs?.Enabled ?? false;
4145

4246
/// <summary>
43-
/// Initializes the meter and creates the instruments (idempotent).
47+
/// Initializes the meter and creates all metric instruments.
4448
/// </summary>
49+
/// <remarks>
50+
/// Initialization is performed at most once. Subsequent calls are ignored.
51+
/// </remarks>
4552
public static void Enable(Meter? meter = null)
4653
{
4754
if (Interlocked.Exchange(ref s_initialized, 1) == 1)

src/HttpUserAgentParser/Telemetry/HttpUserAgentParserTelemetry.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,17 @@ public static bool IsCacheSizeEnabled
6161
/// Enables core EventCounter telemetry for the parser.
6262
/// </summary>
6363
public static void Enable() => Interlocked.Or(ref s_enabledFlags, EventCountersFlag);
64-
/// <summary>
64+
65+
/// <summary>
66+
/// Enables native System.Diagnostics.Metrics telemetry for the parser.
67+
/// </summary>
68+
public static void EnableMeters(Meter? meter = null)
69+
{
70+
HttpUserAgentParserMeters.Enable(meter);
71+
Interlocked.Or(ref s_enabledFlags, MetersFlag);
72+
}
73+
74+
/// <summary>
6575
/// Records a parse request event.
6676
/// </summary>
6777
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -144,18 +154,8 @@ public static void ConcurrentCacheSizeSet(int size)
144154

145155
#if DEBUG
146156
/// <summary>
147-
/// Resets telemetry state for unit testing.
148-
/// </summary>f ((flags & MetersFlag) != 0)
149-
{
150-
HttpUserAgentParserMeters.ConcurrentCacheMiss();
151-
}
152-
}
153-
154-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
155-
public static void ConcurrentCacheSizeSet(int size)
156-
=> HttpUserAgentParserTelemetryState.SetConcurrentCacheSize(size);
157-
158-
#if DEBUG
157+
/// Resets static state to support isolated unit tests.
158+
/// </summary>
159159
public static void ResetForTests()
160160
{
161161
Volatile.Write(ref s_enabledFlags, 0);

src/HttpUserAgentParser/Telemetry/HttpUserAgentParserTelemetryState.cs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,37 @@
44

55
namespace MyCSharp.HttpUserAgentParser.Telemetry;
66

7+
/// <summary>
8+
/// Holds shared telemetry state for the concurrent dictionary cache.
9+
/// </summary>
10+
/// <remarks>
11+
/// The state is updated independently of whether telemetry is currently enabled
12+
/// so that polling-based instruments can report correct values once a listener
13+
/// attaches.
14+
/// </remarks>
715
[ExcludeFromCodeCoverage]
816
internal static class HttpUserAgentParserTelemetryState
917
{
1018
private static long s_concurrentCacheSize;
1119

12-
public static long ConcurrentCacheSize => Volatile.Read(ref s_concurrentCacheSize);
20+
/// <summary>
21+
/// Gets the current size of the concurrent dictionary cache.
22+
/// </summary>
23+
public static long ConcurrentCacheSize
24+
=> Volatile.Read(ref s_concurrentCacheSize);
1325

14-
public static void SetConcurrentCacheSize(int size) => Volatile.Write(ref s_concurrentCacheSize, size);
26+
/// <summary>
27+
/// Updates the current size of the concurrent dictionary cache.
28+
/// </summary>
29+
/// <param name="size">Current number of entries in the cache.</param>
30+
public static void SetConcurrentCacheSize(int size)
31+
=> Volatile.Write(ref s_concurrentCacheSize, size);
1532

1633
#if DEBUG
17-
public static void ResetForTests() => Volatile.Write(ref s_concurrentCacheSize, 0);
34+
/// <summary>
35+
/// Resets the telemetry state for unit tests.
36+
/// </summary>
37+
public static void ResetForTests()
38+
=> Volatile.Write(ref s_concurrentCacheSize, 0);
1839
#endif
1940
}

0 commit comments

Comments
 (0)