Skip to content

Commit

Permalink
[Azure.ResourceDetectors] Add appservice service.instance.id (#1012)
Browse files Browse the repository at this point in the history
  • Loading branch information
vishweshbankwar authored Feb 21, 2023
1 parent 9277930 commit d3f6d09
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,35 @@ public sealed class AppServiceResourceDetector : IResourceDetector
/// <inheritdoc/>
public Resource? Detect()
{
string? serviceName = null;
List<KeyValuePair<string, object>>? 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<KeyValuePair<string, object>>? attributeList = null;
serviceInstanceId = Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID");
if (serviceName != null)
{
attributeList = new();

if (serviceName != null)
{
attributeList = new();
attributeList.Add(new KeyValuePair<string, object>(ResourceSemanticConventions.AttributeServiceName, serviceName));

attributeList.Add(new KeyValuePair<string, object>(ResourceSemanticConventions.AttributeServiceName, serviceName));

// TODO: Add other attributes e.g. service.instance.id
if (serviceInstanceId != null)
{
attributeList.Add(new KeyValuePair<string, object>(ResourceSemanticConventions.AttributeServiceInstance, serviceInstanceId));
}
}
else
{
return Resource.Empty;
}
}
else
catch
{
// TODO: log exception.
return Resource.Empty;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, object>(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<string, object>(ResourceSemanticConventions.AttributeServiceName, "AzureAppService"), resource.Attributes);
Assert.Contains(new KeyValuePair<string, object>(ResourceSemanticConventions.AttributeServiceInstance, "AzureInstance"), resource.Attributes);
}

[Fact]
Expand All @@ -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);
}
}

0 comments on commit d3f6d09

Please sign in to comment.