From e72e0d7ea6f20a43aaed66a85b86b8d12cce2364 Mon Sep 17 00:00:00 2001 From: Sourabh Jain Date: Mon, 8 Aug 2022 16:56:41 +0530 Subject: [PATCH 1/6] hashed vmid and processname --- .../src/Telemetry/ClientTelemetry.cs | 3 +- .../src/Telemetry/ClientTelemetryOptions.cs | 1 + .../src/Telemetry/Compute.cs | 3 +- .../src/Telemetry/VmMetadataApiHandler.cs | 27 +------------ .../src/Util/CosmosUtils.cs | 38 +++++++++++++++++++ .../ClientTelemetryTests.cs | 6 ++- .../VmMetadataApiHandlerTest.cs | 9 +++-- 7 files changed, 54 insertions(+), 33 deletions(-) create mode 100644 Microsoft.Azure.Cosmos/src/Util/CosmosUtils.cs diff --git a/Microsoft.Azure.Cosmos/src/Telemetry/ClientTelemetry.cs b/Microsoft.Azure.Cosmos/src/Telemetry/ClientTelemetry.cs index 43f8cb841a..63496183f1 100644 --- a/Microsoft.Azure.Cosmos/src/Telemetry/ClientTelemetry.cs +++ b/Microsoft.Azure.Cosmos/src/Telemetry/ClientTelemetry.cs @@ -19,6 +19,7 @@ namespace Microsoft.Azure.Cosmos.Telemetry using Microsoft.Azure.Documents.Collections; using Microsoft.Azure.Documents.Rntbd; using Newtonsoft.Json; + using Util; /// /// This class collects and send all the telemetry information. @@ -99,7 +100,7 @@ private ClientTelemetry( this.clientTelemetryInfo = new ClientTelemetryProperties( clientId: clientId, - processId: System.Diagnostics.Process.GetCurrentProcess().ProcessName, + processId: CosmosUtils.ComputeHash(System.Diagnostics.Process.GetCurrentProcess().ProcessName), userAgent: userAgent, connectionMode: connectionMode, preferredRegions: preferredRegions, diff --git a/Microsoft.Azure.Cosmos/src/Telemetry/ClientTelemetryOptions.cs b/Microsoft.Azure.Cosmos/src/Telemetry/ClientTelemetryOptions.cs index e8dc0cbc13..cb090a0c0c 100644 --- a/Microsoft.Azure.Cosmos/src/Telemetry/ClientTelemetryOptions.cs +++ b/Microsoft.Azure.Cosmos/src/Telemetry/ClientTelemetryOptions.cs @@ -7,6 +7,7 @@ namespace Microsoft.Azure.Cosmos.Telemetry using Microsoft.Azure.Cosmos.Core.Trace; using Microsoft.Azure.Documents; using Newtonsoft.Json; + using Util; internal static class ClientTelemetryOptions { diff --git a/Microsoft.Azure.Cosmos/src/Telemetry/Compute.cs b/Microsoft.Azure.Cosmos/src/Telemetry/Compute.cs index 03d2cdb40f..8cbc7b82aa 100644 --- a/Microsoft.Azure.Cosmos/src/Telemetry/Compute.cs +++ b/Microsoft.Azure.Cosmos/src/Telemetry/Compute.cs @@ -6,6 +6,7 @@ namespace Microsoft.Azure.Cosmos.Telemetry { using System; using Newtonsoft.Json; + using Util; [Serializable] internal sealed class Compute @@ -24,7 +25,7 @@ public Compute( this.AzEnvironment = azEnvironment; this.OSType = oSType; this.VMSize = vMSize; - this.VMId = "vmId:" + vMId; + this.VMId = "vmId:" + CosmosUtils.ComputeHash(vMId); } [JsonProperty(PropertyName = "location")] diff --git a/Microsoft.Azure.Cosmos/src/Telemetry/VmMetadataApiHandler.cs b/Microsoft.Azure.Cosmos/src/Telemetry/VmMetadataApiHandler.cs index 9dbebeb1e6..6a9c994427 100644 --- a/Microsoft.Azure.Cosmos/src/Telemetry/VmMetadataApiHandler.cs +++ b/Microsoft.Azure.Cosmos/src/Telemetry/VmMetadataApiHandler.cs @@ -12,6 +12,7 @@ namespace Microsoft.Azure.Cosmos.Telemetry using Microsoft.Azure.Cosmos.Core.Trace; using Microsoft.Azure.Documents; using Newtonsoft.Json.Linq; + using Util; /// /// Task to collect virtual machine metadata information. using instance metedata service API. @@ -139,35 +140,11 @@ internal static string GetCloudInformation() return VmMetadataApiHandler.azMetadata?.Compute?.AzEnvironment ?? VmMetadataApiHandler.nonAzureCloud; } - /// - /// Hash a passed Value - /// - /// - /// hashed Value - internal static string ComputeHash(string rawData) - { - if (string.IsNullOrEmpty(rawData)) - { - throw new ArgumentNullException(nameof(rawData)); - } - - // Create a SHA256 - using (SHA256 sha256Hash = SHA256.Create()) - { - // ComputeHash - returns byte array - byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(rawData)); - Array.Resize(ref bytes, 16); - - // Convert byte array to a string - return new Guid(bytes).ToString(); - } - } - private static readonly Lazy uniqueId = new Lazy(() => { try { - return "hashedMachineName:" + VmMetadataApiHandler.ComputeHash(Environment.MachineName); + return "hashedMachineName:" + CosmosUtils.ComputeHash(Environment.MachineName); } catch (Exception ex) { diff --git a/Microsoft.Azure.Cosmos/src/Util/CosmosUtils.cs b/Microsoft.Azure.Cosmos/src/Util/CosmosUtils.cs new file mode 100644 index 0000000000..eca157d751 --- /dev/null +++ b/Microsoft.Azure.Cosmos/src/Util/CosmosUtils.cs @@ -0,0 +1,38 @@ +//------------------------------------------------------------ +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------ + +namespace Microsoft.Azure.Cosmos.Util +{ + using System; + using System.Collections.Generic; + using System.Security.Cryptography; + using System.Text; + + internal class CosmosUtils + { + /// + /// Hash a passed Value + /// + /// + /// hashed Value + internal static string ComputeHash(string rawData) + { + if (string.IsNullOrEmpty(rawData)) + { + throw new ArgumentNullException(nameof(rawData)); + } + + // Create a SHA256 + using (SHA256 sha256Hash = SHA256.Create()) + { + // ComputeHash - returns byte array + byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(rawData)); + Array.Resize(ref bytes, 16); + + // Convert byte array to a string + return new Guid(bytes).ToString(); + } + } + } +} diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/ClientTelemetryTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/ClientTelemetryTests.cs index 06d9e80b86..4693c570a5 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/ClientTelemetryTests.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/ClientTelemetryTests.cs @@ -23,6 +23,7 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests using Documents.Rntbd; using System.Globalization; using System.Linq; + using Cosmos.Util; [TestClass] public class ClientTelemetryTests : BaseCosmosClientHelper @@ -935,6 +936,7 @@ private static void AssertAccountLevelInformation( Assert.IsNull(telemetryInfo.AcceleratedNetworking); Assert.IsNotNull(telemetryInfo.ClientId); Assert.IsNotNull(telemetryInfo.ProcessId); + Assert.AreEqual(CosmosUtils.ComputeHash(System.Diagnostics.Process.GetCurrentProcess().ProcessName), telemetryInfo.ProcessId); Assert.IsNotNull(telemetryInfo.UserAgent); Assert.IsNotNull(telemetryInfo.ConnectionMode); @@ -948,11 +950,11 @@ private static void AssertAccountLevelInformation( { if (isAzureInstance.Value) { - Assert.AreEqual("vmId:d0cb93eb-214b-4c2b-bd3d-cc93e90d9efd", machineId.First(), $"Generated Machine id is : {machineId.First()}"); + Assert.AreEqual($"vmId:{CosmosUtils.ComputeHash("d0cb93eb-214b-4c2b-bd3d-cc93e90d9efd")}", machineId.First(), $"Generated Machine id is : {machineId.First()}"); } else { - Assert.AreNotEqual("vmId:d0cb93eb-214b-4c2b-bd3d-cc93e90d9efd", machineId.First(), $"Generated Machine id is : {machineId.First()}"); + Assert.AreNotEqual($"vmId:{CosmosUtils.ComputeHash("d0cb93eb-214b-4c2b-bd3d-cc93e90d9efd")}", machineId.First(), $"Generated Machine id is : {machineId.First()}"); Assert.AreEqual(1, machineId.Count, $"Multiple Machine Id has been generated i.e {JsonConvert.SerializeObject(machineId)}"); } } diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/VmMetadataApiHandlerTest.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/VmMetadataApiHandlerTest.cs index ea600dda3c..57c75bca46 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/VmMetadataApiHandlerTest.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/VmMetadataApiHandlerTest.cs @@ -18,6 +18,7 @@ namespace Microsoft.Azure.Cosmos using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; using Newtonsoft.Json; + using Util; [TestClass] public class VmMetadataApiHandlerTest @@ -56,7 +57,7 @@ static Task sendFunc(HttpRequestMessage request, Cancellati VmMetadataApiHandler.TryInitialize(cosmoshttpClient); await Task.Delay(2000); - Assert.AreEqual("vmId:d0cb93eb-214b-4c2b-bd3d-cc93e90d9efd", VmMetadataApiHandler.GetMachineId()); + Assert.AreEqual($"vmId:{CosmosUtils.ComputeHash("d0cb93eb-214b-4c2b-bd3d-cc93e90d9efd")}", VmMetadataApiHandler.GetMachineId()); Assert.AreEqual(VmMetadataApiHandler.GetMachineRegion(), "eastus"); } @@ -88,7 +89,7 @@ static Task sendFunc(HttpRequestMessage request, Cancellati [TestMethod] public async Task GetHashedMachineNameAsMachineIdTest() { - string expectedMachineId = "hashedMachineName:" + VmMetadataApiHandler.ComputeHash(Environment.MachineName); + string expectedMachineId = "hashedMachineName:" + CosmosUtils.ComputeHash(Environment.MachineName); static Task sendFunc(HttpRequestMessage request, CancellationToken cancellationToken) { throw new Exception("error while making API call"); }; @@ -105,7 +106,7 @@ public async Task GetHashedMachineNameAsMachineIdTest() [TestMethod] public void ComputeHashTest() { - string hashedValue = VmMetadataApiHandler.ComputeHash("abc"); + string hashedValue = CosmosUtils.ComputeHash("abc"); Assert.AreEqual("bf1678ba-018f-eacf-4141-40de5dae2223", hashedValue); } @@ -128,7 +129,7 @@ public async Task ParseAzureVMMetadataTest() Assert.AreEqual("AzurePublicCloud", metadata.Compute.AzEnvironment); Assert.AreEqual("Linux", metadata.Compute.OSType); Assert.AreEqual("Standard_D2s_v3", metadata.Compute.VMSize); - Assert.AreEqual("vmId:d0cb93eb-214b-4c2b-bd3d-cc93e90d9efd", metadata.Compute.VMId); + Assert.AreEqual($"vmId:{CosmosUtils.ComputeHash("d0cb93eb-214b-4c2b-bd3d-cc93e90d9efd")}", metadata.Compute.VMId); } [TestMethod] From aa7e3853fcc1b004044b66aa56ca939c344796ff Mon Sep 17 00:00:00 2001 From: Sourabh Jain Date: Mon, 8 Aug 2022 23:49:35 +0530 Subject: [PATCH 2/6] appended hashedvmid --- Microsoft.Azure.Cosmos/src/Telemetry/Compute.cs | 2 +- .../src/Telemetry/VmMetadataApiHandler.cs | 9 +++++++-- .../ClientTelemetryTests.cs | 4 ++-- .../VmMetadataApiHandlerTest.cs | 6 +++--- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/Microsoft.Azure.Cosmos/src/Telemetry/Compute.cs b/Microsoft.Azure.Cosmos/src/Telemetry/Compute.cs index 8cbc7b82aa..3d2b01c90a 100644 --- a/Microsoft.Azure.Cosmos/src/Telemetry/Compute.cs +++ b/Microsoft.Azure.Cosmos/src/Telemetry/Compute.cs @@ -25,7 +25,7 @@ public Compute( this.AzEnvironment = azEnvironment; this.OSType = oSType; this.VMSize = vMSize; - this.VMId = "vmId:" + CosmosUtils.ComputeHash(vMId); + this.VMId = VmMetadataApiHandler.HashedVmIdPrefix + CosmosUtils.ComputeHash(vMId); } [JsonProperty(PropertyName = "location")] diff --git a/Microsoft.Azure.Cosmos/src/Telemetry/VmMetadataApiHandler.cs b/Microsoft.Azure.Cosmos/src/Telemetry/VmMetadataApiHandler.cs index 6a9c994427..724f7c1f7c 100644 --- a/Microsoft.Azure.Cosmos/src/Telemetry/VmMetadataApiHandler.cs +++ b/Microsoft.Azure.Cosmos/src/Telemetry/VmMetadataApiHandler.cs @@ -21,7 +21,12 @@ namespace Microsoft.Azure.Cosmos.Telemetry /// internal static class VmMetadataApiHandler { + internal const string HashedMachineNamePrefix = "hashedMachineName:"; + internal const string HashedVmIdPrefix = "hashedVmId:"; + internal const string UuidPrefix = "uuid:"; + internal static readonly Uri vmMetadataEndpointUrl = new ("http://169.254.169.254/metadata/instance?api-version=2020-06-01"); + private static readonly string nonAzureCloud = "NonAzureVM"; private static readonly object lockObject = new object(); @@ -144,14 +149,14 @@ internal static string GetCloudInformation() { try { - return "hashedMachineName:" + CosmosUtils.ComputeHash(Environment.MachineName); + return VmMetadataApiHandler.HashedMachineNamePrefix + CosmosUtils.ComputeHash(Environment.MachineName); } catch (Exception ex) { DefaultTrace.TraceWarning("Error while generating hashed machine name " + ex.Message); } - return "uuid:" + Guid.NewGuid().ToString(); + return VmMetadataApiHandler.UuidPrefix + Guid.NewGuid().ToString(); }); } diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/ClientTelemetryTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/ClientTelemetryTests.cs index 4693c570a5..696d134e17 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/ClientTelemetryTests.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/ClientTelemetryTests.cs @@ -950,11 +950,11 @@ private static void AssertAccountLevelInformation( { if (isAzureInstance.Value) { - Assert.AreEqual($"vmId:{CosmosUtils.ComputeHash("d0cb93eb-214b-4c2b-bd3d-cc93e90d9efd")}", machineId.First(), $"Generated Machine id is : {machineId.First()}"); + Assert.AreEqual($"{VmMetadataApiHandler.HashedVmIdPrefix}{CosmosUtils.ComputeHash("d0cb93eb-214b-4c2b-bd3d-cc93e90d9efd")}", machineId.First(), $"Generated Machine id is : {machineId.First()}"); } else { - Assert.AreNotEqual($"vmId:{CosmosUtils.ComputeHash("d0cb93eb-214b-4c2b-bd3d-cc93e90d9efd")}", machineId.First(), $"Generated Machine id is : {machineId.First()}"); + Assert.AreNotEqual($"{VmMetadataApiHandler.HashedVmIdPrefix}{CosmosUtils.ComputeHash("d0cb93eb-214b-4c2b-bd3d-cc93e90d9efd")}", machineId.First(), $"Generated Machine id is : {machineId.First()}"); Assert.AreEqual(1, machineId.Count, $"Multiple Machine Id has been generated i.e {JsonConvert.SerializeObject(machineId)}"); } } diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/VmMetadataApiHandlerTest.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/VmMetadataApiHandlerTest.cs index 57c75bca46..cf599321d1 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/VmMetadataApiHandlerTest.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/VmMetadataApiHandlerTest.cs @@ -57,7 +57,7 @@ static Task sendFunc(HttpRequestMessage request, Cancellati VmMetadataApiHandler.TryInitialize(cosmoshttpClient); await Task.Delay(2000); - Assert.AreEqual($"vmId:{CosmosUtils.ComputeHash("d0cb93eb-214b-4c2b-bd3d-cc93e90d9efd")}", VmMetadataApiHandler.GetMachineId()); + Assert.AreEqual($"{VmMetadataApiHandler.HashedVmIdPrefix}{CosmosUtils.ComputeHash("d0cb93eb-214b-4c2b-bd3d-cc93e90d9efd")}", VmMetadataApiHandler.GetMachineId()); Assert.AreEqual(VmMetadataApiHandler.GetMachineRegion(), "eastus"); } @@ -89,7 +89,7 @@ static Task sendFunc(HttpRequestMessage request, Cancellati [TestMethod] public async Task GetHashedMachineNameAsMachineIdTest() { - string expectedMachineId = "hashedMachineName:" + CosmosUtils.ComputeHash(Environment.MachineName); + string expectedMachineId = VmMetadataApiHandler.HashedMachineNamePrefix + CosmosUtils.ComputeHash(Environment.MachineName); static Task sendFunc(HttpRequestMessage request, CancellationToken cancellationToken) { throw new Exception("error while making API call"); }; @@ -129,7 +129,7 @@ public async Task ParseAzureVMMetadataTest() Assert.AreEqual("AzurePublicCloud", metadata.Compute.AzEnvironment); Assert.AreEqual("Linux", metadata.Compute.OSType); Assert.AreEqual("Standard_D2s_v3", metadata.Compute.VMSize); - Assert.AreEqual($"vmId:{CosmosUtils.ComputeHash("d0cb93eb-214b-4c2b-bd3d-cc93e90d9efd")}", metadata.Compute.VMId); + Assert.AreEqual($"{VmMetadataApiHandler.HashedVmIdPrefix}{CosmosUtils.ComputeHash("d0cb93eb-214b-4c2b-bd3d-cc93e90d9efd")}", metadata.Compute.VMId); } [TestMethod] From 1bfa0a867917a7945d80e6f14c1131d7609dc40e Mon Sep 17 00:00:00 2001 From: Sourabh Jain Date: Tue, 9 Aug 2022 08:35:54 +0530 Subject: [PATCH 3/6] dummycommit --- .../Microsoft.Azure.Cosmos.EmulatorTests/ClientTelemetryTests.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/ClientTelemetryTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/ClientTelemetryTests.cs index 696d134e17..c313f5358c 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/ClientTelemetryTests.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/ClientTelemetryTests.cs @@ -958,7 +958,6 @@ private static void AssertAccountLevelInformation( Assert.AreEqual(1, machineId.Count, $"Multiple Machine Id has been generated i.e {JsonConvert.SerializeObject(machineId)}"); } } - } [TestMethod] From 54ccf6801839d9d2ec79d81ef26f4e690d744fa9 Mon Sep 17 00:00:00 2001 From: Sourabh Jain Date: Wed, 10 Aug 2022 00:06:40 +0530 Subject: [PATCH 4/6] use string format --- Microsoft.Azure.Cosmos/src/Telemetry/Compute.cs | 2 +- Microsoft.Azure.Cosmos/src/Telemetry/VmMetadataApiHandler.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Microsoft.Azure.Cosmos/src/Telemetry/Compute.cs b/Microsoft.Azure.Cosmos/src/Telemetry/Compute.cs index 3d2b01c90a..95768b4851 100644 --- a/Microsoft.Azure.Cosmos/src/Telemetry/Compute.cs +++ b/Microsoft.Azure.Cosmos/src/Telemetry/Compute.cs @@ -25,7 +25,7 @@ public Compute( this.AzEnvironment = azEnvironment; this.OSType = oSType; this.VMSize = vMSize; - this.VMId = VmMetadataApiHandler.HashedVmIdPrefix + CosmosUtils.ComputeHash(vMId); + this.VMId = $"{VmMetadataApiHandler.HashedVmIdPrefix}{CosmosUtils.ComputeHash(vMId)}"; } [JsonProperty(PropertyName = "location")] diff --git a/Microsoft.Azure.Cosmos/src/Telemetry/VmMetadataApiHandler.cs b/Microsoft.Azure.Cosmos/src/Telemetry/VmMetadataApiHandler.cs index 724f7c1f7c..9bc10e9db2 100644 --- a/Microsoft.Azure.Cosmos/src/Telemetry/VmMetadataApiHandler.cs +++ b/Microsoft.Azure.Cosmos/src/Telemetry/VmMetadataApiHandler.cs @@ -149,14 +149,14 @@ internal static string GetCloudInformation() { try { - return VmMetadataApiHandler.HashedMachineNamePrefix + CosmosUtils.ComputeHash(Environment.MachineName); + return $"{VmMetadataApiHandler.HashedMachineNamePrefix}{CosmosUtils.ComputeHash(Environment.MachineName)}"; } catch (Exception ex) { DefaultTrace.TraceWarning("Error while generating hashed machine name " + ex.Message); } - return VmMetadataApiHandler.UuidPrefix + Guid.NewGuid().ToString(); + return $"{VmMetadataApiHandler.UuidPrefix}{Guid.NewGuid()}"; }); } From 173b4c4f8d58a13c405bc2c3f10b3f7d6f15c1d9 Mon Sep 17 00:00:00 2001 From: Sourabh Jain Date: Wed, 17 Aug 2022 00:36:03 +0530 Subject: [PATCH 5/6] renamed util class name --- Microsoft.Azure.Cosmos/src/Telemetry/ClientTelemetry.cs | 2 +- Microsoft.Azure.Cosmos/src/Telemetry/Compute.cs | 2 +- .../src/Telemetry/VmMetadataApiHandler.cs | 2 +- .../src/Util/{CosmosUtils.cs => HashingExtension.cs} | 2 +- .../VmMetadataApiHandlerTest.cs | 8 ++++---- 5 files changed, 8 insertions(+), 8 deletions(-) rename Microsoft.Azure.Cosmos/src/Util/{CosmosUtils.cs => HashingExtension.cs} (96%) diff --git a/Microsoft.Azure.Cosmos/src/Telemetry/ClientTelemetry.cs b/Microsoft.Azure.Cosmos/src/Telemetry/ClientTelemetry.cs index 63496183f1..e2d8ac0b93 100644 --- a/Microsoft.Azure.Cosmos/src/Telemetry/ClientTelemetry.cs +++ b/Microsoft.Azure.Cosmos/src/Telemetry/ClientTelemetry.cs @@ -100,7 +100,7 @@ private ClientTelemetry( this.clientTelemetryInfo = new ClientTelemetryProperties( clientId: clientId, - processId: CosmosUtils.ComputeHash(System.Diagnostics.Process.GetCurrentProcess().ProcessName), + processId: HashingExtension.ComputeHash(System.Diagnostics.Process.GetCurrentProcess().ProcessName), userAgent: userAgent, connectionMode: connectionMode, preferredRegions: preferredRegions, diff --git a/Microsoft.Azure.Cosmos/src/Telemetry/Compute.cs b/Microsoft.Azure.Cosmos/src/Telemetry/Compute.cs index 95768b4851..b09af55cce 100644 --- a/Microsoft.Azure.Cosmos/src/Telemetry/Compute.cs +++ b/Microsoft.Azure.Cosmos/src/Telemetry/Compute.cs @@ -25,7 +25,7 @@ public Compute( this.AzEnvironment = azEnvironment; this.OSType = oSType; this.VMSize = vMSize; - this.VMId = $"{VmMetadataApiHandler.HashedVmIdPrefix}{CosmosUtils.ComputeHash(vMId)}"; + this.VMId = $"{VmMetadataApiHandler.HashedVmIdPrefix}{HashingExtension.ComputeHash(vMId)}"; } [JsonProperty(PropertyName = "location")] diff --git a/Microsoft.Azure.Cosmos/src/Telemetry/VmMetadataApiHandler.cs b/Microsoft.Azure.Cosmos/src/Telemetry/VmMetadataApiHandler.cs index 9bc10e9db2..e8c4a802ef 100644 --- a/Microsoft.Azure.Cosmos/src/Telemetry/VmMetadataApiHandler.cs +++ b/Microsoft.Azure.Cosmos/src/Telemetry/VmMetadataApiHandler.cs @@ -149,7 +149,7 @@ internal static string GetCloudInformation() { try { - return $"{VmMetadataApiHandler.HashedMachineNamePrefix}{CosmosUtils.ComputeHash(Environment.MachineName)}"; + return $"{VmMetadataApiHandler.HashedMachineNamePrefix}{HashingExtension.ComputeHash(Environment.MachineName)}"; } catch (Exception ex) { diff --git a/Microsoft.Azure.Cosmos/src/Util/CosmosUtils.cs b/Microsoft.Azure.Cosmos/src/Util/HashingExtension.cs similarity index 96% rename from Microsoft.Azure.Cosmos/src/Util/CosmosUtils.cs rename to Microsoft.Azure.Cosmos/src/Util/HashingExtension.cs index eca157d751..cd3f190b15 100644 --- a/Microsoft.Azure.Cosmos/src/Util/CosmosUtils.cs +++ b/Microsoft.Azure.Cosmos/src/Util/HashingExtension.cs @@ -9,7 +9,7 @@ namespace Microsoft.Azure.Cosmos.Util using System.Security.Cryptography; using System.Text; - internal class CosmosUtils + internal class HashingExtension { /// /// Hash a passed Value diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/VmMetadataApiHandlerTest.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/VmMetadataApiHandlerTest.cs index cf599321d1..ed25aa72eb 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/VmMetadataApiHandlerTest.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/VmMetadataApiHandlerTest.cs @@ -57,7 +57,7 @@ static Task sendFunc(HttpRequestMessage request, Cancellati VmMetadataApiHandler.TryInitialize(cosmoshttpClient); await Task.Delay(2000); - Assert.AreEqual($"{VmMetadataApiHandler.HashedVmIdPrefix}{CosmosUtils.ComputeHash("d0cb93eb-214b-4c2b-bd3d-cc93e90d9efd")}", VmMetadataApiHandler.GetMachineId()); + Assert.AreEqual($"{VmMetadataApiHandler.HashedVmIdPrefix}{HashingExtension.ComputeHash("d0cb93eb-214b-4c2b-bd3d-cc93e90d9efd")}", VmMetadataApiHandler.GetMachineId()); Assert.AreEqual(VmMetadataApiHandler.GetMachineRegion(), "eastus"); } @@ -89,7 +89,7 @@ static Task sendFunc(HttpRequestMessage request, Cancellati [TestMethod] public async Task GetHashedMachineNameAsMachineIdTest() { - string expectedMachineId = VmMetadataApiHandler.HashedMachineNamePrefix + CosmosUtils.ComputeHash(Environment.MachineName); + string expectedMachineId = VmMetadataApiHandler.HashedMachineNamePrefix + HashingExtension.ComputeHash(Environment.MachineName); static Task sendFunc(HttpRequestMessage request, CancellationToken cancellationToken) { throw new Exception("error while making API call"); }; @@ -106,7 +106,7 @@ public async Task GetHashedMachineNameAsMachineIdTest() [TestMethod] public void ComputeHashTest() { - string hashedValue = CosmosUtils.ComputeHash("abc"); + string hashedValue = HashingExtension.ComputeHash("abc"); Assert.AreEqual("bf1678ba-018f-eacf-4141-40de5dae2223", hashedValue); } @@ -129,7 +129,7 @@ public async Task ParseAzureVMMetadataTest() Assert.AreEqual("AzurePublicCloud", metadata.Compute.AzEnvironment); Assert.AreEqual("Linux", metadata.Compute.OSType); Assert.AreEqual("Standard_D2s_v3", metadata.Compute.VMSize); - Assert.AreEqual($"{VmMetadataApiHandler.HashedVmIdPrefix}{CosmosUtils.ComputeHash("d0cb93eb-214b-4c2b-bd3d-cc93e90d9efd")}", metadata.Compute.VMId); + Assert.AreEqual($"{VmMetadataApiHandler.HashedVmIdPrefix}{HashingExtension.ComputeHash("d0cb93eb-214b-4c2b-bd3d-cc93e90d9efd")}", metadata.Compute.VMId); } [TestMethod] From bfb6c1386eefc85fc12f1dac2b101f150915d009 Mon Sep 17 00:00:00 2001 From: Sourabh Jain Date: Wed, 17 Aug 2022 00:53:35 +0530 Subject: [PATCH 6/6] fix refrence --- .../ClientTelemetryTests.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/ClientTelemetryTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/ClientTelemetryTests.cs index c313f5358c..8ce2aa9327 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/ClientTelemetryTests.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/ClientTelemetryTests.cs @@ -936,7 +936,7 @@ private static void AssertAccountLevelInformation( Assert.IsNull(telemetryInfo.AcceleratedNetworking); Assert.IsNotNull(telemetryInfo.ClientId); Assert.IsNotNull(telemetryInfo.ProcessId); - Assert.AreEqual(CosmosUtils.ComputeHash(System.Diagnostics.Process.GetCurrentProcess().ProcessName), telemetryInfo.ProcessId); + Assert.AreEqual(HashingExtension.ComputeHash(System.Diagnostics.Process.GetCurrentProcess().ProcessName), telemetryInfo.ProcessId); Assert.IsNotNull(telemetryInfo.UserAgent); Assert.IsNotNull(telemetryInfo.ConnectionMode); @@ -950,11 +950,11 @@ private static void AssertAccountLevelInformation( { if (isAzureInstance.Value) { - Assert.AreEqual($"{VmMetadataApiHandler.HashedVmIdPrefix}{CosmosUtils.ComputeHash("d0cb93eb-214b-4c2b-bd3d-cc93e90d9efd")}", machineId.First(), $"Generated Machine id is : {machineId.First()}"); + Assert.AreEqual($"{VmMetadataApiHandler.HashedVmIdPrefix}{HashingExtension.ComputeHash("d0cb93eb-214b-4c2b-bd3d-cc93e90d9efd")}", machineId.First(), $"Generated Machine id is : {machineId.First()}"); } else { - Assert.AreNotEqual($"{VmMetadataApiHandler.HashedVmIdPrefix}{CosmosUtils.ComputeHash("d0cb93eb-214b-4c2b-bd3d-cc93e90d9efd")}", machineId.First(), $"Generated Machine id is : {machineId.First()}"); + Assert.AreNotEqual($"{VmMetadataApiHandler.HashedVmIdPrefix}{HashingExtension.ComputeHash("d0cb93eb-214b-4c2b-bd3d-cc93e90d9efd")}", machineId.First(), $"Generated Machine id is : {machineId.First()}"); Assert.AreEqual(1, machineId.Count, $"Multiple Machine Id has been generated i.e {JsonConvert.SerializeObject(machineId)}"); } }