Creating Custom Metrics | MuleSoft Documentation
# Creating Custom Metrics
The Anypoint Custom Metrics connector enables you to send operational and business metric values that you define from an app to Anypoint Monitoring. You can then use the built-in **Custom metrics** dashboard and a custom dashboard in **Advanced** mode to visualize the custom metrics in real time.
Define metrics in the Anypoint Custom Metrics connector to select which metrics to display in the dashboard. You can filter and group the metrics along the dimensions you define to include multiple custom and built-in metrics in a single dashboard visualization.
## Before You Begin
Run your Mule app on a supported Mule runtime version. For supported versions, see Mule Runtime Release Notes.
You must also install the Anypoint Monitoring Agent.
Review Custom Metrics Connector to understand connector configuration details. Connector configuration is required in your Mule app flow before you deploy or redeploy your app to a server or server cluster.
## Locate the Custom Metrics Connector
From Anypoint Monitoring, click **Custom Metrics** to locate **Anypoint Monitoring Custom Metrics Connector**. Alternatively, you can search for the connector in Anypoint Exchange.
## Custom Metrics Best Practices
Best practices include using time period and frequency effectively as well as using data with low cardinality dimensions.
### Use Time Period and Frequency Effectively
Each chart in the Custom Metrics dashboard represents the output of a query. Two parameters in the dashboard, _time range_ and _refresh frequency_ , affect the response time for the query that generates a chart. 

Time range
    
Determines how much data is fetched or processed to generate the chart. For example, more data is fetched and processed when displaying a monthly chart compared to displaying a daily chart. 

Refresh frequency
    
Determines how often a chart refreshes. High refresh frequencies put more stress on a system and can affect performance. For example, a refresh rate of one hour sends a query every hour, and a refresh rate of one minute sends the same query every minute.
Increasing time range (such as daily to monthly) or increasing refresh frequency (from one hour to one minute) affects the system response time. Reduce time range or reduce the refresh frequency to improve the chart response time.
### Use Low Cardinality Dimensions
Most custom metric dimensions have less than 100 possible unique values, but some may have a thousand unique values, such as `country_code`, `state_code`, `city_code`, and `zip`. Do not add _high cardinality_ data, or data with many unique values, to custom metrics. Some examples of high cardinality dimensions include `customer_id`, `user_id`, `session_id`, `order_id`, `transaction_id`, `email_address`, `token_id`, `product_id`, `location_id` (latitude/longitude), `ip_address`, and `phone_number`. Charts configured for custom metrics with high cardinality dimensions can adversely affect the response time of dashboards. For best results, restrict dimensions to have only a few unique values.
If a particular custom metrics chart slows down and affects other charts in the system, you can:
  * Create a new custom metric without the offending high cardinality dimension
  * Replace a high cardinality dimension with a low cardinality dimension, such as:
    * `category_id` instead of `product_id`
    * `country_code` instead of `location_id`
    * `order_count` instead of `order_id`
  * Avoid using UUIDs and timestamps when creating new custom metrics
  * Reduce the time range of the custom metric chart, for example, create a daily or weekly chart instead of a monthly or yearly chart
  * Reduce the refresh frequency, for example, refresh hourly instead of every minute.


### Restricted Keywords
Due to internal database restrictions, you cannot use the following keywords to define `dimensionName` or `factName` while using the Anypoint Custom Metrics connector:
Restricted Keywords
  * `ANALYZE`
  * `CONTINUOUS`
  * `DATABASE`
  * `DATABASES`
  * `DEFAULT`
  * `DESTINATIONS`
  * `DIAGNOSTICS`
  * `DISTINCT`
  * `DURATION`
  * `EXPLAIN`
  * `MEASUREMENT`
  * `MEASUREMENTS`
  * `PASSWORD`
  * `POLICIES`
  * `PRIVILEGES`
  * `QUERIES`
  * `REPLICATION`
  * `RESAMPLE`
  * `RETENTION`
  * `SOFFSET`
  * `SUBSCRIPTION`
  * `SUBSCRIPTIONS`


