gcp stackdriver tracing: attach user id (#11033)

This commit is contained in:
Matteo Pagliazzi
2019-02-27 18:47:33 +01:00
committed by GitHub
parent bad06ba449
commit cc766d2260
3 changed files with 26 additions and 6 deletions

View File

@@ -12,15 +12,13 @@ const setupNconf = require('./libs/setupNconf');
setupNconf(); setupNconf();
const nconf = require('nconf'); const nconf = require('nconf');
const IS_PROD = nconf.get('IS_PROD'); // Initialize @google-cloud/trace-agent
const STACKDRIVER_TRACING_ENABLED = nconf.get('ENABLE_STACKDRIVER_TRACING') === 'true'; require('./libs/gcpTraceAgent');
if (IS_PROD && STACKDRIVER_TRACING_ENABLED) {
require('@google-cloud/trace-agent').start(); // eslint-disable-line global-require
}
const cluster = require('cluster'); const cluster = require('cluster');
const logger = require('./libs/logger'); const logger = require('./libs/logger');
const IS_PROD = nconf.get('IS_PROD');
const IS_DEV = nconf.get('IS_DEV'); const IS_DEV = nconf.get('IS_DEV');
const CORES = Number(nconf.get('WEB_CONCURRENCY')) || 0; const CORES = Number(nconf.get('WEB_CONCURRENCY')) || 0;

View File

@@ -0,0 +1,12 @@
const nconf = require('nconf');
const IS_PROD = nconf.get('IS_PROD');
const STACKDRIVER_TRACING_ENABLED = nconf.get('ENABLE_STACKDRIVER_TRACING') === 'true';
let tracer = null;
if (IS_PROD && STACKDRIVER_TRACING_ENABLED) {
tracer = require('@google-cloud/trace-agent').start(); // eslint-disable-line global-require
}
export default tracer;

View File

@@ -6,6 +6,7 @@ import {
} from '../models/user'; } from '../models/user';
import nconf from 'nconf'; import nconf from 'nconf';
import url from 'url'; import url from 'url';
import gcpStackdriverTracer from '../libs/gcpTraceAgent';
const COMMUNITY_MANAGER_EMAIL = nconf.get('EMAILS_COMMUNITY_MANAGER_EMAIL'); const COMMUNITY_MANAGER_EMAIL = nconf.get('EMAILS_COMMUNITY_MANAGER_EMAIL');
@@ -34,6 +35,13 @@ function getUserFields (options, req) {
return `notifications ${userFieldOptions.join(' ')}`; return `notifications ${userFieldOptions.join(' ')}`;
} }
// Make sure stackdriver traces are storing the user id
function stackdriverTraceUserId (userId) {
if (gcpStackdriverTracer) {
gcpStackdriverTracer.getCurrentRootSpan().addLabel('userId', userId);
}
}
// Strins won't be translated here because getUserLanguage has not run yet // Strins won't be translated here because getUserLanguage has not run yet
// Authenticate a request through the x-api-user and x-api key header // Authenticate a request through the x-api-user and x-api key header
@@ -64,8 +72,9 @@ export function authWithHeaders (options = {}) {
if (user.auth.blocked) throw new NotAuthorized(res.t('accountSuspended', {communityManagerEmail: COMMUNITY_MANAGER_EMAIL, userId: user._id})); if (user.auth.blocked) throw new NotAuthorized(res.t('accountSuspended', {communityManagerEmail: COMMUNITY_MANAGER_EMAIL, userId: user._id}));
res.locals.user = user; res.locals.user = user;
req.session.userId = user._id; req.session.userId = user._id;
stackdriverTraceUserId(user._id);
return next(); return next();
}) })
.catch(next); .catch(next);
@@ -93,6 +102,7 @@ export function authWithSession (req, res, next) {
if (!user) throw new NotAuthorized(res.t('invalidCredentials')); if (!user) throw new NotAuthorized(res.t('invalidCredentials'));
res.locals.user = user; res.locals.user = user;
stackdriverTraceUserId(user._id);
return next(); return next();
}) })
.catch(next); .catch(next);