Skip to content

Commit dade0e8

Browse files
committed
PR 29: Merge fixAITelemitery to master +semver: minor
Telemetry refined and considered functional. Access to telemetry can be requested. - Refactor of all AI Telemitery - Added dependancy tracking...
1 parent 37cf2ca commit dade0e8

File tree

10 files changed

+238
-99
lines changed

10 files changed

+238
-99
lines changed

TfsWitMigrator.Core/ComponentContext/TeamProjectContext.cs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Diagnostics;
44
using Microsoft.TeamFoundation;
55
using Microsoft.ApplicationInsights;
6+
using System.Collections.Generic;
67

78
namespace VSTS.DataBulkEditor.Engine
89
{
@@ -40,21 +41,39 @@ public void Connect()
4041
{
4142
if (_Collection == null)
4243
{
44+
Stopwatch connectionTimer = new Stopwatch();
45+
connectionTimer.Start();
4346
Trace.WriteLine("Creating TfsTeamProjectCollection Object ");
4447
_Collection = new TfsTeamProjectCollection(_CollectionUrl);
4548
try
4649
{
50+
Trace.WriteLine(string.Format("Connected to {0} ", _Collection.Uri.ToString()));
51+
Trace.WriteLine(string.Format("validating security for {0} ", _Collection.AuthorizedIdentity.ToString()));
4752
_Collection.EnsureAuthenticated();
53+
connectionTimer.Stop();
54+
Telemetry.Current.TrackEvent("ConnectionEstablished",
55+
new Dictionary<string, string> {
56+
{ "CollectionUrl", _CollectionUrl.ToString() },
57+
{ "TeamProjectName", _TeamProjectName}
58+
},
59+
new Dictionary<string, double> {
60+
{ "ConnectionTimer", connectionTimer.ElapsedMilliseconds }
61+
});
62+
Trace.TraceInformation(string.Format(" Access granted "));
4863
}
4964
catch (TeamFoundationServiceUnavailableException ex)
5065
{
51-
TelemetryClient tc = new TelemetryClient();
52-
tc.TrackException(ex);
66+
Telemetry.Current.TrackException(ex,
67+
new Dictionary<string, string> {
68+
{ "CollectionUrl", _CollectionUrl.ToString() },
69+
{ "TeamProjectName", _TeamProjectName}
70+
},
71+
new Dictionary<string, double> {
72+
{ "ConnectionTimer", connectionTimer.ElapsedMilliseconds }
73+
});
5374
Trace.TraceWarning(string.Format(" [EXCEPTION] {0}", ex.Message));
5475
throw ex;
55-
}
56-
Trace.WriteLine(string.Format("validating security for {0} ", _Collection.AuthorizedIdentity.ToString()));
57-
Trace.WriteLine(string.Format("Connected to {0} ", _Collection.Uri.ToString()));
76+
}
5877
}
5978
}
6079
}

TfsWitMigrator.Core/ComponentContext/TfsQueryContext.cs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,40 @@ public void AddParameter(string name, string value)
2727
public WorkItemCollection Execute()
2828
{
2929
WorkItemCollection wc;
30-
var ai = new TelemetryClient();
31-
Stopwatch sw = new Stopwatch();
32-
sw.Start();
30+
var startTime = DateTime.UtcNow;
31+
Stopwatch queryTimer = new Stopwatch();
32+
33+
queryTimer.Start();
3334
try
3435
{
3536
wc = storeContext.Store.Query(Query, parameters);
36-
ai.TrackMetric("QueryCount", wc.Count);
37-
sw.Stop();
38-
ai.TrackRequest("TFS Query", DateTime.Now, sw.Elapsed, "200", true);
39-
}
37+
queryTimer.Stop();
38+
Telemetry.Current.TrackDependency("TeamService", "Query", startTime, queryTimer.Elapsed, true);
39+
// Add additional bits to reuse the paramiters dictionary for telemitery
40+
parameters.Add("CollectionUrl", storeContext.Store.TeamProjectCollection.Uri.ToString());
41+
parameters.Add("Query", Query);
42+
Telemetry.Current.TrackEvent("QueryComplete",
43+
parameters,
44+
new Dictionary<string, double> {
45+
{ "QueryTime", queryTimer.ElapsedMilliseconds },
46+
{ "QueryCount", wc.Count }
47+
});
48+
Trace.TraceInformation(string.Format(" Query Complete: found {0} work items ", wc.Count));
49+
50+
}
4051
catch (Exception ex)
4152
{
42-
ai.TrackRequest("TFS Query", DateTime.Now, sw.Elapsed, "500", false);
43-
ai.TrackException(ex);
44-
throw;
53+
queryTimer.Stop();
54+
Telemetry.Current.TrackDependency("TeamService", "Query", startTime, queryTimer.Elapsed, false);
55+
Telemetry.Current.TrackException(ex,
56+
new Dictionary<string, string> {
57+
{ "CollectionUrl", storeContext.Store.TeamProjectCollection.Uri.ToString() }
58+
},
59+
new Dictionary<string, double> {
60+
{ "QueryTime",queryTimer.ElapsedMilliseconds }
61+
});
62+
Trace.TraceWarning(string.Format(" [EXCEPTION] {0}", ex.Message));
63+
throw ex;
4564
}
4665
return wc;
4766
}

