Skip to content

Commit

Permalink
Set operation name for consumer spans (#37696)
Browse files Browse the repository at this point in the history
* Set operation name for consumer spans

* refactor

* fix test
  • Loading branch information
vishweshbankwar authored Jul 18, 2023
1 parent 13a80e0 commit 483718b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ private void SetMessagingRequestProperties(Activity activity, ref AzMonList mess
var (messagingUrl, source) = messagingTagObjects.GetMessagingUrlAndSourceOrTarget(activity.Kind);
Url = messagingUrl?.Truncate(SchemaConstants.RequestData_Url_MaxLength);
Source = source?.Truncate(SchemaConstants.RequestData_Source_MaxLength);
Name = activity.DisplayName;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,30 @@ public TelemetryItem(Activity activity, ref ActivityTagsProcessor activityTagsPr

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

if (activity.Kind == ActivityKind.Server && activityTagsProcessor.activityType.HasFlag(OperationType.V2))
if (activity.GetTelemetryType() == TelemetryType.Request)
{
Tags[ContextTagKeys.AiOperationName.ToString()] = TraceHelper.GetOperationNameV2(activity, ref activityTagsProcessor.MappedTags);
Tags[ContextTagKeys.AiLocationIp.ToString()] = TraceHelper.GetLocationIp(ref activityTagsProcessor.MappedTags);
}
else if (activity.Kind == ActivityKind.Server && activityTagsProcessor.activityType.HasFlag(OperationType.Http))
{
Tags[ContextTagKeys.AiOperationName.ToString()] = TraceHelper.GetOperationName(activity, ref activityTagsProcessor.MappedTags);
Tags[ContextTagKeys.AiLocationIp.ToString()] = TraceHelper.GetLocationIp(ref activityTagsProcessor.MappedTags);
if (activityTagsProcessor.activityType.HasFlag(OperationType.V2))
{
Tags[ContextTagKeys.AiOperationName.ToString()] = TraceHelper.GetOperationNameV2(activity, ref activityTagsProcessor.MappedTags);
}
else if (activityTagsProcessor.activityType.HasFlag(OperationType.Http))
{
Tags[ContextTagKeys.AiOperationName.ToString()] = TraceHelper.GetOperationName(activity, ref activityTagsProcessor.MappedTags);
}
else
{
Tags[ContextTagKeys.AiOperationName.ToString()] = activity.DisplayName;
}

// Set ip in case of server spans only.
if (activity.Kind == ActivityKind.Server)
{
var locationIp = TraceHelper.GetLocationIp(ref activityTagsProcessor.MappedTags);
if (locationIp != null)
{
Tags[ContextTagKeys.AiLocationIp.ToString()] = locationIp;
}
}
}

var userAgent = AzMonList.GetTagValue(ref activityTagsProcessor.MappedTags, SemanticConventions.AttributeUserAgentOriginal)?.ToString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public static void AssertActivity_As_RequestTelemetry(
Assert.Equal(2, telemetryItem.Data.BaseData.Version); // telemetry api version
Assert.Equal("00000000-0000-0000-0000-000000000000", telemetryItem.InstrumentationKey);

var expectedTagsCount = 5;
var expectedTagsCount = 6;

Assert.Equal(expectedTagsCount, telemetryItem.Tags.Count);
Assert.Equal(expectedTraceId, telemetryItem.Tags["ai.operation.id"]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ public void ValidateMessagingRequestData()

var requestData = new RequestData(2, activity, ref activityTagsProcessor);

Assert.Equal("RequestDataNewActivity", requestData.Name);
// Name is set later via operation name on TelemetryItem
Assert.Null(requestData.Name);
Assert.Equal(activity.Context.SpanId.ToHexString(), requestData.Id);
Assert.Equal("my.servicebus.windows.net/queueName", requestData.Url);
Assert.Equal("0", requestData.ResponseCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ public void AiUserAgentIsSetAsUserAgentOriginal()
}

[Fact]
public void AiLocationIpIsNullByDefault()
public void AiLocationIpIsNotSetByDefault()
{
using ActivitySource activitySource = new ActivitySource(ActivitySourceName);
using var activity = activitySource.StartActivity(
Expand All @@ -292,7 +292,7 @@ public void AiLocationIpIsNullByDefault()
var telemetryItems = TraceHelper.OtelToAzureMonitorTrace(new Batch<Activity>(new Activity[] { activity }, 1), null, "instrumentationKey");
var telemetryItem = telemetryItems.FirstOrDefault();

Assert.Null(telemetryItem?.Tags[ContextTagKeys.AiLocationIp.ToString()]);
Assert.False(telemetryItem?.Tags.TryGetValue(ContextTagKeys.AiLocationIp.ToString(),out _));
}

[Fact]
Expand Down Expand Up @@ -404,6 +404,31 @@ public void RequestNameMatchesOperationNameV2()
Assert.Equal(requestData.Name, telemetryItem?.Tags[ContextTagKeys.AiOperationName.ToString()]);
}

[Fact]
public void RequestNameMatchesOperationNameForConsumerSpans()
{
using ActivitySource activitySource = new ActivitySource(ActivitySourceName);
using var activity = activitySource.StartActivity(
ActivityName,
ActivityKind.Consumer,
null,
startTime: DateTime.UtcNow);

Assert.NotNull(activity);
activity.DisplayName = "displayname";

activity.SetTag(SemanticConventions.AttributeMessagingSystem, "Eventhub");

var activityTagsProcessor = TraceHelper.EnumerateActivityTags(activity);
var telemetryItems = TraceHelper.OtelToAzureMonitorTrace(new Batch<Activity>(new Activity[] { activity }, 1), null, "instrumentationKey");
var telemetryItem = telemetryItems.FirstOrDefault();
var requestData = telemetryItem?.Data.BaseData as RequestData;

Assert.NotNull(requestData);
Assert.Equal("displayname", requestData.Name);
Assert.Equal(requestData.Name, telemetryItem?.Tags[ContextTagKeys.AiOperationName.ToString()]);
}

/// <summary>
/// If SERVICE.NAME is not defined, it will fall-back to "unknown_service".
/// (https://github.com/open-telemetry/opentelemetry-specification/tree/main/specification/resource/semantic_conventions#semantic-attributes-with-sdk-provided-default-value).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ internal void AssertRequestTelemetry(TelemetryItem telemetryItem, string expecte
// Tags
Assert.Contains("ai.operation.id", telemetryItem.Tags.Keys);
Assert.Contains("ai.operation.name", telemetryItem.Tags.Keys);
Assert.Contains("ai.location.ip", telemetryItem.Tags.Keys);
Assert.Contains("ai.cloud.role", telemetryItem.Tags.Keys);
Assert.Contains("ai.cloud.roleInstance", telemetryItem.Tags.Keys);
Assert.Contains("ai.internal.sdkVersion", telemetryItem.Tags.Keys);
Expand Down

0 comments on commit 483718b

Please sign in to comment.