Skip to content

Fix: Cloud RoleName/Instance update applies to all telemetry#3129

Merged
rajkumar-rangaraj merged 3 commits intomainfrom
rajrang/fixCloudRole
Feb 23, 2026
Merged

Fix: Cloud RoleName/Instance update applies to all telemetry#3129
rajkumar-rangaraj merged 3 commits intomainfrom
rajrang/fixCloudRole

Conversation

@rajkumar-rangaraj
Copy link
Copy Markdown
Member

@rajkumar-rangaraj rajkumar-rangaraj commented Feb 21, 2026

Issue

Setting TelemetryClient.Context.Cloud.RoleName after construction has no effect.

In 2.x, customers could set the cloud role name at any time:

var client = new TelemetryClient(config);
client.Context.Cloud.RoleName = "MyWebApp"; // worked in 2.x, silently ignored in 3.x

In 3.x, Context.Cloud.RoleName is read during the constructor (ApplyCloudContextToResource()), but at that point the value is still null. The OTel Resource is then built and becomes immutable — any value set afterward is ignored.

Fix

CloudContext.RoleName and CloudContext.RoleInstance setters now write to well-known environment variables:

  • APPLICATIONINSIGHTS_CLOUD_ROLE_NAME
  • APPLICATIONINSIGHTS_CLOUD_ROLE_INSTANCE

The Azure Monitor Exporter reads these in SetResourceSdkVersionAndIkey() (the single method where cloud role tags are stamped on every telemetry envelope) and overrides the immutable Resource value. The env var is read once and cached — zero ongoing cost for customers who don't use this feature.

Requires companion PR in azure-sdk-for-net for the exporter-side change.

Follow up

  • Set CloudContext to public.

Checklist

  • I ran Unit Tests locally.
  • CHANGELOG.md updated with one line description of the fix, and a link to the original issue if available.

For significant contributions please make sure you have completed the following items:

  • Design discussion issue #
  • Changes in public surface reviewed

The PR will trigger build, unit tests, and functional tests automatically. Please follow these instructions to build and test locally.

Notes for authors:

  • FxCop and other analyzers will fail the build. To see these errors yourself, compile localy using the Release configuration.

Setting TelemetryClient.Context.Cloud.RoleName and RoleInstance now updates environment variables, ensuring changes are reflected in all telemetry. Added unit test and changelog entry to verify and document this behavior.
Copilot AI review requested due to automatic review settings February 21, 2026 02:06
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR attempts to fix a regression in 3.x where setting TelemetryClient.Context.Cloud.RoleName or RoleInstance after TelemetryClient construction has no effect. The solution writes these values to environment variables that the Azure Monitor Exporter can read at export time, working around the immutable OpenTelemetry Resource. This requires a companion PR in azure-sdk-for-net.

Changes:

  • Modified CloudContext setters to write to environment variables when RoleName/RoleInstance are set
  • Added a test verifying environment variables are set correctly
  • Updated CHANGELOG to document the fix

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.

File Description
CHANGELOG.md Documents the fix with an incomplete PR link
CloudContext.cs Modified RoleName and RoleInstance setters to call Environment.SetEnvironmentVariable
TelemetryClientTest.cs Added test verifying environment variables are set when CloudContext properties are assigned

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@rajkumar-rangaraj rajkumar-rangaraj merged commit 73c567f into main Feb 23, 2026
18 checks passed
@rajkumar-rangaraj rajkumar-rangaraj deleted the rajrang/fixCloudRole branch February 23, 2026 23:00
This was referenced Apr 2, 2026
This was referenced Apr 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants