Skip to content

Commit

Permalink
{AzureMonitorDistro] Added Azure Container Apps resource detector. (#…
Browse files Browse the repository at this point in the history
…41803)

* Added Azure Container Apps resource detector.

* Update changelog
  • Loading branch information
rajkumar-rangaraj committed Feb 6, 2024
1 parent c8416d0 commit f250ca2
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public static OpenTelemetryBuilder UseAzureMonitor(this OpenTelemetryBuilder bui
Action<ResourceBuilder> configureResource = (r) => r
.AddAttributes(new[] { new KeyValuePair<string, object>("telemetry.distro.name", "Azure.Monitor.OpenTelemetry.AspNetCore") })
.AddDetector(new AppServiceResourceDetector())
.AddDetector(new AzureContainerAppsResourceDetector())
.AddDetector(new AzureVMResourceDetector());

builder.ConfigureResource(configureResource);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
// <copyright file="AppServiceResourceDetector.cs" company="OpenTelemetry Authors">
// 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.
// </copyright>
// SPDX-License-Identifier: Apache-2.0

#nullable enable

Expand Down Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Resource detector for Azure Container Apps environment.
/// </summary>
internal sealed class AzureContainerAppsResourceDetector : IResourceDetector
{
internal static readonly IReadOnlyDictionary<string, string> AzureContainerResourceAttributes = new Dictionary<string, string>
{
{ ResourceSemanticConventions.AttributeServiceInstance, ResourceAttributeConstants.AzureContainerAppsReplicaNameEnvVar },
{ ResourceSemanticConventions.AttributeServiceVersion, ResourceAttributeConstants.AzureContainerAppsRevisionEnvVar },
};

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

try
{
var containerAppName = Environment.GetEnvironmentVariable(ResourceAttributeConstants.AzureContainerAppsNameEnvVar);

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));
}
}
}
}
catch
{
// TODO: log exception.
return Resource.Empty;
}

return new Resource(attributeList);
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
// <copyright file="AzureVMResourceDetector.cs" company="OpenTelemetry Authors">
// 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.
// </copyright>
// SPDX-License-Identifier: Apache-2.0

#nullable enable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
// <copyright file="AzureVmMetaDataRequestor.cs" company="OpenTelemetry Authors">
// 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.
// </copyright>
// SPDX-License-Identifier: Apache-2.0

#nullable enable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
// <copyright file="AzureVmMetadataResponse.cs" company="OpenTelemetry Authors">
// 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.
// </copyright>
// SPDX-License-Identifier: Apache-2.0

#nullable enable

using System.Text.Json.Serialization;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

namespace OpenTelemetry.ResourceDetectors.Azure;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
// <copyright file="ResourceAttributeConstants.cs" company="OpenTelemetry Authors">
// 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.
// </copyright>
// SPDX-License-Identifier: Apache-2.0

namespace OpenTelemetry.ResourceDetectors.Azure;

Expand All @@ -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";
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
// <copyright file="ResourceSemanticConventions.cs" company="OpenTelemetry Authors">
// 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.
// </copyright>
// SPDX-License-Identifier: Apache-2.0

#nullable enable

namespace OpenTelemetry.Resources;
namespace OpenTelemetry.Trace;

internal static class ResourceSemanticConventions
{
Expand Down

0 comments on commit f250ca2

Please sign in to comment.