Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add image version telemetry #4893

Merged
53 changes: 34 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" },
aleksandrlevochkin marked this conversation as resolved.
Show resolved Hide resolved
};

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

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

private void OutputImageVersion(IExecutionContext context)
{
string imageVersion = System.Environment.GetEnvironmentVariable(Constants.ImageVersionVariable);

if (imageVersion != null)
KonstantinTyukalov marked this conversation as resolved.
Show resolved Hide resolved
{
context.Output(StringUtil.Loc("ImageVersionLog", imageVersion));
var telemetryData = new Dictionary<string, string>()
{
{ "JobId", context?.Variables?.System_JobId?.ToString() ?? string.Empty },
{ "ImageVersion", imageVersion },
};

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

private void OutputSetupInfo(IExecutionContext context)
{
try
Expand Down Expand Up @@ -724,28 +745,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
Loading