Skip to content

Commit af6e5c0

Browse files
authored
Start tracing send recieve (#360)
* trace HgRunner.Run calls * trace individual hg resume pushes and pulls * trace Synchronizer.cs SyncNow, SendToOthers, PullFromOthers and Merge * guard against null activity in `TagResumableParameters`
1 parent 51c25ac commit af6e5c0

File tree

5 files changed

+53
-0
lines changed

5 files changed

+53
-0
lines changed

src/LibChorus/LibChorus.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<PackageId>SIL.Chorus.LibChorus</PackageId>
77
<TargetFrameworks>net462;netstandard2.0</TargetFrameworks>
88
<RepositoryUrl>https://github.com/sillsdev/chorus.git</RepositoryUrl>
9+
<LangVersion>default</LangVersion>
910
</PropertyGroup>
1011

1112
<ItemGroup>
@@ -21,6 +22,7 @@
2122
<PackageReference Include="SIL.Lift" Version="15.0.0-*" />
2223
<PackageReference Include="SIL.ReleaseTasks" Version="3.0.0" PrivateAssets="All" />
2324
<PackageReference Include="System.ComponentModel.Composition" Version="6.0.0" />
25+
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="9.0.2" />
2426
<PackageReference Include="System.Resources.Extensions" Version="6.0.0" />
2527
<PackageReference Include="System.ServiceModel.Primitives" Version="4.8.1" />
2628
</ItemGroup>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) 2025-2025 SIL International
2+
// This software is licensed under the MIT License (http://opensource.org/licenses/MIT)
3+
4+
using System.Diagnostics;
5+
using Chorus.VcsDrivers.Mercurial;
6+
using JetBrains.Annotations;
7+
8+
namespace Chorus
9+
{
10+
public static class LibChorusActivitySource
11+
{
12+
public const string ActivitySourceName = "SIL.LibChorus";
13+
internal static readonly ActivitySource Value = new ActivitySource(ActivitySourceName);
14+
15+
public static void TagResumableParameters(this Activity activity, string direction, HgResumeApiParameters request)
16+
{
17+
if (activity is null) return;
18+
activity.SetTag($"app.chorus.resumable.{direction}.chunk-size", request.ChunkSize);
19+
activity.SetTag($"app.chorus.resumable.{direction}.bundle-size", request.BundleSize);
20+
activity.SetTag($"app.chorus.resumable.{direction}.start-of-window", request.StartOfWindow);
21+
activity.SetTag($"app.chorus.resumable.{direction}.trans-id", request.TransId);
22+
activity.SetTag($"app.chorus.resumable.{direction}.repo-id", request.RepoId);
23+
activity.SetTag($"app.chorus.resumable.{direction}.quantity", request.Quantity);
24+
}
25+
}
26+
}

src/LibChorus/VcsDrivers/Mercurial/HgResumeTransport.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,9 @@ private PushStatus FinishPush(string transactionId)
505505

