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

Fix #2221 TagObjects not guaranteed to be unique #2241

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Changes from all 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
35 changes: 26 additions & 9 deletions src/Elastic.Apm/OpenTelemetry/ElasticActivityListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@ namespace Elastic.Apm.OpenTelemetry
public class ElasticActivityListener : IDisposable
{
private static readonly string[] ServerPortAttributeKeys = new[] { SemanticConventions.ServerPort, SemanticConventions.NetPeerPort };
private static readonly string[] ServerAddressAttributeKeys = new[] { SemanticConventions.ServerAddress, SemanticConventions.NetPeerName, SemanticConventions.NetPeerIp };
private static readonly string[] HttpAttributeKeys = new[] { SemanticConventions.UrlFull, SemanticConventions.HttpUrl, SemanticConventions.HttpScheme };

private static readonly string[] ServerAddressAttributeKeys =
new[] { SemanticConventions.ServerAddress, SemanticConventions.NetPeerName, SemanticConventions.NetPeerIp };

private static readonly string[] HttpAttributeKeys =
new[] { SemanticConventions.UrlFull, SemanticConventions.HttpUrl, SemanticConventions.HttpScheme };

private static readonly string[] HttpUrlAttributeKeys = new[] { SemanticConventions.UrlFull, SemanticConventions.HttpUrl };

private readonly ConditionalWeakTable<Activity, Span> _activeSpans = new();
Expand Down Expand Up @@ -153,8 +158,7 @@ private void CreateSpanForActivity(Activity activity, long timestamp, List<SpanL
_activeTransactions.Remove(activity);
transaction.Duration = activity.Duration.TotalMilliseconds;

if (activity.TagObjects.Any())
transaction.Otel.Attributes = new Dictionary<string, object>(activity.TagObjects);
UpdateOTelAttributes(activity, transaction.Otel);

InferTransactionType(transaction, activity);

Expand Down Expand Up @@ -184,12 +188,20 @@ private void CreateSpanForActivity(Activity activity, long timestamp, List<SpanL
}
};

private static void UpdateOTelAttributes(Activity activity, OTel otel)
{
if (!activity.TagObjects.Any()) return;

otel.Attributes ??= new Dictionary<string, object>();
foreach (var (key, value) in activity.TagObjects)
otel.Attributes[key] = value;
}

private static void UpdateSpan(Activity activity, Span span)
{
span.Duration = activity.Duration.TotalMilliseconds;

if (activity.TagObjects.Any())
span.Otel.Attributes = new Dictionary<string, object>(activity.TagObjects);
UpdateOTelAttributes(activity, span.Otel);

InferSpanTypeAndSubType(span, activity);

Expand Down Expand Up @@ -291,7 +303,9 @@ static string ToResourceName(string type, string name)
span.Type = ApiConstants.TypeMessaging;
span.Subtype = messagingSystem;
serviceTargetType = span.Subtype;
serviceTargetName = TryGetStringValue(activity, SemanticConventions.MessagingDestination, out var messagingDestination) ? messagingDestination : null;
serviceTargetName = TryGetStringValue(activity, SemanticConventions.MessagingDestination, out var messagingDestination)
? messagingDestination
: null;
resource = ToResourceName(span.Subtype, serviceTargetName);
}
else if (TryGetStringValue(activity, SemanticConventions.RpcSystem, out var rpcSystem))
Expand All @@ -301,10 +315,13 @@ static string ToResourceName(string type, string name)
serviceTargetType = span.Subtype;
serviceTargetName = !string.IsNullOrEmpty(netName)
? netName
: TryGetStringValue(activity, SemanticConventions.RpcService, out var rpcService) ? rpcService : null;
: TryGetStringValue(activity, SemanticConventions.RpcService, out var rpcService)
? rpcService
: null;
resource = serviceTargetName ?? span.Subtype;
}
else if (activity.TagObjects.Any(n => n.Key == SemanticConventions.HttpUrl || n.Key == SemanticConventions.UrlFull || n.Key == SemanticConventions.HttpScheme))
else if (activity.TagObjects.Any(n =>
n.Key == SemanticConventions.HttpUrl || n.Key == SemanticConventions.UrlFull || n.Key == SemanticConventions.HttpScheme))
{
var hasHttpHost = TryGetStringValue(activity, SemanticConventions.HttpHost, out var httpHost);
var hasHttpScheme = TryGetStringValue(activity, SemanticConventions.HttpScheme, out var httpScheme);
Expand Down
Loading