Skip to content

Commit

Permalink
Manage debug parameters with a DebugUtil helper (#852)
Browse files Browse the repository at this point in the history
* Manage debug parameters with a `DebugUtil` helper

This is mainly to collect all of the supported debug parameters into one place.

Tired of having to hunt for them.

* Need to init RLS for navigator tests now
  • Loading branch information
gigabo authored and doug-wade committed Jan 30, 2017
1 parent bbe94c3 commit 0a8ddd8
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 4 deletions.
5 changes: 4 additions & 1 deletion packages/react-server/core/ClientController.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var React = require('react'),
logging = require('./logging'),
RequestContext = require('./context/RequestContext'),
RequestLocalStorage = require('./util/RequestLocalStorage'),
DebugUtil = require('./util/DebugUtil'),
Q = require('q'),
cssHelper = require('./util/ClientCssHelper'),
EventEmitter = require("events").EventEmitter,
Expand Down Expand Up @@ -421,7 +422,9 @@ class ClientController extends EventEmitter {
_handleDebugParams(page) {
if (!page) return;

const params = page.getRequest().getQuery();
DebugUtil.setRequest(page.getRequest());

const params = DebugUtil.getAllDebugValues();

// Allow adjustment of log levels.
_.forEach({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import Navigator from "../../../context/Navigator";
import History from "../../../components/History";
import ExpressServerRequest from "../../../ExpressServerRequest";
import NavigatorRoutes from "./NavigatorRoutes";
import RequestLocalStorage from "../../../util/RequestLocalStorage";

class RequestContextStub {
constructor(options) {
this.navigator = new Navigator(this, options);
}
navigate (request, type) {
this.navigator.navigate(request, type);
RequestLocalStorage.startRequest(() => {
this.navigator.navigate(request, type);
});
}
framebackControllerWillHandle() { return false; }
getMobileDetect() { return null; }
Expand Down
5 changes: 5 additions & 0 deletions packages/react-server/core/context/Navigator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var EventEmitter = require('events').EventEmitter,
History = require("../components/History"),
ReactServerAgent = require("../ReactServerAgent"),
PageUtil = require("../util/PageUtil"),
DebugUtil = require("../util/DebugUtil"),
{setResponseLoggerPage} = SERVER_SIDE ? require('../logging/response') : { setResponseLoggerPage: () => {} };

var _ = {
Expand Down Expand Up @@ -50,6 +51,10 @@ class Navigator extends EventEmitter {

this._haveInitialized = true;

// Pull debug parameters out of the query string and expose via a well
// defined interface.
DebugUtil.setRequest(request);

var route = this.router.getRoute(request.getUrl(), { method: request.getMethod() });

if (route) {
Expand Down
5 changes: 3 additions & 2 deletions packages/react-server/core/renderMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var logger = require('./logging').getLogger(__LOGGER__),
MobileDetect = require('mobile-detect'),
RequestContext = require('./context/RequestContext'),
RequestLocalStorage = require('./util/RequestLocalStorage'),
DebugUtil = require('./util/DebugUtil'),
RLS = RequestLocalStorage.getNamespace(),
LABString = require('./util/LABString'),
Q = require('q'),
Expand Down Expand Up @@ -715,7 +716,7 @@ function writeBody(req, res, context, start, page) {
// Some time has already elapsed since the request started.
// Note that you can override `FAILSAFE_RENDER_TIMEOUT` with a
// `?_debug_render_timeout={ms}` query string parameter.
var totalWait = req.query._debug_render_timeout || FAILSAFE_RENDER_TIMEOUT
var totalWait = DebugUtil.getRenderTimeout() || FAILSAFE_RENDER_TIMEOUT
, timeRemaining = totalWait - (new Date - start)

var retval = Q.defer();
Expand Down Expand Up @@ -988,7 +989,7 @@ function wrapUpLateArrivals(){

function closeBody(req, res) {
// Flush timing/log data to the response document
if (req.query._debug_output_logs) {
if (DebugUtil.getOutputLogs()) {
flushLogsToResponse(res);
}
res.write("</div></body></html>");
Expand Down
38 changes: 38 additions & 0 deletions packages/react-server/core/util/DebugUtil.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import RequestLocalStorage from "./RequestLocalStorage";
import forEach from "lodash/forEach";
import map from "lodash/map";
import assign from "lodash/assign";
import pick from "lodash/pick";

const RLS = RequestLocalStorage.getNamespace();

// This module provides methods that expose values from the query string.
//
// So, for example, if you pass `?_debug_render_timeout=1000` then you can
// call `DebugUtil.getRenderTimeout()` and receive `1000`.
//
// Additionally you may call `DebugUtil.getAllDebugValues()` to obtain an
// object with all debug parameters extracted from the query string.
//
const DEBUG_PARAMS = {
getRenderTimeout : "_debug_render_timeout",
getOutputLogs : "_debug_output_logs",
getLogLevel : "_react_server_log_level",
getLogLevelMain : "_react_server_log_level_main",
getLogLevelTime : "_react_server_log_level_time",
getLogLevelGauge : "_react_server_log_level_gauge",
};

const DebugUtil = {
setRequest(req) {
assign(RLS(), pick(req.getQuery(), map(DEBUG_PARAMS, v => v)));
},
getAllDebugValues() {
return assign({}, RLS());
},
};

// Make the methods.
forEach(DEBUG_PARAMS, (param, method) => DebugUtil[method] = () => RLS()[param]);

export default DebugUtil;

0 comments on commit 0a8ddd8

Please sign in to comment.