From d3f6d09b65a7878cf18b22eeda826149279daa71 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Tue, 21 Feb 2023 09:59:48 -0800 Subject: [PATCH] [Azure.ResourceDetectors] Add appservice service.instance.id (#1012) --- .../AppServiceResourceDetector.cs | 36 +++++++++++-------- .../AzureResourceDetectorTests.cs | 25 ++++++------- 2 files changed, 34 insertions(+), 27 deletions(-) diff --git a/src/OpenTelemetry.ResourceDetectors.Azure/AppServiceResourceDetector.cs b/src/OpenTelemetry.ResourceDetectors.Azure/AppServiceResourceDetector.cs index e9ed7bbf43..3ab8352c10 100644 --- a/src/OpenTelemetry.ResourceDetectors.Azure/AppServiceResourceDetector.cs +++ b/src/OpenTelemetry.ResourceDetectors.Azure/AppServiceResourceDetector.cs @@ -29,29 +29,35 @@ public sealed class AppServiceResourceDetector : IResourceDetector /// public Resource? Detect() { - string? serviceName = null; + List>? attributeList = null; + try { + string? serviceName = null; + string? serviceInstanceId = null; + // https://learn.microsoft.com/azure/app-service/reference-app-settings?tabs=kudu%2Cdotnet#app-environment serviceName = Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME"); - } - catch - { - // TODO: log exception. - } - - List>? attributeList = null; + serviceInstanceId = Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID"); + if (serviceName != null) + { + attributeList = new(); - if (serviceName != null) - { - attributeList = new(); + attributeList.Add(new KeyValuePair(ResourceSemanticConventions.AttributeServiceName, serviceName)); - attributeList.Add(new KeyValuePair(ResourceSemanticConventions.AttributeServiceName, serviceName)); - - // TODO: Add other attributes e.g. service.instance.id + if (serviceInstanceId != null) + { + attributeList.Add(new KeyValuePair(ResourceSemanticConventions.AttributeServiceInstance, serviceInstanceId)); + } + } + else + { + return Resource.Empty; + } } - else + catch { + // TODO: log exception. return Resource.Empty; } diff --git a/test/OpenTelemetry.ResourceDetectors.Azure.Tests/AzureResourceDetectorTests.cs b/test/OpenTelemetry.ResourceDetectors.Azure.Tests/AzureResourceDetectorTests.cs index a785f9b133..2e91877be4 100644 --- a/test/OpenTelemetry.ResourceDetectors.Azure.Tests/AzureResourceDetectorTests.cs +++ b/test/OpenTelemetry.ResourceDetectors.Azure.Tests/AzureResourceDetectorTests.cs @@ -22,22 +22,17 @@ namespace OpenTelemetry.ResourceDetectors.Azure.Tests; -public class AzureResourceDetectorTests +public class AzureResourceDetectorTests : IDisposable { [Fact] public void AppServiceResourceDetectorReturnsResourceWithAttributes() { - try - { - Environment.SetEnvironmentVariable("WEBSITE_SITE_NAME", "AzureAppService"); - var resource = ResourceBuilder.CreateEmpty().AddDetector(new AppServiceResourceDetector()).Build(); - Assert.NotNull(resource); - Assert.Contains(new KeyValuePair(ResourceSemanticConventions.AttributeServiceName, "AzureAppService"), resource.Attributes); - } - finally - { - Environment.SetEnvironmentVariable("WEBSITE_SITE_NAME", null); - } + Environment.SetEnvironmentVariable("WEBSITE_SITE_NAME", "AzureAppService"); + Environment.SetEnvironmentVariable("WEBSITE_INSTANCE_ID", "AzureInstance"); + var resource = ResourceBuilder.CreateEmpty().AddDetector(new AppServiceResourceDetector()).Build(); + Assert.NotNull(resource); + Assert.Contains(new KeyValuePair(ResourceSemanticConventions.AttributeServiceName, "AzureAppService"), resource.Attributes); + Assert.Contains(new KeyValuePair(ResourceSemanticConventions.AttributeServiceInstance, "AzureInstance"), resource.Attributes); } [Fact] @@ -46,4 +41,10 @@ public void AppServiceResourceDetectorReturnsNullOutsideOfAppService() var resource = new AppServiceResourceDetector().Detect(); Assert.Empty(resource.Attributes); } + + public void Dispose() + { + Environment.SetEnvironmentVariable("WEBSITE_SITE_NAME", null); + Environment.SetEnvironmentVariable("WEBSITE_INSTANCE_ID", null); + } }