diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-06-07 22:20:33 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2026-06-07 22:20:33 -0500 |
| commit | 7332a75946d52f905f7eb8e1a469eb9a358b3b9a (patch) | |
| tree | 2fc3f3ee1d09dc141419304d07dcca3165f0234a /src | |
| parent | 294fcabd3928b420a4dfbd7ddd0dc38ada523165 (diff) | |
| download | slskd-7332a75946d52f905f7eb8e1a469eb9a358b3b9a.tar.gz | |
add metrics for downloads
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Transfers/Downloads/DownloadService.cs | 47 | ||||
| -rw-r--r-- | src/slskd/Transfers/Uploads/UploadService.cs | 18 |
2 files changed, 58 insertions, 7 deletions
diff --git a/src/slskd/Transfers/Downloads/DownloadService.cs b/src/slskd/Transfers/Downloads/DownloadService.cs index 7c794e55..7a797904 100644 --- a/src/slskd/Transfers/Downloads/DownloadService.cs +++ b/src/slskd/Transfers/Downloads/DownloadService.cs @@ -1132,6 +1132,8 @@ namespace slskd.Transfers.Downloads var retryOptions = OptionsMonitor.CurrentValue.Transfers.Download.Retry; var updateSyncRoot = new SemaphoreSlim(1, 1); + var stateHistory = new List<TransferStates>(); + try { using var rateLimiter = new RateLimiter(250, concurrencyLimit: 1, flushOnDispose: true); @@ -1172,6 +1174,24 @@ namespace slskd.Transfers.Downloads transfer.State = TransferStates.Queued | TransferStates.Locally; } + transfer = transfer.WithSoulseekTransfer(args.Transfer); + + if (!TransferStateCategories.Queued.Contains(args.PreviousState) && TransferStateCategories.Queued.Contains(transfer.State)) + { + Telemetry.Metrics.Transfers.Downloads.Queued.Files.Inc(1); + Telemetry.Metrics.Transfers.Downloads.Queued.Bytes.Inc(transfer.Size); + } + + if (!TransferStateCategories.InProgress.Contains(args.PreviousState) && TransferStateCategories.InProgress.Contains(transfer.State)) + { + Telemetry.Metrics.Transfers.Downloads.Queued.Files.Dec(1); + Telemetry.Metrics.Transfers.Downloads.Queued.Bytes.Dec(transfer.Size); + Telemetry.Metrics.Transfers.Downloads.InProgress.Files.Inc(1); + Telemetry.Metrics.Transfers.Downloads.InProgress.Bytes.Inc(transfer.Size); + } + + stateHistory.Add(transfer.State); + // todo: broadcast SynchronizedUpdate(transfer, semaphore: updateSyncRoot, cancellationToken: cancellationToken); } @@ -1335,6 +1355,10 @@ namespace slskd.Transfers.Downloads // todo: broadcast to signalr hub SynchronizedUpdate(transfer, semaphore: updateSyncRoot, cancellationToken: CancellationToken.None); + Telemetry.Metrics.Transfers.Downloads.Completed.Succeeded.Inc(1); + Telemetry.Metrics.Transfers.Downloads.Completed.Bytes.Inc(transfer.BytesTransferred); + Telemetry.Metrics.Transfers.Downloads.Completed.AverageSpeed.Observe(transfer.AverageSpeed); + Log.Debug("Successfully updated Transfer for {Filename} from {Username} (state: {State}, progress: {Progress})", transfer.Filename, transfer.Username, transfer.State, transfer.PercentComplete); /* @@ -1430,6 +1454,7 @@ namespace slskd.Transfers.Downloads // todo: broadcast SynchronizedUpdate(transfer, semaphore: updateSyncRoot, cancellationToken: CancellationToken.None); + Telemetry.Metrics.Transfers.Downloads.Completed.Failed.Inc(1); throw; } @@ -1441,11 +1466,33 @@ namespace slskd.Transfers.Downloads // todo: broadcast SynchronizedUpdate(transfer, semaphore: updateSyncRoot, cancellationToken: CancellationToken.None); + Telemetry.Metrics.Transfers.Downloads.Completed.Failed.Inc(1); throw; } finally { + Log.Debug("Download of {Filename} from user {Username} completed with states: {@States}", stateHistory); + + // figure out the most recent state that was either InProgress or Queued so that we can decrement the count and bytes + // for transfers that succeed this will be InProgress, for transfers that never started this will be Queued (cancelled or something) + // state will almost certainly transition to errored or completed before we get here, so we can't just take the last state + var mostRecentQueuedOrInProgressState = stateHistory + .LastOrDefault(s => TransferStateCategories.InProgress.Contains(s) || TransferStateCategories.Queued.Contains(s)); + + if (TransferStateCategories.InProgress.Contains(mostRecentQueuedOrInProgressState)) + { + // if the transfer was ever in progress at any time, we decrement because we should have incremented on that transition + Telemetry.Metrics.Transfers.Downloads.InProgress.Files.Dec(1); + Telemetry.Metrics.Transfers.Downloads.InProgress.Bytes.Dec(transfer.Size); + } + else if (TransferStateCategories.Queued.Contains(mostRecentQueuedOrInProgressState)) + { + // if the transfer never transitioned to in progress but it was queued at any point, decrement queued + Telemetry.Metrics.Transfers.Downloads.Queued.Files.Dec(1); + Telemetry.Metrics.Transfers.Downloads.Queued.Bytes.Dec(transfer.Size); + } + if (CancellationTokens.TryRemove(transfer.Id, out var storedCancellationTokenSource)) { storedCancellationTokenSource?.Dispose(); diff --git a/src/slskd/Transfers/Uploads/UploadService.cs b/src/slskd/Transfers/Uploads/UploadService.cs index 894f739b..0fed6636 100644 --- a/src/slskd/Transfers/Uploads/UploadService.cs +++ b/src/slskd/Transfers/Uploads/UploadService.cs @@ -357,7 +357,7 @@ namespace slskd.Transfers.Uploads if (!TransferStateCategories.InProgress.Contains(args.PreviousState) && TransferStateCategories.InProgress.Contains(transfer.State)) { Telemetry.Metrics.Transfers.Uploads.Queued.Files.Dec(1); - Telemetry.Metrics.Transfers.Uploads.Queued.Bytes.Dec(transfer.Size - transfer.StartOffset); + Telemetry.Metrics.Transfers.Uploads.Queued.Bytes.Dec(transfer.Size); Telemetry.Metrics.Transfers.Uploads.InProgress.Files.Inc(1); Telemetry.Metrics.Transfers.Uploads.InProgress.Bytes.Inc(transfer.Size); } @@ -520,19 +520,23 @@ namespace slskd.Transfers.Uploads { Log.Debug("Upload of {Filename} to user {Username} completed with states: {@States}", stateHistory); - // make sure we decrement the right offset. we have to do it here and inspect history; we have - // no other way of knowing, and we can't do it on the fly because state transitions may not be deterministic - if (stateHistory.Any(s => TransferStateCategories.InProgress.Contains(s))) + // figure out the most recent state that was either InProgress or Queued so that we can decrement the count and bytes + // for transfers that succeed this will be InProgress, for transfers that never started this will be Quued (cancelled or something) + // state will almost certainly transition to errored or completed before we get here, so we can't just take the last state + var mostRecentQueuedOrInProgressState = stateHistory + .LastOrDefault(s => TransferStateCategories.InProgress.Contains(s) || TransferStateCategories.Queued.Contains(s)); + + if (TransferStateCategories.InProgress.Contains(mostRecentQueuedOrInProgressState)) { // if the transfer was ever in progress at any time, we decrement because we should have incremented on that transition Telemetry.Metrics.Transfers.Uploads.InProgress.Files.Dec(1); - Telemetry.Metrics.Transfers.Uploads.InProgress.Bytes.Dec(transfer.Size - transfer.StartOffset); + Telemetry.Metrics.Transfers.Uploads.InProgress.Bytes.Dec(transfer.Size); } - else if (stateHistory.Any(s => TransferStateCategories.Queued.Contains(s))) + else if (TransferStateCategories.Queued.Contains(mostRecentQueuedOrInProgressState)) { // if the transfer never transitioned to in progress but it was queued at any point, decrement queued Telemetry.Metrics.Transfers.Uploads.Queued.Files.Dec(1); - Telemetry.Metrics.Transfers.Uploads.Queued.Bytes.Dec(transfer.Size - transfer.StartOffset); + Telemetry.Metrics.Transfers.Uploads.Queued.Bytes.Dec(transfer.Size); } if (host != Program.LocalHostName) |
