Skip to content

Commit

Permalink
Add image version telemetry (#4893)
Browse files Browse the repository at this point in the history
* Add image version telemetry

* Add null checks for job id

* Remove empty lines to work around CA rule

* Extract image version output to a method

* Extract another method

* Trace when image version is unset. Publish telemetry in any case
  • Loading branch information
aleksandrlevochkin authored Jul 16, 2024
1 parent ddc249f commit b74907b
Showing 1 changed file with 40 additions and 19 deletions.
59 changes: 40 additions & 19 deletions src/Agent.Worker/JobExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,7 @@ public async Task<List<IStep>> InitializeJob(IExecutionContext jobContext, Pipel

// Machine specific setup info
OutputSetupInfo(context);

string imageVersion = System.Environment.GetEnvironmentVariable(Constants.ImageVersionVariable);
if (imageVersion != null)
{
context.Output(StringUtil.Loc("ImageVersionLog", imageVersion));
}

OutputImageVersion(context);
context.Output(StringUtil.Loc("UserNameLog", System.Environment.UserName));

// Print proxy setting information for better diagnostic experience
Expand Down Expand Up @@ -498,7 +492,6 @@ public async Task<List<IStep>> InitializeJob(IExecutionContext jobContext, Pipel
}
}
}

List<IStep> steps = new List<IStep>();
steps.AddRange(preJobSteps);
steps.AddRange(jobSteps);
Expand All @@ -515,7 +508,6 @@ public async Task<List<IStep>> InitializeJob(IExecutionContext jobContext, Pipel
// Set the VSTS_PROCESS_LOOKUP_ID env variable.
context.SetVariable(Constants.ProcessLookupId, _processLookupId, false, false);
context.Output("Start tracking orphan processes.");

// Take a snapshot of current running processes
Dictionary<int, Process> processes = SnapshotProcesses();
foreach (var proc in processes)
Expand All @@ -534,7 +526,7 @@ public async Task<List<IStep>> InitializeJob(IExecutionContext jobContext, Pipel
if (AgentKnobs.FailJobWhenAgentDies.GetValue(jobContext).AsBoolean() &&
HostContext.AgentShutdownToken.IsCancellationRequested)
{
PublishTelemetry(jobContext, TaskResult.Failed.ToString(), "110");
PublishAgentShutdownTelemetry(jobContext, context);
Trace.Error($"Caught Agent Shutdown exception from JobExtension Initialization: {ex.Message}");
context.Error(ex);
context.Result = TaskResult.Failed;
Expand Down Expand Up @@ -564,6 +556,18 @@ public async Task<List<IStep>> InitializeJob(IExecutionContext jobContext, Pipel
}
}

private void PublishAgentShutdownTelemetry(IExecutionContext jobContext, IExecutionContext childContext)
{
var telemetryData = new Dictionary<string, string>
{
{ "JobId", childContext?.Variables?.System_JobId?.ToString() ?? string.Empty },
{ "JobResult", TaskResult.Failed.ToString() },
{ "TracePoint", "110" },
};

PublishTelemetry(jobContext, telemetryData, "AgentShutdown");
}

public async Task FinalizeJob(IExecutionContext jobContext)
{
Trace.Entering();
Expand Down Expand Up @@ -691,6 +695,29 @@ private Dictionary<int, Process> SnapshotProcesses()
return snapshot;
}

private void OutputImageVersion(IExecutionContext context)
{
string imageVersion = System.Environment.GetEnvironmentVariable(Constants.ImageVersionVariable);
string jobId = context?.Variables?.System_JobId?.ToString() ?? string.Empty;

if (imageVersion != null)
{
context.Output(StringUtil.Loc("ImageVersionLog", imageVersion));
}
else
{
Trace.Info($"Image version for job id {jobId} is not set");
}

var telemetryData = new Dictionary<string, string>()
{
{ "JobId", jobId },
{ "ImageVersion", imageVersion },
};

PublishTelemetry(context, telemetryData, "ImageVersionTelemetry");
}

private void OutputSetupInfo(IExecutionContext context)
{
try
Expand Down Expand Up @@ -724,28 +751,22 @@ private void OutputSetupInfo(IExecutionContext context)
}
}

private void PublishTelemetry(IExecutionContext context, string Task_Result, string TracePoint)
private void PublishTelemetry(IExecutionContext context, Dictionary<string, string> telemetryData, string feature)
{
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.Properties.Add("feature", feature);

var publishTelemetryCmd = new TelemetryCommandExtension();
publishTelemetryCmd.Initialize(HostContext);
publishTelemetryCmd.ProcessCommand(context, cmd);
}
catch (Exception ex)
{
Trace.Warning($"Unable to publish agent shutdown telemetry data. Exception: {ex}");
Trace.Warning($"Unable to publish telemetry data. Exception: {ex}");
}
}
}
Expand Down

0 comments on commit b74907b

Please sign in to comment.