-
Notifications
You must be signed in to change notification settings - Fork 807
/
Copy pathconnection_metrics.ts
165 lines (146 loc) · 5.17 KB
/
connection_metrics.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2020 The Outline Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import {Table} from '@google-cloud/bigquery';
import {InsertableTable} from './infrastructure/table';
import {
HourlyConnectionMetricsReport,
HourlyUserConnectionMetricsReport,
HourlyUserConnectionMetricsReportByLocation,
} from './model';
const TERABYTE = Math.pow(2, 40);
export interface ConnectionRow {
serverId: string;
startTimestamp: string; // ISO formatted string.
endTimestamp: string; // ISO formatted string.
bytesTransferred: number;
tunnelTimeSec?: number;
countries?: string[];
asn?: number;
}
export class BigQueryConnectionsTable implements InsertableTable<ConnectionRow> {
constructor(private bigqueryTable: Table) {}
async insert(rows: ConnectionRow[]): Promise<void> {
await this.bigqueryTable.insert(rows);
}
}
export function postConnectionMetrics(
table: InsertableTable<ConnectionRow>,
report: HourlyConnectionMetricsReport
): Promise<void> {
return table.insert(getConnectionRowsFromReport(report));
}
function getConnectionRowsFromReport(report: HourlyConnectionMetricsReport): ConnectionRow[] {
const startTimestampStr = new Date(report.startUtcMs).toISOString();
const endTimestampStr = new Date(report.endUtcMs).toISOString();
const rows = [];
for (const userReport of report.userReports) {
// User reports come in 2 flavors: "per location" and "per key". We no longer store the
// "per key" reports.
if (isPerLocationUserReport(userReport)) {
rows.push({
serverId: report.serverId,
startTimestamp: startTimestampStr,
endTimestamp: endTimestampStr,
bytesTransferred: userReport.bytesTransferred,
tunnelTimeSec: userReport.tunnelTimeSec || undefined,
countries: userReport.countries,
asn: userReport.asn || undefined,
});
}
}
return rows;
}
function isPerLocationUserReport(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
userReport: HourlyUserConnectionMetricsReport
): userReport is HourlyUserConnectionMetricsReportByLocation {
return 'countries' in userReport;
}
// Returns true iff testObject contains a valid HourlyConnectionMetricsReport.
export function isValidConnectionMetricsReport(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
testObject: any
): testObject is HourlyConnectionMetricsReport {
if (!testObject) {
console.debug('Missing test object');
return false;
}
const requiredConnectionMetricsFields = ['serverId', 'startUtcMs', 'endUtcMs', 'userReports'];
for (const fieldName of requiredConnectionMetricsFields) {
if (!testObject[fieldName]) {
console.debug(`Missing required field \`${fieldName}\``);
return false;
}
}
if (typeof testObject.serverId !== 'string') {
console.debug('Invalid `serverId`');
return false;
}
if (
typeof testObject.startUtcMs !== 'number' ||
typeof testObject.endUtcMs !== 'number' ||
testObject.startUtcMs >= testObject.endUtcMs
) {
console.debug('Invalid `startUtcMs` and/or `endUtcMs`');
return false;
}
if (testObject.userReports.length === 0) {
console.debug('At least 1 user report must be provided');
return false;
}
for (const userReport of testObject.userReports) {
if (userReport.userId && typeof userReport.userId !== 'string') {
console.debug('Invalid `serverId`');
return false;
}
// We used to set a limit of 1TB per access key, then per location. We later
// realized that a server may use a single key, or all the traffic may come
// from a single location.
// However, as we report hourly, it's unlikely we hit 1TB, so we keep the
// check for now to try and prevent malicious reports.
if (
typeof userReport.bytesTransferred !== 'number' ||
userReport.bytesTransferred < 0 ||
userReport.bytesTransferred > TERABYTE
) {
console.debug('Invalid `bytesTransferred`');
return false;
}
if (
userReport.tunnelTimeSec &&
(typeof userReport.tunnelTimeSec !== 'number' || userReport.tunnelTimeSec < 0)
) {
console.debug('Invalid `tunnelTimeSec`');
return false;
}
if (userReport.countries) {
if (!Array.isArray(userReport.countries)) {
console.debug('Invalid `countries`');
return false;
}
for (const country of userReport.countries) {
if (typeof country !== 'string') {
console.debug('Invalid `countries`');
return false;
}
}
}
if (userReport.asn && typeof userReport.asn !== 'number') {
console.debug('Invalid `asn`');
return false;
}
}
// Request is a valid HourlyConnectionMetricsReport.
return true;
}