@@ -10,35 +10,38 @@ namespace OpenTelemetry.Resources.Azure;
1010/// </summary>
1111internal sealed class AzureContainerAppsResourceDetector : IResourceDetector
1212{
13- internal static readonly IReadOnlyDictionary < string , string > AzureContainerResourceAttributes = new Dictionary < string , string >
13+ internal static readonly IReadOnlyDictionary < string , string > AzureContainerAppResourceAttributes = new Dictionary < string , string >
1414 {
1515 { ResourceSemanticConventions . AttributeServiceInstance , ResourceAttributeConstants . AzureContainerAppsReplicaNameEnvVar } ,
1616 { ResourceSemanticConventions . AttributeServiceVersion , ResourceAttributeConstants . AzureContainerAppsRevisionEnvVar } ,
1717 } ;
1818
19+ internal static readonly IReadOnlyDictionary < string , string > AzureContainerAppJobResourceAttributes = new Dictionary < string , string >
20+ {
21+ { ResourceSemanticConventions . AttributeServiceInstance , ResourceAttributeConstants . AzureContainerAppsReplicaNameEnvVar } ,
22+ { ResourceSemanticConventions . AttributeServiceVersion , ResourceAttributeConstants . AzureContainerAppJobExecutionNameEnvVar } ,
23+ } ;
24+
1925 /// <inheritdoc/>
2026 public Resource Detect ( )
2127 {
22- List < KeyValuePair < string , object > > attributeList = new ( ) ;
23-
28+ List < KeyValuePair < string , object > > attributeList = new List < KeyValuePair < string , object > > ( ) ;
2429 try
2530 {
2631 var containerAppName = Environment . GetEnvironmentVariable ( ResourceAttributeConstants . AzureContainerAppsNameEnvVar ) ;
32+ var containerAppJobName = Environment . GetEnvironmentVariable ( ResourceAttributeConstants . AzureContainerAppJobNameEnvVar ) ;
2733
2834 if ( containerAppName != null )
2935 {
30- attributeList . Add ( new KeyValuePair < string , object > ( ResourceSemanticConventions . AttributeServiceName , containerAppName ) ) ;
31- attributeList . Add ( new KeyValuePair < string , object > ( ResourceSemanticConventions . AttributeCloudProvider , ResourceAttributeConstants . AzureCloudProviderValue ) ) ;
32- attributeList . Add ( new KeyValuePair < string , object > ( ResourceSemanticConventions . AttributeCloudPlatform , ResourceAttributeConstants . AzureContainerAppsPlatformValue ) ) ;
33-
34- foreach ( var kvp in AzureContainerResourceAttributes )
35- {
36- var attributeValue = Environment . GetEnvironmentVariable ( kvp . Value ) ;
37- if ( attributeValue != null )
38- {
39- attributeList . Add ( new KeyValuePair < string , object > ( kvp . Key , attributeValue ) ) ;
40- }
41- }
36+ AddBaseAttributes ( attributeList , containerAppName ) ;
37+
38+ AddResourceAttributes ( attributeList , AzureContainerAppResourceAttributes ) ;
39+ }
40+ else if ( containerAppJobName != null )
41+ {
42+ AddBaseAttributes ( attributeList , containerAppJobName ) ;
43+
44+ AddResourceAttributes ( attributeList , AzureContainerAppJobResourceAttributes ) ;
4245 }
4346 }
4447 catch
@@ -49,4 +52,23 @@ public Resource Detect()
4952
5053 return new Resource ( attributeList ) ;
5154 }
55+
56+ private static void AddResourceAttributes ( List < KeyValuePair < string , object > > attributeList , IReadOnlyDictionary < string , string > resourceAttributes )
57+ {
58+ foreach ( var kvp in resourceAttributes )
59+ {
60+ var attributeValue = Environment . GetEnvironmentVariable ( kvp . Value ) ;
61+ if ( attributeValue != null )
62+ {
63+ attributeList . Add ( new KeyValuePair < string , object > ( kvp . Key , attributeValue ) ) ;
64+ }
65+ }
66+ }
67+
68+ private static void AddBaseAttributes ( List < KeyValuePair < string , object > > attributeList , string serviceName )
69+ {
70+ attributeList . Add ( new KeyValuePair < string , object > ( ResourceSemanticConventions . AttributeServiceName , serviceName ) ) ;
71+ attributeList . Add ( new KeyValuePair < string , object > ( ResourceSemanticConventions . AttributeCloudProvider , ResourceAttributeConstants . AzureCloudProviderValue ) ) ;
72+ attributeList . Add ( new KeyValuePair < string , object > ( ResourceSemanticConventions . AttributeCloudPlatform , ResourceAttributeConstants . AzureContainerAppsPlatformValue ) ) ;
73+ }
5274}
0 commit comments