Commit d28c2082 authored by Savorboard's avatar Savorboard

support event data for Diagnostics.

parent e6bad00b
......@@ -3,8 +3,10 @@
using System;
using System.Data;
using System.Diagnostics;
using System.Threading.Tasks;
using DotNetCore.CAP.Infrastructure;
using DotNetCore.CAP.Internal;
using DotNetCore.CAP.Models;
using Microsoft.Extensions.Logging;
......@@ -15,6 +17,11 @@ namespace DotNetCore.CAP.Abstractions
private readonly IDispatcher _dispatcher;
private readonly ILogger _logger;
// diagnostics listener
// ReSharper disable once InconsistentNaming
private static readonly DiagnosticListener s_diagnosticListener =
new DiagnosticListener(CapDiagnosticListenerExtensions.DiagnosticListenerName);
protected CapPublisherBase(ILogger<CapPublisherBase> logger, IDispatcher dispatcher)
{
_logger = logger;
......@@ -75,13 +82,13 @@ namespace DotNetCore.CAP.Abstractions
protected virtual string Serialize<T>(T obj, string callbackName = null)
{
var packer = (IMessagePacker) ServiceProvider.GetService(typeof(IMessagePacker));
var packer = (IMessagePacker)ServiceProvider.GetService(typeof(IMessagePacker));
string content;
if (obj != null)
{
if (Helper.IsComplexType(obj.GetType()))
{
var serializer = (IContentSerializer) ServiceProvider.GetService(typeof(IContentSerializer));
var serializer = (IContentSerializer)ServiceProvider.GetService(typeof(IContentSerializer));
content = serializer.Serialize(obj);
}
else
......@@ -179,8 +186,8 @@ namespace DotNetCore.CAP.Abstractions
private void PublishWithTrans<T>(string name, T contentObj, string callbackName = null)
{
try
{
Guid operationId = default(Guid);
var content = Serialize(contentObj, callbackName);
var message = new CapPublishedMessage
......@@ -190,6 +197,10 @@ namespace DotNetCore.CAP.Abstractions
StatusName = StatusName.Scheduled
};
try
{
operationId = s_diagnosticListener.WritePublishMessageStoreBefore(message);
var id = Execute(DbConnection, DbTransaction, message);
ClosedCap();
......@@ -197,15 +208,15 @@ namespace DotNetCore.CAP.Abstractions
if (id > 0)
{
_logger.LogInformation($"message [{message}] has been persisted in the database.");
s_diagnosticListener.WritePublishMessageStoreAfter(operationId, message);
message.Id = id;
Enqueue(message);
}
}
catch (Exception e)
{
_logger.LogError("An exception was occurred when publish message. exception message:" + e.Message, e);
s_diagnosticListener.WritePublishMessageStoreError(operationId, message, e);
Console.WriteLine(e);
throw;
}
......
......@@ -55,6 +55,7 @@
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="System.Data.Common" Version="4.3.0" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="4.4.1" />
<PackageReference Include="System.Threading.ThreadPool" Version="4.3.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.0.0" />
......
......@@ -2,6 +2,7 @@
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
......@@ -25,6 +26,11 @@ namespace DotNetCore.CAP
private Task _compositeTask;
private bool _disposed;
// diagnostics listener
// ReSharper disable once InconsistentNaming
private static readonly DiagnosticListener s_diagnosticListener =
new DiagnosticListener(CapDiagnosticListenerExtensions.DiagnosticListenerName);
public ConsumerHandler(IConsumerClientFactory consumerClientFactory,
IDispatcher dispatcher,
IStorageConnection connection,
......@@ -91,21 +97,34 @@ namespace DotNetCore.CAP
private void RegisterMessageProcessor(IConsumerClient client)
{
client.OnMessageReceived += (sender, message) =>
client.OnMessageReceived += (sender, messageContext) =>
{
Guid operationId = default(Guid);
var receivedMessage = new CapReceivedMessage(messageContext)
{
StatusName = StatusName.Scheduled
};
try
{
var storedMessage = StoreMessage(message);
operationId = s_diagnosticListener.WriteReceiveMessageStoreBefore(receivedMessage);
StoreMessage(receivedMessage);
client.Commit();
_dispatcher.EnqueueToExecute(storedMessage);
s_diagnosticListener.WriteReceiveMessageStoreAfter(operationId, receivedMessage);
_dispatcher.EnqueueToExecute(receivedMessage);
}
catch (Exception e)
{
_logger.LogError(e, "An exception occurred when storage received message. Message:'{0}'.",
message);
_logger.LogError(e, "An exception occurred when storage received message. Message:'{0}'.", messageContext);
client.Reject();
s_diagnosticListener.WriteReceiveMessageStoreError(operationId, receivedMessage, e);
}
};
......@@ -139,15 +158,12 @@ namespace DotNetCore.CAP
}
}
private CapReceivedMessage StoreMessage(MessageContext messageContext)
{
var receivedMessage = new CapReceivedMessage(messageContext)
private void StoreMessage(CapReceivedMessage receivedMessage)
{
StatusName = StatusName.Scheduled
};
var id = _connection.StoreReceivedMessageAsync(receivedMessage).GetAwaiter().GetResult();
var id = _connection.StoreReceivedMessageAsync(receivedMessage)
.GetAwaiter().GetResult();
receivedMessage.Id = id;
return receivedMessage;
}
}
}
\ No newline at end of file
......@@ -20,6 +20,11 @@ namespace DotNetCore.CAP
private readonly CapOptions _options;
private readonly IStateChanger _stateChanger;
// diagnostics listener
// ReSharper disable once InconsistentNaming
private static readonly DiagnosticListener s_diagnosticListener =
new DiagnosticListener(CapDiagnosticListenerExtensions.DiagnosticListenerName);
protected BasePublishMessageSender(
ILogger logger,
CapOptions options,
......@@ -37,6 +42,7 @@ namespace DotNetCore.CAP
public async Task<OperateResult> SendAsync(CapPublishedMessage message)
{
var sp = Stopwatch.StartNew();
var operationId = s_diagnosticListener.WritePublishBefore(message);
var result = await PublishAsync(message.Name, message.Content);
......@@ -45,15 +51,18 @@ namespace DotNetCore.CAP
if (result.Succeeded)
{
await SetSuccessfulState(message);
s_diagnosticListener.WritePublishAfter(operationId, message);
_logger.MessageHasBeenSent(sp.Elapsed.TotalSeconds);
return OperateResult.Success;
}
else
{
s_diagnosticListener.WritePublishError(operationId, message, result.Exception);
_logger.MessagePublishException(message.Id, result.Exception);
await SetFailedState(message, result.Exception, out bool stillRetry);
if (stillRetry)
{
_logger.SenderRetrying(3);
......@@ -62,6 +71,7 @@ namespace DotNetCore.CAP
}
return OperateResult.Failed(result.Exception);
}
}
private static bool UpdateMessageForRetryAsync(CapPublishedMessage message)
{
......
......@@ -18,10 +18,14 @@ namespace DotNetCore.CAP
private readonly ICallbackMessageSender _callbackMessageSender;
private readonly IStorageConnection _connection;
private readonly ILogger _logger;
private readonly IStateChanger _stateChanger;
private readonly CapOptions _options;
private readonly MethodMatcherCache _selector;
private readonly IStateChanger _stateChanger;
// diagnostics listener
// ReSharper disable once InconsistentNaming
private static readonly DiagnosticListener s_diagnosticListener =
new DiagnosticListener(CapDiagnosticListenerExtensions.DiagnosticListenerName);
public DefaultSubscriberExecutor(
ILogger<DefaultSubscriberExecutor> logger,
......@@ -139,12 +143,18 @@ namespace DotNetCore.CAP
var error = $"message can not be found subscriber, Message:{receivedMessage},\r\n see: https://github.com/dotnetcore/CAP/issues/63";
throw new SubscriberNotFoundException(error);
}
Guid operationId = default(Guid);
var consumerContext = new ConsumerContext(executor, receivedMessage.ToMessageContext());
try
{
var consumerContext = new ConsumerContext(executor, receivedMessage.ToMessageContext());
operationId = s_diagnosticListener.WriteConsumerInvokeBefore(consumerContext);
var ret = await Invoker.InvokeAsync(consumerContext);
s_diagnosticListener.WriteConsumerInvokeAfter(operationId,consumerContext);
if (!string.IsNullOrEmpty(ret.CallbackName))
{
await _callbackMessageSender.SendAsync(ret.MessageId, ret.CallbackName, ret.Result);
......@@ -152,6 +162,8 @@ namespace DotNetCore.CAP
}
catch (Exception ex)
{
s_diagnosticListener.WriteConsumerInvokeError(operationId, consumerContext, ex);
throw new SubscriberExecutionFailedException(ex.Message, ex);
}
}
......
This diff is collapsed.
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