Skip to content

Commit

Permalink
add support for Container App Jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
hansmbakker committed Sep 11, 2024
1 parent ea5d607 commit f20605c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,38 @@ namespace OpenTelemetry.Resources.Azure;
/// </summary>
internal sealed class AzureContainerAppsResourceDetector : IResourceDetector
{
internal static readonly IReadOnlyDictionary<string, string> AzureContainerResourceAttributes = new Dictionary<string, string>
internal static readonly IReadOnlyDictionary<string, string> AzureContainerAppResourceAttributes = new Dictionary<string, string>
{
{ ResourceSemanticConventions.AttributeServiceInstance, ResourceAttributeConstants.AzureContainerAppsReplicaNameEnvVar },
{ ResourceSemanticConventions.AttributeServiceVersion, ResourceAttributeConstants.AzureContainerAppsRevisionEnvVar },
};

internal static readonly IReadOnlyDictionary<string, string> AzureContainerAppJobResourceAttributes = new Dictionary<string, string>
{
{ ResourceSemanticConventions.AttributeServiceInstance, ResourceAttributeConstants.AzureContainerAppsReplicaNameEnvVar },
{ ResourceSemanticConventions.AttributeServiceVersion, ResourceAttributeConstants.AzureContainerAppJobExecutionNameEnvVar },
};

/// <inheritdoc/>
public Resource Detect()
{
List<KeyValuePair<string, object>> attributeList = new();

List<KeyValuePair<string, object>> attributeList = new List<KeyValuePair<string, object>>();
try
{
var containerAppName = Environment.GetEnvironmentVariable(ResourceAttributeConstants.AzureContainerAppsNameEnvVar);
var containerAppJobName = Environment.GetEnvironmentVariable(ResourceAttributeConstants.AzureContainerAppJobNameEnvVar);

if (containerAppName != null)
{
attributeList.Add(new KeyValuePair<string, object>(ResourceSemanticConventions.AttributeServiceName, containerAppName));
attributeList.Add(new KeyValuePair<string, object>(ResourceSemanticConventions.AttributeCloudProvider, ResourceAttributeConstants.AzureCloudProviderValue));
attributeList.Add(new KeyValuePair<string, object>(ResourceSemanticConventions.AttributeCloudPlatform, ResourceAttributeConstants.AzureContainerAppsPlatformValue));

foreach (var kvp in AzureContainerResourceAttributes)
{
var attributeValue = Environment.GetEnvironmentVariable(kvp.Value);
if (attributeValue != null)
{
attributeList.Add(new KeyValuePair<string, object>(kvp.Key, attributeValue));
}
}
AddBaseAttributes(attributeList, containerAppName);

AddResourceAttributes(attributeList, AzureContainerAppResourceAttributes);
}
else if (containerAppJobName != null)
{
AddBaseAttributes(attributeList, containerAppJobName);

AddResourceAttributes(attributeList, AzureContainerAppJobResourceAttributes);
}
}
catch
Expand All @@ -49,4 +52,23 @@ public Resource Detect()

return new Resource(attributeList);
}

private static void AddResourceAttributes(List<KeyValuePair<string, object>> attributeList, IReadOnlyDictionary<string, string> resourceAttributes)
{
foreach (var kvp in resourceAttributes)
{
var attributeValue = Environment.GetEnvironmentVariable(kvp.Value);
if (attributeValue != null)
{
attributeList.Add(new KeyValuePair<string, object>(kvp.Key, attributeValue));
}
}
}

private static void AddBaseAttributes(List<KeyValuePair<string, object>> attributeList, string serviceName)
{
attributeList.Add(new KeyValuePair<string, object>(ResourceSemanticConventions.AttributeServiceName, serviceName));
attributeList.Add(new KeyValuePair<string, object>(ResourceSemanticConventions.AttributeCloudProvider, ResourceAttributeConstants.AzureCloudProviderValue));
attributeList.Add(new KeyValuePair<string, object>(ResourceSemanticConventions.AttributeCloudPlatform, ResourceAttributeConstants.AzureContainerAppsPlatformValue));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ internal sealed class ResourceAttributeConstants
internal const string AzureContainerAppsReplicaNameEnvVar = "CONTAINER_APP_REPLICA_NAME";
internal const string AzureContainerAppsRevisionEnvVar = "CONTAINER_APP_REVISION";

internal const string AzureContainerAppJobNameEnvVar = "CONTAINER_APP_JOB_NAME";
internal const string AzureContainerAppJobExecutionNameEnvVar = "CONTAINER_APP_JOB_EXECUTION_NAME";

// Azure resource attributes constant values
internal const string AzureAppServicePlatformValue = "azure_app_service";
internal const string AzureCloudProviderValue = "azure";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void AzureContainerAppsResourceDetectorReturnsResourceWithAttributes()
{
try
{
foreach (var kvp in AzureContainerAppsResourceDetector.AzureContainerResourceAttributes)
foreach (var kvp in AzureContainerAppsResourceDetector.AzureContainerAppResourceAttributes)
{
Environment.SetEnvironmentVariable(kvp.Value, kvp.Key);
}
Expand All @@ -113,7 +113,34 @@ public void AzureContainerAppsResourceDetectorReturnsResourceWithAttributes()

Assert.Contains(new KeyValuePair<string, object>(ResourceSemanticConventions.AttributeServiceName, "containerAppName"), resource.Attributes);

foreach (var kvp in AzureContainerAppsResourceDetector.AzureContainerResourceAttributes)
foreach (var kvp in AzureContainerAppsResourceDetector.AzureContainerAppResourceAttributes)
{
Assert.Contains(new KeyValuePair<string, object>(kvp.Key, kvp.Key), resource.Attributes);
}
}

[Fact]
public void AzureContainerAppsJobResourceDetectorReturnsResourceWithAttributes()
{
try
{
foreach (var kvp in AzureContainerAppsResourceDetector.AzureContainerAppJobResourceAttributes)
{
Environment.SetEnvironmentVariable(kvp.Value, kvp.Key);
}

Environment.SetEnvironmentVariable(ResourceAttributeConstants.AzureContainerAppJobNameEnvVar, "containerAppName");
}
catch
{
}

var resource = ResourceBuilder.CreateEmpty().AddAzureContainerAppsDetector().Build();
Assert.NotNull(resource);

Assert.Contains(new KeyValuePair<string, object>(ResourceSemanticConventions.AttributeServiceName, "containerAppName"), resource.Attributes);

foreach (var kvp in AzureContainerAppsResourceDetector.AzureContainerAppJobResourceAttributes)
{
Assert.Contains(new KeyValuePair<string, object>(kvp.Key, kvp.Key), resource.Attributes);
}
Expand All @@ -126,7 +153,12 @@ public void Dispose()
Environment.SetEnvironmentVariable(kvp.Value, null);
}

foreach (var kvp in AzureContainerAppsResourceDetector.AzureContainerResourceAttributes)
foreach (var kvp in AzureContainerAppsResourceDetector.AzureContainerAppResourceAttributes)
{
Environment.SetEnvironmentVariable(kvp.Value, null);
}

foreach (var kvp in AzureContainerAppsResourceDetector.AzureContainerAppJobResourceAttributes)
{
Environment.SetEnvironmentVariable(kvp.Value, null);
}
Expand Down

0 comments on commit f20605c

Please sign in to comment.