From a577c7f2c6405d8035b33875334f9b90ea7fccb2 Mon Sep 17 00:00:00 2001 From: John Erickson Date: Fri, 26 Apr 2024 15:15:11 -0700 Subject: [PATCH 1/2] add context to PC measure plugin callback times log less more logging add tests --- MSBuildCache.sln | 6 +++ src/AzurePipelines.Tests/LoggingTests.cs | 39 +++++++++++++++++++ ...t.MSBuildCache.AzurePipelines.Tests.csproj | 23 +++++++++++ ...crosoft.MSBuildCache.AzurePipelines.csproj | 3 ++ .../PipelineCachingCacheClient.cs | 25 ++++++------ 5 files changed, 85 insertions(+), 11 deletions(-) create mode 100644 src/AzurePipelines.Tests/LoggingTests.cs create mode 100644 src/AzurePipelines.Tests/Microsoft.MSBuildCache.AzurePipelines.Tests.csproj diff --git a/MSBuildCache.sln b/MSBuildCache.sln index 91f9157..518e29c 100644 --- a/MSBuildCache.sln +++ b/MSBuildCache.sln @@ -34,6 +34,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.MSBuildCache.Loca EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.MSBuildCache.Repack.Tests", "src\Repack.Tests\Microsoft.MSBuildCache.Repack.Tests.csproj", "{3BCB6452-B087-4A03-8418-C79F2715DDE7}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.MSBuildCache.AzurePipelines.Tests", "src\AzurePipelines.Tests\Microsoft.MSBuildCache.AzurePipelines.Tests.csproj", "{A8156008-A0EC-4F17-BCF3-A6F07AEF4D22}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|x64 = Debug|x64 @@ -68,6 +70,9 @@ Global {3BCB6452-B087-4A03-8418-C79F2715DDE7}.Debug|x64.Build.0 = Debug|x64 {3BCB6452-B087-4A03-8418-C79F2715DDE7}.Release|x64.ActiveCfg = Release|x64 {3BCB6452-B087-4A03-8418-C79F2715DDE7}.Release|x64.Build.0 = Release|x64 + {A8156008-A0EC-4F17-BCF3-A6F07AEF4D22}.Debug|x64.ActiveCfg = Debug|x64 + {A8156008-A0EC-4F17-BCF3-A6F07AEF4D22}.Debug|x64.Build.0 = Debug|x64 + {A8156008-A0EC-4F17-BCF3-A6F07AEF4D22}.Release|x64.ActiveCfg = Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -80,6 +85,7 @@ Global {97357681-C75E-445D-8547-46F312D01CED} = {EFFB5949-347C-4F28-8964-571D5C6B6209} {F6586428-E047-42C8-B0AC-048DF6DFAF18} = {EFFB5949-347C-4F28-8964-571D5C6B6209} {3BCB6452-B087-4A03-8418-C79F2715DDE7} = {EFFB5949-347C-4F28-8964-571D5C6B6209} + {A8156008-A0EC-4F17-BCF3-A6F07AEF4D22} = {EFFB5949-347C-4F28-8964-571D5C6B6209} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {F1CDA78F-A666-431B-BF44-56DA7DF193BA} diff --git a/src/AzurePipelines.Tests/LoggingTests.cs b/src/AzurePipelines.Tests/LoggingTests.cs new file mode 100644 index 0000000..4276a38 --- /dev/null +++ b/src/AzurePipelines.Tests/LoggingTests.cs @@ -0,0 +1,39 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using BuildXL.Cache.ContentStore.Interfaces.Logging; +using BuildXL.Cache.ContentStore.Interfaces.Tracing; +using BuildXL.Cache.ContentStore.Logging; +using Microsoft.MSBuildCache.AzurePipelines; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Microsoft.MSBuildCache.Tests; + +[TestClass] +public class LoggingTests +{ + [TestMethod] + public void CacheContextIsEmbedded() + { + using ILogger logger = new Logger(); + Context defaultContext = new(logger); + Context cacheContext1 = new(logger); + string message1 = "Hello world!"; + string embedded = PipelineCachingCacheClient.EmbedCacheContext(cacheContext1, message1); + PipelineCachingCacheClient.TryExtractContext(embedded, defaultContext, out Context cacheContext2, out string message2); + Assert.AreEqual(message1, message2); + Assert.AreEqual(cacheContext1.TraceId, cacheContext2.TraceId); + } + + [TestMethod] + public void CacheContextIsNotEmbedded() + { + using ILogger logger = new Logger(); + Context defaultContext = new(logger); + Context cacheContext1 = new(logger); + string message1 = "Hello world!"; + PipelineCachingCacheClient.TryExtractContext(message1, defaultContext, out Context cacheContext2, out string message2); + Assert.AreEqual(message1, message2); + Assert.AreEqual(defaultContext.TraceId, cacheContext2.TraceId); + } +} \ No newline at end of file diff --git a/src/AzurePipelines.Tests/Microsoft.MSBuildCache.AzurePipelines.Tests.csproj b/src/AzurePipelines.Tests/Microsoft.MSBuildCache.AzurePipelines.Tests.csproj new file mode 100644 index 0000000..770476b --- /dev/null +++ b/src/AzurePipelines.Tests/Microsoft.MSBuildCache.AzurePipelines.Tests.csproj @@ -0,0 +1,23 @@ + + + + x64 + x64 + net472;net8.0 + Microsoft.MSBuildCache.AzurePipelines.Tests + + $(NoWarn);CA1861 + + $(NoWarn);CA1034 + + + + + + + + + + + + diff --git a/src/AzurePipelines/Microsoft.MSBuildCache.AzurePipelines.csproj b/src/AzurePipelines/Microsoft.MSBuildCache.AzurePipelines.csproj index 64e072e..11c6229 100644 --- a/src/AzurePipelines/Microsoft.MSBuildCache.AzurePipelines.csproj +++ b/src/AzurePipelines/Microsoft.MSBuildCache.AzurePipelines.csproj @@ -5,6 +5,9 @@ $(Platform) net472;net8.0 + + + diff --git a/src/AzurePipelines/PipelineCachingCacheClient.cs b/src/AzurePipelines/PipelineCachingCacheClient.cs index 44d1bd6..f6e2d55 100644 --- a/src/AzurePipelines/PipelineCachingCacheClient.cs +++ b/src/AzurePipelines/PipelineCachingCacheClient.cs @@ -113,7 +113,7 @@ public PipelineCachingCacheClient( _azureDevopsTracer = new CallbackAppTraceSource( (rawMessage, level) => { - TryExtractContext(rawMessage, out Context cacheContext, out string message); + TryExtractContext(rawMessage, RootContext, out Context cacheContext, out string message); message = $"PipelineCachingCacheClient [{level}]: {message}"; switch (level) { @@ -778,26 +778,29 @@ private string ConvertAbsolutePathToUriPath(string path) return $"/{path}"; } - private const string EmbeddedCacheContextHeader = "[[CacheContext:"; - private static readonly Regex extractCacheContext = new Regex(@"\[\[CacheContext:(.*)\]\](.*)", RegexOptions.Compiled); + private const string EmbeddedCacheContextHeader = "<< - $"{EmbeddedCacheContextHeader}{cacheContext.TraceId}]]{message}"; + internal static string EmbedCacheContext(Context cacheContext, string message) => + $"{EmbeddedCacheContextHeader}{cacheContext.TraceId}{EmbeddedCacheContextTail}{message}"; - private void TryExtractContext(string both, out Context context, out string message) + internal static void TryExtractContext(string both, Context defaultContext, out Context context, out string message) { Match match; - if (both.StartsWith(EmbeddedCacheContextHeader, StringComparison.Ordinal) && +#pragma warning disable CA2249 // cross-compiling for netfx + if (both.IndexOf(EmbeddedCacheContextHeader, StringComparison.Ordinal) >= 0 && (match = extractCacheContext.Match(both)).Success && - Guid.TryParse(match.Captures[0].Value, out Guid contextGuid)) + Guid.TryParse(match.Groups[2].Value, out Guid contextGuid)) { - context = new Context(contextGuid, RootContext.Logger); - message = match.Captures[1].Value; +#pragma warning restore CA2249 // cross-compiling for netfx + context = new Context(contextGuid, defaultContext.Logger); + message = match.Groups[1].Value + match.Groups[3].Value; } else { message = both; - context = RootContext; + context = defaultContext; } } } From fe684c3d61b893622399ca9fda5c6ebef5243937 Mon Sep 17 00:00:00 2001 From: John Erickson Date: Thu, 30 May 2024 15:31:30 -0700 Subject: [PATCH 2/2] tests work in release build --- MSBuildCache.sln | 1 + ...soft.MSBuildCache.AzurePipelines.Tests.csproj | 16 ++++++++++++++++ src/AzurePipelines/PipelineCachingCacheClient.cs | 6 ++++-- src/Common/Microsoft.MSBuildCache.Common.csproj | 2 +- 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/MSBuildCache.sln b/MSBuildCache.sln index 518e29c..024f297 100644 --- a/MSBuildCache.sln +++ b/MSBuildCache.sln @@ -73,6 +73,7 @@ Global {A8156008-A0EC-4F17-BCF3-A6F07AEF4D22}.Debug|x64.ActiveCfg = Debug|x64 {A8156008-A0EC-4F17-BCF3-A6F07AEF4D22}.Debug|x64.Build.0 = Debug|x64 {A8156008-A0EC-4F17-BCF3-A6F07AEF4D22}.Release|x64.ActiveCfg = Release|x64 + {A8156008-A0EC-4F17-BCF3-A6F07AEF4D22}.Release|x64.Build.0 = Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/AzurePipelines.Tests/Microsoft.MSBuildCache.AzurePipelines.Tests.csproj b/src/AzurePipelines.Tests/Microsoft.MSBuildCache.AzurePipelines.Tests.csproj index 770476b..897b4ea 100644 --- a/src/AzurePipelines.Tests/Microsoft.MSBuildCache.AzurePipelines.Tests.csproj +++ b/src/AzurePipelines.Tests/Microsoft.MSBuildCache.AzurePipelines.Tests.csproj @@ -20,4 +20,20 @@ + + + + + + + + + + + + + + + + diff --git a/src/AzurePipelines/PipelineCachingCacheClient.cs b/src/AzurePipelines/PipelineCachingCacheClient.cs index f6e2d55..1af3d67 100644 --- a/src/AzurePipelines/PipelineCachingCacheClient.cs +++ b/src/AzurePipelines/PipelineCachingCacheClient.cs @@ -788,12 +788,14 @@ internal static string EmbedCacheContext(Context cacheContext, string message) = internal static void TryExtractContext(string both, Context defaultContext, out Context context, out string message) { Match match; -#pragma warning disable CA2249 // cross-compiling for netfx +#if NETFRAMEWORK if (both.IndexOf(EmbeddedCacheContextHeader, StringComparison.Ordinal) >= 0 && +#else + if (both.Contains(EmbeddedCacheContextHeader, StringComparison.Ordinal) && +#endif (match = extractCacheContext.Match(both)).Success && Guid.TryParse(match.Groups[2].Value, out Guid contextGuid)) { -#pragma warning restore CA2249 // cross-compiling for netfx context = new Context(contextGuid, defaultContext.Logger); message = match.Groups[1].Value + match.Groups[3].Value; } diff --git a/src/Common/Microsoft.MSBuildCache.Common.csproj b/src/Common/Microsoft.MSBuildCache.Common.csproj index bd1abbe..7fc36ac 100644 --- a/src/Common/Microsoft.MSBuildCache.Common.csproj +++ b/src/Common/Microsoft.MSBuildCache.Common.csproj @@ -12,7 +12,7 @@ - +