TfsWitMigrator.Core/ComponentContext/WorkItemStoreContext.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Microsoft.TeamFoundation.WorkItemTracking.Client;
44
using System.Collections.Generic;
55
using System.Text.RegularExpressions;
6+
using System.Diagnostics;
67

78
namespace VSTS.DataBulkEditor.Engine
89
{
@@ -17,9 +18,31 @@ public class WorkItemStoreContext
1718

1819
public WorkItemStoreContext(ITeamProjectContext targetTfs, WorkItemStoreFlags bypassRules)
1920
{
21+
var startTime = DateTime.UtcNow;
22+
var timer = System.Diagnostics.Stopwatch.StartNew();
2023
this.targetTfs = targetTfs;
2124
this.bypassRules = bypassRules;
22-
wistore = new WorkItemStore(targetTfs.Collection, bypassRules);
25+
try
26+
{
27+
wistore = new WorkItemStore(targetTfs.Collection, bypassRules);
28+
timer.Stop();
29+
Telemetry.Current.TrackDependency("TeamService", "GetWorkItemStore", startTime, timer.Elapsed, true);
30+
}
31+
catch (Exception ex)
32+
{
33+
timer.Stop();
34+
Telemetry.Current.TrackDependency("TeamService", "GetWorkItemStore", startTime, timer.Elapsed, false);
35+
Telemetry.Current.TrackException(ex,
36+
new Dictionary<string, string> {
37+
{ "CollectionUrl", targetTfs.Collection.Uri.ToString() }
38+
},
39+
new Dictionary<string, double> {
40+
{ "Time",timer.ElapsedMilliseconds }
41+
});
42+
Trace.TraceWarning(string.Format(" [EXCEPTION] {0}", ex.Message));
43+
throw ex;
44+
}
45+
2346
foundWis = new Dictionary<int, WorkItem>();
2447
}
2548

TfsWitMigrator.Core/FieldMaps/FieldMapBase.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ public void Execute(WorkItem source, WorkItem target)
1919
}
2020
catch (Exception ex)
2121
{
22-
TelemetryClient tc = new TelemetryClient();
23-
tc.TrackException(ex);
24-
Trace.TraceWarning(string.Format(" [EXCEPTION] {0}", ex.Message));
25-
//throw;
22+
Telemetry.Current.TrackException(ex,
23+
new Dictionary<string, string> {
24+
{ "Source", source.Id.ToString() },
25+
{ "Target", target.Id.ToString()}
26+
});
27+
Trace.TraceError(string.Format(" [EXCEPTION] {0}", ex.Message));
2628
}
2729
}
2830

TfsWitMigrator.Core/MigrationContext/MigrationContextBase.cs

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public abstract class MigrationContextBase : ITfsProcessingContext
1818

1919
public MigrationContextBase(MigrationEngine me)
2020
{
21-
21+
2222
this.me = me;
2323
}
2424

@@ -34,41 +34,55 @@ public ProcessingStatus Status
3434

