diff --git a/BASE/Test/Microsoft.ApplicationInsights.Test/Microsoft.ApplicationInsights.Tests/TelemetryClientTest.cs b/BASE/Test/Microsoft.ApplicationInsights.Test/Microsoft.ApplicationInsights.Tests/TelemetryClientTest.cs
index 3bc0f475d..8a1e5d9c3 100644
--- a/BASE/Test/Microsoft.ApplicationInsights.Test/Microsoft.ApplicationInsights.Tests/TelemetryClientTest.cs
+++ b/BASE/Test/Microsoft.ApplicationInsights.Test/Microsoft.ApplicationInsights.Tests/TelemetryClientTest.cs
@@ -2151,6 +2151,24 @@ public void TrackRequest_BothUserIdAndAuthenticatedUserId_MapsToBothAttributes()
#endregion
+ [Fact]
+ public void CloudContextRoleNameSetsEnvironmentVariable()
+ {
+ try
+ {
+ this.telemetryClient.Context.Cloud.RoleName = "TestRoleName";
+ Assert.Equal("TestRoleName", Environment.GetEnvironmentVariable("APPLICATIONINSIGHTS_CLOUD_ROLE_NAME"));
+
+ this.telemetryClient.Context.Cloud.RoleInstance = "TestRoleInstance";
+ Assert.Equal("TestRoleInstance", Environment.GetEnvironmentVariable("APPLICATIONINSIGHTS_CLOUD_ROLE_INSTANCE"));
+ }
+ finally
+ {
+ Environment.SetEnvironmentVariable("APPLICATIONINSIGHTS_CLOUD_ROLE_NAME", null);
+ Environment.SetEnvironmentVariable("APPLICATIONINSIGHTS_CLOUD_ROLE_INSTANCE", null);
+ }
+ }
+
private double ComputeSomethingHeavy()
{
var random = new Random();
diff --git a/BASE/src/Microsoft.ApplicationInsights/Extensibility/Implementation/CloudContext.cs b/BASE/src/Microsoft.ApplicationInsights/Extensibility/Implementation/CloudContext.cs
index 92d8c5e57..b827919bd 100644
--- a/BASE/src/Microsoft.ApplicationInsights/Extensibility/Implementation/CloudContext.cs
+++ b/BASE/src/Microsoft.ApplicationInsights/Extensibility/Implementation/CloudContext.cs
@@ -1,10 +1,22 @@
namespace Microsoft.ApplicationInsights.Extensibility.Implementation
{
+ using System;
+
///
/// Encapsulates information about a cloud where an application is running.
///
internal sealed class CloudContext
{
+ ///
+ /// Environment variable key used to communicate cloud role name override to the exporter.
+ ///
+ internal const string CloudRoleNameEnvironmentVariable = "APPLICATIONINSIGHTS_CLOUD_ROLE_NAME";
+
+ ///
+ /// Environment variable key used to communicate cloud role instance override to the exporter.
+ ///
+ internal const string CloudRoleInstanceEnvironmentVariable = "APPLICATIONINSIGHTS_CLOUD_ROLE_INSTANCE";
+
private string roleName;
private string roleInstance;
@@ -17,8 +29,16 @@ internal CloudContext()
///
public string RoleName
{
- get { return string.IsNullOrEmpty(this.roleName) ? null : this.roleName; }
- set { this.roleName = value; }
+ get
+ {
+ return string.IsNullOrEmpty(this.roleName) ? null : this.roleName;
+ }
+
+ set
+ {
+ this.roleName = value;
+ Environment.SetEnvironmentVariable(CloudRoleNameEnvironmentVariable, value);
+ }
}
///
@@ -26,8 +46,16 @@ public string RoleName
///
public string RoleInstance
{
- get { return string.IsNullOrEmpty(this.roleInstance) ? null : this.roleInstance; }
- set { this.roleInstance = value; }
+ get
+ {
+ return string.IsNullOrEmpty(this.roleInstance) ? null : this.roleInstance;
+ }
+
+ set
+ {
+ this.roleInstance = value;
+ Environment.SetEnvironmentVariable(CloudRoleInstanceEnvironmentVariable, value);
+ }
}
}
}
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8f0a51371..3fb00b558 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,7 +1,8 @@
# Changelog
## Unreleased
-- [Fix bug where Debug/Trace level logs from TrackTrace API were not emitted to Application Insights](https://github.com/microsoft/ApplicationInsights-dotnet/pull/3121/changes)
+- [Fix `TelemetryClient.Context.Cloud.RoleName` set after construction now applies to all telemetry.](https://github.com/microsoft/ApplicationInsights-dotnet/pull/3129)
+- [Fix bug where Debug/Trace level logs from TrackTrace API were not emitted to Application Insights](https://github.com/microsoft/ApplicationInsights-dotnet/pull/3121)
- [Fix Track API calls to not mutate the passed in dictionary if it is readonly](https://github.com/microsoft/ApplicationInsights-dotnet/pull/3119)
- [Fix `NullReferenceException` in `TelemetryClient.Flush()` and `FlushAsync()` when called from DI scenarios](https://github.com/microsoft/ApplicationInsights-dotnet/pull/3125)
- [Fix `ApplicationVersion` ignored: `ApplicationInsightsServiceOptions.ApplicationVersion` is now applied as `service.version` on the OpenTelemetry Resource for both AspNetCore and WorkerService packages](https://github.com/microsoft/ApplicationInsights-dotnet/pull/3124)