Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
83b291f
initial draft for public context
harsimar Mar 3, 2026
1b65fa9
component context
harsimar Mar 4, 2026
1426546
add device version
harsimar Mar 5, 2026
f817df5
public cloud
harsimar Mar 6, 2026
13b81bb
remove context not in schema, remove metric context support, need to …
harsimar Mar 7, 2026
38e697b
p1 - add processors, will use later
harsimar Mar 10, 2026
e4a8793
more changes, figure out why logging processor doesn't work in tests …
harsimar Mar 13, 2026
485ff79
cleanup
harsimar Mar 16, 2026
eb4db4d
Add TelemetryContextLogProcessorTests and optimize TelemetryContext p…
rajkumar-rangaraj Mar 20, 2026
696acc8
Add TelemetryContext processors for Activity and Logger in Applicatio…
rajkumar-rangaraj Mar 20, 2026
2985a41
fix user agent
harsimar Mar 20, 2026
0883c23
bring back user agent & update documentation
harsimar Mar 20, 2026
888664c
Merge branch 'main' into harskaur/publicContext
harsimar Mar 23, 2026
c0abfd4
Merge branch 'main' into harskaur/publicContext
harsimar Mar 23, 2026
956a581
Remove Cloud and Component context settings from HomeController example
Copilot Mar 27, 2026
384a7c3
Replace real connection string with dummy value; remove Setting Conte…
Copilot Mar 27, 2026
7d23d45
Replace Assert.Fail with Assert.True(false, ...) for xUnit v2 compati…
Copilot Mar 27, 2026
97e9ae9
pr comments
harsimar Mar 30, 2026
4c30f54
Merge branch 'harskaur/publicContext' of https://github.com/microsoft…
harsimar Mar 30, 2026
13b1932
Merge branch 'main' into harskaur/publicContext
harsimar Mar 30, 2026
c984b3a
Merge branch 'main' into harskaur/publicContext
rajkumar-rangaraj Mar 31, 2026
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 @@ -146,8 +146,6 @@ Microsoft.ApplicationInsights.Extensibility.Implementation.UserContext.Authentic
Microsoft.ApplicationInsights.Extensibility.Implementation.UserContext.AuthenticatedUserId.set -> void
Microsoft.ApplicationInsights.Extensibility.Implementation.UserContext.Id.get -> string
Microsoft.ApplicationInsights.Extensibility.Implementation.UserContext.Id.set -> void
Microsoft.ApplicationInsights.Extensibility.Implementation.UserContext.UserAgent.get -> string
Comment thread
harsimar marked this conversation as resolved.
Microsoft.ApplicationInsights.Extensibility.Implementation.UserContext.UserAgent.set -> void
Microsoft.ApplicationInsights.Extensibility.IOperationHolder<T>
Microsoft.ApplicationInsights.Extensibility.IOperationHolder<T>.Telemetry.get -> T
Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Microsoft.ApplicationInsights.DataContracts.TelemetryContext.Device.get -> Microsoft.ApplicationInsights.Extensibility.Implementation.DeviceContext
Microsoft.ApplicationInsights.DataContracts.TelemetryContext.Session.get -> Microsoft.ApplicationInsights.Extensibility.Implementation.SessionContext
Microsoft.ApplicationInsights.Extensibility.Implementation.DeviceContext
Microsoft.ApplicationInsights.Extensibility.Implementation.DeviceContext.Id.get -> string
Microsoft.ApplicationInsights.Extensibility.Implementation.DeviceContext.Id.set -> void
Microsoft.ApplicationInsights.Extensibility.Implementation.DeviceContext.Model.get -> string
Microsoft.ApplicationInsights.Extensibility.Implementation.DeviceContext.Model.set -> void
Microsoft.ApplicationInsights.Extensibility.Implementation.DeviceContext.OperatingSystem.get -> string
Microsoft.ApplicationInsights.Extensibility.Implementation.DeviceContext.OperatingSystem.set -> void
Microsoft.ApplicationInsights.Extensibility.Implementation.DeviceContext.Type.get -> string
Microsoft.ApplicationInsights.Extensibility.Implementation.DeviceContext.Type.set -> void
Microsoft.ApplicationInsights.Extensibility.Implementation.OperationContext.SyntheticSource.get -> string
Microsoft.ApplicationInsights.Extensibility.Implementation.OperationContext.SyntheticSource.set -> void
Microsoft.ApplicationInsights.Extensibility.Implementation.SessionContext
Microsoft.ApplicationInsights.Extensibility.Implementation.SessionContext.Id.get -> string
Microsoft.ApplicationInsights.Extensibility.Implementation.SessionContext.Id.set -> void
Microsoft.ApplicationInsights.Extensibility.Implementation.UserContext.AccountId.get -> string
Microsoft.ApplicationInsights.Extensibility.Implementation.UserContext.AccountId.set -> void
Microsoft.ApplicationInsights.DataContracts.TelemetryContext.Component.get -> Microsoft.ApplicationInsights.Extensibility.Implementation.ComponentContext
Microsoft.ApplicationInsights.Extensibility.Implementation.ComponentContext
Microsoft.ApplicationInsights.Extensibility.Implementation.ComponentContext.Version.get -> string
Microsoft.ApplicationInsights.Extensibility.Implementation.ComponentContext.Version.set -> void
Microsoft.ApplicationInsights.DataContracts.TelemetryContext.Cloud.get -> Microsoft.ApplicationInsights.Extensibility.Implementation.CloudContext
Microsoft.ApplicationInsights.Extensibility.Implementation.CloudContext
Microsoft.ApplicationInsights.Extensibility.Implementation.CloudContext.RoleName.get -> string
Microsoft.ApplicationInsights.Extensibility.Implementation.CloudContext.RoleName.set -> void
Microsoft.ApplicationInsights.Extensibility.Implementation.CloudContext.RoleInstance.get -> string
Microsoft.ApplicationInsights.Extensibility.Implementation.CloudContext.RoleInstance.set -> void
2 changes: 1 addition & 1 deletion BASE/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ In version 3.x, the following properties remain publicly settable on telemetry i
**Available Context Properties:**
| Context | Properties | Notes |
|---------|-----------|-------|
| `User` | `Id`, `AuthenticatedUserId`, `UserAgent` | Be mindful of PII |
| `User` | `Id`, `AuthenticatedUserId` | Be mindful of PII |
| `Operation` | `Name`| |
| `Location` | `Ip` | |
Comment thread
harsimar marked this conversation as resolved.
Outdated
| `GlobalProperties` | (dictionary) | Custom key-value pairs |
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2169,6 +2169,20 @@ public void CloudContextRoleNameSetsEnvironmentVariable()
}
}

