diff --git a/tracer/src/Datadog.Trace/Debugger/SpanCodeOrigin/SpanCodeOrigin.cs b/tracer/src/Datadog.Trace/Debugger/SpanCodeOrigin/SpanCodeOrigin.cs index 6dbdfca75dca..b622cddaceda 100644 --- a/tracer/src/Datadog.Trace/Debugger/SpanCodeOrigin/SpanCodeOrigin.cs +++ b/tracer/src/Datadog.Trace/Debugger/SpanCodeOrigin/SpanCodeOrigin.cs @@ -36,21 +36,21 @@ internal SpanCodeOrigin(DebuggerSettings settings) internal void SetCodeOriginForExitSpan(Span? span) { - if (ShouldSkipExitSpan()) + if (span?.Tags is WebTags { SpanKind: SpanKinds.Server }) { + // entry span + Log.Debug("SetCodeOriginForExitSpan: Skipping server entry span {SpanID}. Code origin will be added later. Service {ServiceName}, Resource: {ResourceName}, Operation: {OperationName}", span.SpanId, span.ServiceName, span.ResourceName, span.OperationName); return; } - if (span == null) + if (ShouldSkipExitSpan()) { - Log.Debug("Can not add code origin for exit span when span is null"); return; } - if (span.Tags is WebTags { SpanKind: SpanKinds.Server }) + if (span == null) { - // entry span - Log.Debug("Skipping span {SpanID}, we will add entry span code origin for it later. Resource: {ResourceName}, Operation: {OperationName}", span.SpanId, span.ResourceName, span.OperationName); + Log.Debug("Can not add code origin for exit span when span is null"); return; } diff --git a/tracer/src/Datadog.Trace/DiagnosticListeners/AspNetCoreDiagnosticObserver.cs b/tracer/src/Datadog.Trace/DiagnosticListeners/AspNetCoreDiagnosticObserver.cs index 0a8b582d4f31..06a1b9d66339 100644 --- a/tracer/src/Datadog.Trace/DiagnosticListeners/AspNetCoreDiagnosticObserver.cs +++ b/tracer/src/Datadog.Trace/DiagnosticListeners/AspNetCoreDiagnosticObserver.cs @@ -533,8 +533,13 @@ private void OnRoutingEndpointMatched(object arg) } else if (routeEndpoint?.RequestDelegate?.TryDuckCast(out var target) == true && target is { Handler: { } handler }) { + Log.Debug("RouteEndpoint?.RequestDelegate?.Method is null. Extracting code origin from RouteEndpoint.RequestDelegate.Target.Handler {Handler}", handler); CurrentCodeOrigin?.SetCodeOriginForEntrySpan(rootSpan, handler.Target?.GetType(), handler.Method); } + else + { + Log.Debug("RouteEndpoint?.RequestDelegate?.Method is null and could not extract handler from RouteEndpoint.RequestDelegate.Target"); + } } if (isFirstExecution) @@ -633,9 +638,16 @@ private void OnMvcBeforeAction(object arg) if (span is not null) { - if (isCodeOriginEnabled && TryGetTypeAndMethod(typedArg, out var type, out var method)) + if (isCodeOriginEnabled) { - CurrentCodeOrigin?.SetCodeOriginForEntrySpan(rootSpan, type, method); + if (TryGetTypeAndMethod(typedArg, out var type, out var method)) + { + CurrentCodeOrigin?.SetCodeOriginForEntrySpan(rootSpan, type, method); + } + else + { + Log.Debug("Could not extract type and method from {ActionDescriptor}", typedArg.ActionDescriptor?.DisplayName); + } } CurrentSecurity.CheckPathParamsFromAction(httpContext, span, typedArg.ActionDescriptor?.Parameters, typedArg.RouteData.Values); @@ -663,14 +675,22 @@ private bool TryGetTypeAndMethod(BeforeActionStruct beforeAction, out Type type, { foreach (var part in compiledPageActionDescriptor.HandlerMethods) { - if (part.TryDuckCast(out HandlerMethodDescriptorStruct methodDesc) - && string.Equals(methodDesc.HttpMethod, beforeAction.HttpContext.Request.Method, StringComparison.OrdinalIgnoreCase)) + if (part.TryDuckCast(out HandlerMethodDescriptorStruct methodDesc)) { - type = compiledPageActionDescriptor.HandlerTypeInfo; - method = methodDesc.MethodInfo; - return true; + if (string.Equals(methodDesc.HttpMethod, beforeAction.HttpContext.Request.Method, StringComparison.OrdinalIgnoreCase)) + { + type = compiledPageActionDescriptor.HandlerTypeInfo; + method = methodDesc.MethodInfo; + return true; + } + else + { + Log.Debug("Ignoring handler method {Method} for HTTP method {HttpMethod}", methodDesc.MethodInfo.Name, methodDesc.HttpMethod); + } } } + + Log.Debug("No matching handler method found for HTTP method {HttpMethod}", beforeAction.HttpContext.Request.Method); } } catch (Exception e)