3535
public void Execute()
3636
{
37-
38-
TelemetryClient tc = new TelemetryClient();
39-
tc.TrackPageView(this.Name);
40-
41-
tc.TrackEvent(string.Format("{0}", this.Name));
42-
var properties = new Dictionary<string, string> {
43-
{ "Processing Engine", this.Name},
44-
{ "Target Project", me.Target.Name},
45-
{ "Target Collection", me.Target.Collection.Name },
46-
{ "Source Project", me.Source.Name},
47-
{ "Source Collection", me.Source.Collection.Name }
48-
};
49-
var measurements = new Dictionary<string, double>();
50-
Stopwatch stopwatch = new Stopwatch();
51-
stopwatch.Start();
37+
Telemetry.Current.TrackEvent("MigrationContextExecute",
38+
new Dictionary<string, string> {
39+
{ "Name", Name},
40+
{ "Target Project", me.Target.Name},
41+
{ "Target Collection", me.Target.Collection.Name },
42+
{ "Source Project", me.Source.Name},
43+
{ "Source Collection", me.Source.Collection.Name }
44+
});
45+
Trace.TraceInformation(string.Format(" Migration Context Start {0} ", Name));
46+
Stopwatch executeTimer = new Stopwatch();
47+
executeTimer.Start();
5248
//////////////////////////////////////////////////
5349
try
5450
{
5551
status = ProcessingStatus.Running;
5652
InternalExecute();
5753
status = ProcessingStatus.Complete;
58-
stopwatch.Stop();
59-
tc.TrackMetric("ExecutionTime", stopwatch.ElapsedMilliseconds);
60-
measurements.Add("ExecutionTime-Milliseconds", stopwatch.ElapsedMilliseconds);
61-
tc.TrackEvent(string.Format("{0}:{1}", this.GetType().Name, status.ToString()), properties, measurements);
62-
54+
executeTimer.Stop();
55+
Telemetry.Current.TrackEvent("MigrationContextComplete",
56+
new Dictionary<string, string> {
57+
{ "Name", Name},
58+
{ "Target Project", me.Target.Name},
59+
{ "Target Collection", me.Target.Collection.Name },
60+
{ "Source Project", me.Source.Name},
61+
{ "Source Collection", me.Source.Collection.Name },
62+
{ "Status", Status.ToString() }
63+
},
64+
new Dictionary<string, double> {
65+
{ "MigrationContextTime", executeTimer.ElapsedMilliseconds }
66+
});
67+
Trace.TraceInformation(string.Format(" Migration Context Complete {0} ", Name));
6368
}
6469
catch (Exception ex)
6570
{
6671
status = ProcessingStatus.Failed;
67-
stopwatch.Stop();
68-
measurements.Add("ExecutionTime-Milliseconds", stopwatch.ElapsedMilliseconds);
69-
// Send the exception telemetry:
70-
tc.TrackException(ex, properties, measurements);
71-
Trace.TraceError(ex.ToString());
72+
executeTimer.Stop();
73+
Telemetry.Current.TrackException(ex,
74+
new Dictionary<string, string> {
75+
{ "Name", Name},
76+
{ "Target Project", me.Target.Name},
77+
{ "Target Collection", me.Target.Collection.Name },
78+
{ "Source Project", me.Source.Name},
79+
{ "Source Collection", me.Source.Collection.Name },
80+
{ "Status", Status.ToString() }
81+
},
82+
new Dictionary<string, double> {
83+
{ "MigrationContextTime", executeTimer.ElapsedMilliseconds }
84+
});
85+
Trace.TraceWarning(string.Format(" [EXCEPTION] {0}", ex.Message));
7286
}
7387
}
7488

TfsWitMigrator.Core/MigrationEngine.cs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,29 +50,40 @@ public string ReflectedWorkItemIdFieldName
5050

5151
public ProcessingStatus Run()
5252
{
53-
var measurements = new Dictionary<string, double>();
54-
Stopwatch stopwatch = new Stopwatch();
55-
stopwatch.Start();
56-
TelemetryClient tc = new TelemetryClient();
57-
tc.Context.User.Id = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
58-
tc.Context.Session.Id = Guid.NewGuid().ToString();
59-
measurements.Add("Processors", processors.Count);
60-
measurements.Add("Actions", processorActions.Count);
61-
measurements.Add("Mappings", fieldMapps.Count);
62-
tc.TrackEvent("MigrationEngine:Run", null, measurements);
53+
Telemetry.Current.TrackEvent("EngineStart",
54+
new Dictionary<string, string> {
55+
{ "Engine", "Migration" }
56+
},
57+
new Dictionary<string, double> {
58+
{ "Processors", processors.Count },
59+
{ "Actions", processorActions.Count},
60+
{ "Mappings", fieldMapps.Count }
61+
});
62+
Stopwatch engineTimer = new Stopwatch();
63+
engineTimer.Start();
6364
ProcessingStatus ps = ProcessingStatus.Complete;
6465
foreach (ITfsProcessingContext process in processors)
6566
{
67+
Stopwatch processorTimer = new Stopwatch();
68+
processorTimer.Start();
6669
process.Execute();
70+
processorTimer.Stop();
71+
Telemetry.Current.TrackEvent("ProcessorComplete", new Dictionary<string, string> { { "Processor", process.Name }, { "Status", process.Status.ToString() } }, new Dictionary<string, double> { { "ProcessingTime", processorTimer.ElapsedMilliseconds } });
6772
if (process.Status == ProcessingStatus.Failed)
6873
{
6974
ps = ProcessingStatus.Failed;
7075
Trace.WriteLine("The Processor {0} entered the failed state...stopping run", process.Name);
7176
break;
7277
}
7378
}
74-
stopwatch.Stop();
75-
tc.TrackMetric("RunTime", stopwatch.ElapsedMilliseconds);
79+
engineTimer.Stop();
80+
Telemetry.Current.TrackEvent("EngineComplete",
81+
new Dictionary<string, string> {
82+
{ "Engine", "Migration" }
83+
},
84+
new Dictionary<string, double> {
85+
{ "EngineTime", engineTimer.ElapsedMilliseconds }
86+
});
7687
return ps;
7788
}
7889