[Fact]
public void ComponentContextVersionSetsEnvironmentVariable()
{
try
{
this.telemetryClient.Context.Component.Version = "1.2.3";
Assert.Equal("1.2.3", Environment.GetEnvironmentVariable("APPLICATIONINSIGHTS_COMPONENT_VERSION"));
}
finally
{
Environment.SetEnvironmentVariable("APPLICATIONINSIGHTS_COMPONENT_VERSION", null);
}
}

private double ComputeSomethingHeavy()
{
var random = new Random();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,37 +77,35 @@ public LocationContext Location
}

/// <summary>
/// Gets the object describing the cloud tracked by this <see cref="TelemetryContext"/>.
/// Gets the object describing the device tracked by this <see cref="TelemetryContext"/>.
Comment thread
harsimar marked this conversation as resolved.
Outdated
/// </summary>
internal CloudContext Cloud
public DeviceContext Device
{
get { return LazyInitializer.EnsureInitialized(ref this.cloud, () => new CloudContext()); }
get { return LazyInitializer.EnsureInitialized(ref this.device, () => new DeviceContext(default)); }
}

/// <summary>
/// Gets the object describing the component tracked by this <see cref="TelemetryContext"/>.
/// Gets the object describing a user session tracked by this <see cref="TelemetryContext"/>.
/// </summary>
internal ComponentContext Component
public SessionContext Session
{
get { return LazyInitializer.EnsureInitialized(ref this.component, () => new ComponentContext()); }
get { return LazyInitializer.EnsureInitialized(ref this.session, () => new SessionContext()); }
}

