|
13 | 13 | // limitations under the License.
|
14 | 14 |
|
15 | 15 | import {PrometheusManagerMetrics} from './manager_metrics';
|
| 16 | +import {PrometheusClient, QueryResultData} from '../infrastructure/prometheus_scraper'; |
16 | 17 | import {FakePrometheusClient} from './mocks/mocks';
|
17 | 18 |
|
| 19 | +export class QueryMapPrometheusClient implements PrometheusClient { |
| 20 | + constructor(private queryMap: {[query: string]: QueryResultData}) {} |
| 21 | + |
| 22 | + async query(_query: string): Promise<QueryResultData> { |
| 23 | + return this.queryMap[_query]; |
| 24 | + } |
| 25 | +} |
| 26 | + |
18 | 27 | describe('PrometheusManagerMetrics', () => {
|
| 28 | + it('getServerMetrics', async (done) => { |
| 29 | + const managerMetrics = new PrometheusManagerMetrics( |
| 30 | + new QueryMapPrometheusClient({ |
| 31 | + 'sum(increase(shadowsocks_data_bytes_per_location{dir=~"c<p|p>t"}[0s])) by (location, asn, asorg)': |
| 32 | + { |
| 33 | + resultType: 'vector', |
| 34 | + result: [ |
| 35 | + { |
| 36 | + metric: { |
| 37 | + location: 'US', |
| 38 | + asn: '49490', |
| 39 | + asorg: null, |
| 40 | + }, |
| 41 | + value: [null, '1000'], |
| 42 | + }, |
| 43 | + ], |
| 44 | + }, |
| 45 | + 'sum(increase(shadowsocks_tunnel_time_seconds_per_location[0s])) by (location, asn, asorg)': |
| 46 | + { |
| 47 | + resultType: 'vector', |
| 48 | + result: [ |
| 49 | + { |
| 50 | + metric: { |
| 51 | + location: 'US', |
| 52 | + asn: '49490', |
| 53 | + asorg: null, |
| 54 | + }, |
| 55 | + value: [null, '1000'], |
| 56 | + }, |
| 57 | + ], |
| 58 | + }, |
| 59 | + 'sum(increase(shadowsocks_data_bytes{dir=~"c<p|p>t"}[0s])) by (access_key)': { |
| 60 | + resultType: 'vector', |
| 61 | + result: [ |
| 62 | + { |
| 63 | + metric: { |
| 64 | + access_key: '0', |
| 65 | + }, |
| 66 | + value: [null, '1000'], |
| 67 | + }, |
| 68 | + ], |
| 69 | + }, |
| 70 | + 'sum(increase(shadowsocks_tunnel_time_seconds[0s])) by (access_key)': { |
| 71 | + resultType: 'vector', |
| 72 | + result: [ |
| 73 | + { |
| 74 | + metric: { |
| 75 | + access_key: '0', |
| 76 | + }, |
| 77 | + value: [null, '1000'], |
| 78 | + }, |
| 79 | + ], |
| 80 | + }, |
| 81 | + }) |
| 82 | + ); |
| 83 | + |
| 84 | + const serverMetrics = await managerMetrics.getServerMetrics({seconds: 0}); |
| 85 | + |
| 86 | + expect(JSON.stringify(serverMetrics, null, 2)).toEqual(`{ |
| 87 | + "server": [ |
| 88 | + { |
| 89 | + "location": "US", |
| 90 | + "asn": 49490, |
| 91 | + "asOrg": "null", |
| 92 | + "tunnelTime": { |
| 93 | + "seconds": 1000 |
| 94 | + }, |
| 95 | + "dataTransferred": { |
| 96 | + "bytes": 1000 |
| 97 | + } |
| 98 | + } |
| 99 | + ], |
| 100 | + "accessKeys": [ |
| 101 | + { |
| 102 | + "accessKeyId": 0, |
| 103 | + "tunnelTime": { |
| 104 | + "seconds": 1000 |
| 105 | + }, |
| 106 | + "dataTransferred": { |
| 107 | + "bytes": 1000 |
| 108 | + } |
| 109 | + } |
| 110 | + ] |
| 111 | +}`); |
| 112 | + done(); |
| 113 | + }); |
| 114 | + |
| 115 | + it('getServerMetrics - does a full outer join on metric data', async (done) => { |
| 116 | + const managerMetrics = new PrometheusManagerMetrics( |
| 117 | + new QueryMapPrometheusClient({ |
| 118 | + 'sum(increase(shadowsocks_data_bytes_per_location{dir=~"c<p|p>t"}[0s])) by (location, asn, asorg)': |
| 119 | + { |
| 120 | + resultType: 'vector', |
| 121 | + result: [ |
| 122 | + { |
| 123 | + metric: { |
| 124 | + location: 'US', |
| 125 | + asn: '49490', |
| 126 | + asorg: null, |
| 127 | + }, |
| 128 | + value: [null, '1000'], |
| 129 | + }, |
| 130 | + ], |
| 131 | + }, |
| 132 | + 'sum(increase(shadowsocks_tunnel_time_seconds_per_location[0s])) by (location, asn, asorg)': |
| 133 | + { |
| 134 | + resultType: 'vector', |
| 135 | + result: [ |
| 136 | + { |
| 137 | + metric: { |
| 138 | + location: 'CA', |
| 139 | + asn: '53520', |
| 140 | + asorg: null, |
| 141 | + }, |
| 142 | + value: [null, '1000'], |
| 143 | + }, |
| 144 | + ], |
| 145 | + }, |
| 146 | + 'sum(increase(shadowsocks_data_bytes{dir=~"c<p|p>t"}[0s])) by (access_key)': { |
| 147 | + resultType: 'vector', |
| 148 | + result: [ |
| 149 | + { |
| 150 | + metric: { |
| 151 | + access_key: '0', |
| 152 | + }, |
| 153 | + value: [null, '1000'], |
| 154 | + }, |
| 155 | + ], |
| 156 | + }, |
| 157 | + 'sum(increase(shadowsocks_tunnel_time_seconds[0s])) by (access_key)': { |
| 158 | + resultType: 'vector', |
| 159 | + result: [ |
| 160 | + { |
| 161 | + metric: { |
| 162 | + access_key: '1', |
| 163 | + }, |
| 164 | + value: [null, '1000'], |
| 165 | + }, |
| 166 | + ], |
| 167 | + }, |
| 168 | + }) |
| 169 | + ); |
| 170 | + |
| 171 | + const serverMetrics = await managerMetrics.getServerMetrics({seconds: 0}); |
| 172 | + |
| 173 | + expect(JSON.stringify(serverMetrics, null, 2)).toEqual(`{ |
| 174 | + "server": [ |
| 175 | + { |
| 176 | + "location": "CA", |
| 177 | + "asn": 53520, |
| 178 | + "asOrg": "null", |
| 179 | + "tunnelTime": { |
| 180 | + "seconds": 1000 |
| 181 | + } |
| 182 | + }, |
| 183 | + { |
| 184 | + "location": "US", |
| 185 | + "asn": 49490, |
| 186 | + "asOrg": "null", |
| 187 | + "dataTransferred": { |
| 188 | + "bytes": 1000 |
| 189 | + } |
| 190 | + } |
| 191 | + ], |
| 192 | + "accessKeys": [ |
| 193 | + { |
| 194 | + "accessKeyId": 1, |
| 195 | + "tunnelTime": { |
| 196 | + "seconds": 1000 |
| 197 | + } |
| 198 | + }, |
| 199 | + { |
| 200 | + "accessKeyId": 0, |
| 201 | + "dataTransferred": { |
| 202 | + "bytes": 1000 |
| 203 | + } |
| 204 | + } |
| 205 | + ] |
| 206 | +}`); |
| 207 | + done(); |
| 208 | + }); |
| 209 | + |
19 | 210 | it('getOutboundByteTransfer', async (done) => {
|
20 | 211 | const managerMetrics = new PrometheusManagerMetrics(
|
21 | 212 | new FakePrometheusClient({'access-key-1': 1000, 'access-key-2': 10000})
|
|
0 commit comments