Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): add getTunneltime to manager metrics #1581

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/shadowbox/server/manager_metrics.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

import {PrometheusManagerMetrics} from './manager_metrics';
import {FakePrometheusClient} from './mocks/mocks';
import {FakePrometheusClient, FakeTunnelTimePrometheusClient} from './mocks/mocks';

describe('PrometheusManagerMetrics', () => {
it('getOutboundByteTransfer', async (done) => {
Expand All @@ -27,4 +27,16 @@ describe('PrometheusManagerMetrics', () => {
expect(bytesTransferredByUserId['access-key-2']).toEqual(10000);
done();
});

it('getTunnelTimeByLocation', async (done) => {
daniellacosse marked this conversation as resolved.
Show resolved Hide resolved
const managerMetrics = new PrometheusManagerMetrics(
new FakeTunnelTimePrometheusClient({US: 1000, CA: 2000})
);
const tunnelTime = await managerMetrics.getTunnelTimeByLocation({hours: 0});
expect(tunnelTime).toEqual([
{location: 'US', asn: undefined, tunnel_time: {hours: 0.28}},
{location: 'CA', asn: undefined, tunnel_time: {hours: 0.56}},
]);
done();
});
});
29 changes: 29 additions & 0 deletions src/shadowbox/server/manager_metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,23 @@
import {PrometheusClient} from '../infrastructure/prometheus_scraper';
import {DataUsageByUser, DataUsageTimeframe} from '../model/metrics';

export type TunnelTimeDimension = 'access_key' | 'country' | 'asn';

interface TunnelTimeRequest {
hours: number;
daniellacosse marked this conversation as resolved.
Show resolved Hide resolved
}

interface TunnelTimeResponse {
location?: string;
asn?: number;
daniellacosse marked this conversation as resolved.
Show resolved Hide resolved
tunnel_time: {
hours: number;
daniellacosse marked this conversation as resolved.
Show resolved Hide resolved
};
}

export interface ManagerMetrics {
getOutboundByteTransfer(timeframe: DataUsageTimeframe): Promise<DataUsageByUser>;
getTunnelTimeByLocation(request: TunnelTimeRequest): Promise<TunnelTimeResponse[]>;
}

// Reads manager metrics from a Prometheus instance.
Expand All @@ -40,4 +55,18 @@ export class PrometheusManagerMetrics implements ManagerMetrics {
}
return {bytesTransferredByUserId: usage};
}

async getTunnelTimeByLocation({hours}: TunnelTimeRequest): Promise<TunnelTimeResponse[]> {
const {result} = await this.prometheusClient.query(
`sum(increase(shadowsocks_tunnel_time_seconds_per_location[${hours}h])) by (location, asn, asorg)`
);

return result.map((entry) => ({
location: entry.metric['location'],
asn: entry.metric['asn'] !== undefined ? parseInt(entry.metric['asn'], 10) : undefined,
tunnel_time: {
hours: Number((parseFloat(entry.value[1]) / 60 / 60).toFixed(2)),
},
}));
}
}
24 changes: 24 additions & 0 deletions src/shadowbox/server/manager_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import * as errors from '../model/errors';
import * as version from './version';

import type {TunnelTimeDimension} from './manager_metrics';

Check warning on line 27 in src/shadowbox/server/manager_service.ts

View workflow job for this annotation

GitHub Actions / Lint

'TunnelTimeDimension' is defined but never used
daniellacosse marked this conversation as resolved.
Show resolved Hide resolved
import {ManagerMetrics} from './manager_metrics';
import {ServerConfigJson} from './server_config';
import {SharedMetricsPublisher} from './shared_metrics';
Expand Down Expand Up @@ -75,10 +76,16 @@
// method: string
[param: string]: unknown;
}

interface RequestQuery {
[param: string]: unknown;
}

// Simplified request and response type interfaces containing only the
// properties we actually use, to make testing easier.
interface RequestType {
params: RequestParams;
query?: RequestQuery;
daniellacosse marked this conversation as resolved.
Show resolved Hide resolved
}
interface ResponseType {
send(code: number, data?: {}): void;
Expand Down Expand Up @@ -156,6 +163,10 @@
);

apiServer.get(`${apiPrefix}/metrics/transfer`, service.getDataUsage.bind(service));
apiServer.get(
`${apiPrefix}/metrics/tunnel/location`,
daniellacosse marked this conversation as resolved.
Show resolved Hide resolved
service.getTunnelTimeByLocation.bind(service)
);
apiServer.get(`${apiPrefix}/metrics/enabled`, service.getShareMetrics.bind(service));
apiServer.put(`${apiPrefix}/metrics/enabled`, service.setShareMetrics.bind(service));

Expand Down Expand Up @@ -599,6 +610,19 @@
}
}

async getTunnelTimeByLocation(req: RequestType, res: ResponseType, next: restify.Next) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename

try {
logging.debug(`getTunnelTime request ${JSON.stringify(req.query)}`);
const response = await this.managerMetrics.getTunnelTimeByLocation({hours: 30 * 24});
res.send(HttpSuccess.OK, response);
logging.debug(`getTunnelTime response ${JSON.stringify(response)}`);
return next();
} catch (error) {
logging.error(error);
return next(new restifyErrors.InternalServerError());
}
}

getShareMetrics(req: RequestType, res: ResponseType, next: restify.Next): void {
logging.debug(`getShareMetrics request ${JSON.stringify(req.params)}`);
const response = {metricsEnabled: this.metricsPublisher.isSharingEnabled()};
Expand Down
18 changes: 18 additions & 0 deletions src/shadowbox/server/mocks/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,21 @@ export class FakePrometheusClient extends PrometheusClient {
return queryResultData;
}
}

export class FakeTunnelTimePrometheusClient extends PrometheusClient {
daniellacosse marked this conversation as resolved.
Show resolved Hide resolved
constructor(public tunnelTimeByLocation: {[location: string]: number}) {
super('');
}

async query(_query: string): Promise<QueryResultData> {
const queryResultData = {result: []} as QueryResultData;
for (const location of Object.keys(this.tunnelTimeByLocation)) {
const tunnelTime = this.tunnelTimeByLocation[location] || 0;
queryResultData.result.push({
metric: {location},
value: [tunnelTime, `${tunnelTime}`],
});
}
return queryResultData;
}
}
Loading