Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import {TSDB} from '../dataSource/tsdb';
import * as _ from 'lodash';
import {MetricsManager} from '../manager/metricsManager';
import {ApplicationService} from './applicationService';
import {IMetricsService} from '../../interface/services/IMetricsService';
const BUILT_IN_PREFIXS = ['system.', 'node.', 'error.', 'middleware.'];
const BUILD_IN_REG = new RegExp(`^(${BUILT_IN_PREFIXS.join('|')})`);
@provide('metricsService')
export class MetricsService implements IMetricsService {
@inject('tsdb')
protected tsdb: TSDB;
@inject('metricsManager')
protected metricsManager: MetricsManager;
@inject('applicationService')
protected applicationService: ApplicationService;
async getMetricsNames(options: ComplexSelector & AppSelector & TimeWindowOptions): Promise {
return this.metricsManager.getMetricsNames(options);
}
async getCustomMetricsNames(options: ComplexSelector & AppSelector & TimeWindowOptions): Promise {
const allMetricsName = await this.metricsManager.getMetricsNames(options);
return allMetricsName.filter((name) => !BUILD_IN_REG.test(name));
}
async queryMetricsLatest(options: ComplexSelector & MetricsNamesSelector & AppSelector): Promise {
import { provide, inject } from 'midway-web';
import { ComplexSelector, MetricsNamesSelector, IndicatorResult, TimeWindowOptions,
AppSelector } from '../../interface/services/common';
import {TSDB} from '../dataSource/tsdb';
import * as _ from 'lodash';
import {MetricsManager} from '../manager/metricsManager';
import {ApplicationService} from './applicationService';
import {IMetricsService} from '../../interface/services/IMetricsService';
const BUILT_IN_PREFIXS = ['system.', 'node.', 'error.', 'middleware.'];
const BUILD_IN_REG = new RegExp(`^(${BUILT_IN_PREFIXS.join('|')})`);
@provide('metricsService')
export class MetricsService implements IMetricsService {
@inject('tsdb')
protected tsdb: TSDB;
@inject('metricsManager')
protected metricsManager: MetricsManager;
@inject('applicationService')
protected applicationService: ApplicationService;
async getMetricsNames(options: ComplexSelector & AppSelector & TimeWindowOptions): Promise {
return this.metricsManager.getMetricsNames(options);
}
async getCustomMetricsNames(options: ComplexSelector & AppSelector & TimeWindowOptions): Promise {
const allMetricsName = await this.metricsManager.getMetricsNames(options);
return allMetricsName.filter((name) => !BUILD_IN_REG.test(name));
}
import { get, inject } from 'midway-web';
import { IRemoteDebugService } from '../../interface/services/IRemoteDebugService';
import { IPrivilegeAdapter } from '../../interface/adapter/IPrivilegeAdapter';
export class RemoteDebugCtrl {
@inject('remoteDebugService')
remoteDebugService: IRemoteDebugService;
@inject('privilegeAdapter')
protected privilegeAdapter: IPrivilegeAdapter;
@get('/getDebuggableHost')
async getDebuggableHost(ctx) {
const query = ctx.query;
const { scope, scopeName, env, ip, hostname } = query;
const uid: string = ctx.uid;
const hasPermission = await this.privilegeAdapter.isAppOps(scope, scopeName, uid);
if (!hasPermission) {
throw new Error('You have no permission to reach this');
}
const data = await this.remoteDebugService.getDebuggableHost({
scope, scopeName, env, ip, hostname, uid,
});
ctx.body = {
import { provide, inject } from 'midway-web';
import * as _ from 'lodash';
import {AppSelector, IndicatorResult, MetricNameJSON, TimeWindowOptions} from '../../interface/services/common';
import {TSDB} from '../dataSource/tsdb';
import {ISadMetricsAdapter} from '../../interface/adapter/ISadMetricsAdapter';
@provide('metricsManager')
export class MetricsManager {
@inject('tsdb')
protected tsdb: TSDB;
@inject('sadMetricsAdapter')
protected sadMetricsAdapter: ISadMetricsAdapter;
static pickLatestDp (dps) {
if (_.isEmpty(dps)) {
return null;
}
let times = [];
for (const key of Object.keys(dps)) {
times.push(parseInt(key, 10));
}
times = times.sort((a, b) => b - a);
const latest = dps[times[0]];
return latest;
import { provide, inject } from 'midway-web';
import * as _ from 'lodash';
import {AppSelector, IndicatorResult, MetricNameJSON, TimeWindowOptions} from '../../interface/services/common';
import {TSDB} from '../dataSource/tsdb';
import {ISadMetricsAdapter} from '../../interface/adapter/ISadMetricsAdapter';
@provide('metricsManager')
export class MetricsManager {
@inject('tsdb')
protected tsdb: TSDB;
@inject('sadMetricsAdapter')
protected sadMetricsAdapter: ISadMetricsAdapter;
static pickLatestDp (dps) {
if (_.isEmpty(dps)) {
return null;
}
let times = [];
for (const key of Object.keys(dps)) {
times.push(parseInt(key, 10));
}
times = times.sort((a, b) => b - a);
const latest = dps[times[0]];
return latest;
}
getMetricsNames(options: AppSelector & TimeWindowOptions): Promise {
import { inject, config } from 'midway-web';
import {AppSelector, HostSelector} from '../../interface/services/common';
import {IPandoraAdapter} from '../../interface/adapter/IPandoraAdapter';
import {IRemoteExecuteAdapter} from '../../interface/adapter/IRemoteExecuteAdapter';
import {parse as urlparse} from 'url';
export class PandoraAdapter implements IPandoraAdapter {
@inject('remoteExecuteAdapter')
protected remoteExecuteAdapter: IRemoteExecuteAdapter;
@config('pandora')
protected config;
async invokeRestful(host: HostSelector, url: string, options?: any) {
const pandoraRestfulPort = this.config.restfulPort;
url = url.replace(/^\//, '');
const cmd = `curl http://127.0.0.1:${pandoraRestfulPort}/${url} 2>/dev/null`;
const res = await this.remoteExecuteAdapter.exec(host, cmd, options).catch((err) => {
if (err.message === 'CALL_ERROR: exit 7') {
err.message = '请求 Pandora 失败,请确保 pandora agent 已正常启动';
}
throw err;
});
return JSON.parse(res);
import * as Interface from '../../interface/services/IErrorService';
import * as moment from 'moment';
import * as _ from 'lodash';
import { inject, get, logger } from 'midway-web';
export class ErrorCtrl {
@logger()
logger;
@inject('errorService')
errorServ;
@get('/queryErrors')
async queryErrors(ctx) {
const startTime: moment.Moment = moment(parseInt(ctx.request.query.startTime, 10));
const endTime: moment.Moment = moment(parseInt(ctx.request.query.endTime, 10));
const ago: moment.Moment = moment().subtract(15, 'minutes');
const _s: moment.Moment = !startTime.isValid() ? ago : startTime;
const _e: moment.Moment = !endTime.isValid() ? moment() : endTime;
const scope: string = _.trim(ctx.request.query.scope || '');
const scopeName: string = _.trim(ctx.request.query.scopeName || '');
const env: string = _.trim(ctx.request.query.env || '');
const logPath: string = _.trim(ctx.request.query.logPath || '');
const ip: string = _.trim(ctx.request.query.ip || '');
const keyword: string = _.trim(ctx.request.query.keyword) || '';
import { inject, get, controller, provide, priority } from 'midway-web';
import {MetricsUtils} from '../util/metricsUtils';
import {MetricNameJSON} from '../../interface/services/common';
@priority(0)
@provide()
@controller('/v2/api/metrics/')
export class MetricsCtrl {
@inject('metricsService')
private metricsService;
@get('/getMetricsNames')
async getMetricsNames(ctx) {
const query = ctx.query;
const {scope, scopeName, env} = query;
const data = await this.metricsService.getMetricsNames({
scope, scopeName, env,
});
ctx.body = {
success: true,
data,
};
}
import { get, inject } from 'midway-web';
import { IRemoteDebugService } from '../../interface/services/IRemoteDebugService';
import { IPrivilegeAdapter } from '../../interface/adapter/IPrivilegeAdapter';
export class RemoteDebugCtrl {
@inject('remoteDebugService')
remoteDebugService: IRemoteDebugService;
@inject('privilegeAdapter')
protected privilegeAdapter: IPrivilegeAdapter;
@get('/getDebuggableHost')
async getDebuggableHost(ctx) {
const query = ctx.query;
const { scope, scopeName, env, ip, hostname } = query;
const uid: string = ctx.uid;
const hasPermission = await this.privilegeAdapter.isAppOps(scope, scopeName, uid);
if (!hasPermission) {
throw new Error('You have no permission to reach this');
}
const data = await this.remoteDebugService.getDebuggableHost({
import { get, post, inject } from 'midway-web';
import {wrapJson} from '../../core/util/util';
export class TracingCtrl {
@inject('traceService')
traceService;
@get('/listFocusTraces')
async focusTraces(ctx) {
const query = ctx.query;
const result = await this.traceService.listFocusTraces(query);
ctx.body = wrapJson(result);
}
@get('/listTraces')
async traces(ctx) {
const query = ctx.query;
const result = await this.traceService.listTraces(query);
ctx.body = wrapJson(result);
}