# AVS Monitor

#### How AVS Monitor Works

The **AVS Monitor** is a vital component of the AVS-SDK, designed to provide real-time metrics tracking for your nodes. With AVS Monitor, you can actively keep track of node uptime, request counts, latency, and error rates, all through a simple and structured interface. This guide introduces the AVS Monitor's setup and available features, empowering you to monitor node performance efficiently.

***

#### Importing and Using AVS Monitor

To begin using the **AVS Monitor** in your project, import it from the AVS-SDK. Below is an example code snippet for how to do this:

```javascript
const AVSMonitor = require('avs-sdk').AVSMonitor; // Import AVSMonitor from avs-sdk
```

After importing, create an instance of AVSMonitor, which will allow you to start monitoring individual nodes, record requests, and retrieve detailed metrics. Now, let’s explore the available functions and understand how you can utilize them for node performance monitoring.

***

#### AVS Monitor: Functions and Usage Overview

The AVS Monitor within the AVS-SDK provides a powerful interface to track and manage your nodes’ health metrics. Here’s a breakdown of the available functions and how to use them effectively.

***

#### Available Functions

**Constructor - `AVSMonitor()`**

* **Purpose**: Instantiates a new AVS Monitor instance with a default set of metrics, including node uptime, request count, latency, and error rate.
* **Usage**: This constructor is the starting point for setting up an AVS Monitor instance. Once instantiated, you can start monitoring nodes.
* **Example**:

  ```javascript
  const monitor = new AVSMonitor();
  ```

  Here, `monitor` represents an instance of AVSMonitor, ready for monitoring node performance.

***

**Start Monitoring - `startMonitoring(nodeId)`**

* **Purpose**: Begins monitoring a specified node, tracking its uptime, request count, and error rate.
* **Usage**: Use `startMonitoring` to initiate tracking of a node’s metrics. Provide a unique `nodeId` to identify which node you’re monitoring.
* **Example**:

  ```javascript
  monitor.startMonitoring('Node1');
  ```
* **What to Expect**: Once monitoring starts, metrics for the specified node will be recorded and emitted as events every 5 seconds. This includes uptime, request count, and error rate.

***

**Record Request - `recordRequest(nodeId, latency, isError)`**

* **Purpose**: Records a request made to the node, logging latency and whether an error occurred.
* **Usage**: Call `recordRequest` whenever a request is made to your node to update its request count and latency, and optionally mark errors.
* **Example**:

  ```javascript
  monitor.recordRequest('Node1', 150, false);
  ```
* **What to Expect**: The request count and latency metrics for the specified node are updated each time this function is called. If `isError` is true, the error count will increment, providing insight into error rates over time.

***

**Get Node Metrics - `getNodeMetrics(nodeId)`**

* **Purpose**: Retrieves the current metrics for a specific node, including uptime, request count, average latency, and error rate.
* **Usage**: Use `getNodeMetrics` to obtain an up-to-date snapshot of a node’s performance.
* **Example**:

  ```javascript
  const metrics = monitor.getNodeMetrics('Node1');
  console.log(metrics);
  ```
* **What to Expect**: This function returns a summary of key metrics for the specified node, allowing you to analyze its performance and detect any potential issues. The metrics include uptime in seconds, the total number of requests, average latency, and error rate.

***

#### Putting It All Together

With AVS Monitor, you gain real-time insight into node performance metrics, making it easy to track and manage node health. Here’s a sample workflow demonstrating how you can leverage AVS Monitor’s features:

```javascript
const AVSMonitor = require('avs-sdk').AVSMonitor;

const monitor = new AVSMonitor();
monitor.startMonitoring('Node1'); // Start monitoring the node

async function monitorNode() {
    monitor.recordRequest('Node1', 120, false); // Record a successful request with latency
    monitor.recordRequest('Node1', 200, true); // Record a failed request with latency

    const metrics = monitor.getNodeMetrics('Node1'); // Fetch the latest metrics
    console.log('Node Metrics:', metrics); // Output the metrics
}
```

#### Summary of Functionality

* **`AVSMonitor()`**: Instantiates an AVS Monitor instance to track node metrics.
* **`startMonitoring(nodeId)`**: Begins monitoring a specified node, emitting metrics at regular intervals.
* **`recordRequest(nodeId, latency, isError)`**: Logs each request to the node, updating request count, latency, and error rate.
* **`getNodeMetrics(nodeId)`**: Retrieves a comprehensive summary of current metrics for the specified node.

The AVS Monitor is crafted to simplify the process of tracking node metrics, making it a crucial tool for maintaining optimal node performance within decentralized systems.


---

# 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://shivanshs-organization-2.gitbook.io/avs-sdk/getting-started/avs-monitor.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.
