Unverified Commit 24d36f36 authored by Lemon's avatar Lemon Committed by GitHub

Fix issue#66 (#67)

parent 3e72e125
...@@ -9,10 +9,8 @@ namespace SkyWalking.Sample.AspNet.Controllers ...@@ -9,10 +9,8 @@ namespace SkyWalking.Sample.AspNet.Controllers
{ {
public async Task<IHttpActionResult> Get() public async Task<IHttpActionResult> Get()
{ {
// var httpClient = new HttpClient(new HttpTracingHandler()); var httpClient = new HttpClient(new HttpTracingHandler());
// var values = await httpClient.GetStringAsync("http://localhost:5002/api/values"); var values = await httpClient.GetStringAsync("http://localhost:5001/api/values");
var values = 1;
return Json(values); return Json(values);
} }
} }
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
* *
*/ */
using System.Collections.Generic;
using SkyWalking.Context.Trace; using SkyWalking.Context.Trace;
namespace SkyWalking.Context namespace SkyWalking.Context
...@@ -29,6 +30,8 @@ namespace SkyWalking.Context ...@@ -29,6 +30,8 @@ namespace SkyWalking.Context
IContextSnapshot Capture { get; } IContextSnapshot Capture { get; }
ISpan ActiveSpan { get; } ISpan ActiveSpan { get; }
IDictionary<string,object> Properties { get; }
void Continued(IContextSnapshot snapshot); void Continued(IContextSnapshot snapshot);
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
*/ */
using System; using System;
using System.Collections.Generic;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using SkyWalking.Boot; using SkyWalking.Boot;
...@@ -98,6 +99,8 @@ namespace SkyWalking.Context ...@@ -98,6 +99,8 @@ namespace SkyWalking.Context
public static IContextSnapshot Capture => _context.Value?.Capture; public static IContextSnapshot Capture => _context.Value?.Capture;
public static IDictionary<string, object> ContextProperties => _context.Value?.Properties;
public static ISpan CreateEntrySpan(string operationName, IContextCarrier carrier) public static ISpan CreateEntrySpan(string operationName, IContextCarrier carrier)
{ {
var samplingService = ServiceManager.Instance.GetService<SamplingService>(); var samplingService = ServiceManager.Instance.GetService<SamplingService>();
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
* *
*/ */
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using SkyWalking.Context.Trace; using SkyWalking.Context.Trace;
...@@ -49,6 +50,8 @@ namespace SkyWalking.Context ...@@ -49,6 +50,8 @@ namespace SkyWalking.Context
} }
} }
public IDictionary<string, object> Properties { get; } = new Dictionary<string, object>();
public void Continued(IContextSnapshot snapshot) public void Continued(IContextSnapshot snapshot)
{ {
} }
...@@ -83,6 +86,13 @@ namespace SkyWalking.Context ...@@ -83,6 +86,13 @@ namespace SkyWalking.Context
if (_spans.Count == 0) if (_spans.Count == 0)
{ {
ListenerManager.NotifyFinish(this); ListenerManager.NotifyFinish(this);
foreach (var item in Properties)
{
if (item.Value is IDisposable disposable)
{
disposable.Dispose();
}
}
} }
} }
......
...@@ -121,6 +121,8 @@ namespace SkyWalking.Context ...@@ -121,6 +121,8 @@ namespace SkyWalking.Context
public IContextSnapshot Capture => InternalCapture(); public IContextSnapshot Capture => InternalCapture();
public ISpan ActiveSpan => InternalActiveSpan(); public ISpan ActiveSpan => InternalActiveSpan();
public IDictionary<string, object> Properties { get; } = new Dictionary<string, object>();
public void Continued(IContextSnapshot snapshot) public void Continued(IContextSnapshot snapshot)
{ {
...@@ -288,6 +290,14 @@ namespace SkyWalking.Context ...@@ -288,6 +290,14 @@ namespace SkyWalking.Context
} }
ListenerManager.NotifyFinish(finishedSegment); ListenerManager.NotifyFinish(finishedSegment);
foreach (var item in Properties)
{
if (item.Value is IDisposable disposable)
{
disposable.Dispose();
}
}
} }
private ISpan InternalActiveSpan() private ISpan InternalActiveSpan()
......
...@@ -32,6 +32,7 @@ namespace SkyWalking.Diagnostics.EntityFrameworkCore ...@@ -32,6 +32,7 @@ namespace SkyWalking.Diagnostics.EntityFrameworkCore
{ {
public class EntityFrameworkCoreDiagnosticProcessor : ITracingDiagnosticProcessor public class EntityFrameworkCoreDiagnosticProcessor : ITracingDiagnosticProcessor
{ {
private const string TRACE_ORM = "TRACE_ORM";
private Func<CommandEventData, string> _operationNameResolver; private Func<CommandEventData, string> _operationNameResolver;
private readonly IEfCoreSpanFactory _efCoreSpanFactory; private readonly IEfCoreSpanFactory _efCoreSpanFactory;
...@@ -69,12 +70,14 @@ namespace SkyWalking.Diagnostics.EntityFrameworkCore ...@@ -69,12 +70,14 @@ namespace SkyWalking.Diagnostics.EntityFrameworkCore
Tags.DbInstance.Set(span, eventData.Command.Connection.Database); Tags.DbInstance.Set(span, eventData.Command.Connection.Database);
Tags.DbStatement.Set(span, eventData.Command.CommandText); Tags.DbStatement.Set(span, eventData.Command.CommandText);
Tags.DbBindVariables.Set(span, BuildParameterVariables(eventData.Command.Parameters)); Tags.DbBindVariables.Set(span, BuildParameterVariables(eventData.Command.Parameters));
ContextManager.ContextProperties[TRACE_ORM] = true;
} }
[DiagnosticName("Microsoft.EntityFrameworkCore.Database.Command.CommandExecuted")] [DiagnosticName("Microsoft.EntityFrameworkCore.Database.Command.CommandExecuted")]
public void CommandExecuted() public void CommandExecuted()
{ {
ContextManager.StopSpan(); ContextManager.StopSpan();
ContextManager.ContextProperties.Remove(TRACE_ORM);
} }
[DiagnosticName("Microsoft.EntityFrameworkCore.Database.Command.CommandError")] [DiagnosticName("Microsoft.EntityFrameworkCore.Database.Command.CommandError")]
...@@ -92,6 +95,7 @@ namespace SkyWalking.Diagnostics.EntityFrameworkCore ...@@ -92,6 +95,7 @@ namespace SkyWalking.Diagnostics.EntityFrameworkCore
} }
span.ErrorOccurred(); span.ErrorOccurred();
ContextManager.StopSpan(span); ContextManager.StopSpan(span);
ContextManager.ContextProperties.Remove(TRACE_ORM);
} }
private string BuildParameterVariables(DbParameterCollection dbParameters) private string BuildParameterVariables(DbParameterCollection dbParameters)
......
...@@ -28,6 +28,7 @@ namespace SkyWalking.Diagnostics.SqlClient ...@@ -28,6 +28,7 @@ namespace SkyWalking.Diagnostics.SqlClient
{ {
public class SqlClientDiagnosticProcessor : ITracingDiagnosticProcessor public class SqlClientDiagnosticProcessor : ITracingDiagnosticProcessor
{ {
private const string TRACE_ORM = "TRACE_ORM";
public string ListenerName { get; } = SqlClientDiagnosticStrings.DiagnosticListenerName; public string ListenerName { get; } = SqlClientDiagnosticStrings.DiagnosticListenerName;
private static string ResolveOperationName(SqlCommand sqlCommand) private static string ResolveOperationName(SqlCommand sqlCommand)
...@@ -39,6 +40,10 @@ namespace SkyWalking.Diagnostics.SqlClient ...@@ -39,6 +40,10 @@ namespace SkyWalking.Diagnostics.SqlClient
[DiagnosticName(SqlClientDiagnosticStrings.SqlBeforeExecuteCommand)] [DiagnosticName(SqlClientDiagnosticStrings.SqlBeforeExecuteCommand)]
public void BeforeExecuteCommand([Property(Name = "Command")] SqlCommand sqlCommand) public void BeforeExecuteCommand([Property(Name = "Command")] SqlCommand sqlCommand)
{ {
if (ContextManager.ContextProperties.ContainsKey(TRACE_ORM))
{
return;
}
var peer = sqlCommand.Connection.DataSource; var peer = sqlCommand.Connection.DataSource;
var span = ContextManager.CreateExitSpan(ResolveOperationName(sqlCommand), peer); var span = ContextManager.CreateExitSpan(ResolveOperationName(sqlCommand), peer);
span.SetLayer(SpanLayer.DB); span.SetLayer(SpanLayer.DB);
...@@ -52,12 +57,20 @@ namespace SkyWalking.Diagnostics.SqlClient ...@@ -52,12 +57,20 @@ namespace SkyWalking.Diagnostics.SqlClient
[DiagnosticName(SqlClientDiagnosticStrings.SqlAfterExecuteCommand)] [DiagnosticName(SqlClientDiagnosticStrings.SqlAfterExecuteCommand)]
public void AfterExecuteCommand() public void AfterExecuteCommand()
{ {
if (ContextManager.ContextProperties.ContainsKey(TRACE_ORM))
{
return;
}
ContextManager.StopSpan(); ContextManager.StopSpan();
} }
[DiagnosticName(SqlClientDiagnosticStrings.SqlErrorExecuteCommand)] [DiagnosticName(SqlClientDiagnosticStrings.SqlErrorExecuteCommand)]
public void ErrorExecuteCommand([Property(Name = "Exception")] Exception ex) public void ErrorExecuteCommand([Property(Name = "Exception")] Exception ex)
{ {
if (ContextManager.ContextProperties.ContainsKey(TRACE_ORM))
{
return;
}
var span = ContextManager.ActiveSpan; var span = ContextManager.ActiveSpan;
span?.ErrorOccurred(); span?.ErrorOccurred();
span?.Log(ex); span?.Log(ex);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment