mirror of
https://github.com/sudoxnym/habitica.git
synced 2026-07-19 18:24:12 +00:00
gcp stackdriver tracing: attach user id (#11033)
This commit is contained in:
parent
bad06ba449
commit
cc766d2260
3 changed files with 26 additions and 6 deletions
|
|
@ -12,15 +12,13 @@ const setupNconf = require('./libs/setupNconf');
|
|||
setupNconf();
|
||||
const nconf = require('nconf');
|
||||
|
||||
const IS_PROD = nconf.get('IS_PROD');
|
||||
const STACKDRIVER_TRACING_ENABLED = nconf.get('ENABLE_STACKDRIVER_TRACING') === 'true';
|
||||
if (IS_PROD && STACKDRIVER_TRACING_ENABLED) {
|
||||
require('@google-cloud/trace-agent').start(); // eslint-disable-line global-require
|
||||
}
|
||||
// Initialize @google-cloud/trace-agent
|
||||
require('./libs/gcpTraceAgent');
|
||||
|
||||
const cluster = require('cluster');
|
||||
const logger = require('./libs/logger');
|
||||
|
||||
const IS_PROD = nconf.get('IS_PROD');
|
||||
const IS_DEV = nconf.get('IS_DEV');
|
||||
const CORES = Number(nconf.get('WEB_CONCURRENCY')) || 0;
|
||||
|
||||
|
|
|
|||
12
website/server/libs/gcpTraceAgent.js
Normal file
12
website/server/libs/gcpTraceAgent.js
Normal 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;
|
||||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Reference in a new issue