[Yandex Cloud documentation](../../index.md) > [Yandex Query](../index.md) > Data sources and sinks > Working with Yandex Monitoring > Writing data

# Writing metrics to Monitoring

[Monitoring](../../monitoring/concepts/index.md) allows you to collect and store metrics, as well as display them as charts on dashboards. Data sent to Monitoring includes `metrics` and their descriptive `labels`.

For example, to track application failures, you can use the failure count per time interval as a metric. Data describing a failure, e.g., a host name and application version, serve as labels. The Monitoring interface allows you to aggregate metrics by label.

Query example for writing metrics from Yandex Query into Monitoring

```sql
INSERT INTO `monitoring`.custom
SELECT
	`my_timestamp`,
	host_name,
	app_version,
	exception_count,
	"exception_monitor" as service_type
FROM $query;
```

During [stream processing](../concepts/stream-processing.md), Yandex Query can send query results to Monitoring as metrics and their labels. 

## Setting up a connection {#setup-connection}

To send metrics to Monitoring:
1. [Navigate](../../console/operations/select-service.md#select-service) to the **Connections** section of the **Yandex Query** interface and click **Create new**.
1. In the window that opens, specify the Monitoring connection name in the **Name** field.
1. In the **Type** dropdown, select `Monitoring`.
1. In the **Service account** field, select an existing service account or create a new one. Assign it the [`monitoring.editor`](../../monitoring/security/index.md#monitoring-editor) permissions allowing it to write metrics.

   To use the service account on your behalf, you need the `iam.serviceAccounts.user` [role](../../iam/security/index.md#iam-serviceAccounts-user).

1. Click **Create** to create a connection.

## Data model {#data-model}

To write metrics to Monitoring, use the following SQL statement:

```sql
INSERT INTO 
	<connection>.custom 
SELECT 
	<fields> 
FROM 
	<query>;
```

Where:

- `<connection>`: Name of the Monitoring connection created in the previous step.
- `<fields>`: List of fields that include a timestamp, metrics, and their labels.
- `<query>`: Yandex Query source data query.

{% note info %}

When writing metrics, use the `INSERT INTO <connection>.custom` statement, where [`custom`](../../monitoring/api-ref/MetricsData/write.md#query_params) is the name reserved in Monitoring for user-defined metrics.

{% endnote %}

To write metrics, use the [write](../../monitoring/api-ref/MetricsData/write.md) Monitoring API method. When writing metrics, provide the following information:
- Timestamp.
- List of metrics and their types. Yandex Query supports `DGAUGE` and `IGAUGE` metric types.
- List of labels.

Yandex Query automatically infers parameter semantics from the SQL query.

| Field type | Description | Limitations |
| --- | --- | --- |
| Time: `Date`, `Datetime`, `Timestamp`, `TzDate`, `TzDatetime`, or `TzTimestamp` | Common timestamp for all metrics | A query can have only one timestamp field. |
| Integer: `Bool`, `Int8`, `Uint8`, `Int16`, `Uint16`, `Int32`, `Uint32`, `Int64`, or `Uint64` | Metric values, `IGAUGE` | The field name in the SQL statement is the metric name. A single query may contain an unlimited number of metrics. |
| Floating point: `Float` or `Double` | Metric values, `DGAUGE` | The field name in the SQL statement is the metric name. A single query may contain an unlimited number of metrics. |
| Text: `String` or `Utf8` | Label values | The field name in the SQL statement serves as the label name, and its text value as the label value. A single query may contain an unlimited number of metrics. |

No other data types are permitted in these fields.

## Metrics writing example {#example}

Query example for writing metrics from Yandex Query to Monitoring:

```sql
INSERT INTO 
	`monitoring`.custom
SELECT
	`my_timestamp`,
	host AS host_name,
	app_version,
	exception_count,
	"exception_monitor" as service_type
FROM $query;
```

Where:

| Field | Type | Description |
| --- | --- | --- |
| `monitoring` | | Monitoring connection name |
| `$query` | | SQL query data source. It can be a YQL subquery or a data source [connection](../quickstart/streaming-example.md) |
| `my_timestamp` | Timestamp | Data source: `my_timestamp` column in the source data `stream` |
| `exception_count` | Metric | Data source: `exception_count` column in the source data `stream` |
| `host_name` | Label | Data source: `host` column in the source data `stream` |
| `app_version` | Label | Data source: `app_version` column in the source data `stream` |

Example of query results in Monitoring:

![](../../_assets/query/monitoring-example.png)