Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AzureMonitorExporter] Add Truncation to ContextTags #37843

Merged
merged 4 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -18,24 +18,24 @@ public TelemetryItem(Activity activity, ref ActivityTagsProcessor activityTagsPr
{
if (activity.ParentSpanId != default)
{
Tags[ContextTagKeys.AiOperationParentId.ToString()] = activity.ParentSpanId.ToHexString();
Tags[ContextTagKeys.AiOperationParentId.ToString()] = activity.ParentSpanId.ToHexString().Truncate(SchemaConstants.Tags_AiOperationParentId_MaxLength);
rajkumar-rangaraj marked this conversation as resolved.
Show resolved Hide resolved
}

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

if (activity.GetTelemetryType() == TelemetryType.Request)
{
if (activityTagsProcessor.activityType.HasFlag(OperationType.V2))
{
Tags[ContextTagKeys.AiOperationName.ToString()] = TraceHelper.GetOperationNameV2(activity, ref activityTagsProcessor.MappedTags);
Tags[ContextTagKeys.AiOperationName.ToString()] = TraceHelper.GetOperationNameV2(activity, ref activityTagsProcessor.MappedTags).Truncate(SchemaConstants.Tags_AiOperationName_MaxLength);
}
else if (activityTagsProcessor.activityType.HasFlag(OperationType.Http))
{
Tags[ContextTagKeys.AiOperationName.ToString()] = TraceHelper.GetOperationName(activity, ref activityTagsProcessor.MappedTags);
Tags[ContextTagKeys.AiOperationName.ToString()] = TraceHelper.GetOperationName(activity, ref activityTagsProcessor.MappedTags).Truncate(SchemaConstants.Tags_AiOperationName_MaxLength);
}
else
{
Tags[ContextTagKeys.AiOperationName.ToString()] = activity.DisplayName;
Tags[ContextTagKeys.AiOperationName.ToString()] = activity.DisplayName.Truncate(SchemaConstants.Tags_AiOperationName_MaxLength);
}

// Set ip in case of server spans only.
Expand Down Expand Up @@ -70,17 +70,17 @@ public TelemetryItem(Activity activity, ref ActivityTagsProcessor activityTagsPr
public TelemetryItem(string name, TelemetryItem telemetryItem, ActivitySpanId activitySpanId, ActivityKind kind, DateTimeOffset activityEventTimeStamp) :
this(name, FormatUtcTimestamp(activityEventTimeStamp.DateTime))
{
Tags[ContextTagKeys.AiOperationParentId.ToString()] = activitySpanId.ToHexString();
Tags[ContextTagKeys.AiOperationId.ToString()] = telemetryItem.Tags[ContextTagKeys.AiOperationId.ToString()];
Tags[ContextTagKeys.AiOperationParentId.ToString()] = activitySpanId.ToHexString().Truncate(SchemaConstants.Tags_AiOperationParentId_MaxLength);
Tags[ContextTagKeys.AiOperationId.ToString()] = telemetryItem.Tags[ContextTagKeys.AiOperationId.ToString()].Truncate(SchemaConstants.Tags_AiOperationId_MaxLength);

if (telemetryItem.Tags.TryGetValue("ai.user.userAgent", out string? userAgent))
{
// todo: update swagger to include this key.
Tags["ai.user.userAgent"] = userAgent;
}

Tags[ContextTagKeys.AiCloudRole.ToString()] = telemetryItem.Tags[ContextTagKeys.AiCloudRole.ToString()];
Tags[ContextTagKeys.AiCloudRoleInstance.ToString()] = telemetryItem.Tags[ContextTagKeys.AiCloudRoleInstance.ToString()];
Tags[ContextTagKeys.AiCloudRole.ToString()] = telemetryItem.Tags[ContextTagKeys.AiCloudRole.ToString()].Truncate(SchemaConstants.Tags_AiCloudRole_MaxLength);
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);
InstrumentationKey = telemetryItem.InstrumentationKey;

Expand All @@ -95,12 +95,12 @@ public TelemetryItem (LogRecord logRecord, AzureMonitorResource? resource, strin
{
if (logRecord.TraceId != default)
{
Tags[ContextTagKeys.AiOperationId.ToString()] = logRecord.TraceId.ToHexString();
Tags[ContextTagKeys.AiOperationId.ToString()] = logRecord.TraceId.ToHexString().Truncate(SchemaConstants.Tags_AiOperationId_MaxLength);
}

if (logRecord.SpanId != default)
{
Tags[ContextTagKeys.AiOperationParentId.ToString()] = logRecord.SpanId.ToHexString();
Tags[ContextTagKeys.AiOperationParentId.ToString()] = logRecord.SpanId.ToHexString().Truncate(SchemaConstants.Tags_AiOperationParentId_MaxLength);
}

InstrumentationKey = instrumentationKey;
Expand All @@ -116,8 +116,8 @@ public TelemetryItem(DateTime time, AzureMonitorResource? resource, string instr
private void SetResourceSdkVersionAndIkey(AzureMonitorResource? resource, string instrumentationKey)
{
InstrumentationKey = instrumentationKey;
Tags[ContextTagKeys.AiCloudRole.ToString()] = resource?.RoleName;
Tags[ContextTagKeys.AiCloudRoleInstance.ToString()] = resource?.RoleInstance;
Tags[ContextTagKeys.AiCloudRole.ToString()] = resource?.RoleName.Truncate(SchemaConstants.Tags_AiCloudRole_MaxLength);
Tags[ContextTagKeys.AiCloudRoleInstance.ToString()] = resource?.RoleInstance.Truncate(SchemaConstants.Tags_AiCloudRoleInstance_MaxLength);
Tags[ContextTagKeys.AiInternalSdkVersion.ToString()] = SdkVersionUtils.s_sdkVersion.Truncate(SchemaConstants.Tags_AiInternalSdkVersion_MaxLength);
}

Expand All @@ -131,7 +131,7 @@ private void SetAuthenticatedUserId(ref ActivityTagsProcessor activityTagsProces
{
if (activityTagsProcessor.EndUserId != null)
{
Tags[ContextTagKeys.AiUserAuthUserId.ToString()] = activityTagsProcessor.EndUserId;
Tags[ContextTagKeys.AiUserAuthUserId.ToString()] = activityTagsProcessor.EndUserId.Truncate(SchemaConstants.Tags_AiUserAuthUserId_MaxLength);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ internal static class SchemaConstants
public const int TelemetryEnvelope_Time_MaxLength = 64;
public const int TelemetryEnvelope_InstrumentationKey_MaxLength = 40;

public const int Tags_AiOperationId_MaxLength = 128;
public const int Tags_AiOperationName_MaxLength = 1024;
public const int Tags_AiOperationParentId_MaxLength = 512;
public const int Tags_AiUserAuthUserId_MaxLength = 1024;
public const int Tags_AiCloudRole_MaxLength = 256;
public const int Tags_AiCloudRoleInstance_MaxLength = 256;
public const int Tags_AiInternalSdkVersion_MaxLength = 64;
}
}