Skip to content

Commit b74907b

Browse files
Add image version telemetry (#4893)
* 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
1 parent ddc249f commit b74907b

File tree

1 file changed

+40
-19
lines changed

1 file changed

+40
-19
lines changed

src/Agent.Worker/JobExtension.cs

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,7 @@ public async Task<List<IStep>> InitializeJob(IExecutionContext jobContext, Pipel
129129

130130
// Machine specific setup info
131131
OutputSetupInfo(context);
132-
133-
string imageVersion = System.Environment.GetEnvironmentVariable(Constants.ImageVersionVariable);
134-
if (imageVersion != null)
135-
{
136-
context.Output(StringUtil.Loc("ImageVersionLog", imageVersion));
137-
}
138-
132+
OutputImageVersion(context);
139133
context.Output(StringUtil.Loc("UserNameLog", System.Environment.UserName));
140134

141135
// Print proxy setting information for better diagnostic experience
@@ -498,7 +492,6 @@ public async Task<List<IStep>> InitializeJob(IExecutionContext jobContext, Pipel
498492
}
499493
}
500494
}
501-
502495
List<IStep> steps = new List<IStep>();
503496
steps.AddRange(preJobSteps);
504497
steps.AddRange(jobSteps);
@@ -515,7 +508,6 @@ public async Task<List<IStep>> InitializeJob(IExecutionContext jobContext, Pipel
515508
// Set the VSTS_PROCESS_LOOKUP_ID env variable.
516509
context.SetVariable(Constants.ProcessLookupId, _processLookupId, false, false);
517510
context.Output("Start tracking orphan processes.");
518-
519511
// Take a snapshot of current running processes
520512
Dictionary<int, Process> processes = SnapshotProcesses();
521513
foreach (var proc in processes)
@@ -534,7 +526,7 @@ public async Task<List<IStep>> InitializeJob(IExecutionContext jobContext, Pipel
534526
if (AgentKnobs.FailJobWhenAgentDies.GetValue(jobContext).AsBoolean() &&
535527
HostContext.AgentShutdownToken.IsCancellationRequested)
536528
{
537-
PublishTelemetry(jobContext, TaskResult.Failed.ToString(), "110");
529+
PublishAgentShutdownTelemetry(jobContext, context);
538530
Trace.Error($"Caught Agent Shutdown exception from JobExtension Initialization: {ex.Message}");
539531
context.Error(ex);
540532
context.Result = TaskResult.Failed;
@@ -564,6 +556,18 @@ public async Task<List<IStep>> InitializeJob(IExecutionContext jobContext, Pipel
564556
}
565557
}
566558

559+
private void PublishAgentShutdownTelemetry(IExecutionContext jobContext, IExecutionContext childContext)
560+
{
561+
var telemetryData = new Dictionary<string, string>
562+
{
563+
{ "JobId", childContext?.Variables?.System_JobId?.ToString() ?? string.Empty },
564+
{ "JobResult", TaskResult.Failed.ToString() },
565+
{ "TracePoint", "110" },
566+
};
567+
568+
PublishTelemetry(jobContext, telemetryData, "AgentShutdown");
569+
}
570+
567571
public async Task FinalizeJob(IExecutionContext jobContext)
568572
{
569573
Trace.Entering();
@@ -691,6 +695,29 @@ private Dictionary<int, Process> SnapshotProcesses()
691695
return snapshot;
692696
}
693697

698+
private void OutputImageVersion(IExecutionContext context)
699+
{
700+
string imageVersion = System.Environment.GetEnvironmentVariable(Constants.ImageVersionVariable);
701+
string jobId = context?.Variables?.System_JobId?.ToString() ?? string.Empty;
702+
703+
if (imageVersion != null)
704+
{
705+
context.Output(StringUtil.Loc("ImageVersionLog", imageVersion));
706+
}
707+
else
708+
{
709+
Trace.Info($"Image version for job id {jobId} is not set");
710+
}
711+
712+
var telemetryData = new Dictionary<string, string>()
713+
{
714+
{ "JobId", jobId },
715+
{ "ImageVersion", imageVersion },
716+
};
717+
718+
PublishTelemetry(context, telemetryData, "ImageVersionTelemetry");
719+
}
720+
694721
private void OutputSetupInfo(IExecutionContext context)
695722
{
696723
try
@@ -724,28 +751,22 @@ private void OutputSetupInfo(IExecutionContext context)
724751
}
725752
}
726753

727-
private void PublishTelemetry(IExecutionContext context, string Task_Result, string TracePoint)
754+
private void PublishTelemetry(IExecutionContext context, Dictionary<string, string> telemetryData, string feature)
728755
{
729756
try
730757
{
731-
var telemetryData = new Dictionary<string, string>
732-
{
733-
{ "JobId", context.Variables.System_JobId.ToString()},
734-
{ "JobResult", Task_Result },
735-
{ "TracePoint", TracePoint},
736-
};
737758
var cmd = new Command("telemetry", "publish");
738759
cmd.Data = JsonConvert.SerializeObject(telemetryData, Formatting.None);
739760
cmd.Properties.Add("area", "PipelinesTasks");
740-
cmd.Properties.Add("feature", "AgentShutdown");
761+
cmd.Properties.Add("feature", feature);
741762

742763
var publishTelemetryCmd = new TelemetryCommandExtension();
743764
publishTelemetryCmd.Initialize(HostContext);
744765
publishTelemetryCmd.ProcessCommand(context, cmd);
745766
}
746767
catch (Exception ex)
747768
{
748-
Trace.Warning($"Unable to publish agent shutdown telemetry data. Exception: {ex}");
769+
Trace.Warning($"Unable to publish telemetry data. Exception: {ex}");
749770
}
750771
}
751772
}

0 commit comments

Comments
 (0)