Skip to content

Commit

Permalink
Merge 03998e4 into 4b7c2fe
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagobustamante committed Mar 23, 2018
2 parents 4b7c2fe + 03998e4 commit 7f05bc9
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 24 deletions.
17 changes: 12 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
"bcryptjs": "^2.4.3",
"body-parser": "^1.18.2",
"bson": "^2.0.0",
"chai": "^4.1.2",
"chalk": "^2.3.2",
"cluster": "^0.7.7",
"compare-versions": "^3.1.0",
Expand Down Expand Up @@ -142,6 +141,7 @@
"@types/winston": "0.0.28",
"@types/xml2js": "^0.4.0",
"@types/yamljs": "^0.2.30",
"chai": "^4.1.2",
"chai-as-promised": "^7.1.1",
"copyfiles": "^1.2.0",
"coveralls": "^2.13.3",
Expand Down
6 changes: 3 additions & 3 deletions src/config/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ export interface ApiConfig {
*/
errorHandler?: MiddlewareConfig;
/**
* Disable all stats recording for this API
* Disable all request log recording for this API
*/
disableStats?: boolean;
disableAnalytics?: boolean;
/**
* Allows you to control when to parse the request body. Just enable it if you need to access the ```request.body```
* inside a proxy middleware, like a ```filter``` or ```interceptor```. You can inform the expected
Expand Down Expand Up @@ -207,7 +207,7 @@ export let apiConfigValidatorSchema = Joi.object().keys({
circuitBreaker: Joi.alternatives([Joi.array().items(apiCircuitBreakerConfigValidatorSchema), apiCircuitBreakerConfigValidatorSchema]),
cors: Joi.alternatives([Joi.array().items(apiCorsConfigSchema), apiCorsConfigSchema]),
description: Joi.string(),
disableStats: Joi.boolean(),
disableAnalytics: Joi.boolean(),
errorHandler: middlewareConfigValidatorSchema,
filter: Joi.alternatives([Joi.array().items(apiFilterSchema), apiFilterSchema]),
group: Joi.alternatives([Joi.array().items(groupValidatorSchema), groupValidatorSchema]),
Expand Down
14 changes: 7 additions & 7 deletions src/config/gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,6 @@ export interface GatewayConfig {
* Configure features globally, to be imported by api configureations
*/
config?: ApiFeaturesConfig;
/**
* Disable all stats recording for the gateway
*/
disableStats?: boolean;
/**
* Inform how request analytics should be stored by the gateway
*/
Expand All @@ -133,10 +129,14 @@ export interface GatewayConfig {
* Inform how request analytics should be stored by the gateway
*/
export interface RequestAnalyticsConfig {
/**
* Enable log recording for the gateway requests
*/
enabled?: boolean;
/**
* The logger middleware
*/
logger: MiddlewareConfig;
logger?: MiddlewareConfig;
}

/**
Expand Down Expand Up @@ -179,7 +179,8 @@ const apiFeaturesConfigSchema = Joi.object().keys({
});

export const requestAnalyticsConfigSchema = Joi.object().keys({
logger: middlewareConfigValidatorSchema.required()
enabled: Joi.boolean(),
logger: middlewareConfigValidatorSchema
});

export const gatewayConfigValidatorSchema = Joi.object().keys({
Expand All @@ -190,7 +191,6 @@ export const gatewayConfigValidatorSchema = Joi.object().keys({
cors: corsConfigSchema,
disableApiIdValidation: Joi.boolean(),
disableCompression: Joi.boolean(),
disableStats: Joi.boolean(),
errorHandler: middlewareConfigValidatorSchema,
filter: Joi.alternatives([Joi.array().items(middlewareConfigValidatorSchema), middlewareConfigValidatorSchema]),
healthcheck: Joi.string(),
Expand Down
4 changes: 3 additions & 1 deletion src/gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,9 @@ export class Gateway extends EventEmitter {
.on(ConfigEvents.CIRCUIT_CHANGED, (id: string, state: string) => this.apiPipeline.circuitChanged(id, state))
.subscribeEvents();
await this.configureAdminServer();
this.requestLogger.initialize();
if (this.requestLogger.isGatewayRequestLogEnabled()) {
this.requestLogger.initialize();
}
} catch (err) {
this.logger.error(`Error configuring gateway server. Config File:\n${JSON.stringify(this.config.gateway)}`);
this.logger.inspectObject(err);
Expand Down
8 changes: 6 additions & 2 deletions src/pipeline/stats/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ export class RequestLogger {
}

isRequestLogEnabled(api: ApiConfig): boolean {
return (!this.config.gateway.disableStats && !api.disableStats);
return (this.isGatewayRequestLogEnabled() && !api.disableAnalytics);
}

isGatewayRequestLogEnabled() {
return this.config.gateway.analytics && this.config.gateway.analytics.enabled;
}

getRequestLog(req: Request): RequestLog {
Expand All @@ -61,7 +65,7 @@ export class RequestLogger {
}

private getRequestLogMiddleware() {
if (this.config.gateway.analytics) {
if (this.config.gateway.analytics && this.config.gateway.analytics.logger) {
return this.config.gateway.analytics.logger;
}
return {
Expand Down
7 changes: 5 additions & 2 deletions src/utils/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,15 @@ export class VersionUpgrades {
if (pac && pac.gateway) {
console.info(chalk.magenta('An older configuration was found. Updating it to the newer format. '
+ 'Check tree-gateway migration guide for more info.'));
pac.gateway = <GatewayConfig>_.omit(pac.gateway, 'statsConfig', 'monitor');
if (!(<any>pac.gateway)['disableStats']) {
pac.gateway.analytics = { enabled: true, logger: { name: 'redis' } };
}
pac.gateway = <GatewayConfig>_.omit(pac.gateway, 'statsConfig', 'monitor', 'disableStats');
}
if (pac.apis) {
pac.apis = pac.apis.map(api => {
if (_.has(api, 'proxy.disableStats')) {
api.disableStats = _.get(api.proxy, 'disableStats');
api.disableAnalytics = _.get(api.proxy, 'disableStats');
}
if (_.has(api, 'proxy.parseReqBody')) {
api.parseReqBody = _.get(api.proxy, 'parseReqBody');
Expand Down
2 changes: 1 addition & 1 deletion test/data/apis/benchmarkHello.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
"timeout": "five seconds"
},
"parseReqBody": false,
"disableStats": true
"disableAnalytics": true
}
4 changes: 3 additions & 1 deletion test/data/tree-gateway.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
},
"gateway": {
"underProxy": false,
"disableStats": true,
"disableCompression": true,
"protocol": {
"http": {
Expand Down Expand Up @@ -73,6 +72,9 @@
"prettyPrint": true,
"outputDir": "./logs"
}
},
"analytics": {
"enabled": false
}
}
}
3 changes: 2 additions & 1 deletion tree-gateway-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ gateway:
message: IP Filtered
database:
checkInterval: 1 second

analytics:
enabled: true
serviceDiscovery:
provider:
name: consul
Expand Down

0 comments on commit 7f05bc9

Please sign in to comment.