Skip to content

Commit f250ca2

Browse files
{AzureMonitorDistro] Added Azure Container Apps resource detector. (#41803)
* Added Azure Container Apps resource detector. * Update changelog
1 parent c8416d0 commit f250ca2

File tree

9 files changed

+77
-87
lines changed

9 files changed

+77
-87
lines changed

sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@
44

55
### Features Added
66

7+
* Added Azure Container Apps resource detector.
8+
([#41803](https://github.com/Azure/azure-sdk-for-net/pull/41803))
9+
710
### Breaking Changes
811

912
### Bugs Fixed
1013

1114
### Other Changes
1215

16+
* Updated the vendored code in the `OpenTelemetry.ResourceDetectors.Azure`
17+
resource detector to include the Azure Container Apps resource detector.
18+
([#41803](https://github.com/Azure/azure-sdk-for-net/pull/41803))
19+
1320
## 1.1.0 (2024-01-25)
1421

1522
### Other Changes

sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/OpenTelemetryBuilderExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public static OpenTelemetryBuilder UseAzureMonitor(this OpenTelemetryBuilder bui
9090
Action<ResourceBuilder> configureResource = (r) => r
9191
.AddAttributes(new[] { new KeyValuePair<string, object>("telemetry.distro.name", "Azure.Monitor.OpenTelemetry.AspNetCore") })
9292
.AddDetector(new AppServiceResourceDetector())
93+
.AddDetector(new AzureContainerAppsResourceDetector())
9394
.AddDetector(new AzureVMResourceDetector());
9495

9596
builder.ConfigureResource(configureResource);

sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Vendoring/OpenTelemetry.ResourceDetectors.Azure/AppServiceResourceDetector.cs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
1-
// <copyright file="AppServiceResourceDetector.cs" company="OpenTelemetry Authors">
21
// Copyright The OpenTelemetry Authors
3-
//
4-
// Licensed under the Apache License, Version 2.0 (the "License");
5-
// you may not use this file except in compliance with the License.
6-
// You may obtain a copy of the License at
7-
//
8-
// http://www.apache.org/licenses/LICENSE-2.0
9-
//
10-
// Unless required by applicable law or agreed to in writing, software
11-
// distributed under the License is distributed on an "AS IS" BASIS,
12-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
// See the License for the specific language governing permissions and
14-
// limitations under the License.
15-
// </copyright>
2+
// SPDX-License-Identifier: Apache-2.0
163

174
#nullable enable
185

@@ -82,7 +69,7 @@ public Resource Detect()
8269
string websiteResourceGroup = Environment.GetEnvironmentVariable(ResourceAttributeConstants.AppServiceResourceGroupEnvVar);
8370
string websiteOwnerName = Environment.GetEnvironmentVariable(ResourceAttributeConstants.AppServiceOwnerNameEnvVar) ?? string.Empty;
8471

85-
int idx = websiteOwnerName.IndexOf('+');
72+
int idx = websiteOwnerName.IndexOf("+", StringComparison.Ordinal);
8673
string subscriptionId = idx > 0 ? websiteOwnerName.Substring(0, idx) : websiteOwnerName;
8774

8875
if (string.IsNullOrEmpty(websiteResourceGroup) || string.IsNullOrEmpty(subscriptionId))
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using OpenTelemetry.Resources;
7+
using OpenTelemetry.Trace;
8+
9+
namespace OpenTelemetry.ResourceDetectors.Azure;
10+
11+
/// <summary>
12+
/// Resource detector for Azure Container Apps environment.
13+
/// </summary>
14+
internal sealed class AzureContainerAppsResourceDetector : IResourceDetector
15+
{
16+
internal static readonly IReadOnlyDictionary<string, string> AzureContainerResourceAttributes = new Dictionary<string, string>
17+
{
18+
{ ResourceSemanticConventions.AttributeServiceInstance, ResourceAttributeConstants.AzureContainerAppsReplicaNameEnvVar },
19+
{ ResourceSemanticConventions.AttributeServiceVersion, ResourceAttributeConstants.AzureContainerAppsRevisionEnvVar },
20+
};
21+
22+
/// <inheritdoc/>
23+
public Resource Detect()
24+
{
25+
List<KeyValuePair<string, object>> attributeList = new();
26+
27+
try
28+
{
29+
var containerAppName = Environment.GetEnvironmentVariable(ResourceAttributeConstants.AzureContainerAppsNameEnvVar);
30+
31+
if (containerAppName != null)
32+
{
33+
attributeList.Add(new KeyValuePair<string, object>(ResourceSemanticConventions.AttributeServiceName, containerAppName));
34+
attributeList.Add(new KeyValuePair<string, object>(ResourceSemanticConventions.AttributeCloudProvider, ResourceAttributeConstants.AzureCloudProviderValue));
35+
attributeList.Add(new KeyValuePair<string, object>(ResourceSemanticConventions.AttributeCloudPlatform, ResourceAttributeConstants.AzureContainerAppsPlatformValue));
36+
37+
foreach (var kvp in AzureContainerResourceAttributes)
38+
{
39+
var attributeValue = Environment.GetEnvironmentVariable(kvp.Value);
40+
if (attributeValue != null)
41+
{
42+
attributeList.Add(new KeyValuePair<string, object>(kvp.Key, attributeValue));
43+
}
44+
}
45+
}
46+
}
47+
catch
48+
{
49+
// TODO: log exception.
50+
return Resource.Empty;
51+
}
52+
53+
return new Resource(attributeList);
54+
}
55+
}

sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Vendoring/OpenTelemetry.ResourceDetectors.Azure/AzureVMResourceDetector.cs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
1-
// <copyright file="AzureVMResourceDetector.cs" company="OpenTelemetry Authors">
21
// Copyright The OpenTelemetry Authors
3-
//
4-
// Licensed under the Apache License, Version 2.0 (the "License");
5-
// you may not use this file except in compliance with the License.
6-
// You may obtain a copy of the License at
7-
//
8-
// http://www.apache.org/licenses/LICENSE-2.0
9-
//
10-
// Unless required by applicable law or agreed to in writing, software
11-
// distributed under the License is distributed on an "AS IS" BASIS,
12-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
// See the License for the specific language governing permissions and
14-
// limitations under the License.
15-
// </copyright>
2+
// SPDX-License-Identifier: Apache-2.0
163

174
#nullable enable
185

sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Vendoring/OpenTelemetry.ResourceDetectors.Azure/AzureVmMetaDataRequestor.cs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
1-
// <copyright file="AzureVmMetaDataRequestor.cs" company="OpenTelemetry Authors">
21
// Copyright The OpenTelemetry Authors
3-
//
4-
// Licensed under the Apache License, Version 2.0 (the "License");
5-
// you may not use this file except in compliance with the License.
6-
// You may obtain a copy of the License at
7-
//
8-
// http://www.apache.org/licenses/LICENSE-2.0
9-
//
10-
// Unless required by applicable law or agreed to in writing, software
11-
// distributed under the License is distributed on an "AS IS" BASIS,
12-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
// See the License for the specific language governing permissions and
14-
// limitations under the License.
15-
// </copyright>
2+
// SPDX-License-Identifier: Apache-2.0
163

174
#nullable enable
185

sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Vendoring/OpenTelemetry.ResourceDetectors.Azure/AzureVmMetadataResponse.cs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,9 @@
1-
// <copyright file="AzureVmMetadataResponse.cs" company="OpenTelemetry Authors">
21
// Copyright The OpenTelemetry Authors
3-
//
4-
// Licensed under the Apache License, Version 2.0 (the "License");
5-
// you may not use this file except in compliance with the License.
6-
// You may obtain a copy of the License at
7-
//
8-
// http://www.apache.org/licenses/LICENSE-2.0
9-
//
10-
// Unless required by applicable law or agreed to in writing, software
11-
// distributed under the License is distributed on an "AS IS" BASIS,
12-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
// See the License for the specific language governing permissions and
14-
// limitations under the License.
15-
// </copyright>
2+
// SPDX-License-Identifier: Apache-2.0
163

174
#nullable enable
185

196
using System.Text.Json.Serialization;
20-
using OpenTelemetry.Resources;
217
using OpenTelemetry.Trace;
228

239
namespace OpenTelemetry.ResourceDetectors.Azure;

sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Vendoring/OpenTelemetry.ResourceDetectors.Azure/ResourceAttributeConstants.cs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
1-
// <copyright file="ResourceAttributeConstants.cs" company="OpenTelemetry Authors">
21
// Copyright The OpenTelemetry Authors
3-
//
4-
// Licensed under the Apache License, Version 2.0 (the "License");
5-
// you may not use this file except in compliance with the License.
6-
// You may obtain a copy of the License at
7-
//
8-
// http://www.apache.org/licenses/LICENSE-2.0
9-
//
10-
// Unless required by applicable law or agreed to in writing, software
11-
// distributed under the License is distributed on an "AS IS" BASIS,
12-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
// See the License for the specific language governing permissions and
14-
// limitations under the License.
15-
// </copyright>
2+
// SPDX-License-Identifier: Apache-2.0
163

174
namespace OpenTelemetry.ResourceDetectors.Azure;
185

@@ -35,8 +22,14 @@ internal sealed class ResourceAttributeConstants
3522
internal const string AppServiceSlotNameEnvVar = "WEBSITE_SLOT_NAME";
3623
internal const string AppServiceStampNameEnvVar = "WEBSITE_HOME_STAMPNAME";
3724

25+
// Azure Container Apps environment variables
26+
internal const string AzureContainerAppsNameEnvVar = "CONTAINER_APP_NAME";
27+
internal const string AzureContainerAppsReplicaNameEnvVar = "CONTAINER_APP_REPLICA_NAME";
28+
internal const string AzureContainerAppsRevisionEnvVar = "CONTAINER_APP_REVISION";
29+
3830
// Azure resource attributes constant values
3931
internal const string AzureAppServicePlatformValue = "azure_app_service";
4032
internal const string AzureCloudProviderValue = "azure";
4133
internal const string AzureVmCloudPlatformValue = "azure_vm";
34+
internal const string AzureContainerAppsPlatformValue = "azure_container_apps";
4235
}

sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Vendoring/Shared/ResourceSemanticConventions.cs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,9 @@
1-
// <copyright file="ResourceSemanticConventions.cs" company="OpenTelemetry Authors">
21
// Copyright The OpenTelemetry Authors
3-
//
4-
// Licensed under the Apache License, Version 2.0 (the "License");
5-
// you may not use this file except in compliance with the License.
6-
// You may obtain a copy of the License at
7-
//
8-
// http://www.apache.org/licenses/LICENSE-2.0
9-
//
10-
// Unless required by applicable law or agreed to in writing, software
11-
// distributed under the License is distributed on an "AS IS" BASIS,
12-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
// See the License for the specific language governing permissions and
14-
// limitations under the License.
15-
// </copyright>
2+
// SPDX-License-Identifier: Apache-2.0
163

174
#nullable enable
185

19-
namespace OpenTelemetry.Resources;
6+
namespace OpenTelemetry.Trace;
207

218
internal static class ResourceSemanticConventions
229
{

0 commit comments

Comments
 (0)