Skip to content
This repository has been archived by the owner on Aug 25, 2021. It is now read-only.

Commit

Permalink
Merge pull request #272 from midwayjs/add_more_metrics
Browse files Browse the repository at this point in the history
Add more metrics
  • Loading branch information
czy88840616 authored Oct 23, 2018
2 parents d7ee046 + 30a50a1 commit 6e5c8b8
Show file tree
Hide file tree
Showing 17 changed files with 555 additions and 114 deletions.
4 changes: 2 additions & 2 deletions .githooks/pre-commit/filter.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/bin/sh
EMAIL=$(git config user.email)
if [[ $EMAIL == *"alibaba-inc"* ]]
if [[ $EMAIL == *"alibaba-inc"* ]] || [[ $EMAIL == *"taobao"* ]]
then
echo "email with *.alibaba-inc.com are not allowed";
echo "email with *.alibaba-inc.com or *.taobao.com are not allowed";
exit 1;
else
echo "";
Expand Down
58 changes: 47 additions & 11 deletions packages/hook/src/patch/HttpServer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

import { Patcher, getRandom64 } from 'pandora-metrics';
import { extractPath } from '../utils/Utils';
import { getRandom64, MetricLevel, MetricName, MetricsClientUtil, Patcher } from 'pandora-metrics';
import { extractPath } from '..';
import { HEADER_TRACE_ID } from '../utils/Constants';
import { parse as parseUrl } from 'url';
import { parse as parseQS, ParsedUrlQuery } from 'querystring';
Expand All @@ -14,6 +12,22 @@ export type bufferTransformer = (buffer, req?: IncomingMessage) => object | stri

export type requestFilter = (req) => boolean;


const MetricsStat = {
/** HTTP */
HTTP_REQUEST: 'middleware.http.request',

HTTP_REQUEST_PATH: 'middleware.http.request.path',

HTTP_GROUP: 'http',

HTTP_PATH: 'path',

HTTP_ERROR_CODE: 400,

HTTP_ILLEGAL_PATH: 'illegal_path'
};

