Skip to content

Commit

Permalink
Telemetry: Fixes Inconsistent behavior of VM Metadata Async Initializ…
Browse files Browse the repository at this point in the history
…ation (#3252)

* add cached machineId field to client config

* allow multiple machineId to exist when using Azure VM

* perf improvement: change string equality check to reference equals
  • Loading branch information
imanvt authored Jun 3, 2022
1 parent cce4250 commit 35c177b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public ClientConfigurationTraceDatum(CosmosClientContext cosmosClientContext, Da
this.cachedNumberOfClientCreated = CosmosClient.numberOfClientsCreated;
this.cachedNumberOfActiveClient = CosmosClient.NumberOfActiveClients;
this.cachedUserAgentString = this.UserAgentContainer.UserAgent;
this.cachedMachineId = VmMetadataApiHandler.GetMachineId();
this.cachedSerializedJson = this.GetSerializedDatum();
this.ProcessorCount = Environment.ProcessorCount;
this.ConnectionMode = cosmosClientContext.ClientOptions.ConnectionMode;
Expand All @@ -56,11 +57,13 @@ public ReadOnlyMemory<byte> SerializedJson
{
if (this.cachedUserAgentString != this.UserAgentContainer.UserAgent ||
this.cachedNumberOfClientCreated != CosmosClient.numberOfClientsCreated ||
this.cachedNumberOfActiveClient != CosmosClient.NumberOfActiveClients)
this.cachedNumberOfActiveClient != CosmosClient.NumberOfActiveClients ||
!ReferenceEquals(this.cachedMachineId, VmMetadataApiHandler.GetMachineId()))
{
this.cachedNumberOfActiveClient = CosmosClient.NumberOfActiveClients;
this.cachedNumberOfClientCreated = CosmosClient.numberOfClientsCreated;
this.cachedUserAgentString = this.UserAgentContainer.UserAgent;
this.cachedMachineId = VmMetadataApiHandler.GetMachineId();
this.cachedSerializedJson = this.GetSerializedDatum();
}

Expand All @@ -76,6 +79,8 @@ public ReadOnlyMemory<byte> SerializedJson

private string cachedUserAgentString;

private string cachedMachineId;

internal override void Accept(ITraceDatumVisitor traceDatumVisitor)
{
traceDatumVisitor.Visit(this);
Expand All @@ -89,7 +94,7 @@ private ReadOnlyMemory<byte> GetSerializedDatum()
jsonTextWriter.WriteFieldName("Client Created Time Utc");
jsonTextWriter.WriteStringValue(this.ClientCreatedDateTimeUtc.ToString("o", CultureInfo.InvariantCulture));
jsonTextWriter.WriteFieldName("MachineId");
jsonTextWriter.WriteStringValue(VmMetadataApiHandler.GetMachineId());
jsonTextWriter.WriteStringValue(this.cachedMachineId);
jsonTextWriter.WriteFieldName("NumberOfClientsCreated");
jsonTextWriter.WriteNumber64Value(this.cachedNumberOfClientCreated);
jsonTextWriter.WriteFieldName("NumberOfActiveClients");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -944,8 +944,6 @@ private static void AssertAccountLevelInformation(
}
}

Assert.AreEqual(1, machineId.Count, $"Multiple Machine Id has been generated i.e {JsonConvert.SerializeObject(machineId)}");

if(isAzureInstance.HasValue)
{
if (isAzureInstance.Value)
Expand All @@ -955,6 +953,7 @@ private static void AssertAccountLevelInformation(
else
{
Assert.AreNotEqual("vmId: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)}");
}
}

Expand Down

0 comments on commit 35c177b

Please sign in to comment.