TfsWitMigrator.Core/ProcessingContext/ProcessingContextBase.cs

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,36 +32,49 @@ public ProcessingStatus Status
3232

3333
public void Execute()
3434
{
35-
TelemetryClient tc = new TelemetryClient();
36-
tc.TrackEvent(string.Format("Execute: {0}", this.Name));
37-
var properties = new Dictionary<string, string> {
38-
{ "Processing Engine", this.Name},
39-
{ "Target Project", me.Target.Name},
40-
{ "Target Collection", me.Target.Collection.Name }
41-
};
42-
var measurements = new Dictionary<string, double>();
43-
Stopwatch stopwatch = new Stopwatch();
44-
stopwatch.Start();
45-
Trace.WriteLine("");
35+
Telemetry.Current.TrackEvent("ProcessingContextExecute",
36+
new Dictionary<string, string> {
37+
{ "Name", Name},
38+
{ "Target Project", me.Target.Name},
39+
{ "Target Collection", me.Target.Collection.Name }
40+
});
41+
Trace.TraceInformation(string.Format("ProcessingContext Start {0} ", Name));
42+
Stopwatch executeTimer = new Stopwatch();
43+
executeTimer.Start();
4644
//////////////////////////////////////////////////
4745
try
4846
{
4947
status = ProcessingStatus.Running;
5048
InternalExecute();
5149
status = ProcessingStatus.Complete;
52-
stopwatch.Stop();
53-
Trace.WriteLine(string.Format(@"EXECUTE DONE in {0:%h} hours {0:%m} minutes {0:s\:fff} seconds", stopwatch.Elapsed));
54-
measurements.Add("ExecutionTime-Milliseconds", stopwatch.ElapsedMilliseconds);
55-
tc.TrackEvent(string.Format("Complete: {0}", this.GetType().Name), properties, measurements);
50+
executeTimer.Stop();
51+
Telemetry.Current.TrackEvent("ProcessingContextComplete",
52+
new Dictionary<string, string> {
53+
{ "Name", Name},
54+
{ "Target Project", me.Target.Name},
55+
{ "Target Collection", me.Target.Collection.Name },
56+
{ "Status", Status.ToString() }
57+
},
58+
new Dictionary<string, double> {
59+
{ "ProcessingContextTime", executeTimer.ElapsedMilliseconds }
60+
});
61+
Trace.TraceInformation(string.Format("ProcessingContext Complete {0} ", Name));
5662
}
5763
catch (Exception ex)
5864
{
5965
status = ProcessingStatus.Failed;
60-
stopwatch.Stop();
61-
measurements.Add("ExecutionTime-Milliseconds", stopwatch.ElapsedMilliseconds);
62-
// Send the exception telemetry:
63-
tc.TrackException(ex, properties, measurements);
64-
Trace.TraceError(ex.ToString());
66+
executeTimer.Stop();
67+
Telemetry.Current.TrackException(ex,
68+
new Dictionary<string, string> {
69+
{ "Name", Name},
70+
{ "Target Project", me.Target.Name},
71+
{ "Target Collection", me.Target.Collection.Name },
72+
{ "Status", Status.ToString() }
73+
},
74+
new Dictionary<string, double> {
75+
{ "ProcessingContextTime", executeTimer.ElapsedMilliseconds }
76+
});
77+
Trace.TraceWarning(string.Format(" [EXCEPTION] {0}", ex.Message));
6578
}
6679
}
6780

0 commit comments

Comments
 (0)