Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/OpenTelemetry.Resources.Azure/AzureVMResourceDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ public Resource Detect()
attributeList.Add(new(field, vmMetaDataResponse.GetValueForField(field)));
}

var resourceGroupName = vmMetaDataResponse.GetValueForField(ResourceAttributeConstants.AzureResourceGroupName);
if (resourceGroupName is { Length: > 0 })
{
attributeList.Add(new(ResourceAttributeConstants.AzureResourceGroupName, resourceGroupName));
}

var subscriptionId = vmMetaDataResponse.GetValueForField(ResourceSemanticConventions.AttributeCloudAccount);
if (subscriptionId is { Length: > 0 })
{
attributeList.Add(new(ResourceSemanticConventions.AttributeCloudAccount, subscriptionId));
}

if (attributeList.Count == 0)
{
vmResource = Resource.Empty;
Expand Down
12 changes: 12 additions & 0 deletions src/OpenTelemetry.Resources.Azure/AzureVmMetadataResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ internal sealed class AzureVmMetadataResponse
[JsonPropertyName("osType")]
public string? OsType { get; set; }

[JsonPropertyName("resourceGroupName")]
public string? ResourceGroupName { get; set; }

[JsonPropertyName("resourceId")]
public string? ResourceId { get; set; }

[JsonPropertyName("subscriptionId")]
public string? SubscriptionId { get; set; }

[JsonPropertyName("version")]
public string? Version { get; set; }

Expand All @@ -37,6 +43,9 @@ internal string GetValueForField(string fieldName)
string? amsValue = null;
switch (fieldName)
{
case ResourceSemanticConventions.AttributeCloudAccount:
amsValue = this.SubscriptionId;
break;
case ResourceSemanticConventions.AttributeCloudPlatform:
amsValue = ResourceAttributeConstants.AzureVmCloudPlatformValue;
break;
Expand Down Expand Up @@ -68,6 +77,9 @@ internal string GetValueForField(string fieldName)
case ResourceSemanticConventions.AttributeOsVersion:
amsValue = this.Version;
break;
case ResourceAttributeConstants.AzureResourceGroupName:
amsValue = this.ResourceGroupName;
break;
case ResourceAttributeConstants.AzureVmScaleSetName:
amsValue = this.VmScaleSetName;
break;
Expand Down
4 changes: 4 additions & 0 deletions src/OpenTelemetry.Resources.Azure/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
requests to the Azure Instance Metadata Service.
([#5167](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/5167))

* Add `cloud.account.id` and `azure.resource_group.name` attributes to the Azure
VM resource detector.
([#5195](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/5195))

## 1.18.0-beta.1

Released 2026-Aug-21
Expand Down
28 changes: 15 additions & 13 deletions src/OpenTelemetry.Resources.Azure/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,21 @@ from the [Azure Instance Metadata
Service](https://learn.microsoft.com/azure/virtual-machines/instance-metadata-service)
rather than from environment variables.

| Attribute | Description |
| ---------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| azure.vm.scaleset.name | The name of the Virtual Machine Scale Set if the VM is part of one. |
| cloud.platform | The cloud platform, which is always set to "azure.vm" in this context. |
| cloud.provider | The cloud service provider, which is always set to "azure" in this context. |
| cloud.region | The Azure region where the Virtual Machine is hosted, such as "East US", "West Europe", etc. |
| cloud.resource_id | The Azure Resource Manager URI uniquely identifying the Azure Virtual Machine. It typically follows this format: "/subscriptions/{subscriptionId}/resourceGroups/{groupName}/providers/Microsoft.Compute/virtualMachines/{vmName}". |
| host.id | A unique identifier for the VM host, for instance, "02aab8a4-74ef-476e-8182-f6d2ba4166a6". |
| host.name | The name of the host machine. |
| host.type | The size of the VM instance, for example, "Standard_D2s_v3". |
| os.type | The type of operating system running on the VM, such as "Linux" or "Windows". |
| os.version | The version of the operating system running on the VM. |
| service.instance.id | An identifier for a specific instance of the service running on the Azure VM, for example, "02aab8a4-74ef-476e-8182-f6d2ba4166a6". |
| Attribute | Description |
| ------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| azure.resource_group.name | The Azure resource group the VM belongs to. |
| azure.vm.scaleset.name | The name of the Virtual Machine Scale Set if the VM is part of one. |
| cloud.account.id | The Azure subscription ID. |
| cloud.platform | The cloud platform, which is always set to "azure.vm" in this context. |
| cloud.provider | The cloud service provider, which is always set to "azure" in this context. |
| cloud.region | The Azure region where the Virtual Machine is hosted, such as "East US", "West Europe", etc. |
| cloud.resource_id | The Azure Resource Manager URI uniquely identifying the Azure Virtual Machine. It typically follows this format: "/subscriptions/{subscriptionId}/resourceGroups/{groupName}/providers/Microsoft.Compute/virtualMachines/{vmName}". |
| host.id | A unique identifier for the VM host, for instance, "02aab8a4-74ef-476e-8182-f6d2ba4166a6". |
| host.name | The name of the host machine. |
| host.type | The size of the VM instance, for example, "Standard_D2s_v3". |
| os.type | The type of operating system running on the VM, such as "Linux" or "Windows". |
| os.version | The version of the operating system running on the VM. |
| service.instance.id | An identifier for a specific instance of the service running on the Azure VM, for example, "02aab8a4-74ef-476e-8182-f6d2ba4166a6". |

## Azure Container Apps Resource Detector

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ internal sealed class ResourceAttributeConstants
{
// AppService resource attributes
internal const string AzureAppServiceStamp = "azure.app.service.stamp";

// Shared Azure resource attributes
internal const string AzureResourceGroupName = "azure.resource_group.name";

// Azure VM resource attributes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,9 @@ public void TestAzureVmResourceDetector()
Location = ResourceSemanticConventions.AttributeCloudRegion,
Name = ResourceSemanticConventions.AttributeHostName,
OsType = ResourceSemanticConventions.AttributeOsType,
ResourceGroupName = ResourceAttributeConstants.AzureResourceGroupName,
ResourceId = ResourceSemanticConventions.AttributeCloudResourceId,
SubscriptionId = ResourceSemanticConventions.AttributeCloudAccount,
Version = ResourceSemanticConventions.AttributeOsVersion,
VmSize = ResourceSemanticConventions.AttributeHostType,
VmScaleSetName = ResourceAttributeConstants.AzureVmScaleSetName,
Expand All @@ -236,6 +238,44 @@ public void TestAzureVmResourceDetector()

Assert.Contains(expectedValue, resource.Attributes);
}

Assert.Contains(new KeyValuePair<string, object>(ResourceAttributeConstants.AzureResourceGroupName, ResourceAttributeConstants.AzureResourceGroupName), resource.Attributes);
Assert.Contains(new KeyValuePair<string, object>(ResourceSemanticConventions.AttributeCloudAccount, ResourceSemanticConventions.AttributeCloudAccount), resource.Attributes);
}

[Theory]
[InlineData(null, null)]
[InlineData("", "")]
[InlineData(null, "")]
[InlineData("", null)]
public void AzureVmResourceDetectorSuppressesEmptyIdentityAttributes(string? resourceGroupName, string? subscriptionId)
{
var originalRequestor = AzureVmMetaDataRequestor.GetAzureVmMetaDataResponse;

try
{
AzureVmMetaDataRequestor.GetAzureVmMetaDataResponse = () =>
{
return new AzureVmMetadataResponse()
{
ResourceGroupName = resourceGroupName,
SubscriptionId = subscriptionId,
};
};

AzureVMResourceDetector.ClearCachedResource();

var resource = ResourceBuilder.CreateEmpty().AddAzureVMDetector().Build();

Assert.NotNull(resource);
Assert.DoesNotContain(resource.Attributes, attribute => attribute.Key == ResourceAttributeConstants.AzureResourceGroupName);
Assert.DoesNotContain(resource.Attributes, attribute => attribute.Key == ResourceSemanticConventions.AttributeCloudAccount);
}
finally
{
AzureVmMetaDataRequestor.GetAzureVmMetaDataResponse = originalRequestor;
AzureVMResourceDetector.ClearCachedResource();
}
}

[Theory]
Expand All @@ -248,6 +288,19 @@ public void AzureVmResourceDetectorNormalizesOsTypeToLowercase(string osType, st
Assert.Equal(expected, response.GetValueForField(ResourceSemanticConventions.AttributeOsType));
}

[Fact]
public void AzureVmResourceDetectorMapsSubscriptionIdAndResourceGroupName()
{
var response = new AzureVmMetadataResponse()
{
ResourceGroupName = "rg-demo",
SubscriptionId = "11111111-2222-3333-4444-555555555555",
};

Assert.Equal("rg-demo", response.GetValueForField(ResourceAttributeConstants.AzureResourceGroupName));
Assert.Equal("11111111-2222-3333-4444-555555555555", response.GetValueForField(ResourceSemanticConventions.AttributeCloudAccount));
}

[Fact]
public void AzureContainerAppsResourceDetectorReturnsResourceWithAttributes()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ public void GetAzureVmMetaData_ResponseWithinLimits_ReturnsFullContent()
// Assert
Assert.NotNull(actual);
Assert.Equal("Standard_D2s_v5", actual.VmSize);
Assert.Equal("rg-demo", actual.ResourceGroupName);
Assert.Equal("11111111-2222-3333-4444-555555555555", actual.SubscriptionId);
}

[Theory]
Expand Down