Skip to content

Commit 4c7bc63

Browse files
authored
Add git telemetry to job runner (#4789)
* Add git telemetry to task runner * Move git telemetry to job runner * Remove unused dependency * Use PublishTelemetry methon * Remove unused dependency * Get git telemetry in task * Minor fix * Minor fix * Minor fix * Minor fix * Remove unused variable * Return execution to JobRunner * Minor fix * Execute git telemetry as async command * Ensure telemetry await at initialize job stage * Move async command names to constants
1 parent 5c5295c commit 4c7bc63

3 files changed

Lines changed: 70 additions & 13 deletions

File tree

src/Agent.Worker/JobExtension.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,25 @@ public async Task<List<IStep>> InitializeJob(IExecutionContext jobContext, Pipel
238238
}
239239
context.Output("Finished checking job knob settings.");
240240

241+
// Ensure that we send git telemetry before potential path env changes during the pipeline execution
242+
var isSelfHosted = StringUtil.ConvertToBoolean(jobContext.Variables.Get(Constants.Variables.Agent.IsSelfHosted));
243+
if (PlatformUtil.RunningOnWindows && isSelfHosted)
244+
{
245+
try
246+
{
247+
var windowsPreinstalledGitCommand = jobContext.AsyncCommands.Find(c => c != null && c.Name == Constants.AsyncExecution.Commands.Names.WindowsPreinstalledGitTelemetry);
248+
if (windowsPreinstalledGitCommand != null)
249+
{
250+
await windowsPreinstalledGitCommand.WaitAsync();
251+
}
252+
}
253+
catch (Exception ex)
254+
{
255+
// Log the error
256+
Trace.Info($"Caught exception from async command WindowsPreinstalledGitTelemetry: {ex}");
257+
}
258+
}
259+
241260
if (PlatformUtil.RunningOnWindows)
242261
{
243262
// This is for internal testing and is not publicly supported. This will be removed from the agent at a later time.

src/Agent.Worker/JobRunner.cs

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,12 @@ public async Task<TaskResult> RunAsync(Pipelines.AgentJobRequestMessage message,
179179
jobContext.SetVariable(Constants.Variables.System.WorkFolder, HostContext.GetDirectory(WellKnownDirectory.Work), isFilePath: true);
180180

181181
var azureVmCheckCommand = jobContext.GetHostContext().GetService<IAsyncCommandContext>();
182-
azureVmCheckCommand.InitializeCommandContext(jobContext, "GetAzureVMMetada");
182+
azureVmCheckCommand.InitializeCommandContext(jobContext, Constants.AsyncExecution.Commands.Names.GetAzureVMMetada);
183183
azureVmCheckCommand.Task = Task.Run(() => jobContext.SetVariable(Constants.Variables.System.IsAzureVM, PlatformUtil.DetectAzureVM() ? "1" : "0"));
184184
jobContext.AsyncCommands.Add(azureVmCheckCommand);
185185

186186
var dockerDetectCommand = jobContext.GetHostContext().GetService<IAsyncCommandContext>();
187-
dockerDetectCommand.InitializeCommandContext(jobContext, "DetectDockerContainer");
187+
dockerDetectCommand.InitializeCommandContext(jobContext, Constants.AsyncExecution.Commands.Names.DetectDockerContainer);
188188
dockerDetectCommand.Task = Task.Run(() => jobContext.SetVariable(Constants.Variables.System.IsDockerContainer, PlatformUtil.DetectDockerContainer() ? "1" : "0"));
189189
jobContext.AsyncCommands.Add(dockerDetectCommand);
190190

@@ -277,6 +277,31 @@ public async Task<TaskResult> RunAsync(Pipelines.AgentJobRequestMessage message,
277277
this.ExpandProperties(sidecar, jobContext.Variables);
278278
}
279279

280+
// Send telemetry in case if git is preinstalled on windows platform
281+
var isSelfHosted = StringUtil.ConvertToBoolean(jobContext.Variables.Get(Constants.Variables.Agent.IsSelfHosted));
282+
if (PlatformUtil.RunningOnWindows && isSelfHosted)
283+
{
284+
var windowsPreinstalledGitCommand = jobContext.GetHostContext().GetService<IAsyncCommandContext>();
285+
windowsPreinstalledGitCommand.InitializeCommandContext(jobContext, Constants.AsyncExecution.Commands.Names.WindowsPreinstalledGitTelemetry);
286+
windowsPreinstalledGitCommand.Task = Task.Run(() =>
287+
{
288+
var hasPreinstalledGit = false;
289+
290+
var filePath = WhichUtil.Which("git.exe", require: false, trace: null);
291+
if (!string.IsNullOrEmpty(filePath))
292+
{
293+
hasPreinstalledGit = true;
294+
}
295+
296+
PublishTelemetry(context: jobContext, area: "PipelinesTasks", feature: "WindowsGitTelemetry", properties: new Dictionary<string, string>
297+
{
298+
{ "hasPreinstalledGit", hasPreinstalledGit.ToString() }
299+
});
300+
});
301+
302+
jobContext.AsyncCommands.Add(windowsPreinstalledGitCommand);
303+
}
304+
280305
// Get the job extension.
281306
Trace.Info("Getting job extension.");
282307
var hostType = jobContext.Variables.System_HostType;
@@ -301,7 +326,13 @@ public async Task<TaskResult> RunAsync(Pipelines.AgentJobRequestMessage message,
301326
if (AgentKnobs.FailJobWhenAgentDies.GetValue(jobContext).AsBoolean() &&
302327
HostContext.AgentShutdownToken.IsCancellationRequested)
303328
{
304-
PublishTelemetry(jobContext, TaskResult.Failed.ToString(), "111");
329+
PublishTelemetry(context: jobContext, area: "PipelinesTasks", feature: "AgentShutdown", properties: new Dictionary<string, string>
330+
{
331+
{ "JobId", jobContext.Variables.System_JobId.ToString() },
332+
{ "JobResult", TaskResult.Failed.ToString() },
333+
{ "TracePoint", "111"},
334+
});
335+
305336
Trace.Error($"Job is canceled during initialize.");
306337
Trace.Error($"Caught exception: {ex}");
307338
return await CompleteJobAsync(jobServer, jobContext, message, TaskResult.Failed);
@@ -610,20 +641,14 @@ private void ReplaceConfigUriBaseInJobRequestMessage(Pipelines.AgentJobRequestMe
610641
}
611642
}
612643

613-
private void PublishTelemetry(IExecutionContext context, string Task_Result, string TracePoint)
644+
private void PublishTelemetry(IExecutionContext context, string area, String feature, Dictionary<string, string> properties)
614645
{
615646
try
616647
{
617-
var telemetryData = new Dictionary<string, string>
618-
{
619-
{ "JobId", context.Variables.System_JobId.ToString()},
620-
{ "JobResult", Task_Result },
621-
{ "TracePoint", TracePoint},
622-
};
623648
var cmd = new Command("telemetry", "publish");
624-
cmd.Data = JsonConvert.SerializeObject(telemetryData, Formatting.None);
625-
cmd.Properties.Add("area", "PipelinesTasks");
626-
cmd.Properties.Add("feature", "AgentShutdown");
649+
cmd.Data = JsonConvert.SerializeObject(properties, Formatting.None);
650+
cmd.Properties.Add("area", area);
651+
cmd.Properties.Add("feature", feature);
627652

628653
var publishTelemetryCmd = new TelemetryCommandExtension();
629654
publishTelemetryCmd.Initialize(HostContext);

src/Microsoft.VisualStudio.Services.Agent/Constants.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,19 @@ public static class DefaultContainerMounts
123123
public static readonly string Tools = "tools";
124124
}
125125

126+
public static class AsyncExecution
127+
{
128+
public static class Commands
129+
{
130+
public static class Names
131+
{
132+
public static readonly string DetectDockerContainer = "DetectDockerContainer";
133+
public static readonly string GetAzureVMMetada = "GetAzureVMMetada";
134+
public static readonly string WindowsPreinstalledGitTelemetry = "WindowsPreinstalledGitTelemetry";
135+
}
136+
}
137+
}
138+
126139
public static class Agent
127140
{
128141
public static readonly TimeSpan ExitOnUnloadTimeout = TimeSpan.FromSeconds(30);

0 commit comments

Comments
 (0)