506506
private PushResponse PushOneChunk(HgResumeApiParameters request, byte[] dataToPush)
507507
{
508+
using var activity = LibChorusActivitySource.Value.StartActivity();
509+
activity?.TagResumableParameters("push", request);
510+
508511
var pushResponse = new PushResponse(PushStatus.Fail);
509512
try
510513
{
@@ -810,6 +813,9 @@ private PullStatus FinishPull(string transactionId)
810813

811814
private PullResponse PullOneChunk(HgResumeApiParameters request)
812815
{
816+
using var activity = LibChorusActivitySource.Value.StartActivity();
817+
activity?.TagResumableParameters("pull", request);
818+
813819
var pullResponse = new PullResponse(PullStatus.Fail);
814820
try
815821
{

src/LibChorus/VcsDrivers/Mercurial/HgRunner.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.ComponentModel;
55
using System.Diagnostics;
66
using System.IO;
7+
using Chorus.Model;
78
using Chorus.Utilities;
89
using SIL.Progress;
910
using SIL.Reporting;
@@ -44,10 +45,14 @@ public static ExecutionResult Run(string commandLine, string fromDirectory)
4445

4546
public static ExecutionResult Run(string commandLine, string fromDirectory, int secondsBeforeTimeOut, IProgress progress)
4647
{
48+
using var activity = LibChorusActivitySource.Value.StartActivity();
49+
activity?.SetTag("app.hg.cmd", ServerSettingsModel.RemovePasswordForLog(commandLine));
50+
activity?.SetTag("app.hg.timeout-sec", secondsBeforeTimeOut);
4751
ExecutionResult result = new ExecutionResult();
4852
Process process = new Process();
4953
if (String.IsNullOrEmpty(MercurialLocation.PathToMercurialFolder))
5054
{
55+
activity?.SetStatus(ActivityStatusCode.Error, "Mercurial location not configured");
5156
throw new ApplicationException("Mercurial location has not been configured.");
5257
}
5358
process.StartInfo.EnvironmentVariables["PYTHONPATH"] = Path.Combine(MercurialLocation.PathToMercurialFolder, "library.zip");
@@ -60,6 +65,7 @@ public static ExecutionResult Run(string commandLine, string fromDirectory, int
6065
process.StartInfo.CreateNoWindow = true;
6166
process.StartInfo.WorkingDirectory = fromDirectory;
6267
process.StartInfo.FileName = MercurialLocation.PathToHgExecutable;
68+
activity?.SetTag("app.hg.binary-path", MercurialLocation.PathToHgExecutable);
6369

6470
var debug = Environment.GetEnvironmentVariable(@"CHORUSDEBUGGING") == null ? String.Empty : @"--debug ";
6571
process.StartInfo.Arguments = commandLine.Replace("hg ", debug); //we don't want the whole command line, just the args portion
@@ -69,6 +75,7 @@ public static ExecutionResult Run(string commandLine, string fromDirectory, int
6975
process.StartInfo.StandardErrorEncoding = System.Text.Encoding.UTF8;
7076
if(!String.IsNullOrEmpty(debug))
7177
{
78+
activity?.SetTag("app.hg.debug", true);
7279
Logger.WriteEvent("Running hg command: hg --debug {0}", commandLine);
7380
}
7481
try
@@ -128,16 +135,19 @@ public static ExecutionResult Run(string commandLine, string fromDirectory, int
128135
{
129136
result.StandardError += Environment.NewLine + "Timed Out after waiting " + secondsBeforeTimeOut + " seconds.";
130137
result.ExitCode = ProcessStream.kTimedOut;
138+
activity?.SetStatus(ActivityStatusCode.Error, "Timeout");
131139
}
132140

133141
else if (progress.CancelRequested)
134142
{
135143
result.StandardError += Environment.NewLine + "User Cancelled.";
136144
result.ExitCode = ProcessStream.kCancelled;
145+
activity?.SetStatus(ActivityStatusCode.Error, "User Cancelled");
137146
}
138147
else
139148
{
140149
result.ExitCode = process.ExitCode;
150+
activity?.SetTag("app.hg.exit-code", result.ExitCode);
141151
}
142152
return result;
143153
}

src/LibChorus/sync/Synchronizer.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ public static Synchronizer FromProjectConfiguration(ProjectFolderConfiguration p
104104

105105
public SyncResults SyncNow(SyncOptions options)
106106
{
107+
using var activity = LibChorusActivitySource.Value.StartActivity();
108+
107109
SyncResults results = new SyncResults();
108110
List<RepositoryAddress> sourcesToTry = options.RepositorySourcesToTry;
109111
// this saves us from trying to connect twice to the same repo that is, for example, not there.
@@ -156,12 +158,14 @@ public SyncResults SyncNow(SyncOptions options)
156158
error.DoNotifications(Repository, _progress);
157159
results.Succeeded = false;
158160
results.ErrorEncountered = error;
161+
activity?.AddException(error);
159162
}
160163
catch (UserCancelledException)
161164
{
162165
results.Succeeded = false;
163166
results.Cancelled = true;
164167
results.ErrorEncountered = null;
168+
activity?.SetTag("app.chorus.sync.cancelled", true);
165169
}
166170
catch (Exception error)
167171
{
@@ -178,6 +182,7 @@ public SyncResults SyncNow(SyncOptions options)
178182

179183
results.Succeeded = false;
180184
results.ErrorEncountered = error;
185+
activity?.AddException(error);
181186
}
182187
finally
183188
{
@@ -245,6 +250,7 @@ public void SetIsOneOfDefaultSyncAddresses(RepositoryAddress address, bool enabl
245250

246251
private void SendToOthers(HgRepository repo, List<RepositoryAddress> sourcesToTry, Dictionary<RepositoryAddress, bool> connectionAttempt)
247252
{
253+
using var activity = LibChorusActivitySource.Value.StartActivity();
248254
foreach (RepositoryAddress address in sourcesToTry)
249255
{
250256
ThrowIfCancelPending();
@@ -325,6 +331,7 @@ private void SendToOneOther(RepositoryAddress address, Dictionary<RepositoryAddr
325331
/// <returns>true if there was at least one successful pull</returns>
326332
private bool PullFromOthers(HgRepository repo, List<RepositoryAddress> sourcesToTry, Dictionary<RepositoryAddress, bool> connectionAttempt)
327333
{
334+
using var activity = LibChorusActivitySource.Value.StartActivity();
328335
bool didGetFromAtLeastOneSource = false;
329336
foreach (RepositoryAddress source in new List<RepositoryAddress>(sourcesToTry)) // LT-18276: apparently possible to modify sourcesToTry
330337
{
@@ -628,6 +635,7 @@ private void TryToMakeCloneForSource(RepositoryAddress repoDescriptor)
628635
#region Merging
629636
private void MergeHeadsOrRollbackAndThrow(HgRepository repo, Revision workingRevBeforeSync)
630637
{
638+
using var activity = LibChorusActivitySource.Value.StartActivity();
631639
try
632640
{
633641
MergeHeads();
@@ -642,6 +650,7 @@ private void MergeHeadsOrRollbackAndThrow(HgRepository repo, Revision workingRev
642650
_progress.WriteException(error);
643651
_progress.WriteError("Rolling back...");
644652
UpdateToTheDescendantRevision(repo, workingRevBeforeSync); //rollback
653+
activity?.AddException(error);
645654
throw;
646655
}
647656
}

0 commit comments

Comments
 (0)