Skip to content

Commit db7b26e

Browse files
author
Timothy Mothra
authored
[AzureMonidorDistro] sync vendored code (Azure#46207)
* update resources.azure * update shared * changelog * typo
1 parent 26e83cc commit db7b26e

15 files changed

+82
-60
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323

2424
### Other Changes
2525

26+
* Updated the code of vendored resource detector library `OpenTelemetry.Resources.Azure` from the OpenTelemetry .NET contrib repository.
27+
Code has been updated to [1.0.0-beta.9](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/tree/Resources.Azure-1.0.0-beta.9/src/OpenTelemetry.Resources.Azure).
28+
([#46207](https://github.com/Azure/azure-sdk-for-net/pull/46207))
29+
2630
* Updated field mappings for telemetry sent to LiveMetrics.
2731
([#45103](https://github.com/Azure/azure-sdk-for-net/pull/45103))
2832

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
using System;
5-
using System.Collections.Generic;
64
using OpenTelemetry.Trace;
75

86
namespace OpenTelemetry.Resources.Azure;
@@ -66,7 +64,7 @@ public Resource Detect()
6664
string? websiteResourceGroup = Environment.GetEnvironmentVariable(ResourceAttributeConstants.AppServiceResourceGroupEnvVar);
6765
string websiteOwnerName = Environment.GetEnvironmentVariable(ResourceAttributeConstants.AppServiceOwnerNameEnvVar) ?? string.Empty;
6866

69-
#if NET6_0_OR_GREATER
67+
#if NET
7068
int idx = websiteOwnerName.IndexOf('+', StringComparison.Ordinal);
7169
#else
7270
int idx = websiteOwnerName.IndexOf("+", StringComparison.Ordinal);
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
using System;
5-
using System.Collections.Generic;
64
using OpenTelemetry.Trace;
75

86
namespace OpenTelemetry.Resources.Azure;
@@ -12,35 +10,38 @@ namespace OpenTelemetry.Resources.Azure;
1210
/// </summary>
1311
internal sealed class AzureContainerAppsResourceDetector : IResourceDetector
1412
{
15-
internal static readonly IReadOnlyDictionary<string, string> AzureContainerResourceAttributes = new Dictionary<string, string>
13+
internal static readonly IReadOnlyDictionary<string, string> AzureContainerAppResourceAttributes = new Dictionary<string, string>
1614
{
1715
{ ResourceSemanticConventions.AttributeServiceInstance, ResourceAttributeConstants.AzureContainerAppsReplicaNameEnvVar },
1816
{ ResourceSemanticConventions.AttributeServiceVersion, ResourceAttributeConstants.AzureContainerAppsRevisionEnvVar },
1917
};
2018

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+
2125
/// <inheritdoc/>
2226
public Resource Detect()
2327
{
24-
List<KeyValuePair<string, object>> attributeList = new();
25-
28+
List<KeyValuePair<string, object>> attributeList = new List<KeyValuePair<string, object>>();
2629
try
2730
{
2831
var containerAppName = Environment.GetEnvironmentVariable(ResourceAttributeConstants.AzureContainerAppsNameEnvVar);
32+
var containerAppJobName = Environment.GetEnvironmentVariable(ResourceAttributeConstants.AzureContainerAppJobNameEnvVar);
2933

3034
if (containerAppName != null)
3135
{
32-
attributeList.Add(new KeyValuePair<string, object>(ResourceSemanticConventions.AttributeServiceName, containerAppName));
33-
attributeList.Add(new KeyValuePair<string, object>(ResourceSemanticConventions.AttributeCloudProvider, ResourceAttributeConstants.AzureCloudProviderValue));
34-
attributeList.Add(new KeyValuePair<string, object>(ResourceSemanticConventions.AttributeCloudPlatform, ResourceAttributeConstants.AzureContainerAppsPlatformValue));
35-
36-
foreach (var kvp in AzureContainerResourceAttributes)
37-
{
38-
var attributeValue = Environment.GetEnvironmentVariable(kvp.Value);
39-
if (attributeValue != null)
40-
{
41-
attributeList.Add(new KeyValuePair<string, object>(kvp.Key, attributeValue));
42-
}
43-
}
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);
4445
}
4546
}
4647
catch
@@ -51,4 +52,23 @@ public Resource Detect()
5152

5253
return new Resource(attributeList);
5354
}
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+
}
5474
}

sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Vendoring/OpenTelemetry.Resources.Azure/AzureResourceBuilderExtensions.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ internal static class AzureResourceBuilderExtensions
1414
/// <summary>
1515
/// Enables Azure App Service resource detector.
1616
/// </summary>
17-
/// <param name="builder"><see cref="ResourceBuilder" /> being configured.</param>
18-
/// <returns>The instance of <see cref="ResourceBuilder" /> being configured.</returns>
17+
/// <param name="builder">The <see cref="ResourceBuilder"/> being configured.</param>
18+
/// <returns>The instance of <see cref="ResourceBuilder"/> being configured.</returns>
1919
public static ResourceBuilder AddAzureAppServiceDetector(this ResourceBuilder builder)
2020
{
2121
Guard.ThrowIfNull(builder);
@@ -25,8 +25,8 @@ public static ResourceBuilder AddAzureAppServiceDetector(this ResourceBuilder bu
2525
/// <summary>
2626
/// Enables Azure VM resource detector.
2727
/// </summary>
28-
/// <param name="builder"><see cref="ResourceBuilder" /> being configured.</param>
29-
/// <returns>The instance of <see cref="ResourceBuilder" /> being configured.</returns>
28+
/// <param name="builder">The <see cref="ResourceBuilder"/> being configured.</param>
29+
/// <returns>The instance of <see cref="ResourceBuilder"/> being configured.</returns>
3030
public static ResourceBuilder AddAzureVMDetector(this ResourceBuilder builder)
3131
{
3232
Guard.ThrowIfNull(builder);
@@ -36,8 +36,8 @@ public static ResourceBuilder AddAzureVMDetector(this ResourceBuilder builder)
3636
/// <summary>
3737
/// Enables Azure Container Apps resource detector.
3838
/// </summary>
39-
/// <param name="builder"><see cref="ResourceBuilder" /> being configured.</param>
40-
/// <returns>The instance of <see cref="ResourceBuilder" /> being configured.</returns>
39+
/// <param name="builder">The <see cref="ResourceBuilder"/> being configured.</param>
40+
/// <returns>The instance of <see cref="ResourceBuilder"/> being configured.</returns>
4141
public static ResourceBuilder AddAzureContainerAppsDetector(this ResourceBuilder builder)
4242
{
4343
Guard.ThrowIfNull(builder);

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
using System.Collections.Generic;
54
using OpenTelemetry.Trace;
65

76
namespace OpenTelemetry.Resources.Azure;

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
using System;
5-
using System.Net.Http;
64
using System.Text.Json;
75

86
namespace OpenTelemetry.Resources.Azure;
@@ -26,7 +24,7 @@ internal static class AzureVmMetaDataRequestor
2624

2725
if (res != null)
2826
{
29-
#if NET6_0_OR_GREATER
27+
#if NET
3028
return JsonSerializer.Deserialize(res, SourceGenerationContext.Default.AzureVmMetadataResponse);
3129
#else
3230
return JsonSerializer.Deserialize<AzureVmMetadataResponse>(res);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ internal sealed class ResourceAttributeConstants
2727
internal const string AzureContainerAppsReplicaNameEnvVar = "CONTAINER_APP_REPLICA_NAME";
2828
internal const string AzureContainerAppsRevisionEnvVar = "CONTAINER_APP_REVISION";
2929

30+
// Azure Container Apps Jobs environment variables
31+
internal const string AzureContainerAppJobNameEnvVar = "CONTAINER_APP_JOB_NAME";
32+
internal const string AzureContainerAppJobExecutionNameEnvVar = "CONTAINER_APP_JOB_EXECUTION_NAME";
33+
3034
// Azure resource attributes constant values
3135
internal const string AzureAppServicePlatformValue = "azure_app_service";
3236
internal const string AzureCloudProviderValue = "azure";

sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Vendoring/OpenTelemetry.Resources.Azure/SourceGenerationContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
#if NET6_0_OR_GREATER
4+
#if NET
55
using System.Text.Json.Serialization;
66

77
namespace OpenTelemetry.Resources.Azure;

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33

44
#nullable enable
55

6-
#pragma warning disable IDE0005 // Using directive is unnecessary.
7-
using System;
86
using System.Diagnostics;
97
using System.Reflection;
10-
#pragma warning restore IDE0005 // Using directive is unnecessary.
118

129
namespace OpenTelemetry.Internal;
1310

@@ -26,7 +23,7 @@ public static string GetPackageVersion(this Assembly assembly)
2623
var informationalVersion = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion;
2724
Debug.Assert(!string.IsNullOrEmpty(informationalVersion), "AssemblyInformationalVersionAttribute was not found in assembly");
2825

29-
#if NET6_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
26+
#if NET || NETSTANDARD2_1_OR_GREATER
3027
var indexOfPlusSign = informationalVersion!.IndexOf('+', StringComparison.Ordinal);
3128
#else
3229
var indexOfPlusSign = informationalVersion!.IndexOf('+');

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@
33

44
#nullable enable
55

6-
#pragma warning disable IDE0005 // Using directive is unnecessary.
7-
using System;
8-
using System.Collections.Generic;
96
using System.Diagnostics;
107
using OpenTelemetry.Internal;
11-
#pragma warning restore IDE0005 // Using directive is unnecessary.
128

139
namespace OpenTelemetry.Instrumentation;
1410

0 commit comments

Comments
 (0)