diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/CHANGELOG.md b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/CHANGELOG.md index 234a2df3e5c0b..cc77c49e24766 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/CHANGELOG.md +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/CHANGELOG.md @@ -4,12 +4,19 @@ ### Features Added +* Added Azure Container Apps resource detector. + ([#41803](https://github.com/Azure/azure-sdk-for-net/pull/41803)) + ### Breaking Changes ### Bugs Fixed ### Other Changes +* Updated the vendored code in the `OpenTelemetry.ResourceDetectors.Azure` + resource detector to include the Azure Container Apps resource detector. + ([#41803](https://github.com/Azure/azure-sdk-for-net/pull/41803)) + ## 1.1.0 (2024-01-25) ### Other Changes diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/OpenTelemetryBuilderExtensions.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/OpenTelemetryBuilderExtensions.cs index 2dcf24064fb7d..433eb8b5af6b6 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/OpenTelemetryBuilderExtensions.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/OpenTelemetryBuilderExtensions.cs @@ -90,6 +90,7 @@ public static OpenTelemetryBuilder UseAzureMonitor(this OpenTelemetryBuilder bui Action configureResource = (r) => r .AddAttributes(new[] { new KeyValuePair("telemetry.distro.name", "Azure.Monitor.OpenTelemetry.AspNetCore") }) .AddDetector(new AppServiceResourceDetector()) + .AddDetector(new AzureContainerAppsResourceDetector()) .AddDetector(new AzureVMResourceDetector()); builder.ConfigureResource(configureResource); diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Vendoring/OpenTelemetry.ResourceDetectors.Azure/AppServiceResourceDetector.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Vendoring/OpenTelemetry.ResourceDetectors.Azure/AppServiceResourceDetector.cs index 4a924a4ea6fb7..efdf6b25c5473 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Vendoring/OpenTelemetry.ResourceDetectors.Azure/AppServiceResourceDetector.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Vendoring/OpenTelemetry.ResourceDetectors.Azure/AppServiceResourceDetector.cs @@ -1,18 +1,5 @@ -// // Copyright The OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// +// SPDX-License-Identifier: Apache-2.0 #nullable enable @@ -82,7 +69,7 @@ public Resource Detect() string websiteResourceGroup = Environment.GetEnvironmentVariable(ResourceAttributeConstants.AppServiceResourceGroupEnvVar); string websiteOwnerName = Environment.GetEnvironmentVariable(ResourceAttributeConstants.AppServiceOwnerNameEnvVar) ?? string.Empty; - int idx = websiteOwnerName.IndexOf('+'); + int idx = websiteOwnerName.IndexOf("+", StringComparison.Ordinal); string subscriptionId = idx > 0 ? websiteOwnerName.Substring(0, idx) : websiteOwnerName; if (string.IsNullOrEmpty(websiteResourceGroup) || string.IsNullOrEmpty(subscriptionId)) diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Vendoring/OpenTelemetry.ResourceDetectors.Azure/AzureContainerAppsResourceDetector.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Vendoring/OpenTelemetry.ResourceDetectors.Azure/AzureContainerAppsResourceDetector.cs new file mode 100644 index 0000000000000..06c9ef51defde --- /dev/null +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Vendoring/OpenTelemetry.ResourceDetectors.Azure/AzureContainerAppsResourceDetector.cs @@ -0,0 +1,55 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +using System; +using System.Collections.Generic; +using OpenTelemetry.Resources; +using OpenTelemetry.Trace; + +namespace OpenTelemetry.ResourceDetectors.Azure; + +/// +/// Resource detector for Azure Container Apps environment. +/// +internal sealed class AzureContainerAppsResourceDetector : IResourceDetector +{ + internal static readonly IReadOnlyDictionary AzureContainerResourceAttributes = new Dictionary + { + { ResourceSemanticConventions.AttributeServiceInstance, ResourceAttributeConstants.AzureContainerAppsReplicaNameEnvVar }, + { ResourceSemanticConventions.AttributeServiceVersion, ResourceAttributeConstants.AzureContainerAppsRevisionEnvVar }, + }; + + /// + public Resource Detect() + { + List> attributeList = new(); + + try + { + var containerAppName = Environment.GetEnvironmentVariable(ResourceAttributeConstants.AzureContainerAppsNameEnvVar); + + if (containerAppName != null) + { + attributeList.Add(new KeyValuePair(ResourceSemanticConventions.AttributeServiceName, containerAppName)); + attributeList.Add(new KeyValuePair(ResourceSemanticConventions.AttributeCloudProvider, ResourceAttributeConstants.AzureCloudProviderValue)); + attributeList.Add(new KeyValuePair(ResourceSemanticConventions.AttributeCloudPlatform, ResourceAttributeConstants.AzureContainerAppsPlatformValue)); + + foreach (var kvp in AzureContainerResourceAttributes) + { + var attributeValue = Environment.GetEnvironmentVariable(kvp.Value); + if (attributeValue != null) + { + attributeList.Add(new KeyValuePair(kvp.Key, attributeValue)); + } + } + } + } + catch + { + // TODO: log exception. + return Resource.Empty; + } + + return new Resource(attributeList); + } +} diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Vendoring/OpenTelemetry.ResourceDetectors.Azure/AzureVMResourceDetector.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Vendoring/OpenTelemetry.ResourceDetectors.Azure/AzureVMResourceDetector.cs index 1e8fc284b584b..50e4eecd1563a 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Vendoring/OpenTelemetry.ResourceDetectors.Azure/AzureVMResourceDetector.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Vendoring/OpenTelemetry.ResourceDetectors.Azure/AzureVMResourceDetector.cs @@ -1,18 +1,5 @@ -// // Copyright The OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// +// SPDX-License-Identifier: Apache-2.0 #nullable enable diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Vendoring/OpenTelemetry.ResourceDetectors.Azure/AzureVmMetaDataRequestor.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Vendoring/OpenTelemetry.ResourceDetectors.Azure/AzureVmMetaDataRequestor.cs index 34f77cacb0f62..0e04dd76bade7 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Vendoring/OpenTelemetry.ResourceDetectors.Azure/AzureVmMetaDataRequestor.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Vendoring/OpenTelemetry.ResourceDetectors.Azure/AzureVmMetaDataRequestor.cs @@ -1,18 +1,5 @@ -// // Copyright The OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// +// SPDX-License-Identifier: Apache-2.0 #nullable enable diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Vendoring/OpenTelemetry.ResourceDetectors.Azure/AzureVmMetadataResponse.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Vendoring/OpenTelemetry.ResourceDetectors.Azure/AzureVmMetadataResponse.cs index a9f8cb4ac7b6d..0a88c4b2debb6 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Vendoring/OpenTelemetry.ResourceDetectors.Azure/AzureVmMetadataResponse.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Vendoring/OpenTelemetry.ResourceDetectors.Azure/AzureVmMetadataResponse.cs @@ -1,23 +1,9 @@ -// // Copyright The OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// +// SPDX-License-Identifier: Apache-2.0 #nullable enable using System.Text.Json.Serialization; -using OpenTelemetry.Resources; using OpenTelemetry.Trace; namespace OpenTelemetry.ResourceDetectors.Azure; diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Vendoring/OpenTelemetry.ResourceDetectors.Azure/ResourceAttributeConstants.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Vendoring/OpenTelemetry.ResourceDetectors.Azure/ResourceAttributeConstants.cs index 4b85409779b0b..4cbb552f7d065 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Vendoring/OpenTelemetry.ResourceDetectors.Azure/ResourceAttributeConstants.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Vendoring/OpenTelemetry.ResourceDetectors.Azure/ResourceAttributeConstants.cs @@ -1,18 +1,5 @@ -// // Copyright The OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// +// SPDX-License-Identifier: Apache-2.0 namespace OpenTelemetry.ResourceDetectors.Azure; @@ -35,8 +22,14 @@ internal sealed class ResourceAttributeConstants internal const string AppServiceSlotNameEnvVar = "WEBSITE_SLOT_NAME"; internal const string AppServiceStampNameEnvVar = "WEBSITE_HOME_STAMPNAME"; + // Azure Container Apps environment variables + internal const string AzureContainerAppsNameEnvVar = "CONTAINER_APP_NAME"; + internal const string AzureContainerAppsReplicaNameEnvVar = "CONTAINER_APP_REPLICA_NAME"; + internal const string AzureContainerAppsRevisionEnvVar = "CONTAINER_APP_REVISION"; + // Azure resource attributes constant values internal const string AzureAppServicePlatformValue = "azure_app_service"; internal const string AzureCloudProviderValue = "azure"; internal const string AzureVmCloudPlatformValue = "azure_vm"; + internal const string AzureContainerAppsPlatformValue = "azure_container_apps"; } diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Vendoring/Shared/ResourceSemanticConventions.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Vendoring/Shared/ResourceSemanticConventions.cs index e54d14cae78c6..64e5bd90f6388 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Vendoring/Shared/ResourceSemanticConventions.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Vendoring/Shared/ResourceSemanticConventions.cs @@ -1,22 +1,9 @@ -// // Copyright The OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// +// SPDX-License-Identifier: Apache-2.0 #nullable enable -namespace OpenTelemetry.Resources; +namespace OpenTelemetry.Trace; internal static class ResourceSemanticConventions {