# Monitoring

Brighter emits monitoring information from an External Bus using a configured [Control Bus](https://brightercommand.github.io/Brighter/ControlBus.html)

## Configuring Monitoring

Firstly [configure a Control Bus](https://brightercommand.github.io/Brighter/ControlBus.html#configure) in the brighter application to emit monitoring messages

## Config file

Monitoring requires a new section to be added to the application config file:

```xml
<configSections>
    <section name="monitoring" type ="paramore.brighter.commandprocessor.monitoring.Configuration.MonitoringConfigurationSection, Brighter.commandprocessor" allowLocation ="true" allowDefinition="Everywhere"/>
</configSections>
```

The monitoring config can then be speicified later in the file:

```xml
<monitoring>
    <monitor isMonitoringEnabled="true" instanceName="ManagementAndMonitoring"/>
</monitoring>
```

This enables runtime changes to enable/disable emitting of monitoring messages.

## Handler Configuration

Each handler that requires monitoring must be configured in two stages, a Handler attribute and container registration of a MonitorHandler for the given request:

For example, given:

* TRequest - a Brighter Request, inheriting from IRequest
* TRequestHandler - handles the TRequest, inheriting IHandleRequest \<TRequest>

### Attribute

The following attribute must be added to the Handle method in the handler, TRequestHandler:

```csharp
[Monitor(step:1, timing:HandlerTiming.Before, handlerType:typeof(TRequestHandler))]
```

Please note the step and timing can vary if monitoring should be after another attribute step, or timing should be emitted after.

### Container registration

The following additional handler must be registered in the application container (where `MonitorHandler<T>` is a built-in Brighter handler):

```csharp
container.Register<TRequest, MonitorHandler<TRequest>>
```

## Monitor message format

A message is emitted from the Control Bus on Handler Entry and Handler Exit. The following is the form of the message:

```javascript
{
    "Exception": null, // or Exception message
    "EventType": "EnterHandler or ExitHandler",
    "EventTime": "2016-06-21T15:48:26.1390192Z",
    "TimeElapsedMs": 0 or Duration,
    "HandlerName": "...",
    "HandlerFullAssemblyName": "...",
    "InstanceName": "ManagementAndMonitoring",
    "RequestBody": "{\"Id\":\"dc32b35f-bc75-4197-9178-c8310a63e4fb\", ... }",
    "Id": "048cc207-e820-40fa-b931-55b60203fbc2"
}
```

Messages can be processed from the queue and interated with your monitoring tool of choice, for example Live python consumers emitting to console or logstash consumption to the ELK stack using relevant plugins to provide performance raditators or dashboards.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://brightercommand.gitbook.io/paramore-brighter-documentation/health-checks-and-observability/monitoring.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