export class HttpServerPatcher extends Patcher {

constructor(options?: {
Expand All @@ -37,7 +51,7 @@ export class HttpServerPatcher extends Patcher {
}

getTraceId(req) {
return req.headers[HEADER_TRACE_ID] || getRandom64();
return req.headers[ HEADER_TRACE_ID ] || getRandom64();
}

createSpan(tracer, tags) {
Expand Down Expand Up @@ -83,7 +97,8 @@ export class HttpServerPatcher extends Patcher {
return false;
}

_beforeExecute(tracer, req, res) {}
_beforeExecute(tracer, req, res) {
}

beforeFinish(span, res) {
span.setTag('http.status_code', {
Expand Down Expand Up @@ -124,7 +139,7 @@ export class HttpServerPatcher extends Patcher {
getFullUrl(req: IncomingMessage): string {
if (!req) return '';

const secure = (<any>req.connection).encrypted || req.headers['x-forwarded-proto'] === 'https';
const secure = (<any>req.connection).encrypted || req.headers[ 'x-forwarded-proto' ] === 'https';

return 'http' + (secure ? 's' : '') + '://' +
req.headers.host +
Expand All @@ -141,7 +156,7 @@ export class HttpServerPatcher extends Patcher {
return function wrappedCreateServer(this: any, requestListener) {
if (requestListener) {

const listener = traceManager.bind(function(req, res) {
const listener = traceManager.bind(function (req, res) {
const requestFilter = options.requestFilter || self.requestFilter;

if (requestFilter(req)) {
Expand Down Expand Up @@ -179,7 +194,7 @@ export class HttpServerPatcher extends Patcher {

return function wrappedRequestEmit(this: IncomingMessage, event) {
if (event === 'data') {
const chunk = arguments[1] || [];
const chunk = arguments[ 1 ] || [];

chunks.push(chunk);
}
Expand All @@ -189,7 +204,7 @@ export class HttpServerPatcher extends Patcher {
});
}

tracer.named(`HTTP-${tags['http.method'].value}:${tags['http.url'].value}`);
tracer.named(`HTTP-${tags[ 'http.method' ].value}:${tags[ 'http.url' ].value}`);
tracer.setCurrentSpan(span);

function onFinishedFactory(eventName) {
Expand Down Expand Up @@ -236,6 +251,27 @@ export class HttpServerPatcher extends Patcher {
}

afterFinish(span, res) {
// overwrite
this.reportMetrics({
rt: span.duration,
resultCode: res.statusCode
});
}

reportMetrics(ctx) {
let responseCode = ctx.resultCode;
if (!responseCode) {
return;
}

let global = new MetricName(MetricsStat.HTTP_REQUEST, {}, MetricLevel.NORMAL);
let client = MetricsClientUtil.getMetricsClient();
let globalCompass = client.getFastCompass(MetricsStat.HTTP_GROUP, global);

if (MetricsStat.HTTP_ERROR_CODE > parseInt(responseCode)) {
globalCompass.record(ctx.rt, 'success');
} else {
globalCompass.record(ctx.rt, 'error');
}

}
}
34 changes: 19 additions & 15 deletions packages/metrics/src/MetricsClient.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import {Metric, MetricName, MetricsRegistry, MetricType} from './common/index';
import {ProxyCreateMessage, ProxyUpdateMessage} from './domain';
import {MetricsConstants} from './MetricsConstants';
import {EnvironmentUtil, Environment} from 'pandora-env';
import {MetricsMessengerClient} from './util/MessengerUtil';
import {Proxiable, Gauge, Counter, Histogram, Meter, Timer} from './client/index';
import {AbstractIndicator} from './indicator/AbstractIndicator';
import {MetricSet} from './common';
import {IMetricsRegistry} from './common/MetricsRegistry';
import { IMetricsRegistry, Metric, MetricName, MetricSet, MetricsRegistry, MetricType } from './common';
import { ProxyCreateMessage, ProxyUpdateMessage } from './domain';
import { MetricsConstants } from './MetricsConstants';
import { Environment, EnvironmentUtil } from 'pandora-env';
import { MetricsMessengerClient } from './util/MessengerUtil';
import { Counter, FastCompass, Gauge, Histogram, Meter, Proxiable, Timer } from './client';
import { AbstractIndicator } from './indicator/AbstractIndicator';

export class MetricsClient extends AbstractIndicator {

Expand Down Expand Up @@ -62,13 +60,13 @@ export class MetricsClient extends AbstractIndicator {
* @param {} name
* @param {Proxiable} metric
*/
register(group: string, name: MetricName | string, metric: Proxiable | Metric ) {
register(group: string, name: MetricName | string, metric: Proxiable | Metric) {
this.debug(`Register: wait register a metrics name = ${name}`);
let newName = this.buildName(name);
// 把应用名加上
newName = newName.tagged('appName', this.getAppName());

if(!metric.type) {
if (!metric.type) {
metric.type = MetricType.GAUGE;
}

Expand All @@ -78,7 +76,7 @@ export class MetricsClient extends AbstractIndicator {
// Gauge 比较特殊,是实际的类,而服务端才是一个代理,和其他 metric 都不同,不需要 proxy
if ((<Proxiable>metric).proxyMethod && (<Proxiable>metric).proxyMethod.length) {
for (let method of (<Proxiable>metric).proxyMethod) {
metric[method] = (...args) => {
metric[ method ] = (...args) => {
this.debug(`Invoke: invoke name = ${newName.getNameKey()}, type = ${metric.type}, method = ${method}, value = ${args}`);
this.report({
action: MetricsConstants.EVT_METRIC_UPDATE,
Expand Down Expand Up @@ -128,7 +126,7 @@ export class MetricsClient extends AbstractIndicator {
}) {
this.debug(`Invoke: invoked, key = ${args.metricKey} `);
let metric = this.allMetricsRegistry.getMetric(MetricName.parseKey(args.metricKey));
if(metric && metric.type === args.type) {
if (metric && metric.type === args.type) {
return await Promise.resolve((<Gauge<any>>metric).getValue());
} else {
this.debug(`Invoke: can not find metric(${args.metricKey}) or type different`);
Expand All @@ -140,7 +138,7 @@ export class MetricsClient extends AbstractIndicator {
*/
protected registerDownlink() {
this.debug(`Register: down link eventKey = ${this.getClientDownlinkKey()}`);
this.messengerClient.query(this.getClientDownlinkKey(), async(message, reply) => {
this.messengerClient.query(this.getClientDownlinkKey(), async (message, reply) => {
try {
reply && reply(await this.invoke(message));
} catch (err) {
Expand Down Expand Up @@ -179,8 +177,14 @@ export class MetricsClient extends AbstractIndicator {
return histogram;
}

getFastCompass(group: string, name: MetricName | string) {
const fastCompass = new FastCompass();
this.register(group, name, fastCompass);
return fastCompass;
}

private buildName(name: MetricName | string): MetricName {
if(typeof name === 'string') {
if (typeof name === 'string') {
name = MetricName.build(<string>name);
}

Expand Down
Loading

0 comments on commit 6e5c8b8

Please sign in to comment.