How to use the elastic-apm-node.startTransaction 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 stelace / stelace / src / services / workflow.js View on Github external
eventSubscriber.on('eventCreated', async ({ event, platformId, env } = {}) => {
    // APM transaction needs to be created for customRequester
    // Note: APM transactions cannot be nested
    const prepareWorkflowsTransaction = apm.startTransaction('Prepare Workflows', 'workflow')
    apm.setUserContext({ id: platformId })
    apm.addLabels({ platformId, env, eventType: event.type })

    /*
    Objects to be shared across ALL workflows for this single event
    Remember that these can be mutated/overwritten by each of them
    */
    let runId
    let workflowsCtx = {} // shared and same for all current event’s workflows
    let vm

    try {
      const {
        Event,
        Workflow,
        WorkflowLog
github stelace / stelace / src / crons / emitTaskEvents.js View on Github external
async function emitTaskEvents () {
  let fetchEventsTransaction = apm.startTransaction('Fetch task events to emit via cron')

  try {
    // 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
github stelace / stelace / src / services / signal.js View on Github external
async function authenticationCallback (data) {
        const name = 'Socket: Signal authentication'
        apm.startTransaction(name)

        const rawApiKey = _.get(data, 'publishableKey')
        // channels can be array of strings or single string
        const channels = _.flatten([_.get(data, 'channels', '')])
        const authToken = _.get(data, 'authToken')

        const organizationIds = [] // to be extracted from channels
        const userIds = [] // there should be a single one at most
        let otherChannels = []
        let platformId
        let env
        let userId
        let hasInternalUserId

        const disconnect = reason => {
          disconnectReason = reason
github stelace / stelace / src / services / workflow.js View on Github external
await bluebird.mapSeries(workflows, async workflow => {
        if (_.isEmpty(workflow.run)) return

        const singleWorkflowTransaction = apm.startTransaction('Execute Workflow', 'workflow')
        singleWorkflowTransaction.action = workflow.name
        apm.setUserContext({ id: platformId })
        apm.addLabels({ env, platformId, eventType: event.type })
        apm.setCustomContext({ workflowId: workflow.id })

        apmSpans.fetchStats = apm.startSpan('Update nbTimesRun')

        runId = Uuid.v4()
        currentWorkflowId = workflow.id

        const initialComputedScript = _getComputedValuesScript(workflow.computed, { reset: true })
        const lastResponses = [] // array of responses
        const responses = {} // step name -> response

        const knex = Workflow.knex()
        await Workflow.query().where('id', currentWorkflowId).patch({
github stelace / stelace / src / crons / emitTaskEvents.js View on Github external
try {
    // 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 })
        }