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

@@ -6,6 +6,7 @@ import {
} from '../models/user';
import nconf from 'nconf';
import url from 'url';
import gcpStackdriverTracer from '../libs/gcpTraceAgent';
const COMMUNITY_MANAGER_EMAIL = nconf.get('EMAILS_COMMUNITY_MANAGER_EMAIL');
@@ -34,6 +35,13 @@ function getUserFields (options, req) {
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
// 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}));
res.locals.user = user;
req.session.userId = user._id;
stackdriverTraceUserId(user._id);
return next();
})
.catch(next);
@@ -93,6 +102,7 @@ export function authWithSession (req, res, next) {
if (!user) throw new NotAuthorized(res.t('invalidCredentials'));
res.locals.user = user;
stackdriverTraceUserId(user._id);
return next();
})
.catch(next);