Custom logger for MSBuild to receive telemetry events (#4551)

* Upgrade MSBuild references to 15.1.0-preview-000370-00

* Custom logger for MSBuild to receive telemetry events

Had to make the telemetry session ID a static variable so that the forwarding app could use it.

I've tested this all manually and will be writing tests after everyone signs off on the implementation here.
This commit is contained in:
Jeff Kluge 2016-10-31 16:16:39 -07:00 committed by Piotr Puszkiewicz
parent d804502a8a
commit 595a768e30
19 changed files with 98 additions and 21 deletions

View file

@ -97,7 +97,7 @@
<!-- Workaround for https://github.com/dotnet/sdk/issues/115 -->
<ItemGroup>
<HackFilesToCopy Include="$(NuGetPackagesDir)\microsoft.build.runtime\15.1.319-preview5\contentFiles\any\netcoreapp1.0\**;$(NuGetPackagesDir)\microsoft.codeanalysis.build.tasks\2.0.0-beta6-60922-08\contentFiles\any\any\**;" />
<HackFilesToCopy Include="$(NuGetPackagesDir)\microsoft.build.runtime\15.1.0-preview-000370-00\contentFiles\any\netcoreapp1.0\**;$(NuGetPackagesDir)\microsoft.codeanalysis.build.tasks\2.0.0-beta6-60922-08\contentFiles\any\any\**;" />
</ItemGroup>
<Copy SourceFiles="@(HackFilesToCopy)"
DestinationFiles="@(HackFilesToCopy->'$(SdkOutputDirectory)/%(RecursiveDir)%(Filename)%(Extension)')" />

View file

@ -37,7 +37,7 @@
<Version>4.0.0-rc-2037</Version>
</PackageReference>
<PackageReference Include="Microsoft.Build.Utilities.Core">
<Version>0.1.0-preview-00043-160929</Version>
<Version>15.1.0-preview-000370-00</Version>
</PackageReference>
<PackageReference Include="Microsoft.DotNet.PlatformAbstractions">
<Version>1.0.1-beta-000933</Version>

View file

@ -36,10 +36,10 @@
<Version>4.0.0-rc-2037</Version>
</PackageReference>
<PackageReference Include="Microsoft.Build">
<Version>15.1.319-preview5</Version>
<Version>15.1.0-preview-000370-00</Version>
</PackageReference>
<PackageReference Include="Microsoft.Build.Utilities.Core">
<Version>15.1.319-preview5</Version>
<Version>15.1.0-preview-000370-00</Version>
</PackageReference>
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.5' ">

View file

@ -13,8 +13,8 @@
"NuGet.Frameworks": "4.0.0-rc-2037",
"NuGet.ProjectModel": "4.0.0-rc-2037",
"Microsoft.Build": "15.1.0-preview-000366-00",
"Microsoft.Build.Utilities.Core": "15.1.0-preview-000366-00"
"Microsoft.Build": "15.1.0-preview-000370-00",
"Microsoft.Build.Utilities.Core": "15.1.0-preview-000370-00"
},
"frameworks": {
"netstandard1.5": {

View file

@ -21,7 +21,7 @@
<PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Build">
<Version>15.1.319-preview5</Version>
<Version>15.1.0-preview-000370-00</Version>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp">
<Version>2.0.0-beta6-60922-08</Version>

View file

@ -11,7 +11,7 @@
"Microsoft.DotNet.Cli.Utils": {
"target": "project"
},
"Microsoft.Build": "15.1.0-preview-000366-00",
"Microsoft.Build": "15.1.0-preview-000370-00",
"Microsoft.CodeAnalysis.CSharp": "2.0.0-beta6-60922-08"
},
"frameworks": {

View file

@ -15,6 +15,7 @@ namespace Microsoft.DotNet.Cli
{
public class Telemetry : ITelemetry
{
internal static string CurrentSessionId = null;
private TelemetryClient _client = null;
private Dictionary<string, string> _commonProperties = null;
@ -34,7 +35,9 @@ namespace Microsoft.DotNet.Cli
public Telemetry () : this(null) { }
public Telemetry(INuGetCacheSentinel sentinel)
public Telemetry(INuGetCacheSentinel sentinel) : this(sentinel, null) { }
public Telemetry(INuGetCacheSentinel sentinel, string sessionId)
{
Enabled = !Env.GetEnvironmentVariableAsBool(TelemetryOptout) && PermissionExists(sentinel);
@ -43,6 +46,9 @@ namespace Microsoft.DotNet.Cli
return;
}
// Store the session ID in a static field so that it can be reused
CurrentSessionId = sessionId ?? Guid.NewGuid().ToString();
//initialize in task to offload to parallel thread
_trackEventTask = Task.Factory.StartNew(() => InitializeTelemetry());
}
@ -76,7 +82,7 @@ namespace Microsoft.DotNet.Cli
{
_client = new TelemetryClient();
_client.InstrumentationKey = InstrumentationKey;
_client.Context.Session.Id = Guid.NewGuid().ToString();
_client.Context.Session.Id = CurrentSessionId;
_client.Context.Device.OperatingSystem = RuntimeEnvironment.OperatingSystem;

View file

@ -5,6 +5,7 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.DotNet.Cli;
@ -12,6 +13,8 @@ namespace Microsoft.DotNet.Tools.MSBuild
{
public class MSBuildForwardingApp
{
internal const string TelemetrySessionIdEnvironmentVariableName = "DOTNET_CLI_TELEMETRY_SESSIONID";
private const string s_msbuildExeName = "MSBuild.dll";
private readonly ForwardingApp _forwardingApp;
@ -28,6 +31,13 @@ namespace Microsoft.DotNet.Tools.MSBuild
public MSBuildForwardingApp(IEnumerable<string> argsToForward)
{
if (Telemetry.CurrentSessionId != null)
{
Type loggerType = typeof(MSBuildLogger);
argsToForward = argsToForward.Concat(new[] {$"\"/Logger:{loggerType.FullName},{loggerType.GetTypeInfo().Assembly.Location};{Telemetry.CurrentSessionId}\""});
}
_forwardingApp = new ForwardingApp(
GetMSBuildExePath(),
_msbuildRequiredParameters.Concat(argsToForward),
@ -36,7 +46,16 @@ namespace Microsoft.DotNet.Tools.MSBuild
public int Execute()
{
return _forwardingApp.Execute();
try
{
Environment.SetEnvironmentVariable(TelemetrySessionIdEnvironmentVariableName, Telemetry.CurrentSessionId);
return _forwardingApp.Execute();
}
finally
{
Environment.SetEnvironmentVariable(TelemetrySessionIdEnvironmentVariableName, null);
}
}
private static string GetMSBuildExePath()

View file

@ -0,0 +1,52 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Configurer;
namespace Microsoft.DotNet.Tools.MSBuild
{
public sealed class MSBuildLogger : Logger
{
private readonly INuGetCacheSentinel _sentinel = new NuGetCacheSentinel();
private readonly ITelemetry _telemetry;
public MSBuildLogger()
{
string sessionId = Environment.GetEnvironmentVariable(MSBuildForwardingApp.TelemetrySessionIdEnvironmentVariableName);
if (sessionId != null)
{
_telemetry = new Telemetry(_sentinel, sessionId);
}
}
public override void Initialize(IEventSource eventSource)
{
if (_telemetry != null)
{
IEventSource2 eventSource2 = eventSource as IEventSource2;
if (eventSource2 != null)
{
eventSource2.TelemetryLogged += (sender, telemetryEventArgs) =>
{
Console.WriteLine($"Received telemetry event '{telemetryEventArgs.EventName}'");
_telemetry.TrackEvent(telemetryEventArgs.EventName, telemetryEventArgs.Properties, measurements: null);
};
}
}
}
public override void Shutdown()
{
_sentinel?.Dispose();
base.Shutdown();
}
}
}

View file

@ -62,7 +62,7 @@
<IncludeAssets>Analyzers;Build;ContentFiles;Native;Runtime</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Build">
<Version>15.1.319-preview5</Version>
<Version>15.1.0-preview-000370-00</Version>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp">
<Version>2.0.0-beta6-60922-08</Version>

View file

@ -57,7 +57,7 @@
"exclude": "compile"
},
"Microsoft.Build": "15.1.0-preview-000366-00",
"Microsoft.Build": "15.1.0-preview-000370-00",
"Microsoft.CodeAnalysis.CSharp": "2.0.0-beta6-60922-08",
"Microsoft.DotNet.PlatformAbstractions": "1.0.1-beta-000933"
},

View file

@ -19,7 +19,7 @@
"tool_nuget": "1.0.0-preview3-*",
"tool_msbuild": "1.0.0-preview3-*",
"Microsoft.Build.Runtime": "15.1.0-preview-000366-00",
"Microsoft.Build.Runtime": "15.1.0-preview-000370-00",
"Microsoft.CodeAnalysis.Build.Tasks": "2.0.0-beta6-60922-08",
"System.Runtime.Serialization.Xml": "4.1.1",
"NuGet.Build.Tasks": "4.0.0-rc-2037",

View file

@ -21,7 +21,7 @@
<Version>1.0.1</Version>
</PackageReference>
<PackageReference Include="Microsoft.Build.Runtime">
<Version>15.1.319-preview5</Version>
<Version>15.1.0-preview-000370-00</Version>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.Build.Tasks">
<Version>2.0.0-beta6-60922-08</Version>

View file

@ -8,7 +8,7 @@
"type": "platform",
"version": "1.0.1"
},
"Microsoft.Build.Runtime": "15.1.0-preview-000366-00",
"Microsoft.Build.Runtime": "15.1.0-preview-000370-00",
"Microsoft.Net.Compilers.netcore": "2.0.0-beta6-60922-08",
"Microsoft.CodeAnalysis.Build.Tasks": "2.0.0-beta6-60922-08",
"Microsoft.Cci": "4.0.0-rc3-24128-00",

View file

@ -14,7 +14,7 @@
<Version>1.0.1</Version>
</PackageReference>
<PackageReference Include="Microsoft.Build.Runtime">
<Version>15.1.319-preview5</Version>
<Version>15.1.0-preview-000370-00</Version>
</PackageReference>
<PackageReference Include="Microsoft.Net.Compilers.netcore">
<Version>2.0.0-beta6-60922-08</Version>

View file

@ -44,7 +44,7 @@
"xunit": "2.2.0-beta3-build3330",
"dotnet-test-xunit": "1.0.0-rc2-350904-49",
"Microsoft.DotNet.PlatformAbstractions": "1.0.1-beta-000933",
"Microsoft.Build.Runtime": "15.1.0-preview-000366-00"
"Microsoft.Build.Runtime": "15.1.0-preview-000370-00"
},
"frameworks": {
"netcoreapp1.0": {

View file

@ -11,7 +11,7 @@
"dotnet": {
"target": "project"
},
"Microsoft.Build": "15.1.0-preview-000366-00",
"Microsoft.Build": "15.1.0-preview-000370-00",
"xunit": "2.2.0-beta3-build3330",
"dotnet-test-xunit": "1.0.0-rc2-350904-49"
},

View file

@ -15,7 +15,7 @@
<Version>1.0.1</Version>
</PackageReference>
<PackageReference Include="Microsoft.Build.Runtime">
<Version>15.1.319-preview5</Version>
<Version>15.1.0-preview-000370-00</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>

View file

@ -15,7 +15,7 @@
"dotnet": {
"target": "project"
},
"Microsoft.Build.Runtime": "15.1.0-preview-000366-00"
"Microsoft.Build.Runtime": "15.1.0-preview-000370-00"
},
"imports": ["dnxcore50", "portable-net45+win8"]
}