1 | # `@shopify/koa-metrics`
|
2 |
|
3 | [![Build Status](https://github.com/Shopify/quilt/workflows/Node-CI/badge.svg?branch=main)](https://github.com/Shopify/quilt/actions?query=workflow%3ANode-CI)
|
4 | [![Build Status](https://github.com/Shopify/quilt/workflows/Ruby-CI/badge.svg?branch=main)](https://github.com/Shopify/quilt/actions?query=workflow%3ARuby-CI)
|
5 | [![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](LICENSE.md) [![npm version](https://badge.fury.io/js/%40shopify%2Fkoa-metrics.svg)](https://badge.fury.io/js/%40shopify%2Fkoa-metrics.svg)
|
6 |
|
7 | Opinionated performance metric tracking for Koa, implemented with DogStatsD.
|
8 |
|
9 | ## Installation
|
10 |
|
11 | ```bash
|
12 | $ yarn add @shopify/koa-metrics
|
13 | ```
|
14 |
|
15 | ## Usage
|
16 |
|
17 | ```javascript
|
18 | import Koa from 'koa';
|
19 | import metrics from '@shopify/koa-metrics';
|
20 |
|
21 | const app = new Koa();
|
22 |
|
23 | app.use(
|
24 | metrics({
|
25 | prefix: 'AppName',
|
26 | host: 'some-statsd-host.com:8125',
|
27 | }),
|
28 | );
|
29 | ```
|
30 |
|
31 | ### Options
|
32 |
|
33 | The passed in `options` object adheres to the following API:
|
34 |
|
35 | #### `prefix` (required)
|
36 |
|
37 | The global StatsD metric name prefix; should be provided in `PascalCase`.
|
38 |
|
39 | #### `host` (required)
|
40 |
|
41 | The url for the StatsD host; should be provided in the format: `hostname:port`.
|
42 |
|
43 | ## API
|
44 |
|
45 | An instance of the `Metrics` object will be available on `ctx.metrics` further down in the middleware stack.
|
46 |
|
47 | ### `Metrics`
|
48 |
|
49 | #### `.distribution(name: string, value: number, sampleRate?: number, tags?: Tags)`
|
50 |
|
51 | Sends a distribution command with the specified `value` in milliseconds.
|
52 |
|
53 | #### `.initTimer(): Timer`
|
54 |
|
55 | Returns a new `Timer` started at the current `process.hrtime()`
|
56 |
|
57 | ### `Timer`
|
58 |
|
59 | #### `.stop(): number`
|
60 |
|
61 | Returns the time, in milliseconds, since the `Timer` was created.
|
62 |
|
63 | ### `Tags`
|
64 |
|
65 | tags are an object keyed by the name of the corresponding tag. For example:
|
66 |
|
67 | ```
|
68 | {
|
69 | name: value,
|
70 | name2: value2
|
71 | }
|
72 | ```
|
73 |
|
74 | ## Intelligent Defaults
|
75 |
|
76 | The global metric name prefix is provided through the [options object](#options).
|
77 |
|
78 | This package automatically provides performance metrics for HTTP requests.
|
79 |
|
80 | ### Standard tags
|
81 |
|
82 | - `path`
|
83 | - `request_method`
|
84 | - `response_code`
|
85 | - `response_type` (eg. `2xx`, `3xx`, ...)
|
86 |
|
87 | ### Default Metrics
|
88 |
|
89 | #### `request_time`
|
90 |
|
91 | Time to complete a request, from the application perspective.
|
92 |
|
93 | #### `request_queuing_time`
|
94 |
|
95 | Time before a request actually started being processed.
|
96 |
|
97 | This metric is emitted when the application start processing a request. It relies on the presence of a header `X-Request-Start` set by the first HTTP hop.
|
98 |
|
99 | #### `request_content_length`
|
100 |
|
101 | This metric is based on the response header `Content-Length`. Some responses don't provide this header (chunked encoding); in those cases, this will not be reported.
|