|
| 1 | +#pragma warning disable SENTRYTRACECONNECTEDMETRICS // IHub.Metrics is experimental |
| 2 | +using Sentry; |
| 3 | +using Sentry.Extensibility; |
| 4 | + |
| 5 | +namespace SymbolCollector.Core; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// A decorator for <see cref="ClientMetrics"/> that also emits metrics to Sentry's trace-connected metrics API. |
| 9 | +/// </summary> |
| 10 | +/// <remarks> |
| 11 | +/// This integrates with Sentry SDK 6.1.0's new experimental trace-connected metrics feature. |
| 12 | +/// Metrics are attached to the current trace/span for correlation in Sentry's UI. |
| 13 | +/// Enable with <c>options.Experimental.EnableMetrics = true</c>. |
| 14 | +/// </remarks> |
| 15 | +public class SentryClientMetrics : ClientMetrics |
| 16 | +{ |
| 17 | + private readonly IHub _hub; |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Creates a new instance using the default <see cref="HubAdapter.Instance"/>. |
| 21 | + /// </summary> |
| 22 | + public SentryClientMetrics() : this(HubAdapter.Instance) |
| 23 | + { |
| 24 | + } |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Creates a new instance with the specified hub for metrics emission. |
| 28 | + /// </summary> |
| 29 | + /// <param name="hub">The Sentry hub to use for emitting metrics.</param> |
| 30 | + public SentryClientMetrics(IHub hub) |
| 31 | + { |
| 32 | + _hub = hub; |
| 33 | + } |
| 34 | + |
| 35 | + private SentryTraceMetrics Metrics => _hub.Metrics; |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Records a file processed event, incrementing both local and Sentry counters. |
| 39 | + /// </summary> |
| 40 | + public override void FileProcessed() |
| 41 | + { |
| 42 | + base.FileProcessed(); |
| 43 | + Metrics.AddCounter("symbol_collector.files_processed", 1); |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Records a Mach-O file discovery. |
| 48 | + /// </summary> |
| 49 | + public override void MachOFileFound() |
| 50 | + { |
| 51 | + base.MachOFileFound(); |
| 52 | + Metrics.AddCounter("symbol_collector.debug_images_found", 1, |
| 53 | + [new KeyValuePair<string, object>("format", "macho")]); |
| 54 | + } |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Records an ELF file discovery. |
| 58 | + /// </summary> |
| 59 | + public override void ElfFileFound() |
| 60 | + { |
| 61 | + base.ElfFileFound(); |
| 62 | + Metrics.AddCounter("symbol_collector.debug_images_found", 1, |
| 63 | + [new KeyValuePair<string, object>("format", "elf")]); |
| 64 | + } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Records a Fat Mach-O file discovery. |
| 68 | + /// </summary> |
| 69 | + public override void FatMachOFileFound() |
| 70 | + { |
| 71 | + base.FatMachOFileFound(); |
| 72 | + Metrics.AddCounter("symbol_collector.debug_images_found", 1, |
| 73 | + [new KeyValuePair<string, object>("format", "fat_macho")]); |
| 74 | + } |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// Records a failed upload. |
| 78 | + /// </summary> |
| 79 | + public override void FailedToUpload() |
| 80 | + { |
| 81 | + base.FailedToUpload(); |
| 82 | + Metrics.AddCounter("symbol_collector.uploads", 1, |
| 83 | + [new KeyValuePair<string, object>("status", "failed")]); |
| 84 | + } |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// Records a parse failure. |
| 88 | + /// </summary> |
| 89 | + public override void FailedToParse() |
| 90 | + { |
| 91 | + base.FailedToParse(); |
| 92 | + Metrics.AddCounter("symbol_collector.parse_failures", 1); |
| 93 | + } |
| 94 | + |
| 95 | + /// <summary> |
| 96 | + /// Records a successful upload. |
| 97 | + /// </summary> |
| 98 | + public override void SuccessfulUpload() |
| 99 | + { |
| 100 | + base.SuccessfulUpload(); |
| 101 | + Metrics.AddCounter("symbol_collector.uploads", 1, |
| 102 | + [new KeyValuePair<string, object>("status", "success")]); |
| 103 | + } |
| 104 | + |
| 105 | + /// <summary> |
| 106 | + /// Records when a file already existed on the server. |
| 107 | + /// </summary> |
| 108 | + public override void AlreadyExisted() |
| 109 | + { |
| 110 | + base.AlreadyExisted(); |
| 111 | + Metrics.AddCounter("symbol_collector.uploads", 1, |
| 112 | + [new KeyValuePair<string, object>("status", "already_exists")]); |
| 113 | + } |
| 114 | + |
| 115 | + /// <summary> |
| 116 | + /// Removes jobs from the in-flight count. |
| 117 | + /// </summary> |
| 118 | + public override void JobsInFlightRemove(int tasksCount) |
| 119 | + { |
| 120 | + base.JobsInFlightRemove(tasksCount); |
| 121 | + Metrics.RecordGauge("symbol_collector.jobs_in_flight", JobsInFlightCount); |
| 122 | + } |
| 123 | + |
| 124 | + /// <summary> |
| 125 | + /// Adds jobs to the in-flight count. |
| 126 | + /// </summary> |
| 127 | + public override void JobsInFlightAdd(int tasksCount) |
| 128 | + { |
| 129 | + base.JobsInFlightAdd(tasksCount); |
| 130 | + Metrics.RecordGauge("symbol_collector.jobs_in_flight", JobsInFlightCount); |
| 131 | + } |
| 132 | + |
| 133 | + /// <summary> |
| 134 | + /// Records bytes uploaded, emitting as a distribution for percentile analysis. |
| 135 | + /// </summary> |
| 136 | + public override void UploadedBytesAdd(long bytes) |
| 137 | + { |
| 138 | + base.UploadedBytesAdd(bytes); |
| 139 | + Metrics.RecordDistribution("symbol_collector.uploaded_bytes", bytes, "byte"); |
| 140 | + } |
| 141 | + |
| 142 | + /// <summary> |
| 143 | + /// Records an unauthorized access error. |
| 144 | + /// </summary> |
| 145 | + public override void FileOrDirectoryUnauthorizedAccess() |
| 146 | + { |
| 147 | + base.FileOrDirectoryUnauthorizedAccess(); |
| 148 | + Metrics.AddCounter("symbol_collector.access_errors", 1, |
| 149 | + [new KeyValuePair<string, object>("type", "unauthorized")]); |
| 150 | + } |
| 151 | + |
| 152 | + /// <summary> |
| 153 | + /// Records a directory not found error. |
| 154 | + /// </summary> |
| 155 | + public override void DirectoryDoesNotExist() |
| 156 | + { |
| 157 | + base.DirectoryDoesNotExist(); |
| 158 | + Metrics.AddCounter("symbol_collector.access_errors", 1, |
| 159 | + [new KeyValuePair<string, object>("type", "directory_not_found")]); |
| 160 | + } |
| 161 | + |
| 162 | + /// <summary> |
| 163 | + /// Records a file not found error. |
| 164 | + /// </summary> |
| 165 | + public override void FileDoesNotExist() |
| 166 | + { |
| 167 | + base.FileDoesNotExist(); |
| 168 | + Metrics.AddCounter("symbol_collector.access_errors", 1, |
| 169 | + [new KeyValuePair<string, object>("type", "file_not_found")]); |
| 170 | + } |
| 171 | +} |
0 commit comments