| 1 | WRStats is a library for production-compatible metric storage and retrieval. |
| 2 | |
| 3 | ## Data model |
| 4 | |
| 5 | Currently, only counters are supported, in the RRDTool sense of an |
| 6 | increment-only value with some support for calculating derivatives (rates). |
| 7 | |
| 8 | Memcached is the intended data store. Since memcached does not support |
| 9 | floating-point increment, metrics can be transparently scaled by a resolution |
| 10 | factor. |
| 11 | |
| 12 | Each metric can contain multiple time-series sequences. A sequence is a set of |
| 13 | counter values, equally spaced in time, with a fixed retention period. So for |
| 14 | example, a metric might contain a sequence with a value for each second, |
| 15 | expiring after one minute, and a sequence with a value for each minute, |
| 16 | expiring after one hour. |
| 17 | |
| 18 | To read a rate, a sequence is selected, then all buckets in the requested time |
| 19 | range are fetched. When the boundary of the time range does not exactly |
| 20 | coincide with the boundary of a bucket, interpolation is applied, to scale down |
| 21 | the counter value from the bucket. |
| 22 | |
| 23 | Interpolation assumes that the rate of events in the time window is constant. |
| 24 | If the end of the bucket would be after the current time, the current time is |
| 25 | used as the bucket end time for interpolation purposes. In other words, the |
| 26 | number of events in the future is assumed to be zero; all events are assumed to |
| 27 | be in the past. |
| 28 | |
| 29 | ## Storage keys |
| 30 | |
| 31 | Storage keys are composed of an array of components, conventionally joined by |
| 32 | the storage class into a string key separated by colons: |
| 33 | |
| 34 | <prefix> : <metric name> : <sequence name> : <time> [ : <entity key> ] |
| 35 | |
| 36 | * The prefix is a string or array of strings which are fixed for a given |
| 37 | factory. The prefix is supposed to identify the caller. |
| 38 | * The metric name identifies the metric in the configuration. |
| 39 | * The sequence name identifies the sequence. If there is only one unnamed |
| 40 | sequence, its name is the empty string. Subsequent sequences will be named |
| 41 | after their key in the configuration array. An explicit sequence name can be |
| 42 | given if desired for stability. |
| 43 | * The time bucket is identified by an integer. |
| 44 | * Additional key components can be supplied. This "entity key" is used to |
| 45 | distinguish different instances of a metric which share the same |
| 46 | configuration. For example, if you want to count the number of edits by |
| 47 | each user, the entity key could include the user ID. |
| 48 | |
| 49 | Entity keys may either be local (LocalEntityKey) or global (GlobalEntityKey). |
| 50 | This flag is passed down to the storage class. If a local entity key is |
| 51 | supplied, MediaWiki will prefix the key with the name of the wiki. |
| 52 | |
| 53 | ## Usage |
| 54 | |
| 55 | ```php |
| 56 | $specs = [ |
| 57 | 'a' => [ |
| 58 | 'type' => 'counter', |
| 59 | 'sequences' => [ [ |
| 60 | 'timeStep' => 10, |
| 61 | 'expiry' => 3600, |
| 62 | ] ], |
| 63 | 'resolution' => 1, |
| 64 | ], |
| 65 | 'b' => [ |
| 66 | 'type' => 'counter', |
| 67 | 'resolution' => 0.01, |
| 68 | 'sequences' => [ [ |
| 69 | 'timeStep' => 1, |
| 70 | 'expiry' => 60, |
| 71 | ] ] |
| 72 | ], |
| 73 | ]; |
| 74 | $entity = new LocalEntityKey( [ 'user_id' => 1 ] ); |
| 75 | |
| 76 | $wrstatsFactory = MediaWikiServices::getInstance()->getWRStatsFactory(); |
| 77 | $writer = $wrstatsFactory->createWriter( $specs ); |
| 78 | $writer->incr( 'a', $entity, 1 ); |
| 79 | $writer->incr( 'b', $entity, 25 ); |
| 80 | $writer->flush(); |
| 81 | |
| 82 | $reader = $wrstatsFactory->createReader( $specs ); |
| 83 | $rateA = $reader->getRate( 'a', $entity, $reader->latest( 60 ) ); |
| 84 | $rateB = $reader->getRate( 'b', $entity, $reader->latest( 120 ) ); |
| 85 | print $rateA->perSecond(); |
| 86 | print $rateB->perMinute(); |
| 87 | ``` |
| 88 | |
| 89 | ## Rate limiter |
| 90 | |
| 91 | WRStatsRateLimiter uses underlying WRStats metrics to implement rate limiting |
| 92 | functionality. The rate limiter allows you to limit the number of actions in a |
| 93 | rolling time window. |
| 94 | |
| 95 | WRStats does not require a synchronous data store, so the counter may overshoot |
| 96 | its limit slightly before actions are prevented. |
| 97 | |
| 98 | Typical usage: |
| 99 | |
| 100 | ```php |
| 101 | $wrstatsFactory = MediaWikiServices::getInstance()->getWRStatsFactory(); |
| 102 | $rateLimiter = $wrstatsFactory->createRateLimiter( |
| 103 | 'by_user' => [ |
| 104 | new LimitCondition( 10, 60 ) // 10 edits per minute |
| 105 | ], |
| 106 | 'MyLimitCaller' |
| 107 | ); |
| 108 | $entity = new LocalEntityKey( [ 'user_id', 1 ] ); |
| 109 | |
| 110 | if ( $rateLimiter->tryIncr( 'by_user', $entity )->isAllowed() ) { |
| 111 | // perform the action |
| 112 | } |
| 113 | ``` |