/// <summary>
/// Gets the object describing the device tracked by this <see cref="TelemetryContext"/>.
/// Gets the object describing the component tracked by this <see cref="TelemetryContext"/>.
/// </summary>
internal DeviceContext Device
public ComponentContext Component
{
#pragma warning disable CS0618 // Type or member is obsolete
get { return LazyInitializer.EnsureInitialized(ref this.device, () => new DeviceContext(default)); }
#pragma warning restore CS0618 // Type or member is obsolete
get { return LazyInitializer.EnsureInitialized(ref this.component, () => new ComponentContext()); }
}

/// <summary>
/// Gets the object describing a user session tracked by this <see cref="TelemetryContext"/>.
/// Gets the object describing the cloud tracked by this <see cref="TelemetryContext"/>.
/// </summary>
internal SessionContext Session
public CloudContext Cloud
{
get { return LazyInitializer.EnsureInitialized(ref this.session, () => new SessionContext()); }
get { return LazyInitializer.EnsureInitialized(ref this.cloud, () => new CloudContext()); }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/// <summary>
/// Encapsulates information about a cloud where an application is running.
/// </summary>
internal sealed class CloudContext
public sealed class CloudContext
{
/// <summary>
/// Environment variable key used to communicate cloud role name override to the exporter.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
namespace Microsoft.ApplicationInsights.Extensibility.Implementation
{
using System;

/// <summary>
/// Encapsulates information describing an Application Insights component.
/// </summary>
Expand All @@ -8,8 +10,13 @@
/// with terminology used by our portal and services and to encourage standardization of terminology within our
/// organization. Once a consensus is reached, we will change type and property names to match.
/// </remarks>
internal sealed class ComponentContext
public sealed class ComponentContext
{
/// <summary>
/// Environment variable key used to communicate component version to the exporter.
/// </summary>
internal const string ComponentVersionEnvironmentVariable = "APPLICATIONINSIGHTS_COMPONENT_VERSION";

private string version;

internal ComponentContext()
Expand All @@ -21,8 +28,16 @@ internal ComponentContext()
/// </summary>
public string Version
{
get { return string.IsNullOrEmpty(this.version) ? null : this.version; }
set { this.version = value; }
get
{
return string.IsNullOrEmpty(this.version) ? null : this.version;
}

set
{
this.version = value;
Environment.SetEnvironmentVariable(ComponentVersionEnvironmentVariable, value);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/// <summary>
/// Encapsulates information about a device where an application is running.
/// </summary>
internal sealed class DeviceContext
public sealed class DeviceContext
{
private readonly IDictionary<string, string> properties;

Expand Down Expand Up @@ -48,21 +48,21 @@ public string OperatingSystem
}

/// <summary>
/// Gets or sets the device OEM for the current device.
/// Gets or sets the device model for the current device.
/// </summary>
public string OemName
public string Model
{
get { return string.IsNullOrEmpty(this.oemName) ? null : this.oemName; }
set { this.oemName = value; }
get { return string.IsNullOrEmpty(this.model) ? null : this.model; }
set { this.model = value; }
}

/// <summary>
/// Gets or sets the device model for the current device.
/// Gets or sets the device OEM for the current device.
/// </summary>
public string Model
internal string OemName
{
get { return string.IsNullOrEmpty(this.model) ? null : this.model; }
set { this.model = value; }
get { return string.IsNullOrEmpty(this.oemName) ? null : this.oemName; }
set { this.oemName = value; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public string Name
/// <summary>
/// Gets or sets the application-defined operation SyntheticSource.
/// </summary>
internal string SyntheticSource
public string SyntheticSource
{
get { return string.IsNullOrEmpty(this.syntheticSource) ? null : this.syntheticSource; }
set { this.syntheticSource = value; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// <summary>
/// Encapsulates information about a user session.
/// </summary>
internal sealed class SessionContext
public sealed class SessionContext
{
private string id;
private bool? isFirst;
Expand All @@ -24,7 +24,7 @@ public string Id
/// <summary>
/// Gets or sets the IsFirst Session for the user.
/// </summary>
public bool? IsFirst
internal bool? IsFirst
{
get { return this.isFirst; }
set { this.isFirst = value; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,6 @@ public string Id
set { this.id = value; }
}

/// <summary>
/// Gets or sets the UserAgent of an application-defined account associated with the user.
/// </summary>
public string UserAgent
{
get { return string.IsNullOrEmpty(this.userAgent) ? null : this.userAgent; }
set { this.userAgent = value; }
}

/// <summary>
/// Gets or sets the authenticated user id.
/// Authenticated user id should be a persistent string that uniquely represents each authenticated user in the application or service.
Expand All @@ -48,10 +39,19 @@ public string AuthenticatedUserId
/// <summary>
/// Gets or sets the ID of an application-defined account associated with the user.
/// </summary>
internal string AccountId
public string AccountId
{
get { return string.IsNullOrEmpty(this.accountId) ? null : this.accountId; }
set { this.accountId = value; }
}

/// <summary>
/// Gets or sets the UserAgent of an application-defined account associated with the user.
/// </summary>
internal string UserAgent
{
get { return string.IsNullOrEmpty(this.userAgent) ? null : this.userAgent; }
set { this.userAgent = value; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,23 @@ public void SetAzureTokenCredential(TokenCredential tokenCredential)
});
}

/// <summary>
/// Prepends a configuration action so it runs before any user-registered configuration.
/// This ensures that enrichment processors (e.g., TelemetryContextLogProcessor) execute
/// before export processors, regardless of the order users call ConfigureOpenTelemetryBuilder.
/// </summary>
internal void PrependOpenTelemetryBuilderConfiguration(Action<IOpenTelemetryBuilder> configure)
{
this.ThrowIfBuilt();

var previousConfiguration = this.builderConfiguration;
this.builderConfiguration = builder =>
{
configure(builder);
previousConfiguration(builder);
};
}

/// <summary>
/// Sets the cloud role name and role instance for telemetry.
/// This configures the OpenTelemetry Resource with service.name, service.namespace, service.instance.id, and service.version attributes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,5 +231,26 @@ internal static class SemanticConventions
/// When present, maps to ai.location.ip context tag.
/// </summary>
public const string AttributeMicrosoftClientIp = "microsoft.client.ip";

/// <summary>Attribute for session ID.</summary>
public const string AttributeMicrosoftSessionId = "microsoft.session.id";

/// <summary>Attribute for device ID.</summary>
public const string AttributeAiDeviceId = "ai.device.id";

/// <summary>Attribute for device model.</summary>
public const string AttributeAiDeviceModel = "ai.device.model";

/// <summary>Attribute for device type.</summary>
public const string AttributeAiDeviceType = "ai.device.type";

/// <summary>Attribute for device OS version.</summary>
public const string AttributeAiDeviceOsVersion = "ai.device.osVersion";

/// <summary>Attribute for synthetic source.</summary>
public const string AttributeMicrosoftSyntheticSource = "microsoft.synthetic_source";

/// <summary>Attribute for user account ID.</summary>
public const string AttributeMicrosoftUserAccountId = "microsoft.user.account_id";
}
}
Loading
Loading