Fix: Cloud RoleName/Instance update applies to all telemetry#3129
Merged
rajkumar-rangaraj merged 3 commits intomainfrom Feb 23, 2026
Merged
Fix: Cloud RoleName/Instance update applies to all telemetry#3129rajkumar-rangaraj merged 3 commits intomainfrom
rajkumar-rangaraj merged 3 commits intomainfrom
Conversation
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.
Contributor
There was a problem hiding this comment.
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.
BASE/src/Microsoft.ApplicationInsights/Extensibility/Implementation/CloudContext.cs
Show resolved
Hide resolved
...icrosoft.ApplicationInsights.Test/Microsoft.ApplicationInsights.Tests/TelemetryClientTest.cs
Show resolved
Hide resolved
BASE/src/Microsoft.ApplicationInsights/Extensibility/Implementation/CloudContext.cs
Show resolved
Hide resolved
...icrosoft.ApplicationInsights.Test/Microsoft.ApplicationInsights.Tests/TelemetryClientTest.cs
Show resolved
Hide resolved
harsimar
approved these changes
Feb 23, 2026
This was referenced Apr 2, 2026
Open
Closed
Open
This was referenced Apr 5, 2026
Merged
Merged
Open
Bump Microsoft.ApplicationInsights.WorkerService from 3.0.0 to 3.1.0
mspnp/cloud-design-patterns#473
Merged
Open
Open
Open
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue
Setting
TelemetryClient.Context.Cloud.RoleNameafter construction has no effect.In 2.x, customers could set the cloud role name at any time:
In 3.x,
Context.Cloud.RoleNameis read during the constructor (ApplyCloudContextToResource()), but at that point the value is stillnull. The OTelResourceis then built and becomes immutable — any value set afterward is ignored.Fix
CloudContext.RoleNameandCloudContext.RoleInstancesetters now write to well-known environment variables:APPLICATIONINSIGHTS_CLOUD_ROLE_NAMEAPPLICATIONINSIGHTS_CLOUD_ROLE_INSTANCEThe 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
CloudContextto public.Checklist
For significant contributions please make sure you have completed the following items:
The PR will trigger build, unit tests, and functional tests automatically. Please follow these instructions to build and test locally.
Notes for authors: