Metrics

Graph & store analytics from your Repeat

The metrics API allows you to read & write analytics that you can later view in your Repeat’s analytics dashboard, including filtering by time window and displaying graphs using avg, sum, min, max and count.

Signature

metrics: {
  write(
		name: string,
		value: number,
		label?: string
	): void;
  async getAll(
		view?: '1h'|'1d'|'7d'|'30d',
		label?: string
	): Promise<Record<MetricName, Record<MetricLabel, { t: string; avg: number; sum: number; count: number; }[]>>>;
  async get(
		name: string,
		view?: '1h'|'1d'|'7d'|'30d',
		label?: string
	): Promise<Record<MetricLabel, { t: string; avg: number; sum: number; count: number; }[]>>;
};

Methods

  • writevoid
    • The write method allows you to write to your Repeat’s analytics by providing a name, value and optional label.

  • getAllPromise<...>
    • The getAll method allows you to retrieve all of the metrics stored for your Repeat within the given time window or label.

  • getPromise<...>
    • The get method allows you to retrieve a specific metric by it’s name within the given time window or label.

       

Examples

Writing metrics

export default {
	async webhook(request: Request, env: Repeat.Env) {
		env.metrics.write('foo', 1);
		env.metrics.write('bar', 2);
	},
};

Retrieving metrics

export default {
	async webhook(request: Request, env: Repeat.Env) {
		env.metrics.write('foo', 1);
		env.metrics.write('bar', 2);

		console.log(JSON.stringify(await env.metrics.getAll(), null, 2));
    // "{
    //   "bar": {
    //     "": [
    //       {
    //         "t": "1668170700000",
    //         "count": "5",
    //         "avg": 2,
    //         "sum": 10,
    //         "min": 2,
    //         "max": 2
    //       }
    //     ]
    //   },
    //   "foo": {
    //     "": [
    //       {
    //         "t": "1668170700000",
    //         "count": "5",
    //         "avg": 1,
    //         "sum": 5,
    //         "min": 1,
    //         "max": 1
    //       }
    //     ]
    //   }
    // }"

		console.log(JSON.stringify(await env.metrics.get('foo'), null, 2));
    // "{
    //   "": [
    //     {
    //       "t": "1668170700000",
    //       "count": "5",
    //       "avg": 1,
    //       "sum": 5,
    //       "min": 1,
    //       "max": 1
    //     }
    //   ]
    // }"
	},
};

Last updated on November 11, 2022