Skip to content

Commit e0fd5a1

Browse files
[AzureMonitorExporter] Add SDK version labels for Application Insights shim packages (#54011)
* Add SDK version labels for Application Insights shim packages * feedback
1 parent 5920767 commit e0fd5a1

File tree

5 files changed

+289
-22
lines changed

5 files changed

+289
-22
lines changed

sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/CustomerSdkStats/CustomerSdkStatsDimensions.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,19 @@ internal static class CustomerSdkStatsDimensions
2020
/// <returns>TagList with common dimensions</returns>
2121
public static TagList GetBaseTags(string telemetryType)
2222
{
23+
var language = SdkVersionUtils.VersionType switch
24+
{
25+
SdkVersionType.Distro => "dotnet-distro",
26+
SdkVersionType.ShimBase => "dotnet-shim",
27+
SdkVersionType.ShimAspNetCore => "dotnet-core",
28+
SdkVersionType.ShimWorkerService => "dotnet-worker",
29+
SdkVersionType.ShimWeb => "dotnet-web",
30+
_ => "dotnet-exp"
31+
};
32+
2333
return new TagList
2434
{
25-
{ "language", SdkVersionUtils.IsDistro ? "dotnet-distro" : "dotnet-exp" },
35+
{ "language", language },
2636
{ "version", SdkVersionUtils.ExtensionsVersion.Truncate(SchemaConstants.Tags_AiInternalSdkVersion_MaxLength) },
2737
{ "computeType", GetComputeType() },
2838
{ "telemetry_type", telemetryType }

sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/ResourceExtensions.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,15 @@ internal static class ResourceExtensions
7575
serviceVersion = _serviceVersion;
7676
break;
7777
case TelemetryDistroNameKey when attribute.Value is string _aiSdkDistroValue:
78-
if (_aiSdkDistroValue == "Azure.Monitor.OpenTelemetry.AspNetCore")
78+
SdkVersionUtils.VersionType = _aiSdkDistroValue switch
7979
{
80-
SdkVersionUtils.IsDistro = true;
81-
}
80+
"Azure.Monitor.OpenTelemetry.AspNetCore" => SdkVersionType.Distro,
81+
"Microsoft.ApplicationInsights" => SdkVersionType.ShimBase,
82+
"Microsoft.ApplicationInsights.AspNetCore" => SdkVersionType.ShimAspNetCore,
83+
"Microsoft.ApplicationInsights.WorkerService" => SdkVersionType.ShimWorkerService,
84+
"Microsoft.ApplicationInsights.Web" => SdkVersionType.ShimWeb,
85+
_ => SdkVersionUtils.VersionType
86+
};
8287
break;
8388
default:
8489
if (attribute.Key.StartsWith("k8s"))

sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/SdkVersionUtils.cs

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ internal static class SdkVersionUtils
1818
{
1919
private static string? s_prefix;
2020
internal static string s_sdkVersion = GetSdkVersion();
21-
internal static bool s_isDistro = false;
21+
internal static SdkVersionType s_sdkVersionType = SdkVersionType.Exporter;
2222

2323
internal static string? SdkVersionPrefix
2424
{
@@ -30,12 +30,12 @@ internal static string? SdkVersionPrefix
3030
}
3131
}
3232

33-
internal static bool IsDistro
33+
internal static SdkVersionType VersionType
3434
{
35-
get => s_isDistro;
35+
get => s_sdkVersionType;
3636
set
3737
{
38-
s_isDistro = value;
38+
s_sdkVersionType = value;
3939
s_sdkVersion = GetSdkVersion();
4040
}
4141
}
@@ -99,14 +99,33 @@ private static string GetSdkVersion()
9999
string? extensionVersion = GetVersion(typeof(LiveMetrics.AzureMonitorLiveMetricsEventSource));
100100
#endif
101101

102-
if (IsDistro)
102+
string extensionLabel;
103+
switch (VersionType)
103104
{
104-
extensionVersion += "-d";
105+
case SdkVersionType.Distro:
106+
extensionLabel = "dst";
107+
break;
108+
case SdkVersionType.ShimBase:
109+
extensionLabel = "sha";
110+
break;
111+
case SdkVersionType.ShimAspNetCore:
112+
extensionLabel = "shc";
113+
break;
114+
case SdkVersionType.ShimWorkerService:
115+
extensionLabel = "shw";
116+
break;
117+
case SdkVersionType.ShimWeb:
118+
extensionLabel = "shf";
119+
break;
120+
case SdkVersionType.Exporter:
121+
default:
122+
extensionLabel = "ext";
123+
break;
105124
}
106125

107126
ExtensionsVersion = extensionVersion ?? "u"; // 'u' for Unknown
108127

109-
return string.Format(CultureInfo.InvariantCulture, $"{sdkVersionPrefix}dotnet{dotnetSdkVersion}:otel{otelSdkVersion}:ext{extensionVersion}");
128+
return string.Format(CultureInfo.InvariantCulture, $"{sdkVersionPrefix}dotnet{dotnetSdkVersion}:otel{otelSdkVersion}:{extensionLabel}{extensionVersion}");
110129
}
111130
catch (Exception ex)
112131
{
@@ -125,4 +144,23 @@ private static string GetSdkVersion()
125144
}
126145
}
127146
}
147+
148+
/// <summary>
149+
/// Specifies the type of SDK for telemetry identification and version labeling.
150+
/// </summary>
151+
public enum SdkVersionType
152+
{
153+
/// <summary>Default Azure Monitor OpenTelemetry Exporter.</summary>
154+
Exporter,
155+
/// <summary>Azure Monitor OpenTelemetry AspNetCore Distro.</summary>
156+
Distro,
157+
/// <summary>Application Insights base shim (Microsoft.ApplicationInsights).</summary>
158+
ShimBase,
159+
/// <summary>Application Insights AspNetCore shim (Microsoft.ApplicationInsights.AspNetCore).</summary>
160+
ShimAspNetCore,
161+
/// <summary>Application Insights WorkerService shim (Microsoft.ApplicationInsights.WorkerService).</summary>
162+
ShimWorkerService,
163+
/// <summary>Application Insights Web shim (Microsoft.ApplicationInsights.Web).</summary>
164+
ShimWeb
165+
}
128166
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System.Diagnostics;
5+
using Azure.Monitor.OpenTelemetry.Exporter.Internals;
6+
using Azure.Monitor.OpenTelemetry.Exporter.Internals.CustomerSdkStats;
7+
using Xunit;
8+
9+
namespace Azure.Monitor.OpenTelemetry.Exporter.Tests.CustomerSdkStats;
10+
11+
public class CustomerSdkStatsDimensionsTests
12+
{
13+
[Theory]
14+
[InlineData(SdkVersionType.Exporter, "dotnet-exp")]
15+
[InlineData(SdkVersionType.Distro, "dotnet-distro")]
16+
[InlineData(SdkVersionType.ShimBase, "dotnet-shim")]
17+
[InlineData(SdkVersionType.ShimAspNetCore, "dotnet-core")]
18+
[InlineData(SdkVersionType.ShimWorkerService, "dotnet-worker")]
19+
[InlineData(SdkVersionType.ShimWeb, "dotnet-web")]
20+
public void GetBaseTags_SetsCorrectLanguageDimensionForEachSdkVersionType(SdkVersionType versionType, string expectedLanguage)
21+
{
22+
// Arrange
23+
var originalVersionType = SdkVersionUtils.s_sdkVersionType;
24+
SdkVersionUtils.VersionType = versionType;
25+
26+
try
27+
{
28+
// Act
29+
var tags = CustomerSdkStatsDimensions.GetBaseTags("REQUEST");
30+
31+
// Assert
32+
Assert.Contains(tags, tag => tag.Key == "language" && tag.Value?.Equals(expectedLanguage) == true);
33+
Assert.Contains(tags, tag => tag.Key == "telemetry_type" && tag.Value?.Equals("REQUEST") == true);
34+
}
35+
finally
36+
{
37+
// Cleanup
38+
SdkVersionUtils.s_sdkVersionType = originalVersionType;
39+
}
40+
}
41+
42+
[Fact]
43+
public void GetBaseTags_IncludesAllRequiredDimensions()
44+
{
45+
// Act
46+
var tags = CustomerSdkStatsDimensions.GetBaseTags("DEPENDENCY");
47+
48+
// Assert
49+
Assert.Contains(tags, tag => tag.Key == "language");
50+
Assert.Contains(tags, tag => tag.Key == "version");
51+
Assert.Contains(tags, tag => tag.Key == "computeType");
52+
Assert.Contains(tags, tag => tag.Key == "telemetry_type" && tag.Value?.Equals("DEPENDENCY") == true);
53+
}
54+
55+
[Theory]
56+
[InlineData("REQUEST", "200")]
57+
[InlineData("DEPENDENCY", "404")]
58+
[InlineData("EXCEPTION", "500")]
59+
public void GetDroppedTags_IncludesDropCodeAndReason(string telemetryType, string dropCode)
60+
{
61+
// Act
62+
var tags = CustomerSdkStatsDimensions.GetDroppedTags(telemetryType, dropCode, "Test reason");
63+
64+
// Assert
65+
Assert.Contains(tags, tag => tag.Key == "telemetry_type" && tag.Value?.Equals(telemetryType) == true);
66+
Assert.Contains(tags, tag => tag.Key == "drop.code" && tag.Value?.Equals(dropCode) == true);
67+
Assert.Contains(tags, tag => tag.Key == "drop.reason" && tag.Value?.Equals("Test reason") == true);
68+
}
69+
70+
[Theory]
71+
[InlineData("REQUEST", "429")]
72+
[InlineData("DEPENDENCY", "503")]
73+
public void GetRetryTags_IncludesRetryCodeAndReason(string telemetryType, string retryCode)
74+
{
75+
// Act
76+
var tags = CustomerSdkStatsDimensions.GetRetryTags(telemetryType, retryCode, "Test retry");
77+
78+
// Assert
79+
Assert.Contains(tags, tag => tag.Key == "telemetry_type" && tag.Value?.Equals(telemetryType) == true);
80+
Assert.Contains(tags, tag => tag.Key == "retry.code" && tag.Value?.Equals(retryCode) == true);
81+
Assert.Contains(tags, tag => tag.Key == "retry.reason" && tag.Value?.Equals("Test retry") == true);
82+
}
83+
84+
[Fact]
85+
public void GetDroppedTags_WithoutReason_DoesNotIncludeReasonTag()
86+
{
87+
// Act
88+
var tags = CustomerSdkStatsDimensions.GetDroppedTags("REQUEST", "200");
89+
90+
// Assert
91+
Assert.DoesNotContain(tags, tag => tag.Key == "drop.reason");
92+
}
93+
94+
[Fact]
95+
public void GetRetryTags_WithoutReason_DoesNotIncludeReasonTag()
96+
{
97+
// Act
98+
var tags = CustomerSdkStatsDimensions.GetRetryTags("REQUEST", "429");
99+
100+
// Assert
101+
Assert.DoesNotContain(tags, tag => tag.Key == "retry.reason");
102+
}
103+
}

0 commit comments

Comments
 (0)