How to use the elastic-apm-node.setUserContext function in elastic-apm-node

To help you get started, we’ve selected a few elastic-apm-node examples, based on popular ways it is used in public projects.

github vpdb / server / src / app / authentication / authentication.api.ts View on Github external
// try to authenticate locally
			const localUser = await this.authenticateLocally(ctx);
			if (localUser) {
				how = 'password';
				authenticatedUser = localUser;

			} else {
				// try to authenticate
				how = 'token';
				authenticatedUser = await this.authenticateWithToken(ctx);
			}

			/* istanbul ignore if */
			if (process.env.ELASTIC_APM_ENABLED) {
				const apm = require('elastic-apm-node');
				apm.setUserContext({ id: authenticatedUser.id, username: authenticatedUser.name, email: authenticatedUser.email });
			}

			// here we're authenticated (but not yet authorized)
			await this.authenticateUser(ctx, authenticatedUser, how);

			// potentially unlock ip block
			await this.ipLockOnSuccess(ctx, config.vpdb.loginBackoff);

		} catch (err) {
			await this.ipLockOnFail(ctx, config.vpdb.loginBackoff, err);
		}
	}
github stelace / stelace / src / crons / emitTaskEvents.js View on Github external
// use ref date because cron job cannot trigger at the specified time (with 0 millisecond)
    const refDate = getRoundedDate(new Date(), nbMinutes)

    const taskConfigs = await getAllStelaceTasks()

    const filteredTaskConfigs = filterTasks(taskConfigs, refDate, nbMinutes)

    fetchEventsTransaction.end()
    fetchEventsTransaction = null // set null to prevent stopping a second time in the finally block

    for (let i = 0; i < filteredTaskConfigs.length; i++) {
      const taskConfig = filteredTaskConfigs[i]
      const { platformId, env, task } = taskConfig

      const emitEventTransaction = apm.startTransaction('Emit task event via cron')
      apm.setUserContext({ id: platformId })
      apm.addLabels({ env, platformId, eventType: task.eventType })
      apm.setCustomContext({ taskId: task.id })

      try {
        // use redlock to ensure the cron process is handled only by one server at a time
        // even within a distributed system
        const lockResource = `locks:stelace_tasks:${task.id}_${refDate}`
        const lock = await redlock.lock(lockResource, lockTtl)

        const alreadyExecuted = await didStelaceTaskExecute({ taskId: task.id, executionDate: refDate })

        if (!alreadyExecuted) {
          await addStelaceTaskExecutionDate({ taskId: task.id, executionDate: refDate })
          await emitTaskEvent({ platformId, env, task })
        }
github posquit0 / koa-rest-api-boilerplate / app / middlewares / apm.js View on Github external
apm.captureError(err);

        debug('Sent error to APM server');
      });
      throw err;
    } finally {
      // Set custom context data
      const reqId = ctx.state.reqId
        || ctx.reqId
        || ctx.req.id
        || ctx.get('X-Request-Id');
      const custom = { reqId };
      apm.setCustomContext(custom);

      // Set user context data
      apm.setUserContext(
        serializeUser(ctx.state.user)
      );
    }
  };
};