Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 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 @@ -17,6 +17,7 @@ internal partial class TelemetryItem
{
private static volatile string? s_cloudRoleNameOverride;
private static volatile string? s_cloudRoleInstanceOverride;
private static volatile string? s_componentVersionOverride;

public TelemetryItem(Activity activity, ref ActivityTagsProcessor activityTagsProcessor, AzureMonitorResource? resource, string instrumentationKey, float sampleRate) :
this(activity.GetTelemetryType() == TelemetryType.Request ? "Request" : "RemoteDependency", FormatUtcTimestamp(activity.StartTimeUtc))
Expand All @@ -28,7 +29,7 @@ public TelemetryItem(Activity activity, ref ActivityTagsProcessor activityTagsPr

Tags[ContextTagKeys.AiOperationId.ToString()] = activity.TraceId.ToHexString();

string? microsoftClientIp = AzMonList.GetTagValue(ref activityTagsProcessor.MappedTags, "microsoft.client.ip")?.ToString();
string? microsoftClientIp = AzMonList.GetTagValue(ref activityTagsProcessor.MappedTags, SemanticConventions.AttributeMicrosoftClientIp)?.ToString();

// Check for microsoft.operation_name override (applies to both request and dependency)
string? overrideOperationName = activityTagsProcessor.HasOverrideAttributes
Expand Down Expand Up @@ -88,6 +89,7 @@ public TelemetryItem(Activity activity, ref ActivityTagsProcessor activityTagsPr
}

SetUserIdAndAuthenticatedUserId(ref activityTagsProcessor);
SetOverrideContextTags(ref activityTagsProcessor);
Comment thread
harsimar marked this conversation as resolved.
Outdated
SetResourceSdkVersionAndIkey(resource, instrumentationKey);

if (sampleRate != 100f)
Expand All @@ -112,6 +114,13 @@ public TelemetryItem(string name, TelemetryItem telemetryItem, ActivitySpanId ac
Tags[ContextTagKeys.AiCloudRoleInstance.ToString()] = telemetryItem.Tags[ContextTagKeys.AiCloudRoleInstance.ToString()].Truncate(SchemaConstants.Tags_AiCloudRoleInstance_MaxLength);
Tags[ContextTagKeys.AiInternalSdkVersion.ToString()] = SdkVersionUtils.s_sdkVersion.Truncate(SchemaConstants.Tags_AiInternalSdkVersion_MaxLength);
Tags[ContextTagKeys.AiApplicationVer.ToString()] = telemetryItem.Tags[ContextTagKeys.AiApplicationVer.ToString()].Truncate(SchemaConstants.Tags_AiApplicationVer_MaxLength);
CopyTagIfPresent(telemetryItem, ContextTagKeys.AiSessionId.ToString());
CopyTagIfPresent(telemetryItem, ContextTagKeys.AiDeviceId.ToString());
CopyTagIfPresent(telemetryItem, ContextTagKeys.AiDeviceModel.ToString());
CopyTagIfPresent(telemetryItem, ContextTagKeys.AiDeviceType.ToString());
CopyTagIfPresent(telemetryItem, ContextTagKeys.AiDeviceOSVersion.ToString());
CopyTagIfPresent(telemetryItem, ContextTagKeys.AiOperationSyntheticSource.ToString());
CopyTagIfPresent(telemetryItem, ContextTagKeys.AiUserAccountId.ToString());
InstrumentationKey = telemetryItem.InstrumentationKey;

if (telemetryItem.SampleRate != 100f)
Expand Down Expand Up @@ -160,6 +169,41 @@ public TelemetryItem (string name, LogRecord logRecord, AzureMonitorResource? re
{
Tags[ContextTagKeys.AiOperationName.ToString()] = logContext.OperationName.Truncate(SchemaConstants.Tags_AiOperationName_MaxLength);
}

if (logContext.SessionId != null)
{
Tags[ContextTagKeys.AiSessionId.ToString()] = logContext.SessionId.Truncate(SchemaConstants.Tags_AiSessionId_MaxLength);
}

if (logContext.DeviceId != null)
{
Tags[ContextTagKeys.AiDeviceId.ToString()] = logContext.DeviceId.Truncate(SchemaConstants.Tags_AiDeviceId_MaxLength);
}

if (logContext.DeviceModel != null)
{
Tags[ContextTagKeys.AiDeviceModel.ToString()] = logContext.DeviceModel.Truncate(SchemaConstants.Tags_AiDeviceModel_MaxLength);
}

if (logContext.DeviceType != null)
{
Tags[ContextTagKeys.AiDeviceType.ToString()] = logContext.DeviceType.Truncate(SchemaConstants.Tags_AiDeviceType_MaxLength);
}

if (logContext.DeviceOsVersion != null)
{
Tags[ContextTagKeys.AiDeviceOSVersion.ToString()] = logContext.DeviceOsVersion.Truncate(SchemaConstants.Tags_AiDeviceOsVersion_MaxLength);
}

if (logContext.SyntheticSource != null)
{
Tags[ContextTagKeys.AiOperationSyntheticSource.ToString()] = logContext.SyntheticSource.Truncate(SchemaConstants.Tags_AiOperationSyntheticSource_MaxLength);
}

if (logContext.UserAccountId != null)
{
Tags[ContextTagKeys.AiUserAccountId.ToString()] = logContext.UserAccountId.Truncate(SchemaConstants.Tags_AiUserAccountId_MaxLength);
}
Comment thread
harsimar marked this conversation as resolved.
}

InstrumentationKey = instrumentationKey;
Expand Down Expand Up @@ -196,6 +240,12 @@ private void SetResourceSdkVersionAndIkey(AzureMonitorResource? resource, string
{
Tags[ContextTagKeys.AiCloudRoleInstance.ToString()] = roleInstance.Truncate(SchemaConstants.Tags_AiCloudRoleInstance_MaxLength);
}

var componentVersion = s_componentVersionOverride ?? (s_componentVersionOverride = Environment.GetEnvironmentVariable(EnvironmentVariableConstants.APPLICATIONINSIGHTS_COMPONENT_VERSION) ?? string.Empty);
if (componentVersion.Length > 0)
{
Tags[ContextTagKeys.AiApplicationVer.ToString()] = componentVersion.Truncate(SchemaConstants.Tags_AiApplicationVer_MaxLength);
}
}

/// <summary>
Expand All @@ -205,13 +255,23 @@ internal static void ResetEnvironmentVariableOverrides()
{
s_cloudRoleNameOverride = null;
s_cloudRoleInstanceOverride = null;
s_componentVersionOverride = null;
}

internal static DateTimeOffset FormatUtcTimestamp(System.DateTime utcTimestamp)
{
return DateTime.SpecifyKind(utcTimestamp, DateTimeKind.Utc);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void CopyTagIfPresent(TelemetryItem source, string key)
{
if (source.Tags.TryGetValue(key, out string? value) && value != null)
Comment thread
harsimar marked this conversation as resolved.
{
Tags[key] = value;
Comment thread
harsimar marked this conversation as resolved.
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void SetUserIdAndAuthenticatedUserId(ref ActivityTagsProcessor activityTagsProcessor)
{
Expand All @@ -225,5 +285,44 @@ private void SetUserIdAndAuthenticatedUserId(ref ActivityTagsProcessor activityT
Tags[ContextTagKeys.AiUserId.ToString()] = activityTagsProcessor.EndUserPseudoId.Truncate(SchemaConstants.Tags_AiUserId_MaxLength);
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void SetOverrideContextTags(ref ActivityTagsProcessor activityTagsProcessor)
{
if (activityTagsProcessor.SessionId != null)
{
Tags[ContextTagKeys.AiSessionId.ToString()] = activityTagsProcessor.SessionId.Truncate(SchemaConstants.Tags_AiSessionId_MaxLength);
}

if (activityTagsProcessor.DeviceId != null)
{
Tags[ContextTagKeys.AiDeviceId.ToString()] = activityTagsProcessor.DeviceId.Truncate(SchemaConstants.Tags_AiDeviceId_MaxLength);
}

if (activityTagsProcessor.DeviceModel != null)
{
Tags[ContextTagKeys.AiDeviceModel.ToString()] = activityTagsProcessor.DeviceModel.Truncate(SchemaConstants.Tags_AiDeviceModel_MaxLength);
}

if (activityTagsProcessor.DeviceType != null)
{
Tags[ContextTagKeys.AiDeviceType.ToString()] = activityTagsProcessor.DeviceType.Truncate(SchemaConstants.Tags_AiDeviceType_MaxLength);
}

if (activityTagsProcessor.DeviceOsVersion != null)
{
Tags[ContextTagKeys.AiDeviceOSVersion.ToString()] = activityTagsProcessor.DeviceOsVersion.Truncate(SchemaConstants.Tags_AiDeviceOsVersion_MaxLength);
}

if (activityTagsProcessor.SyntheticSource != null)
{
Tags[ContextTagKeys.AiOperationSyntheticSource.ToString()] = activityTagsProcessor.SyntheticSource.Truncate(SchemaConstants.Tags_AiOperationSyntheticSource_MaxLength);
}

if (activityTagsProcessor.UserAccountId != null)
{
Tags[ContextTagKeys.AiUserAccountId.ToString()] = activityTagsProcessor.UserAccountId.Truncate(SchemaConstants.Tags_AiUserAccountId_MaxLength);
}
Comment thread
harsimar marked this conversation as resolved.
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ internal struct ActivityTagsProcessor
// Others
SemanticConventions.AttributeEnduserId,
SemanticConventions.AttributeEnduserPseudoId,
"microsoft.client.ip",
SemanticConventions.AttributeMicrosoftClientIp,

// Microsoft Application Insights Override Attributes
SemanticConventions.AttributeMicrosoftDependencyData,
Expand All @@ -71,7 +71,16 @@ internal struct ActivityTagsProcessor
SemanticConventions.AttributeMicrosoftRequestName,
SemanticConventions.AttributeMicrosoftRequestUrl,
SemanticConventions.AttributeMicrosoftRequestSource,
SemanticConventions.AttributeMicrosoftRequestResultCode
SemanticConventions.AttributeMicrosoftRequestResultCode,

// Context tag attributes from Application Insights shim
SemanticConventions.AttributeMicrosoftSessionId,
SemanticConventions.AttributeAiDeviceId,
SemanticConventions.AttributeAiDeviceModel,
SemanticConventions.AttributeAiDeviceType,
SemanticConventions.AttributeAiDeviceOsVersion,
SemanticConventions.AttributeMicrosoftSyntheticSource,
SemanticConventions.AttributeMicrosoftUserAccountId,
Comment thread
harsimar marked this conversation as resolved.
};

internal static readonly HashSet<string> s_semanticsSet = new(s_semantics);
Expand All @@ -87,6 +96,20 @@ internal struct ActivityTagsProcessor

public string? EndUserPseudoId { get; private set; } = null;

public string? SessionId { get; private set; } = null;

public string? DeviceId { get; private set; } = null;

public string? DeviceModel { get; private set; } = null;

public string? DeviceType { get; private set; } = null;

public string? DeviceOsVersion { get; private set; } = null;

public string? SyntheticSource { get; private set; } = null;

public string? UserAccountId { get; private set; } = null;

public bool HasOverrideAttributes { get; private set; } = false;

public ActivityTagsProcessor()
Expand Down Expand Up @@ -132,6 +155,27 @@ public void CategorizeTags(Activity activity)
case SemanticConventions.AttributeEnduserPseudoId:
EndUserPseudoId = tag.Value.ToString();
continue;
case SemanticConventions.AttributeMicrosoftSessionId:
Comment thread
harsimar marked this conversation as resolved.
SessionId = tag.Value.ToString();
continue;
case SemanticConventions.AttributeAiDeviceId:
DeviceId = tag.Value.ToString();
continue;
case SemanticConventions.AttributeAiDeviceModel:
DeviceModel = tag.Value.ToString();
continue;
case SemanticConventions.AttributeAiDeviceType:
DeviceType = tag.Value.ToString();
continue;
case SemanticConventions.AttributeAiDeviceOsVersion:
DeviceOsVersion = tag.Value.ToString();
continue;
case SemanticConventions.AttributeMicrosoftSyntheticSource:
SyntheticSource = tag.Value.ToString();
continue;
case SemanticConventions.AttributeMicrosoftUserAccountId:
UserAccountId = tag.Value.ToString();
continue;
case SemanticConventions.AttributeMicrosoftDependencyData:
case SemanticConventions.AttributeMicrosoftDependencyName:
case SemanticConventions.AttributeMicrosoftDependencyTarget:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ internal struct LogContextInfo
public string? EndUserId;
public string? UserAgent;
public string? OperationName;
public string? SessionId;
public string? DeviceId;
public string? DeviceModel;
public string? DeviceType;
public string? DeviceOsVersion;
public string? SyntheticSource;
public string? UserAccountId;
Comment thread
harsimar marked this conversation as resolved.

/// <summary>
/// Returns true if any context field has been set.
Expand All @@ -23,6 +30,13 @@ internal struct LogContextInfo
EndUserPseudoId != null ||
EndUserId != null ||
UserAgent != null ||
OperationName != null;
OperationName != null ||
SessionId != null ||
DeviceId != null ||
DeviceModel != null ||
DeviceType != null ||
DeviceOsVersion != null ||
SyntheticSource != null ||
UserAccountId != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ internal static class LogsHelper
private const string EndUserIdAttributeName = "enduser.id";
private const string UserAgentOriginalAttributeName = "user_agent.original";
private const string OperationNameAttributeName = "microsoft.operation_name";
private const string SessionIdAttributeName = "microsoft.session.id";
private const string DeviceIdAttributeName = "ai.device.id";
private const string DeviceModelAttributeName = "ai.device.model";
private const string DeviceTypeAttributeName = "ai.device.type";
private const string DeviceOsVersionAttributeName = "ai.device.osVersion";
Comment thread
harsimar marked this conversation as resolved.
private const string SyntheticSourceAttributeName = "microsoft.synthetic_source";
private const string UserAccountIdAttributeName = "microsoft.user.account_id";
private const string AvailabilityIdAttributeName = "microsoft.availability.id";
private const string AvailabilityNameAttributeName = "microsoft.availability.name";
private const string AvailabilityDurationAttributeName = "microsoft.availability.duration";
Expand Down Expand Up @@ -169,6 +176,27 @@ internal static void ProcessLogRecordProperties(LogRecord logRecord, IDictionary
case OperationNameAttributeName:
logContext.OperationName = item.Value?.ToString();
break;
case SessionIdAttributeName:
logContext.SessionId = item.Value?.ToString();
break;
case DeviceIdAttributeName:
logContext.DeviceId = item.Value?.ToString();
break;
case DeviceModelAttributeName:
logContext.DeviceModel = item.Value?.ToString();
break;
case DeviceTypeAttributeName:
logContext.DeviceType = item.Value?.ToString();
break;
case DeviceOsVersionAttributeName:
logContext.DeviceOsVersion = item.Value?.ToString();
break;
case SyntheticSourceAttributeName:
logContext.SyntheticSource = item.Value?.ToString();
break;
case UserAccountIdAttributeName:
logContext.UserAccountId = item.Value?.ToString();
break;
default:
// Note: if Key exceeds MaxLength, the entire KVP will be dropped.
if (item.Key.Length <= SchemaConstants.MessageData_Properties_MaxKeyLength && item.Value != null)
Expand Down Expand Up @@ -287,6 +315,27 @@ internal static void ProcessLogRecordProperties(LogRecord logRecord, IDictionary
case OperationNameAttributeName:
logContext.OperationName = item.Value?.ToString();
break;
case SessionIdAttributeName:
logContext.SessionId = item.Value?.ToString();
break;
case DeviceIdAttributeName:
logContext.DeviceId = item.Value?.ToString();
break;
case DeviceModelAttributeName:
logContext.DeviceModel = item.Value?.ToString();
break;
case DeviceTypeAttributeName:
logContext.DeviceType = item.Value?.ToString();
break;
case DeviceOsVersionAttributeName:
logContext.DeviceOsVersion = item.Value?.ToString();
break;
case SyntheticSourceAttributeName:
logContext.SyntheticSource = item.Value?.ToString();
break;
case UserAccountIdAttributeName:
logContext.UserAccountId = item.Value?.ToString();
break;
default:
if (item.Key.Length <= SchemaConstants.AvailabilityData_Properties_MaxValueLength && item.Value != null && !properties.ContainsKey(item.Key))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ internal static (List<TelemetryItem> TelemetryItems, TelemetrySchemaTypeCounter
{
try
{
telemetryItems.Add(new TelemetryItem(metricPoint.EndTime.UtcDateTime, resource, instrumentationKey)
var telemetryItem = new TelemetryItem(metricPoint.EndTime.UtcDateTime, resource, instrumentationKey)
{
Data = new MonitorBase
{
BaseType = "MetricData",
BaseData = new MetricsData(Version, metric, metricPoint)
}
});
};
telemetryItems.Add(telemetryItem);
Comment thread
harsimar marked this conversation as resolved.
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ internal static class EnvironmentVariableConstants
APPLICATIONINSIGHTS_SDKSTATS_EXPORT_INTERVAL,
APPLICATIONINSIGHTS_CLOUD_ROLE_NAME,
APPLICATIONINSIGHTS_CLOUD_ROLE_INSTANCE,
APPLICATIONINSIGHTS_COMPONENT_VERSION,
FUNCTIONS_WORKER_RUNTIME,
LOCALAPPDATA,
TEMP,
Expand Down Expand Up @@ -154,5 +155,11 @@ internal static class EnvironmentVariableConstants
/// the cloud role instance after the OTel Resource has been built and is immutable.
/// </summary>
public const string APPLICATIONINSIGHTS_CLOUD_ROLE_INSTANCE = "APPLICATIONINSIGHTS_CLOUD_ROLE_INSTANCE";

/// <summary>
/// Set by the Application Insights shim (TelemetryClient.Context.Component.Version) to override
/// the application version after the OTel Resource has been built and is immutable.
/// </summary>
public const string APPLICATIONINSIGHTS_COMPONENT_VERSION = "APPLICATIONINSIGHTS_COMPONENT_VERSION";
}
}
Loading
Loading