## Configure Custom Metrics from Anypoint Studio
  1. From Anypoint Monitoring:
    1. Click **Custom Metrics**.
    2. Click **Custom Metrics Connector** for the latest Mule version to open the connector on Anypoint Exchange.
    3. Click **Dependency Snippet** , and copy the Maven snippet.
  2. In Anypoint Studio, add the `<dependency/>` snippet for the latest version of the connector to your Mule project’s `pom.xml` file within the `<dependencies/>` element, for example:
```
<dependencies>
...
 <dependency>
  <groupId>com.mule.modules</groupId>
  <artifactId>mule-custom-metrics-extension</artifactId>
  <version>2.3.1</version>
  <classifier>mule-plugin</classifier>
 </dependency>
</dependencies>
```

Your connector version value might differ from the example. After you add the dependency, the connector becomes available in the Studio palette.
If you encounter issues with this step, see Add Modules to Your Project to add the connector in Studio from Exchange.
  3. From the palette, drag the **Send Custom Metric** operation to the canvas for your project.
  4. Click the **Send Custom Metric** operation to add values for the operation.
  5. Add the dimension for the metric:
    1. In **Metric name** , enter a meaningful value, for example "sales."
    2. In **Enter contextual information** select **Edit inline** to enter the name and value for the dimension.
    3. Click the green plus on the left to enter the values.
    4. In the **Dimension** dialog box, enter the **Name** and **Value** for the dimension.
    5. Leave the **Collapsible** option with the default of **True**.
    6. Click **Finish**. You can repeat these steps to add multiple key values.
  6. Enter the facts for the metric. Values for measurements are numerical.
    1. In **Enter measurements** select **Edit inline** to enter the name and value for the metric.
    2. In the **Fact** dialog box, enter a **Name** and numerical **Value** for the fact.
For facts, the value must always be numerical. This is not validated at design time, but a non-numerical value will cause a runtime exception once deployed.   
---  
    3. Leave the **Collapsible** option with the default of **True**.
    4. Click **Finish**. You can repeat these steps to add multiple key values.
  7. Select the **Throw runtime exception on validation failure** option if you want the flow to break if invalid values are entered for the metric. If this option is left unchecked, an error is will be logged, but the flow won’t break.
  8. Proceed to Get App Metrics in Anypoint Monitoring.


## Get App Metrics in Anypoint Monitoring
  1. Deploy the app.
For guidance, see Deploy Mule Applications.
  2. In Anypoint Monitoring, add a new custom dashboard for the custom metric.
  3. Configure a chart to use your custom metric.
For guidance, see Add Charts to a Custom Dashboard.
This example shows a configuration for a graph that displays t-shirt sales.
You only see the custom metrics in the pre-populated metric selection drop-down lists once something is logged for the custom metric.   
---  


## Use the Custom Metrics Dashboard
Custom metrics are not collected for the app until you configure the Custom Metrics Connector in your application flow and deploy the application to a server or server cluster.
  1. In Anypoint Monitoring, select the application to view metrics for, and click **View**.
  2. Click the **Custom metrics** tab.
If the Custom Metrics connector has collected one or more metrics in the specified time range, one chart per defined metric is displayed.   
---  
  3. Click **Select a measurement for this chart** to display the measurement for the chart.
  4. Use the drop-down lists at the bottom of the chart to filter the data by:
     * **Aggregation** - Possible aggregations include:
       * **None** (default) - No data aggregation.
       * **count** - Displays the total number of items collected for the measurement.
       * **distinct** - Returns the unique values for the given measurement.
       * **integral** - Returns the area under the curve for subsequent values.
       * **mean** - Returns the arithmetic average of the specified measurement over a given interval of time.
       * **median** - Displays the middle value for the collected metrics.
       * **mode** - Displays the most frequent value in the metrics collected for the defined measurement.
       * **sum** - Displays the sum of all the values for the defined measurement.
     * **Measurement** - The choices in this list are determined by the measurement values you define in the Custom Metrics connector.
     * **Group by** - The choices in this list are determined by the values you define in the Custom Metrics connector.


## See Also
Anypoint Custom Metrics Connector Reference