# Metrics ingest

Send custom metric points with a metric source’s token, and read back the points and limits that token’s source holds.

Its operations take the source's token as `Authorization: Bearer <token>`, never an API key.

**Endpoints**

- `POST/api/metric-sources/ingest`
- `GET/api/metric-sources/ingest`

## Send metric points

POST/api/metric-sources/ingest

Sends a batch of custom metric points with a metric source’s token. The body is a JSON envelope `{"points": […]}`, a JSON array, one point or newline-delimited JSON, any of them gzipped. Each point names its metric by key (`metric`) or by uuid (`metric_uuid`) and carries a numeric `value` and an optional `recorded_at`. The answer counts what was queued and, per reason, what was refused; a batch can be partly accepted.

**Limits** (by default):

-   1,000 points in one request.
-   A body of 1,048,576 bytes as sent and 10,485,760 bytes after decompression.
-   A `recorded_at` at most 300 seconds ahead of the server’s clock, and at most 1,095 days back.

### Headers

- `Idempotency-Key` (string): Any text of 1 to 255 visible ASCII characters. Required for points older than 30 days. A repeat of the same batch with the same key within 30 days is answered as the first was and counted once; a different batch under a used key is refused.

### Body parameters

- `points` (array of objects, required): The points of the batch.
  - `metric` (string, required): The metric key, as it appears in WatchTower15: `A-Z a-z 0-9 . _ -`, up to 100 characters. Taken exactly as sent. Send this or `metric_uuid`, not both.
  - `metric_uuid` (string (uuid)): The metric’s uuid, instead of its key. Never creates a metric, and must belong to the source whose token is used.
  - `value` (number, required): A number. For a level, the measurement (negative and fractional are fine). For a count, how many occurrences the point represents; a count cannot be negative.
  - `metric_type` (string): The type to create a new metric as. Honoured only on the push that creates the metric; any other value refuses the point. Values:`count``level`(an open set: new values may be added)
  - `recorded_at` (string): When the value was measured: RFC 3339, or a Unix epoch in seconds or milliseconds as a number or a string. A time without a timezone is read as UTC. Defaults to now.

### Returns

`200 OK`:

What was queued and how many points each reason refused.

The response panel's status selector shows each failure this operation can answer.

**POST /api/metric-sources/ingest**

curl:

```bash
curl -X POST https://api.watchtower15.com/api/metric-sources/ingest \
  -H "Authorization: Bearer $WT15_METRICS_TOKEN" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: $(uuidgen)" \
  -d '{
    "points": [
      {
        "metric": "orders.completed",
        "metric_type": "count",
        "value": 1
      }
    ]
  }'
```

PHP:

```php
$wt15 = WatchTower15\Client::fromEnvironment();

$result = $wt15->ingest->metrics->push([['metric' => 'orders.completed', 'metric_type' => 'count', 'value' => 1]]);
```

Node.js:

```ts
const wt15 = WatchTower15.fromEnvironment();

const result = await wt15.ingest.metrics.push([{ metric: 'orders.completed', metric_type: 'count', value: 1 }]);
```

Python:

```python
client = Client.from_environment()

result = client.ingest.metrics.push([{'metric': 'orders.completed', 'metric_type': 'count', 'value': 1}])
```

**Response (200 OK)**

```json
{
  "status": "success",
  "status_code": 1,
  "data": {
    "accepted": true,
    "batch_id": "example",
    "points": 1,
    "rejected": {
      "over_metric_cap": 1,
      "metric_create_busy": 1,
      "bad_metric_key": 1,
      "unknown_metric_uuid": 1,
      "ambiguous_identifier": 1,
      "future_timestamp": 1,
      "too_old": 1,
      "non_numeric": 1,
      "negative_on_count_metric": 1,
      "over_point_rate": 1,
      "over_backfill_budget": 1,
      "missing_idempotency_key": 1,
      "metric_key_deleted": 1,
      "metric_awaiting_approval": 1,
      "bad_metric_type": 1
    }
  },
  "request_id": "example",
  "timestamp": 1794214800000,
  "message": "example"
}
```

**Response (400 Bad Request)**

The body is empty, or is not one of the accepted forms, or cannot be decompressed.

```json
{
  "status": "failure",
  "status_code": 400,
  "data": null,
  "request_id": "example",
  "timestamp": 1794214800000,
  "message": "example",
  "errors": {}
}
```

**Response (403 Forbidden)**

The token is missing, malformed, unknown, switched off or belongs to another kind of source. Every failure to authenticate is this one answer.

```json
{
  "status": "failure",
  "status_code": 403,
  "data": null,
  "request_id": "example",
  "timestamp": 1794214800000,
  "message": "example",
  "errors": {}
}
```

**Response (413 Content Too Large)**

The body is larger than the limit, or holds more points than a request may.

```json
{
  "status": "failure",
  "status_code": 413,
  "data": null,
  "request_id": "example",
  "timestamp": 1794214800000,
  "message": "example",
  "errors": {}
}
```

**Response (429 Too Many Requests)**

A rate limit is reached; `Retry-After` says how many seconds to wait.

```json
{
  "status": "failure",
  "status_code": 429,
  "data": null,
  "request_id": "example",
  "timestamp": 1794214800000,
  "message": "example",
  "errors": {}
}
```

**Response (503 Service Unavailable)**

The custom metrics store refused the write or did not answer; send the batch again shortly.

```json
{
  "status": "failure",
  "status_code": 503,
  "data": null,
  "request_id": "example",
  "timestamp": 1794214800000,
  "message": "example",
  "errors": {}
}
```

## List a source's metrics

GET/api/metric-sources/ingest

Returns the metrics of the token’s own metric source, deleted ones included, sorted by key, each with its type and status, and the organisation’s metric allowance and use. Read only: it does not count as a push.

### Returns

`200 OK`:

The source’s metrics and the organisation’s metric allowance.

The response panel's status selector shows each failure this operation can answer.

**GET /api/metric-sources/ingest**

curl:

```bash
curl https://api.watchtower15.com/api/metric-sources/ingest \
  -H "Authorization: Bearer $WT15_METRICS_TOKEN"
```

PHP:

```php
$wt15 = WatchTower15\Client::fromEnvironment();

$listing = $wt15->ingest->metrics->list();
```

Node.js:

```ts
const wt15 = WatchTower15.fromEnvironment();

const listing = await wt15.ingest.metrics.list();
```

Python:

```python
client = Client.from_environment()

listing = client.ingest.metrics.list()
```

**Response (200 OK)**

```json
{
  "status": "success",
  "status_code": 1,
  "data": {
    "metrics": [
      {
        "uuid": "75666699-b77d-4631-b576-041e8102a55b",
        "key": "example",
        "type": "example",
        "status": "example"
      }
    ],
    "limits": {
      "max_metrics": 1,
      "used": 1
    }
  },
  "request_id": "example",
  "timestamp": 1794214800000,
  "message": "example"
}
```

**Response (403 Forbidden)**

The token is missing, malformed, unknown, switched off or belongs to another kind of source. Every failure to authenticate is this one answer.

```json
{
  "status": "failure",
  "status_code": 403,
  "data": null,
  "request_id": "example",
  "timestamp": 1794214800000,
  "message": "example",
  "errors": {}
}
```

**Response (429 Too Many Requests)**

A rate limit is reached; `Retry-After` says how many seconds to wait.

```json
{
  "status": "failure",
  "status_code": 429,
  "data": null,
  "request_id": "example",
  "timestamp": 1794214800000,
  "message": "example",
  "errors": {}
}
```

Source: https://developer.watchtower15.com/ingest/reference/metrics/
