@@ -129,13 +129,7 @@ public async Task<List<IStep>> InitializeJob(IExecutionContext jobContext, Pipel
129
129
130
130
// Machine specific setup info
131
131
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 ) ;
139
133
context . Output ( StringUtil . Loc ( "UserNameLog" , System . Environment . UserName ) ) ;
140
134
141
135
// Print proxy setting information for better diagnostic experience
@@ -498,7 +492,6 @@ public async Task<List<IStep>> InitializeJob(IExecutionContext jobContext, Pipel
498
492
}
499
493
}
500
494
}
501
-
502
495
List < IStep > steps = new List < IStep > ( ) ;
503
496
steps . AddRange ( preJobSteps ) ;
504
497
steps . AddRange ( jobSteps ) ;
@@ -515,7 +508,6 @@ public async Task<List<IStep>> InitializeJob(IExecutionContext jobContext, Pipel
515
508
// Set the VSTS_PROCESS_LOOKUP_ID env variable.
516
509
context . SetVariable ( Constants . ProcessLookupId , _processLookupId , false , false ) ;
517
510
context . Output ( "Start tracking orphan processes." ) ;
518
-
519
511
// Take a snapshot of current running processes
520
512
Dictionary < int , Process > processes = SnapshotProcesses ( ) ;
521
513
foreach ( var proc in processes )
@@ -534,7 +526,7 @@ public async Task<List<IStep>> InitializeJob(IExecutionContext jobContext, Pipel
534
526
if ( AgentKnobs . FailJobWhenAgentDies . GetValue ( jobContext ) . AsBoolean ( ) &&
535
527
HostContext . AgentShutdownToken . IsCancellationRequested )
536
528
{
537
- PublishTelemetry ( jobContext , TaskResult . Failed . ToString ( ) , "110" ) ;
529
+ PublishAgentShutdownTelemetry ( jobContext , context ) ;
538
530
Trace . Error ( $ "Caught Agent Shutdown exception from JobExtension Initialization: { ex . Message } ") ;
539
531
context . Error ( ex ) ;
540
532
context . Result = TaskResult . Failed ;
@@ -564,6 +556,18 @@ public async Task<List<IStep>> InitializeJob(IExecutionContext jobContext, Pipel
564
556
}
565
557
}
566
558
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
+
567
571
public async Task FinalizeJob ( IExecutionContext jobContext )
568
572
{
569
573
Trace . Entering ( ) ;
@@ -691,6 +695,29 @@ private Dictionary<int, Process> SnapshotProcesses()
691
695
return snapshot ;
692
696
}
693
697
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
+
694
721
private void OutputSetupInfo ( IExecutionContext context )
695
722
{
696
723
try
@@ -724,28 +751,22 @@ private void OutputSetupInfo(IExecutionContext context)
724
751
}
725
752
}
726
753
727
- private void PublishTelemetry ( IExecutionContext context , string Task_Result , string TracePoint )
754
+ private void PublishTelemetry ( IExecutionContext context , Dictionary < string , string > telemetryData , string feature )
728
755
{
729
756
try
730
757
{
731
- var telemetryData = new Dictionary < string , string >
732
- {
733
- { "JobId" , context . Variables . System_JobId . ToString ( ) } ,
734
- { "JobResult" , Task_Result } ,
735
- { "TracePoint" , TracePoint } ,
736
- } ;
737
758
var cmd = new Command ( "telemetry" , "publish" ) ;
738
759
cmd . Data = JsonConvert . SerializeObject ( telemetryData , Formatting . None ) ;
739
760
cmd . Properties . Add ( "area" , "PipelinesTasks" ) ;
740
- cmd . Properties . Add ( "feature" , "AgentShutdown" ) ;
761
+ cmd . Properties . Add ( "feature" , feature ) ;
741
762
742
763
var publishTelemetryCmd = new TelemetryCommandExtension ( ) ;
743
764
publishTelemetryCmd . Initialize ( HostContext ) ;
744
765
publishTelemetryCmd . ProcessCommand ( context , cmd ) ;
745
766
}
746
767
catch ( Exception ex )
747
768
{
748
- Trace . Warning ( $ "Unable to publish agent shutdown telemetry data. Exception: { ex } ") ;
769
+ Trace . Warning ( $ "Unable to publish telemetry data. Exception: { ex } ") ;
749
770
}
750
771
}
751
772
}
0 commit comments