| | | 1 | | using System.Diagnostics; |
| | | 2 | | using System.Runtime.CompilerServices; |
| | | 3 | | |
| | | 4 | | namespace NGql.Core.Observability; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Specialized observability for pooling operations with detailed cache performance tracking |
| | | 8 | | /// </summary> |
| | | 9 | | internal static class PoolingObservability |
| | | 10 | | { |
| | | 11 | | // Cache levels for standardized naming |
| | | 12 | | private const string ThreadLocalCache = "thread_local"; |
| | | 13 | | private const string GlobalPool = "global_pool"; |
| | | 14 | | private const string Allocation = "allocation"; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Records a thread-local cache hit with observability |
| | | 18 | | /// </summary> |
| | | 19 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 20 | | internal static void RecordThreadLocalHit(string poolType) |
| | | 21 | | { |
| | | 22 | | // Metrics |
| | 7551 | 23 | | NGqlTelemetry.RecordPoolOperation(poolType, "get", ThreadLocalCache, -1); |
| | | 24 | | |
| | | 25 | | // Tracing (only if active listener exists) |
| | 7551 | 26 | | using var activity = NGqlActivity.StartPooling(poolType, "get") |
| | 7551 | 27 | | .WithPoolingTags(poolType, ThreadLocalCache, -1); |
| | 15102 | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Records a global pool hit with observability |
| | | 32 | | /// </summary> |
| | | 33 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 34 | | internal static void RecordGlobalPoolHit(string poolType, int currentPoolSize) |
| | | 35 | | { |
| | | 36 | | // Metrics |
| | 1857 | 37 | | NGqlTelemetry.RecordPoolOperation(poolType, "get", GlobalPool, currentPoolSize); |
| | | 38 | | |
| | | 39 | | // Tracing (only if active listener exists) |
| | 1857 | 40 | | using var activity = NGqlActivity.StartPooling(poolType, "get") |
| | 1857 | 41 | | .WithPoolingTags(poolType, GlobalPool, currentPoolSize); |
| | 3714 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Records a new allocation (cache miss) with observability |
| | | 46 | | /// </summary> |
| | | 47 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 48 | | internal static void RecordAllocation(string poolType) |
| | | 49 | | { |
| | | 50 | | // Metrics |
| | 1242 | 51 | | NGqlTelemetry.RecordPoolOperation(poolType, "allocate", Allocation, 0); |
| | | 52 | | |
| | | 53 | | // Tracing (only if active listener exists) |
| | 1242 | 54 | | using var activity = NGqlActivity.StartPooling(poolType, "allocate") |
| | 1242 | 55 | | .WithPoolingTags(poolType, Allocation, 0); |
| | 2484 | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Records pool efficiency statistics for monitoring |
| | | 60 | | /// </summary> |
| | | 61 | | internal static void RecordPoolEfficiency(string poolType, PoolEfficiencyStats stats) |
| | | 62 | | { |
| | 12 | 63 | | using var activity = NGqlActivity.StartPooling(poolType, "efficiency_report") |
| | 12 | 64 | | .WithTag("pool.thread_local_hit_rate", stats.ThreadLocalHitRate) |
| | 12 | 65 | | .WithTag("pool.global_pool_hit_rate", stats.GlobalPoolHitRate) |
| | 12 | 66 | | .WithTag("pool.allocation_rate", stats.AllocationRate) |
| | 12 | 67 | | .WithTag("pool.total_operations", stats.TotalOperations); |
| | | 68 | | |
| | | 69 | | // These could be exposed as gauges for monitoring dashboards |
| | 12 | 70 | | activity.AddEvent("efficiency_calculated", new ActivityTagsCollection |
| | 12 | 71 | | { |
| | 12 | 72 | | ["thread_local_efficiency"] = stats.ThreadLocalHitRate, |
| | 12 | 73 | | ["global_pool_efficiency"] = stats.GlobalPoolHitRate, |
| | 12 | 74 | | ["cache_effectiveness"] = stats.ThreadLocalHitRate + stats.GlobalPoolHitRate |
| | 12 | 75 | | }); |
| | 24 | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Pool efficiency statistics for detailed monitoring |
| | | 80 | | /// </summary> |
| | | 81 | | internal readonly struct PoolEfficiencyStats |
| | | 82 | | { |
| | | 83 | | public readonly double ThreadLocalHitRate; |
| | | 84 | | public readonly double GlobalPoolHitRate; |
| | | 85 | | public readonly double AllocationRate; |
| | | 86 | | public readonly long TotalOperations; |
| | | 87 | | |
| | | 88 | | public PoolEfficiencyStats(long threadLocalHits, long globalHits, long allocations) |
| | | 89 | | { |
| | 18 | 90 | | TotalOperations = threadLocalHits + globalHits + allocations; |
| | 18 | 91 | | ThreadLocalHitRate = TotalOperations > 0 ? (double)threadLocalHits / TotalOperations : 0; |
| | 18 | 92 | | GlobalPoolHitRate = TotalOperations > 0 ? (double)globalHits / TotalOperations : 0; |
| | 18 | 93 | | AllocationRate = TotalOperations > 0 ? (double)allocations / TotalOperations : 0; |
| | 18 | 94 | | } |
| | | 95 | | } |
| | | 96 | | } |