| 141 | } |
| 142 | |
| 143 | export class TaskNotesPerformanceProfiler { |
| 144 | private namespace: string; |
| 145 | private isEnabledCallback: () => boolean; |
| 146 | private logger: TaskNotesLogger; |
| 147 | private now: () => number; |
| 148 | private sampleLimit: number; |
| 149 | private slowThresholdMs: number; |
| 150 | private enabledOverride: boolean | null = null; |
| 151 | private metrics = new Map<string, MetricAggregate>(); |
| 152 | private counters = new Map<string, CounterAggregate>(); |
| 153 | private gauges = new Map<string, GaugeAggregate>(); |
| 154 | |
| 155 | constructor(options: PerformanceProfilerOptions = {}) { |
| 156 | this.namespace = options.namespace?.trim() || "tasknotes"; |
| 157 | this.isEnabledCallback = options.isEnabled || (() => false); |
| 158 | this.logger = options.logger || defaultLogger; |
| 159 | this.now = options.now || defaultNow; |
| 160 | this.sampleLimit = Math.max(1, options.sampleLimit || DEFAULT_SAMPLE_LIMIT); |
| 161 | this.slowThresholdMs = Math.max(0, options.slowThresholdMs ?? DEFAULT_SLOW_THRESHOLD_MS); |
| 162 | } |
| 163 | |
| 164 | isEnabled(): boolean { |
| 165 | return this.enabledOverride ?? this.isEnabledCallback(); |
| 166 | } |
| 167 | |
| 168 | enable(): void { |
| 169 | this.enabledOverride = true; |
| 170 | } |
| 171 | |
| 172 | disable(): void { |
| 173 | this.enabledOverride = false; |
| 174 | } |
| 175 | |
| 176 | clearEnabledOverride(): void { |
| 177 | this.enabledOverride = null; |
| 178 | } |
| 179 | |
| 180 | child(namespace: string): TaskNotesPerformanceProfiler { |
| 181 | return new TaskNotesPerformanceProfiler({ |
| 182 | namespace: [this.namespace, namespace].filter(Boolean).join("."), |
| 183 | isEnabled: () => this.isEnabled(), |
| 184 | logger: this.logger, |
| 185 | now: this.now, |
| 186 | sampleLimit: this.sampleLimit, |
| 187 | slowThresholdMs: this.slowThresholdMs, |
| 188 | }); |
| 189 | } |
| 190 | |
| 191 | start(name: string, details?: PerformanceProfilerDetails): PerformanceProfilerSpan { |
| 192 | if (!this.isEnabled()) { |
| 193 | return { end: () => 0 }; |
| 194 | } |
| 195 | |
| 196 | const startedAt = this.now(); |
| 197 | let ended = false; |
| 198 | |
| 199 | return { |
| 200 | end: (endDetails?: PerformanceProfilerDetails): number => { |
nothing calls this directly
no outgoing calls
no test coverage detected