Skip to content

Commit

Permalink
Generic telemetry (actions#1321)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
fhammerl and thboop authored Sep 20, 2021
1 parent f259e57 commit 740fb43
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@
"requireExactSource": false,
},
],
}
}
13 changes: 10 additions & 3 deletions src/Runner.Worker/ActionCommandManager.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -113,6 +111,15 @@ public bool TryProcessCommand(IExecutionContext context, string input, Container
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;
Expand Down
5 changes: 5 additions & 0 deletions src/Runner.Worker/ExecutionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public interface IExecutionContext : IRunnerService
Dictionary<string, VariableValue> JobOutputs { get; }
ActionsEnvironmentReference ActionsEnvironment { get; }
List<ActionsStepTelemetry> ActionsStepsTelemetry { get; }
List<JobTelemetry> JobTelemetry { get; }
DictionaryContextData ExpressionValues { get; }
IList<IFunctionInfo> ExpressionFunctions { get; }
JobContext JobContext { get; }
Expand Down Expand Up @@ -150,6 +151,7 @@ public sealed class ExecutionContext : RunnerService, IExecutionContext

public ActionsEnvironmentReference ActionsEnvironment { get; private set; }
public List<ActionsStepTelemetry> ActionsStepsTelemetry { get; private set; }
public List<JobTelemetry> JobTelemetry { get; private set; }
public DictionaryContextData ExpressionValues { get; } = new DictionaryContextData();
public IList<IFunctionInfo> ExpressionFunctions { get; } = new List<IFunctionInfo>();

Expand Down Expand Up @@ -294,6 +296,7 @@ public void RegisterPostJobStep(IStep step)
child.ContextName = contextName;
child.EmbeddedId = embeddedId;
child.SiblingScopeName = siblingScopeName;
child.JobTelemetry = JobTelemetry;
if (intraActionState == null)
{
child.IntraActionState = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
Expand Down Expand Up @@ -650,6 +653,8 @@ public void InitializeJob(Pipelines.AgentJobRequestMessage message, Cancellation
// ActionsStepTelemetry
ActionsStepsTelemetry = new List<ActionsStepTelemetry>();

JobTelemetry = new List<JobTelemetry>();

// Service container info
Global.ServiceContainers = new List<ContainerInfo>();

Expand Down
14 changes: 13 additions & 1 deletion src/Runner.Worker/JobRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,12 @@ private async Task<TaskResult> CompleteJobAsync(IJobServer jobServer, IExecution
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<Exception>();
Expand Down Expand Up @@ -273,6 +277,14 @@ private async Task<TaskResult> CompleteJobAsync(IJobServer jobServer, IExecution
throw new AggregateException(exceptions);
}

private void MaskTelemetrySecrets(List<JobTelemetry> jobTelemetry)
{
foreach (var telemetryItem in jobTelemetry)
{
telemetryItem.Message = HostContext.SecretMasker.MaskSecrets(telemetryItem.Message);
}
}

private async Task ShutdownQueue(bool throwOnFailure)
{
if (_jobServerQueue != null)
Expand Down
20 changes: 20 additions & 0 deletions src/Sdk/DTWebApi/WebApi/JobEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,19 @@ public JobCompletedEvent(
{
this.ActionsEnvironment = actionsEnvironment;
this.ActionsStepsTelemetry = actionsStepsTelemetry;
}

public JobCompletedEvent(
Int64 requestId,
Guid jobId,
TaskResult result,
Dictionary<String, VariableValue> outputs,
ActionsEnvironmentReference actionsEnvironment,
List<ActionsStepTelemetry> actionsStepsTelemetry,
List<JobTelemetry> jobTelemetry)
: this(requestId, jobId, result, outputs, actionsEnvironment, actionsStepsTelemetry)
{
this.JobTelemetry = jobTelemetry;
}

[DataMember(EmitDefaultValue = false)]
Expand Down Expand Up @@ -189,6 +202,13 @@ public List<ActionsStepTelemetry> ActionsStepsTelemetry
get;
set;
}

[DataMember(EmitDefaultValue = false)]
public List<JobTelemetry> JobTelemetry
{
get;
set;
}
}

[DataContract]
Expand Down
17 changes: 17 additions & 0 deletions src/Sdk/DTWebApi/WebApi/JobTelemetry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Runtime.Serialization;

namespace GitHub.DistributedTask.WebApi
{
/// <summary>
/// Information about a job run on the runner
/// </summary>
[DataContract]
public class JobTelemetry
{
[DataMember(EmitDefaultValue = false)]
public string Message { get; set; }

[DataMember(EmitDefaultValue = false)]
public JobTelemetryType Type { get; set; }
}
}
13 changes: 13 additions & 0 deletions src/Sdk/DTWebApi/WebApi/JobTelemetryType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Runtime.Serialization;

namespace GitHub.DistributedTask.WebApi
{
public enum JobTelemetryType
{
[EnumMember]
General = 0,

[EnumMember]
ActionCommand = 1,
}
}

0 comments on commit 740fb43

Please sign in to comment.