Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
namespace Microsoft.ApplicationInsights.Extensibility.Implementation
{
using System;

/// <summary>
/// Encapsulates information about a cloud where an application is running.
/// </summary>
internal sealed class CloudContext
{
/// <summary>
/// Environment variable key used to communicate cloud role name override to the exporter.
/// </summary>
internal const string CloudRoleNameEnvironmentVariable = "APPLICATIONINSIGHTS_CLOUD_ROLE_NAME";

/// <summary>
/// Environment variable key used to communicate cloud role instance override to the exporter.
/// </summary>
internal const string CloudRoleInstanceEnvironmentVariable = "APPLICATIONINSIGHTS_CLOUD_ROLE_INSTANCE";

private string roleName;
private string roleInstance;

Expand All @@ -17,17 +29,33 @@ internal CloudContext()
/// </summary>
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);
}
}

/// <summary>
/// Gets or sets the role instance.
/// </summary>
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);
}
}
}
}
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
Loading