ServiceControl publishes events that enable the creation of custom notifications and integrations to monitor the health of the system. ServiceControl processes messages from the error queue as well as data sent by endpoints using the NServiceBus.Heartbeat and NServiceBus.CustomChecks packages. When messages fail, heartbeats stop arriving, or custom checks fail, ServiceControl publishes events that any subscribing endpoint can react to.
See Monitor with ServiceControl events for a sample.
External notification events are sent in batches. If a problem is encountered partway through a batch, the entire batch will be re-sent. This can result in receiving multiple events for a single notification.
MessageFailed events
Once a message arrives in the error queue, ServiceControl will publish a MessageFailed event. This message contains:
- The endpoint that sent the message
- The endpoint that received the message
- The cause of the failure (i.e. the exception type and message)
- The original message headers
- The original message body, in some cases (see below)
Whether the event includes the body depends on the storage the Error instance uses and on the body itself:
| Storage | Message body | Included in the event |
|---|---|---|
| RavenDB | Text, smaller than 85 KB, with full-text body indexing enabled | The full body |
| RavenDB | Any other body | No body |
| SQL Server or PostgreSQL | Text, up to ServiceControl/ bytes | The full body |
| SQL Server or PostgreSQL | Text, larger than MaxBodySizeToStore bytes | The start of the body, truncated to at most MaxBodySizeToStore bytes |
| SQL Server or PostgreSQL | Not text, such as binary or compressed bodies | No body |
See which bodies count as text for SQL Server and PostgreSQL storage.
Handlers must allow for a missing or truncated body. The event does not indicate whether the body was truncated, and a truncated JSON or XML body is not valid and cannot be deserialized.
Subscribe
It is possible to subscribe to this event type and act on the messages, for example: by sending an email or triggering a text message.
To subscribe to the MessageFailed event:
- Create an NServiceBus endpoint.
- Install the ServiceControl.Contracts NuGet package.
- Configure the endpoint to use
SystemJsonSerializeras the message published by ServiceControl uses JSON serialization. Configure the endpoint with the following conventions, as the events published by ServiceControl do not derive fromIEvent.
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
var conventions = endpointConfiguration.Conventions();
conventions.DefiningEventsAs(
type =>
{
return typeof(IEvent).IsAssignableFrom(type) ||
// include ServiceControl events
type.Namespace != null &&
type.Namespace.StartsWith("ServiceControl.Contracts");
});
- Add a message handler for the
MessageFailedevent in the endpoint. In the following example, there is a simple HTTP call to show how to integrate with a third-party system to provide notification of the error.
class MessageFailedHandler :
IHandleMessages<MessageFailed>
{
public Task Handle(MessageFailed message, IMessageHandlerContext context)
{
var failedMessageId = message.FailedMessageId;
var exceptionMessage = message.FailureDetails.Exception.Message;
var chatMessage = $@"Message with id: {failedMessageId} failed.
Reason: '{exceptionMessage}'.
Open in ServicePulse: {GetServicePulseUri(failedMessageId)}";
using (var client = new ChatClient())
{
client.PostChatMessage(chatMessage);
}
return Task.CompletedTask;
}
Endpoints that subscribe to ServiceControl events should not use the same error and audit queues as other endpoints. Using the same error queue could cause an infinite feedback loop if processing a MessageFailed message failed. Using the same audit queue will cause the processing of the MessageFailed messages to be included in the messages search results, meaning the failure and the failure notification will be returned. See also: Recoverability and Audit Queue Settings.
Registering the publisher for message-driven publish/subscribe
Transports that use message-driven publish-subscribe must have the ServiceControl instance registered as the publisher of the MessageFailed event.
The routing config code API can be used:
var routing = transport.Routing();
routing.RegisterPublisher(typeof(ServiceControl.Contracts.MessageFailed).Assembly, "Particular.ServiceControl");
Transports that natively support publish and subscribe do not require any additional configuration.
Monitoring events
ServiceControl will also publish events based on collected monitoring data.
See Heartbeat Notification Events and Custom Check Notification Events for a description of these events.
Other events
ServiceControl will also publish events related to archiving and retrying messages:
FailedMessagesArchived: Event emitted for failed messages that were archived, indicating they won’t be retriedFailedMessagesUnArchived: Event emitted for failed messages that were un-archived (restored from the archive), making them eligible for retry or further actionMessageFailureResolvedByRetry: Event emitted by ServiceControl for each failed message that succeeded after retryingMessageFailureResolvedManually: Event emitted by ServiceControl for each failed message that was manually marked as resolved, typically via the "Resolve" or "Resolve All" actions in ServicePulseMessageEditedAndRetried: Event emitted by ServiceControl every time that a failed message was edited and retried
Decommissioning subscribers to ServiceControl events
ServiceControl uses event publishing to expose information to subscribers. When using a persistence-based transport an internal reference will be kept to each subscriber. If a subscriber for an event cannot be contacted then a log entry will be written with the following error:
Failed dispatching external integration event
An event will also be published and displayed in the ServicePulse dashboard with the following text:
'EVENTTYPE' failed to be published to other integration points. Reason for failure: REASON.
To avoid this situation, it is important to properly decommission an endpoint that subscribes to ServiceControl events. To do this, disable auto-subscription and then unsubscribe from all events.
Enabling publishing
In ServiceControl version 7 and later, publishing is disabled by default. To publish events, set EnableExternalIntegrationsPublishing to true. Leave publishing disabled in systems that have no subscribers for ServiceControl events, to avoid unneeded traffic to the broker.
- Ingestion-only workers: set the same value on every ingestion-only worker. Each worker stores the
MessageFailedevents for the messages it ingests, and the Error instance publishes them. A worker without the setting stores none, so failures it ingests publish noMessageFailedevent, without any error.