-
Notifications
You must be signed in to change notification settings - Fork 295
Vendor OpenTelemetry.Resources.Azure resource detectors #3093
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
NETCORE/src/Shared/Vendoring/OpenTelemetry.Resources.Azure/AppServiceResourceDetector.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| #nullable enable | ||
|
|
||
| namespace Microsoft.ApplicationInsights.Shared.Vendoring.OpenTelemetry.Resources.Azure | ||
| { | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using global::OpenTelemetry.Resources; | ||
|
|
||
| /// <summary> | ||
| /// Resource detector for Azure AppService environment. | ||
| /// </summary> | ||
| internal sealed class AppServiceResourceDetector : IResourceDetector | ||
| { | ||
| internal static readonly IReadOnlyDictionary<string, string> AppServiceResourceAttributes = new Dictionary<string, string> | ||
| { | ||
| { ResourceSemanticConventions.AttributeCloudRegion, ResourceAttributeConstants.AppServiceRegionNameEnvVar }, | ||
| { ResourceSemanticConventions.AttributeDeploymentEnvironment, ResourceAttributeConstants.AppServiceSlotNameEnvVar }, | ||
| { ResourceSemanticConventions.AttributeHostId, ResourceAttributeConstants.AppServiceHostNameEnvVar }, | ||
| { ResourceSemanticConventions.AttributeServiceInstance, ResourceAttributeConstants.AppServiceInstanceIdEnvVar }, | ||
| { ResourceAttributeConstants.AzureAppServiceStamp, ResourceAttributeConstants.AppServiceStampNameEnvVar }, | ||
| }; | ||
|
|
||
| /// <inheritdoc/> | ||
| public Resource Detect() | ||
| { | ||
| List<KeyValuePair<string, object>> attributeList = new List<KeyValuePair<string, object>>(); | ||
|
|
||
| try | ||
| { | ||
| var websiteSiteName = Environment.GetEnvironmentVariable(ResourceAttributeConstants.AppServiceSiteNameEnvVar); | ||
|
|
||
| if (websiteSiteName != null) | ||
| { | ||
| attributeList.Add(new KeyValuePair<string, object>(ResourceSemanticConventions.AttributeServiceName, websiteSiteName)); | ||
| attributeList.Add(new KeyValuePair<string, object>(ResourceSemanticConventions.AttributeCloudProvider, ResourceAttributeConstants.AzureCloudProviderValue)); | ||
| attributeList.Add(new KeyValuePair<string, object>(ResourceSemanticConventions.AttributeCloudPlatform, ResourceAttributeConstants.AzureAppServicePlatformValue)); | ||
|
|
||
| var azureResourceUri = GetAzureResourceURI(websiteSiteName); | ||
| if (azureResourceUri != null) | ||
| { | ||
| attributeList.Add(new KeyValuePair<string, object>(ResourceSemanticConventions.AttributeCloudResourceId, azureResourceUri)); | ||
| } | ||
|
|
||
| foreach (var kvp in AppServiceResourceAttributes) | ||
| { | ||
| 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); | ||
| } | ||
|
|
||
| private static string? GetAzureResourceURI(string websiteSiteName) | ||
| { | ||
| var websiteResourceGroup = Environment.GetEnvironmentVariable(ResourceAttributeConstants.AppServiceResourceGroupEnvVar); | ||
| var websiteOwnerName = Environment.GetEnvironmentVariable(ResourceAttributeConstants.AppServiceOwnerNameEnvVar) ?? string.Empty; | ||
|
|
||
| #if NET | ||
| var idx = websiteOwnerName.IndexOf('+', StringComparison.Ordinal); | ||
| #else | ||
| var idx = websiteOwnerName.IndexOf("+", StringComparison.Ordinal); | ||
| #endif | ||
| var subscriptionId = idx > 0 ? websiteOwnerName.Substring(0, idx) : websiteOwnerName; | ||
|
|
||
| return string.IsNullOrEmpty(websiteResourceGroup) || string.IsNullOrEmpty(subscriptionId) | ||
| ? null | ||
| : $"/subscriptions/{subscriptionId}/resourceGroups/{websiteResourceGroup}/providers/Microsoft.Web/sites/{websiteSiteName}"; | ||
| } | ||
| } | ||
| } |
77 changes: 77 additions & 0 deletions
77
.../src/Shared/Vendoring/OpenTelemetry.Resources.Azure/AzureContainerAppsResourceDetector.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| namespace Microsoft.ApplicationInsights.Shared.Vendoring.OpenTelemetry.Resources.Azure | ||
| { | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using global::OpenTelemetry.Resources; | ||
|
|
||
| /// <summary> | ||
| /// Resource detector for Azure Container Apps environment. | ||
| /// </summary> | ||
| internal sealed class AzureContainerAppsResourceDetector : IResourceDetector | ||
| { | ||
| 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>>(); | ||
| try | ||
| { | ||
| var containerAppName = Environment.GetEnvironmentVariable(ResourceAttributeConstants.AzureContainerAppsNameEnvVar); | ||
| var containerAppJobName = Environment.GetEnvironmentVariable(ResourceAttributeConstants.AzureContainerAppJobNameEnvVar); | ||
|
|
||
| if (containerAppName != null) | ||
| { | ||
| AddBaseAttributes(attributeList, containerAppName); | ||
|
|
||
| AddResourceAttributes(attributeList, AzureContainerAppResourceAttributes); | ||
| } | ||
| else if (containerAppJobName != null) | ||
| { | ||
| AddBaseAttributes(attributeList, containerAppJobName); | ||
|
|
||
| AddResourceAttributes(attributeList, AzureContainerAppJobResourceAttributes); | ||
| } | ||
| } | ||
| catch | ||
| { | ||
| // TODO: log exception. | ||
| return Resource.Empty; | ||
| } | ||
|
|
||
| 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)); | ||
| } | ||
| } | ||
| } |
47 changes: 47 additions & 0 deletions
47
NETCORE/src/Shared/Vendoring/OpenTelemetry.Resources.Azure/AzureResourceBuilderExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| namespace Microsoft.ApplicationInsights.Shared.Vendoring.OpenTelemetry.Resources | ||
| { | ||
| using global::OpenTelemetry.Resources; | ||
| using Microsoft.ApplicationInsights.Shared.Vendoring.OpenTelemetry.Resources.Azure; | ||
|
|
||
| /// <summary> | ||
| /// Extension methods to simplify registering of Azure resource detectors. | ||
| /// </summary> | ||
| internal static class AzureResourceBuilderExtensions | ||
| { | ||
| /// <summary> | ||
| /// Enables Azure App Service resource detector. | ||
| /// </summary> | ||
| /// <param name="builder">The <see cref="ResourceBuilder"/> being configured.</param> | ||
| /// <returns>The instance of <see cref="ResourceBuilder"/> being configured.</returns> | ||
| public static ResourceBuilder AddAzureAppServiceDetector(this ResourceBuilder builder) | ||
| { | ||
| Guard.ThrowIfNull(builder); | ||
| return builder.AddDetector(new AppServiceResourceDetector()); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Enables Azure VM resource detector. | ||
| /// </summary> | ||
| /// <param name="builder">The <see cref="ResourceBuilder"/> being configured.</param> | ||
| /// <returns>The instance of <see cref="ResourceBuilder"/> being configured.</returns> | ||
| public static ResourceBuilder AddAzureVMDetector(this ResourceBuilder builder) | ||
| { | ||
| Guard.ThrowIfNull(builder); | ||
| return builder.AddDetector(new AzureVMResourceDetector()); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Enables Azure Container Apps resource detector. | ||
| /// </summary> | ||
| /// <param name="builder">The <see cref="ResourceBuilder"/> being configured.</param> | ||
| /// <returns>The instance of <see cref="ResourceBuilder"/> being configured.</returns> | ||
| public static ResourceBuilder AddAzureContainerAppsDetector(this ResourceBuilder builder) | ||
| { | ||
| Guard.ThrowIfNull(builder); | ||
| return builder.AddDetector(new AzureContainerAppsResourceDetector()); | ||
| } | ||
| } | ||
| } | ||
rajkumar-rangaraj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
72 changes: 72 additions & 0 deletions
72
NETCORE/src/Shared/Vendoring/OpenTelemetry.Resources.Azure/AzureVMResourceDetector.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| #nullable enable | ||
|
|
||
| namespace Microsoft.ApplicationInsights.Shared.Vendoring.OpenTelemetry.Resources.Azure | ||
| { | ||
| using System.Collections.Generic; | ||
| using global::OpenTelemetry; | ||
| using global::OpenTelemetry.Resources; | ||
|
|
||
| /// <summary> | ||
| /// Resource detector for Azure VM environment. | ||
| /// </summary> | ||
| internal sealed class AzureVMResourceDetector : IResourceDetector | ||
| { | ||
| internal static readonly IReadOnlyCollection<string> ExpectedAzureAmsFields = | ||
| [ | ||
| ResourceAttributeConstants.AzureVmScaleSetName, | ||
| ResourceAttributeConstants.AzureVmSku, | ||
| ResourceSemanticConventions.AttributeCloudPlatform, | ||
| ResourceSemanticConventions.AttributeCloudProvider, | ||
| ResourceSemanticConventions.AttributeCloudRegion, | ||
| ResourceSemanticConventions.AttributeCloudResourceId, | ||
| ResourceSemanticConventions.AttributeHostId, | ||
| ResourceSemanticConventions.AttributeHostName, | ||
| ResourceSemanticConventions.AttributeHostType, | ||
| ResourceSemanticConventions.AttributeOsType, | ||
| ResourceSemanticConventions.AttributeOsVersion, | ||
| ResourceSemanticConventions.AttributeServiceInstance | ||
| ]; | ||
rajkumar-rangaraj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private static Resource? vmResource; | ||
|
|
||
| /// <inheritdoc/> | ||
| public Resource Detect() | ||
| { | ||
| try | ||
| { | ||
| if (vmResource != null) | ||
| { | ||
| return vmResource; | ||
| } | ||
|
|
||
| // Prevents the http operations from being instrumented. | ||
| using var scope = SuppressInstrumentationScope.Begin(); | ||
|
|
||
| var vmMetaDataResponse = AzureVmMetaDataRequestor.GetAzureVmMetaDataResponse(); | ||
| if (vmMetaDataResponse == null) | ||
| { | ||
| vmResource = Resource.Empty; | ||
rajkumar-rangaraj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return vmResource; | ||
| } | ||
|
|
||
| var attributeList = new List<KeyValuePair<string, object>>(ExpectedAzureAmsFields.Count); | ||
| foreach (var field in ExpectedAzureAmsFields) | ||
| { | ||
| attributeList.Add(new KeyValuePair<string, object>(field, vmMetaDataResponse.GetValueForField(field))); | ||
| } | ||
|
|
||
| vmResource = new Resource(attributeList); | ||
rajkumar-rangaraj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| catch | ||
| { | ||
| // TODO: log exception. | ||
| vmResource = Resource.Empty; | ||
rajkumar-rangaraj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return vmResource; | ||
rajkumar-rangaraj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
40 changes: 40 additions & 0 deletions
40
NETCORE/src/Shared/Vendoring/OpenTelemetry.Resources.Azure/AzureVmMetaDataRequestor.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| namespace Microsoft.ApplicationInsights.Shared.Vendoring.OpenTelemetry.Resources.Azure | ||
| { | ||
| using System; | ||
| using System.Net.Http; | ||
| #nullable enable | ||
|
|
||
rajkumar-rangaraj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| using System.Text.Json; | ||
|
|
||
| internal static class AzureVmMetaDataRequestor | ||
rajkumar-rangaraj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| private const string AzureVmMetadataEndpointURL = "http://169.254.169.254/metadata/instance/compute?api-version=2021-12-13&format=json"; | ||
|
|
||
| public static Func<AzureVmMetadataResponse?> GetAzureVmMetaDataResponse { get; internal set; } = GetAzureVmMetaDataResponseDefault!; | ||
|
|
||
rajkumar-rangaraj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public static AzureVmMetadataResponse? GetAzureVmMetaDataResponseDefault() | ||
| { | ||
| using var httpClient = new HttpClient() { Timeout = TimeSpan.FromSeconds(2) }; | ||
|
|
||
| httpClient.DefaultRequestHeaders.Add("Metadata", "True"); | ||
| #pragma warning disable AZC0102 // Do not use GetAwaiter().GetResult(). | ||
| #pragma warning disable AZC0104 // Use EnsureCompleted() directly on asynchronous method return value. | ||
| var res = httpClient.GetStringAsync(new Uri(AzureVmMetadataEndpointURL)).ConfigureAwait(false).GetAwaiter().GetResult(); | ||
rajkumar-rangaraj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #pragma warning restore AZC0104 // Use EnsureCompleted() directly on asynchronous method return value. | ||
| #pragma warning restore AZC0102 // Do not use GetAwaiter().GetResult(). | ||
|
|
||
| if (res != null) | ||
| { | ||
| #if NET | ||
| return JsonSerializer.Deserialize(res, SourceGenerationContext.Default.AzureVmMetadataResponse); | ||
| #else | ||
| return JsonSerializer.Deserialize<AzureVmMetadataResponse>(res); | ||
| #endif | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.