From 740fb4373187d62de7ad0b4b249df922c1a70b62 Mon Sep 17 00:00:00 2001 From: Ferenc Hammerl <31069338+fhammerl@users.noreply.github.com> Date: Mon, 20 Sep 2021 14:44:50 +0200 Subject: [PATCH] Generic telemetry (#1321) * Add generateIdTokenUrl as an env var * Add generateIdTokenUrl to env vars * Add basic telemetry class and submit it on jobcompleted * Use constructor overload * Rename telemetry to jobTelemetry * Rename telemetry file * Make JobTelemetryType a string * Collect telemetry * Remove debugger * Update src/Runner.Worker/ActionCommandManager.cs Co-authored-by: Thomas Boop <52323235+thboop@users.noreply.github.com> * Use same JobTelemetry for all contexts * Mask telemetry data * Mask in JobRunner instead * Empty line * Change method signature Returning with a List suggests we clone it and that the original doesn't change.. * Update launch.json Co-authored-by: Thomas Boop <52323235+thboop@users.noreply.github.com> --- .vscode/launch.json | 2 +- src/Runner.Worker/ActionCommandManager.cs | 13 ++++++++++--- src/Runner.Worker/ExecutionContext.cs | 5 +++++ src/Runner.Worker/JobRunner.cs | 14 +++++++++++++- src/Sdk/DTWebApi/WebApi/JobEvent.cs | 20 ++++++++++++++++++++ src/Sdk/DTWebApi/WebApi/JobTelemetry.cs | 17 +++++++++++++++++ src/Sdk/DTWebApi/WebApi/JobTelemetryType.cs | 13 +++++++++++++ 7 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 src/Sdk/DTWebApi/WebApi/JobTelemetry.cs create mode 100644 src/Sdk/DTWebApi/WebApi/JobTelemetryType.cs diff --git a/.vscode/launch.json b/.vscode/launch.json index c4efc63ce..86b93c33b 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -54,4 +54,4 @@ "requireExactSource": false, }, ], -} \ No newline at end of file +} diff --git a/src/Runner.Worker/ActionCommandManager.cs b/src/Runner.Worker/ActionCommandManager.cs index aaa13d3de..0519d05bb 100644 --- a/src/Runner.Worker/ActionCommandManager.cs +++ b/src/Runner.Worker/ActionCommandManager.cs @@ -1,7 +1,5 @@ -using GitHub.DistributedTask.Pipelines; -using GitHub.DistributedTask.Pipelines.ContextData; +using GitHub.DistributedTask.Pipelines.ContextData; using GitHub.DistributedTask.WebApi; -using GitHub.Runner.Common.Util; using GitHub.Runner.Worker.Container; using System; using System.Collections.Generic; @@ -113,6 +111,15 @@ namespace GitHub.Runner.Worker context.Output(input); context.Debug("Paused processing commands until '##[{actionCommand.Data}]' is received"); _stopToken = actionCommand.Data; + if (_registeredCommands.Contains(actionCommand.Data) || string.IsNullOrEmpty(actionCommand.Data)) + { + var telemetry = new JobTelemetry + { + Message = $"Invoked ::stopCommand:: with token: [{actionCommand.Data}]", + Type = JobTelemetryType.ActionCommand + }; + context.JobTelemetry.Add(telemetry); + } _stopProcessCommand = true; _registeredCommands.Add(_stopToken); return true; diff --git a/src/Runner.Worker/ExecutionContext.cs b/src/Runner.Worker/ExecutionContext.cs index dea12711b..274bef361 100644 --- a/src/Runner.Worker/ExecutionContext.cs +++ b/src/Runner.Worker/ExecutionContext.cs @@ -52,6 +52,7 @@ namespace GitHub.Runner.Worker Dictionary JobOutputs { get; } ActionsEnvironmentReference ActionsEnvironment { get; } List ActionsStepsTelemetry { get; } + List JobTelemetry { get; } DictionaryContextData ExpressionValues { get; } IList ExpressionFunctions { get; } JobContext JobContext { get; } @@ -150,6 +151,7 @@ namespace GitHub.Runner.Worker public ActionsEnvironmentReference ActionsEnvironment { get; private set; } public List ActionsStepsTelemetry { get; private set; } + public List JobTelemetry { get; private set; } public DictionaryContextData ExpressionValues { get; } = new DictionaryContextData(); public IList ExpressionFunctions { get; } = new List(); @@ -294,6 +296,7 @@ namespace GitHub.Runner.Worker child.ContextName = contextName; child.EmbeddedId = embeddedId; child.SiblingScopeName = siblingScopeName; + child.JobTelemetry = JobTelemetry; if (intraActionState == null) { child.IntraActionState = new Dictionary(StringComparer.OrdinalIgnoreCase); @@ -650,6 +653,8 @@ namespace GitHub.Runner.Worker // ActionsStepTelemetry ActionsStepsTelemetry = new List(); + JobTelemetry = new List(); + // Service container info Global.ServiceContainers = new List(); diff --git a/src/Runner.Worker/JobRunner.cs b/src/Runner.Worker/JobRunner.cs index 43c5f5f82..faeb4dc8e 100644 --- a/src/Runner.Worker/JobRunner.cs +++ b/src/Runner.Worker/JobRunner.cs @@ -228,8 +228,12 @@ namespace GitHub.Runner.Worker return result; } + // Make sure we don't submit secrets as telemetry + MaskTelemetrySecrets(jobContext.JobTelemetry); + Trace.Info("Raising job completed event."); - var jobCompletedEvent = new JobCompletedEvent(message.RequestId, message.JobId, result, jobContext.JobOutputs, jobContext.ActionsEnvironment, jobContext.ActionsStepsTelemetry); + var jobCompletedEvent = new JobCompletedEvent(message.RequestId, message.JobId, result, jobContext.JobOutputs, jobContext.ActionsEnvironment, jobContext.ActionsStepsTelemetry, jobContext.JobTelemetry); + var completeJobRetryLimit = 5; var exceptions = new List(); @@ -273,6 +277,14 @@ namespace GitHub.Runner.Worker throw new AggregateException(exceptions); } + private void MaskTelemetrySecrets(List jobTelemetry) + { + foreach (var telemetryItem in jobTelemetry) + { + telemetryItem.Message = HostContext.SecretMasker.MaskSecrets(telemetryItem.Message); + } + } + private async Task ShutdownQueue(bool throwOnFailure) { if (_jobServerQueue != null) diff --git a/src/Sdk/DTWebApi/WebApi/JobEvent.cs b/src/Sdk/DTWebApi/WebApi/JobEvent.cs index 37974451a..b838cf58b 100644 --- a/src/Sdk/DTWebApi/WebApi/JobEvent.cs +++ b/src/Sdk/DTWebApi/WebApi/JobEvent.cs @@ -153,6 +153,19 @@ namespace GitHub.DistributedTask.WebApi { this.ActionsEnvironment = actionsEnvironment; this.ActionsStepsTelemetry = actionsStepsTelemetry; + } + + public JobCompletedEvent( + Int64 requestId, + Guid jobId, + TaskResult result, + Dictionary outputs, + ActionsEnvironmentReference actionsEnvironment, + List actionsStepsTelemetry, + List jobTelemetry) + : this(requestId, jobId, result, outputs, actionsEnvironment, actionsStepsTelemetry) + { + this.JobTelemetry = jobTelemetry; } [DataMember(EmitDefaultValue = false)] @@ -189,6 +202,13 @@ namespace GitHub.DistributedTask.WebApi get; set; } + + [DataMember(EmitDefaultValue = false)] + public List JobTelemetry + { + get; + set; + } } [DataContract] diff --git a/src/Sdk/DTWebApi/WebApi/JobTelemetry.cs b/src/Sdk/DTWebApi/WebApi/JobTelemetry.cs new file mode 100644 index 000000000..5da4743f1 --- /dev/null +++ b/src/Sdk/DTWebApi/WebApi/JobTelemetry.cs @@ -0,0 +1,17 @@ +using System.Runtime.Serialization; + +namespace GitHub.DistributedTask.WebApi +{ + /// + /// Information about a job run on the runner + /// + [DataContract] + public class JobTelemetry + { + [DataMember(EmitDefaultValue = false)] + public string Message { get; set; } + + [DataMember(EmitDefaultValue = false)] + public JobTelemetryType Type { get; set; } + } +} diff --git a/src/Sdk/DTWebApi/WebApi/JobTelemetryType.cs b/src/Sdk/DTWebApi/WebApi/JobTelemetryType.cs new file mode 100644 index 000000000..35b552e6d --- /dev/null +++ b/src/Sdk/DTWebApi/WebApi/JobTelemetryType.cs @@ -0,0 +1,13 @@ +using System.Runtime.Serialization; + +namespace GitHub.DistributedTask.WebApi +{ + public enum JobTelemetryType + { + [EnumMember] + General = 0, + + [EnumMember] + ActionCommand = 1, + } +}