Skip to content

Commit

Permalink
Add git telemetry to job runner (#4789)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
vmapetr committed May 24, 2024
1 parent 5c5295c commit 4c7bc63
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 13 deletions.
19 changes: 19 additions & 0 deletions src/Agent.Worker/JobExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,25 @@ public async Task<List<IStep>> InitializeJob(IExecutionContext jobContext, Pipel
}
context.Output("Finished checking job knob settings.");

// Ensure that we send git telemetry before potential path env changes during the pipeline execution
var isSelfHosted = StringUtil.ConvertToBoolean(jobContext.Variables.Get(Constants.Variables.Agent.IsSelfHosted));
if (PlatformUtil.RunningOnWindows && isSelfHosted)
{
try
{
var windowsPreinstalledGitCommand = jobContext.AsyncCommands.Find(c => c != null && c.Name == Constants.AsyncExecution.Commands.Names.WindowsPreinstalledGitTelemetry);
if (windowsPreinstalledGitCommand != null)
{
await windowsPreinstalledGitCommand.WaitAsync();
}
}
catch (Exception ex)
{
// Log the error
Trace.Info($"Caught exception from async command WindowsPreinstalledGitTelemetry: {ex}");
}
}

if (PlatformUtil.RunningOnWindows)
{
// This is for internal testing and is not publicly supported. This will be removed from the agent at a later time.
Expand Down
51 changes: 38 additions & 13 deletions src/Agent.Worker/JobRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,12 @@ public async Task<TaskResult> RunAsync(Pipelines.AgentJobRequestMessage message,
jobContext.SetVariable(Constants.Variables.System.WorkFolder, HostContext.GetDirectory(WellKnownDirectory.Work), isFilePath: true);

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

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

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

// Send telemetry in case if git is preinstalled on windows platform
var isSelfHosted = StringUtil.ConvertToBoolean(jobContext.Variables.Get(Constants.Variables.Agent.IsSelfHosted));
if (PlatformUtil.RunningOnWindows && isSelfHosted)
{
var windowsPreinstalledGitCommand = jobContext.GetHostContext().GetService<IAsyncCommandContext>();
windowsPreinstalledGitCommand.InitializeCommandContext(jobContext, Constants.AsyncExecution.Commands.Names.WindowsPreinstalledGitTelemetry);
windowsPreinstalledGitCommand.Task = Task.Run(() =>
{
var hasPreinstalledGit = false;
var filePath = WhichUtil.Which("git.exe", require: false, trace: null);
if (!string.IsNullOrEmpty(filePath))
{
hasPreinstalledGit = true;
}
PublishTelemetry(context: jobContext, area: "PipelinesTasks", feature: "WindowsGitTelemetry", properties: new Dictionary<string, string>
{
{ "hasPreinstalledGit", hasPreinstalledGit.ToString() }
});
});

jobContext.AsyncCommands.Add(windowsPreinstalledGitCommand);
}

// Get the job extension.
Trace.Info("Getting job extension.");
var hostType = jobContext.Variables.System_HostType;
Expand All @@ -301,7 +326,13 @@ public async Task<TaskResult> RunAsync(Pipelines.AgentJobRequestMessage message,
if (AgentKnobs.FailJobWhenAgentDies.GetValue(jobContext).AsBoolean() &&
HostContext.AgentShutdownToken.IsCancellationRequested)
{
PublishTelemetry(jobContext, TaskResult.Failed.ToString(), "111");
PublishTelemetry(context: jobContext, area: "PipelinesTasks", feature: "AgentShutdown", properties: new Dictionary<string, string>
{
{ "JobId", jobContext.Variables.System_JobId.ToString() },
{ "JobResult", TaskResult.Failed.ToString() },
{ "TracePoint", "111"},
});

Trace.Error($"Job is canceled during initialize.");
Trace.Error($"Caught exception: {ex}");
return await CompleteJobAsync(jobServer, jobContext, message, TaskResult.Failed);
Expand Down Expand Up @@ -610,20 +641,14 @@ private void ReplaceConfigUriBaseInJobRequestMessage(Pipelines.AgentJobRequestMe
}
}

private void PublishTelemetry(IExecutionContext context, string Task_Result, string TracePoint)
private void PublishTelemetry(IExecutionContext context, string area, String feature, Dictionary<string, string> properties)
{
try
{
var telemetryData = new Dictionary<string, string>
{
{ "JobId", context.Variables.System_JobId.ToString()},
{ "JobResult", Task_Result },
{ "TracePoint", TracePoint},
};
var cmd = new Command("telemetry", "publish");
cmd.Data = JsonConvert.SerializeObject(telemetryData, Formatting.None);
cmd.Properties.Add("area", "PipelinesTasks");
cmd.Properties.Add("feature", "AgentShutdown");
cmd.Data = JsonConvert.SerializeObject(properties, Formatting.None);
cmd.Properties.Add("area", area);
cmd.Properties.Add("feature", feature);

var publishTelemetryCmd = new TelemetryCommandExtension();
publishTelemetryCmd.Initialize(HostContext);
Expand Down
13 changes: 13 additions & 0 deletions src/Microsoft.VisualStudio.Services.Agent/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,19 @@ public static class DefaultContainerMounts
public static readonly string Tools = "tools";
}

public static class AsyncExecution
{
public static class Commands
{
public static class Names
{
public static readonly string DetectDockerContainer = "DetectDockerContainer";
public static readonly string GetAzureVMMetada = "GetAzureVMMetada";
public static readonly string WindowsPreinstalledGitTelemetry = "WindowsPreinstalledGitTelemetry";
}
}
}

public static class Agent
{
public static readonly TimeSpan ExitOnUnloadTimeout = TimeSpan.FromSeconds(30);
Expand Down

0 comments on commit 4c7bc63

Please sign in to comment.