|
| 1 | +using System; |
| 2 | +using System.Threading; |
| 3 | +using System.Diagnostics.Tracing; |
| 4 | +using System.Runtime.CompilerServices; |
| 5 | + |
| 6 | +namespace Npgsql |
| 7 | +{ |
| 8 | + sealed class NpgsqlEventSource : EventSource |
| 9 | + { |
| 10 | + public static readonly NpgsqlEventSource Log = new NpgsqlEventSource(); |
| 11 | + |
| 12 | + const string EventSourceName = "Npgsql"; |
| 13 | + |
| 14 | + internal const int CommandStartId = 3; |
| 15 | + internal const int CommandStopId = 4; |
| 16 | + |
| 17 | +#if !NET461 && !NETSTANDARD2_0 |
| 18 | + IncrementingPollingCounter? _bytesWrittenPerSecondCounter; |
| 19 | + IncrementingPollingCounter? _bytesReadPerSecondCounter; |
| 20 | + |
| 21 | + IncrementingPollingCounter? _commandsPerSecondCounter; |
| 22 | + PollingCounter? _totalCommandsCounter; |
| 23 | + PollingCounter? _failedCommandsCounter; |
| 24 | + PollingCounter? _currentCommandsCounter; |
| 25 | + PollingCounter? _preparedCommandsRatioCounter; |
| 26 | + |
| 27 | + PollingCounter? _poolsCounter; |
| 28 | + PollingCounter? _idleConnectionsCounter; |
| 29 | + PollingCounter? _busyConnectionsCounter; |
| 30 | +#endif |
| 31 | + long _bytesWritten; |
| 32 | + long _bytesRead; |
| 33 | + |
| 34 | + long _totalCommands; |
| 35 | + long _totalPreparedCommands; |
| 36 | + long _currentCommands; |
| 37 | + long _failedCommands; |
| 38 | + |
| 39 | + int _pools; |
| 40 | + |
| 41 | + internal NpgsqlEventSource() : base(EventSourceName) {} |
| 42 | + |
| 43 | + // NOTE |
| 44 | + // - The 'Start' and 'Stop' suffixes on the following event names have special meaning in EventSource. They |
| 45 | + // enable creating 'activities'. |
| 46 | + // For more information, take a look at the following blog post: |
| 47 | + // https://blogs.msdn.microsoft.com/vancem/2015/09/14/exploring-eventsource-activity-correlation-and-causation-features/ |
| 48 | + // - A stop event's event id must be next one after its start event. |
| 49 | + |
| 50 | + internal void BytesWritten(long bytesWritten) => Interlocked.Add(ref _bytesWritten, bytesWritten); |
| 51 | + internal void BytesRead(long bytesRead) => Interlocked.Add(ref _bytesRead, bytesRead); |
| 52 | + |
| 53 | + [Event(CommandStartId, Level = EventLevel.Informational)] |
| 54 | + public void CommandStart(string sql) |
| 55 | + { |
| 56 | + Interlocked.Increment(ref _totalCommands); |
| 57 | + Interlocked.Increment(ref _currentCommands); |
| 58 | + WriteEvent(CommandStartId, sql); |
| 59 | + } |
| 60 | + |
| 61 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 62 | + [Event(CommandStopId, Level = EventLevel.Informational)] |
| 63 | + public void CommandStop() |
| 64 | + { |
| 65 | + Interlocked.Decrement(ref _currentCommands); |
| 66 | + WriteEvent(CommandStopId); |
| 67 | + } |
| 68 | + |
| 69 | + internal void CommandStartPrepared() => Interlocked.Increment(ref _totalPreparedCommands); |
| 70 | + |
| 71 | + internal void CommandFailed() => Interlocked.Increment(ref _failedCommands); |
| 72 | + |
| 73 | + internal void PoolCreated() => Interlocked.Increment(ref _pools); |
| 74 | + |
| 75 | +#if !NET461 && !NETSTANDARD2_0 |
| 76 | + static int GetIdleConnections() |
| 77 | + { |
| 78 | + // Note: there's no attempt here to be coherent in terms of race conditions, especially not with regards |
| 79 | + // to different counters. So idle and busy and be unsynchronized, as they're not polled together. |
| 80 | + var sum = 0; |
| 81 | + foreach (var kv in PoolManager.Pools) |
| 82 | + { |
| 83 | + var pool = kv.Pool; |
| 84 | + if (pool == null) |
| 85 | + return sum; |
| 86 | + sum += pool.State.Idle; |
| 87 | + } |
| 88 | + return sum; |
| 89 | + } |
| 90 | + |
| 91 | + static int GetBusyConnections() |
| 92 | + { |
| 93 | + // Note: there's no attempt here to be coherent in terms of race conditions, especially not with regards |
| 94 | + // to different counters. So idle and busy and be unsynchronized, as they're not polled together. |
| 95 | + var sum = 0; |
| 96 | + foreach (var kv in PoolManager.Pools) |
| 97 | + { |
| 98 | + var pool = kv.Pool; |
| 99 | + if (pool == null) |
| 100 | + return sum; |
| 101 | + sum += pool.State.Busy; |
| 102 | + } |
| 103 | + return sum; |
| 104 | + } |
| 105 | + |
| 106 | + protected override void OnEventCommand(EventCommandEventArgs command) |
| 107 | + { |
| 108 | + if (command.Command == EventCommand.Enable) |
| 109 | + { |
| 110 | + // Comment taken from RuntimeEventSource in CoreCLR |
| 111 | + // NOTE: These counters will NOT be disposed on disable command because we may be introducing |
| 112 | + // a race condition by doing that. We still want to create these lazily so that we aren't adding |
| 113 | + // overhead by at all times even when counters aren't enabled. |
| 114 | + // On disable, PollingCounters will stop polling for values so it should be fine to leave them around. |
| 115 | + |
| 116 | + _bytesWrittenPerSecondCounter = new IncrementingPollingCounter("bytes-written-per-second", this, () => _bytesWritten) |
| 117 | + { |
| 118 | + DisplayName = "Bytes Written", |
| 119 | + DisplayRateTimeScale = TimeSpan.FromSeconds(1) |
| 120 | + }; |
| 121 | + |
| 122 | + _bytesReadPerSecondCounter = new IncrementingPollingCounter("bytes-read-per-second", this, () => _bytesRead) |
| 123 | + { |
| 124 | + DisplayName = "Bytes Read", |
| 125 | + DisplayRateTimeScale = TimeSpan.FromSeconds(1) |
| 126 | + }; |
| 127 | + |
| 128 | + _commandsPerSecondCounter = new IncrementingPollingCounter("commands-per-second", this, () => _totalCommands) |
| 129 | + { |
| 130 | + DisplayName = "Command Rate", |
| 131 | + DisplayRateTimeScale = TimeSpan.FromSeconds(1) |
| 132 | + }; |
| 133 | + |
| 134 | + _totalCommandsCounter = new PollingCounter("total-commands", this, () => _totalCommands) |
| 135 | + { |
| 136 | + DisplayName = "Total Commands", |
| 137 | + }; |
| 138 | + |
| 139 | + _currentCommandsCounter = new PollingCounter("current-commands", this, () => _currentCommands) |
| 140 | + { |
| 141 | + DisplayName = "Current Commands" |
| 142 | + }; |
| 143 | + |
| 144 | + _failedCommandsCounter = new PollingCounter("failed-commands", this, () => _failedCommands) |
| 145 | + { |
| 146 | + DisplayName = "Failed Commands" |
| 147 | + }; |
| 148 | + |
| 149 | +// _preparedCommandsRatioCounter = new PollingCounter("prepared-commands-ratio", this, () => (double)_totalPreparedCommands / (double)_totalCommands) |
| 150 | + _preparedCommandsRatioCounter = new PollingCounter("prepared-commands-ratio", this, () => |
| 151 | + { |
| 152 | + Console.WriteLine($"{(double)_totalPreparedCommands} / {(double)_totalCommands}"); |
| 153 | + return (double)_totalPreparedCommands / (double)_totalCommands; |
| 154 | + }) |
| 155 | + { |
| 156 | + DisplayName = "Prepared Commands Ratio", |
| 157 | + DisplayUnits = "%" |
| 158 | + }; |
| 159 | + |
| 160 | + _poolsCounter = new PollingCounter("connection-pools", this, () => _pools) |
| 161 | + { |
| 162 | + DisplayName = "Connection Pools" |
| 163 | + }; |
| 164 | + |
| 165 | + _idleConnectionsCounter = new PollingCounter("idle-connections", this, () => GetIdleConnections()) |
| 166 | + { |
| 167 | + DisplayName = "Idle Connections" |
| 168 | + }; |
| 169 | + |
| 170 | + _busyConnectionsCounter = new PollingCounter("busy-connections", this, () => GetBusyConnections()) |
| 171 | + { |
| 172 | + DisplayName = "Busy Connections" |
| 173 | + }; |
| 174 | + } |
| 175 | + } |
| 176 | +#endif |
| 177 | + } |
| 178 | +} |
0 commit comments