Skip to content

Commit

Permalink
[AzureMonitorExporter] Merge existing and new Request/Dependency Tele…
Browse files Browse the repository at this point in the history
…metry. (#37534)

* Sematics Refactor - Request/Dependency Telemetry.

* Fix nits

* Remove DefaultSchemaVersion

* Remove unrelated changes.

* Remove unrelated changes

* PR feedback.

* resultcode fix

* Rename SetMessagingRequestProperties
  • Loading branch information
rajkumar-rangaraj committed Jul 11, 2023
1 parent 3ab9a7a commit 7c27998
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 193 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
Expand All @@ -17,66 +18,41 @@ internal partial class RemoteDependencyData

public RemoteDependencyData(int version, Activity activity, ref ActivityTagsProcessor activityTagsProcessor) : base(version)
{
string? dependencyName = null;
bool isNewSchemaVersion = false;
Properties = new ChangeTrackingDictionary<string, string>();
Measurements = new ChangeTrackingDictionary<string, double>();

string? httpUrl = null;
string dependencyName;

if (activityTagsProcessor.activityType == OperationType.Http)
if (activityTagsProcessor.activityType.HasFlag(OperationType.V2))
{
httpUrl = activityTagsProcessor.MappedTags.GetDependencyUrl();
dependencyName = activityTagsProcessor.MappedTags.GetHttpDependencyName(httpUrl) ?? activity.DisplayName;
isNewSchemaVersion = true;
activityTagsProcessor.activityType &= ~OperationType.V2;
}
else
{
dependencyName = activity.DisplayName;
}

Name = dependencyName.Truncate(SchemaConstants.RemoteDependencyData_Name_MaxLength);
Id = activity.Context.SpanId.ToHexString();
Duration = activity.Duration < SchemaConstants.RemoteDependencyData_Duration_LessThanDays
? activity.Duration.ToString("c", CultureInfo.InvariantCulture)
: SchemaConstants.Duration_MaxValue;
Success = activity.Status != ActivityStatusCode.Error;

switch (activityTagsProcessor.activityType)
{
case OperationType.Http:
Data = httpUrl.Truncate(SchemaConstants.RemoteDependencyData_Data_MaxLength);
Target = activityTagsProcessor.MappedTags.GetHttpDependencyTarget().Truncate(SchemaConstants.RemoteDependencyData_Target_MaxLength);
Type = "Http";
ResultCode = AzMonList.GetTagValue(ref activityTagsProcessor.MappedTags, SemanticConventions.AttributeHttpStatusCode)
?.ToString().Truncate(SchemaConstants.RemoteDependencyData_ResultCode_MaxLength)
?? "0";
SetHttpDependencyPropertiesAndDependencyName(activity, ref activityTagsProcessor.MappedTags, isNewSchemaVersion, out dependencyName);
break;
case OperationType.Db:
var depDataAndType = AzMonList.GetTagValues(ref activityTagsProcessor.MappedTags, SemanticConventions.AttributeDbStatement, SemanticConventions.AttributeDbSystem);
Data = depDataAndType[0]?.ToString().Truncate(SchemaConstants.RemoteDependencyData_Data_MaxLength);
var dbNameAndTarget = activityTagsProcessor.MappedTags.GetDbDependencyTargetAndName();
Target = dbNameAndTarget.DbTarget.Truncate(SchemaConstants.RemoteDependencyData_Target_MaxLength);

// special case for db.name
var sanitizedDbName = dbNameAndTarget.DbName.Truncate(SchemaConstants.KVP_MaxValueLength);
if (sanitizedDbName != null)
{
Properties.Add(SemanticConventions.AttributeDbName, sanitizedDbName);
}
Type = s_sqlDbs.Contains(depDataAndType[1]?.ToString()) ? "SQL" : depDataAndType[1]?.ToString().Truncate(SchemaConstants.RemoteDependencyData_Type_MaxLength);
SetDbDependencyProperties(ref activityTagsProcessor.MappedTags);
break;
case OperationType.Rpc:
var depInfo = AzMonList.GetTagValues(ref activityTagsProcessor.MappedTags, SemanticConventions.AttributeRpcService, SemanticConventions.AttributeRpcSystem, SemanticConventions.AttributeRpcStatus);
Data = depInfo[0]?.ToString().Truncate(SchemaConstants.RemoteDependencyData_Data_MaxLength);
Type = depInfo[1]?.ToString().Truncate(SchemaConstants.RemoteDependencyData_Type_MaxLength);
ResultCode = depInfo[2]?.ToString().Truncate(SchemaConstants.RemoteDependencyData_ResultCode_MaxLength);
SetRpcDependencyProperties(ref activityTagsProcessor.MappedTags);
break;
case OperationType.Messaging:
depDataAndType = AzMonList.GetTagValues(ref activityTagsProcessor.MappedTags, SemanticConventions.AttributeMessagingUrl, SemanticConventions.AttributeMessagingSystem);
Data = depDataAndType[0]?.ToString().Truncate(SchemaConstants.RemoteDependencyData_Data_MaxLength);
Type = depDataAndType[1]?.ToString().Truncate(SchemaConstants.RemoteDependencyData_Type_MaxLength);
SetMessagingDependencyProperties(activity, ref activityTagsProcessor.MappedTags);
break;
}

dependencyName ??= activity.DisplayName;
Name = dependencyName.Truncate(SchemaConstants.RemoteDependencyData_Name_MaxLength);
Id = activity.Context.SpanId.ToHexString();
Duration = activity.Duration < SchemaConstants.RemoteDependencyData_Duration_LessThanDays
? activity.Duration.ToString("c", CultureInfo.InvariantCulture)
: SchemaConstants.Duration_MaxValue;
Success = activity.Status != ActivityStatusCode.Error;

if (activityTagsProcessor.AzureNamespace != null)
{
if (activity.Kind == ActivityKind.Internal)
Expand All @@ -102,5 +78,64 @@ public RemoteDependencyData(int version, Activity activity, ref ActivityTagsProc
TraceHelper.AddActivityLinksToProperties(activity, ref activityTagsProcessor.UnMappedTags);
TraceHelper.AddPropertiesToTelemetry(Properties, ref activityTagsProcessor.UnMappedTags);
}

private void SetHttpDependencyPropertiesAndDependencyName(Activity activity, ref AzMonList httpTagObjects, bool isNewSchemaVersion, out string dependencyName)
{
string? httpUrl;
string? resultCode;
string? target;

if (isNewSchemaVersion)
{
httpUrl = AzMonList.GetTagValue(ref httpTagObjects, SemanticConventions.AttributeUrlFull)?.ToString();
dependencyName = httpTagObjects.GetNewSchemaHttpDependencyName(httpUrl) ?? activity.DisplayName;
target = httpTagObjects.GetNewSchemaHttpDependencyTarget();
resultCode = AzMonList.GetTagValue(ref httpTagObjects, SemanticConventions.AttributeHttpResponseStatusCode)?.ToString();
}
else
{
httpUrl = httpTagObjects.GetDependencyUrl();
dependencyName = httpTagObjects.GetHttpDependencyName(httpUrl) ?? activity.DisplayName;
target = httpTagObjects.GetHttpDependencyTarget();
resultCode = AzMonList.GetTagValue(ref httpTagObjects, SemanticConventions.AttributeHttpStatusCode)?.ToString();
}

Type = "Http";
Data = httpUrl.Truncate(SchemaConstants.RemoteDependencyData_Data_MaxLength);
Target = target.Truncate(SchemaConstants.RemoteDependencyData_Target_MaxLength);
ResultCode = resultCode?.Truncate(SchemaConstants.RemoteDependencyData_ResultCode_MaxLength) ?? "0";
}

private void SetDbDependencyProperties(ref AzMonList dbTagObjects)
{
var dbAttributeTagObjects = AzMonList.GetTagValues(ref dbTagObjects, SemanticConventions.AttributeDbStatement, SemanticConventions.AttributeDbSystem);
Data = dbAttributeTagObjects[0]?.ToString().Truncate(SchemaConstants.RemoteDependencyData_Data_MaxLength);
var (DbName, DbTarget) = dbTagObjects.GetDbDependencyTargetAndName();
Target = DbTarget.Truncate(SchemaConstants.RemoteDependencyData_Target_MaxLength);
Type = s_sqlDbs.Contains(dbAttributeTagObjects[1]?.ToString()) ? "SQL" : dbAttributeTagObjects[1]?.ToString().Truncate(SchemaConstants.RemoteDependencyData_Type_MaxLength);

// special case for db.name
var sanitizedDbName = DbName.Truncate(SchemaConstants.KVP_MaxValueLength);
if (sanitizedDbName != null)
{
Properties.Add(SemanticConventions.AttributeDbName, sanitizedDbName);
}
}

private void SetRpcDependencyProperties(ref AzMonList rpcTagObjects)
{
var rpcAttributeTagObjects = AzMonList.GetTagValues(ref rpcTagObjects, SemanticConventions.AttributeRpcService, SemanticConventions.AttributeRpcSystem, SemanticConventions.AttributeRpcStatus);
Data = rpcAttributeTagObjects[0]?.ToString().Truncate(SchemaConstants.RemoteDependencyData_Data_MaxLength);
Type = rpcAttributeTagObjects[1]?.ToString().Truncate(SchemaConstants.RemoteDependencyData_Type_MaxLength);
ResultCode = rpcAttributeTagObjects[2]?.ToString().Truncate(SchemaConstants.RemoteDependencyData_ResultCode_MaxLength);
}

private void SetMessagingDependencyProperties(Activity activity, ref AzMonList messagingTagObjects)
{
var messagingAttributeTagObjects = AzMonList.GetTagValues(ref messagingTagObjects, SemanticConventions.AttributeMessagingUrl, SemanticConventions.AttributeMessagingSystem);
var messagingUrl = messagingAttributeTagObjects[0]?.ToString();
Data = messagingUrl?.Truncate(SchemaConstants.RemoteDependencyData_Data_MaxLength);
Type = messagingAttributeTagObjects[1]?.ToString().Truncate(SchemaConstants.RemoteDependencyData_Type_MaxLength);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,32 @@ internal partial class RequestData
{
public RequestData(int version, Activity activity, ref ActivityTagsProcessor activityTagsProcessor) : base(version)
{
string? url = null;
string? responseCode = null;
Properties = new ChangeTrackingDictionary<string, string>();
Measurements = new ChangeTrackingDictionary<string, double>();

switch (activityTagsProcessor.activityType)
{
case OperationType.Http | OperationType.V2:
SetHttpV2RequestPropertiesAndResponseCode(activity, ref activityTagsProcessor.MappedTags, out responseCode);
break;
case OperationType.Http:
url = activityTagsProcessor.MappedTags.GetRequestUrl();
SetHttpRequestPropertiesAndResponseCode(activity, ref activityTagsProcessor.MappedTags, out responseCode);
break;
case OperationType.Messaging:
url = AzMonList.GetTagValue(ref activityTagsProcessor.MappedTags, SemanticConventions.AttributeMessagingUrl)?.ToString();
SetMessagingRequestProperties(activity, ref activityTagsProcessor.MappedTags);
break;
}

Id = activity.Context.SpanId.ToHexString();
Name = TraceHelper.GetOperationName(activity, ref activityTagsProcessor.MappedTags).Truncate(SchemaConstants.RequestData_Name_MaxLength);
Name = Name ?? activity.DisplayName;
Duration = activity.Duration < SchemaConstants.RequestData_Duration_LessThanDays
? activity.Duration.ToString("c", CultureInfo.InvariantCulture)
: SchemaConstants.Duration_MaxValue;
ResponseCode = AzMonList.GetTagValue(ref activityTagsProcessor.MappedTags, SemanticConventions.AttributeHttpStatusCode)
?.ToString().Truncate(SchemaConstants.RequestData_ResponseCode_MaxLength)
?? "0";
ResponseCode = responseCode ?? "0";

Success = IsSuccess(activity, ResponseCode, activityTagsProcessor.activityType);

Url = url.Truncate(SchemaConstants.RequestData_Url_MaxLength);
Properties = new ChangeTrackingDictionary<string, string>();
Measurements = new ChangeTrackingDictionary<string, double>();

if (activity.Kind == ActivityKind.Consumer)
{
TraceHelper.AddEnqueuedTimeToMeasurementsAndLinksToProperties(activity, Measurements, ref activityTagsProcessor.UnMappedTags);
Expand All @@ -65,5 +64,29 @@ internal static bool IsSuccess(Activity activity, string? responseCode, Operatio
return activity.Status != ActivityStatusCode.Error;
}
}

private void SetHttpRequestPropertiesAndResponseCode(Activity activity, ref AzMonList httpTagObjects, out string responseCode)
{
Url = httpTagObjects.GetRequestUrl().Truncate(SchemaConstants.RequestData_Url_MaxLength);
Name = TraceHelper.GetOperationName(activity, ref httpTagObjects).Truncate(SchemaConstants.RequestData_Name_MaxLength);
responseCode = AzMonList.GetTagValue(ref httpTagObjects, SemanticConventions.AttributeHttpStatusCode)
?.ToString().Truncate(SchemaConstants.RequestData_ResponseCode_MaxLength)
?? "0";
}

private void SetHttpV2RequestPropertiesAndResponseCode(Activity activity, ref AzMonList httpTagObjects, out string responseCode)
{
Url = httpTagObjects.GetNewSchemaRequestUrl().Truncate(SchemaConstants.RequestData_Url_MaxLength);
Name = TraceHelper.GetNewSchemaOperationName(activity, Url, ref httpTagObjects).Truncate(SchemaConstants.RequestData_Name_MaxLength);
responseCode = AzMonList.GetTagValue(ref httpTagObjects, SemanticConventions.AttributeHttpResponseStatusCode)
?.ToString().Truncate(SchemaConstants.RequestData_ResponseCode_MaxLength)
?? "0";
}

private void SetMessagingRequestProperties(Activity activity, ref AzMonList messagingTagObjects)
{
Url = AzMonList.GetTagValue(ref messagingTagObjects, SemanticConventions.AttributeMessagingUrl)?.ToString().Truncate(SchemaConstants.RequestData_Url_MaxLength);
Name = activity.DisplayName;
}
}
}

0 comments on commit 7c27998

Please sign in to comment.