Skip to content

Commit

Permalink
Diagnostics: Adds VM Region into Client Configuration (#3254)
Browse files Browse the repository at this point in the history
* add method to retrieve vm region

* Add VM Region into Client Configuration

* add testing logic for new get machine region method

* fix cache checking order
  • Loading branch information
imanvt authored Jun 6, 2022
1 parent dfa0b7c commit a39e23f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
9 changes: 9 additions & 0 deletions Microsoft.Azure.Cosmos/src/Telemetry/VmMetadataApiHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ internal static Compute GetMachineInfo()
return VmMetadataApiHandler.azMetadata?.Compute;
}

/// <summary>
/// Get Machine Region (If Azure System) else null
/// </summary>
/// <returns>VM region</returns>
internal static string GetMachineRegion()
{
return VmMetadataApiHandler.azMetadata?.Compute?.Location;
}

/// <summary>
/// Hash a passed Value
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public ClientConfigurationTraceDatum(CosmosClientContext cosmosClientContext, Da
this.cachedSerializedJson = this.GetSerializedDatum();
this.ProcessorCount = Environment.ProcessorCount;
this.ConnectionMode = cosmosClientContext.ClientOptions.ConnectionMode;
this.cachedVMRegion = VmMetadataApiHandler.GetMachineRegion();
}

public DateTime ClientCreatedDateTimeUtc { get; }
Expand All @@ -58,12 +59,14 @@ public ReadOnlyMemory<byte> SerializedJson
if (this.cachedUserAgentString != this.UserAgentContainer.UserAgent ||
this.cachedNumberOfClientCreated != CosmosClient.numberOfClientsCreated ||
this.cachedNumberOfActiveClient != CosmosClient.NumberOfActiveClients ||
!ReferenceEquals(this.cachedMachineId, VmMetadataApiHandler.GetMachineId()))
!ReferenceEquals(this.cachedMachineId, VmMetadataApiHandler.GetMachineId()) ||
!ReferenceEquals(this.cachedVMRegion, VmMetadataApiHandler.GetMachineRegion()))
{
this.cachedNumberOfActiveClient = CosmosClient.NumberOfActiveClients;
this.cachedNumberOfClientCreated = CosmosClient.numberOfClientsCreated;
this.cachedUserAgentString = this.UserAgentContainer.UserAgent;
this.cachedMachineId = VmMetadataApiHandler.GetMachineId();
this.cachedVMRegion = VmMetadataApiHandler.GetMachineRegion();
this.cachedSerializedJson = this.GetSerializedDatum();
}

Expand All @@ -80,6 +83,7 @@ public ReadOnlyMemory<byte> SerializedJson
private string cachedUserAgentString;

private string cachedMachineId;
private string cachedVMRegion;

internal override void Accept(ITraceDatumVisitor traceDatumVisitor)
{
Expand All @@ -95,6 +99,11 @@ private ReadOnlyMemory<byte> GetSerializedDatum()
jsonTextWriter.WriteStringValue(this.ClientCreatedDateTimeUtc.ToString("o", CultureInfo.InvariantCulture));
jsonTextWriter.WriteFieldName("MachineId");
jsonTextWriter.WriteStringValue(this.cachedMachineId);
if (this.cachedVMRegion != null)
{
jsonTextWriter.WriteFieldName("VM Region");
jsonTextWriter.WriteStringValue(this.cachedVMRegion);
}
jsonTextWriter.WriteFieldName("NumberOfClientsCreated");
jsonTextWriter.WriteNumber64Value(this.cachedNumberOfClientCreated);
jsonTextWriter.WriteFieldName("NumberOfActiveClients");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ static Task<HttpResponseMessage> sendFunc(HttpRequestMessage request, Cancellati

await Task.Delay(2000);
Assert.AreEqual("vmId:d0cb93eb-214b-4c2b-bd3d-cc93e90d9efd", VmMetadataApiHandler.GetMachineId());
Assert.AreEqual(VmMetadataApiHandler.GetMachineRegion(), "eastus");
}

[TestMethod]
Expand All @@ -81,6 +82,7 @@ static Task<HttpResponseMessage> sendFunc(HttpRequestMessage request, Cancellati
await Task.Delay(2000);
Assert.IsNull(VmMetadataApiHandler.GetMachineInfo());
Assert.IsNotNull(VmMetadataApiHandler.GetMachineId());
Assert.IsNull(VmMetadataApiHandler.GetMachineRegion());
}

[TestMethod]
Expand Down

0 comments on commit a39e23f

Please sign in to comment.