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

Commit

Permalink
feat: add fastcompass for http counter
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Oct 23, 2018
1 parent d7ee046 commit 32d4d50
Show file tree
Hide file tree
Showing 16 changed files with 555 additions and 109 deletions.
57 changes: 49 additions & 8 deletions packages/hook/src/patch/HttpServer.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
'use strict';

import { Patcher, getRandom64 } from 'pandora-metrics';
import { getRandom64, MetricLevel, MetricName, MetricsClientUtil, Patcher } from 'pandora-metrics';
import { extractPath } from '../utils/Utils';
import { HEADER_TRACE_ID } from '../utils/Constants';
import { parse as parseUrl } from 'url';
import { parse as parseQS, ParsedUrlQuery } from 'querystring';
import * as http from 'http';
import { IncomingMessage } from 'http';
import parseInt = require('lodash/fp/parseInt');

const debug = require('debug')('Pandora:Hook:HttpServerPatcher');

export type bufferTransformer = (buffer, req?: IncomingMessage) => object | string;

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 +54,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 +100,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 +142,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 +159,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 +197,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 +207,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 +254,29 @@ 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 32d4d50

Please sign in to comment.