Workflows quick start guide

Latest Dynatrace

Let's walk through a Hello World example, add some tasks, and set an automatic trigger.

Hello World

In this procedure, we create a workflow with a single Run JavaScript task and run it.

You don't need to write JavaScript for this procedure; we'll use the default sample JavaScript.

Goals
  • Familiarize yourself with basic Workflows activities
  • Verify that you have all the permissions needed to run workflows and custom scripts
  1. Sign in to Dynatrace.

  2. Go to Workflows Workflows.

    Make sure you're on the Workflows tab, not the Executions tab.

  3. To create your first workflow, select Add Workflow in the upper-right corner of the page.

    Select "Workflow" to create a new workflow

    The workflow editor opens.

    Empty workflow editor

  4. Select the workflow title (default: Untitled workflow) and enter a custom name such as Quick Start.

    Rename workflow

  5. In the Select a trigger pane on the right, scroll down and select On demand trigger. We are going to create a workflow that you run manually via the web UI or REST API.

    Select "On demand trigger"

  6. To add the first task to your workflow, hover over the trigger node of the workflow graph and select Add.

    Add task to "On demand trigger"

  7. In the Choose action pane on the right, select Run Javascript.

    Select "Run JavaScript"

    The workflow now has its first task and shows the input configuration on the right. It comes with sample code that retrieves who started the workflow execution and logs it.

  8. Select Run to execute the workflow.

    Run the current workflow

    The first time you run a workflow, you are prompted to authorize the automation service to run a workflow as your user. Select Allow and run if you agree with that. You can always restrain the configuration in the settings.

    Error?

    If you get a "forbidden" error when you run your workflow, check your authorization settings.

    1. In the upper-right corner, select Settings > Authorization settings.
    2. Verify that you have all required permissions.
    3. Run the workflow again.
  9. The app switches to the workflow monitor, which is a live view of the execution you just started.

    • This view automatically refreshes until the execution reaches a final state.
    • The right pane shows details of the execution.
      • While the workflow is executing, you should see State set to Running.
      • When the execution is finished, you should see State set to Success.
      • If you don't get Success, State info will tell you why.
    • You can inspect the details of individual tasks by selecting them in the graph.

    Workflow monitor after running first workflow successfully

You have successfully run your first workflow.

Multiple Tasks

Now that you have verified that you can run workflows and custom scripts, let's add another task and send a custom event to Grail.

Goals

Now we're going to learn how to:

  • Edit a workflow
  • Add more tasks to a workflow
  • Rename a task
  • Run JavaScript from a task
  • Run DQL from a task
  1. From the workflow monitor, select Edit workflow to return to the workflow editor.

    Select "Edit workflow"

  2. To add another task, hover over the node to which you want to append it and select Add.

    You can add tasks at any point in the workflow. You can also have multiple tasks in parallel. Let's add one after our first run_javascript_1 as we plan to use some information from here later.

    Add another task

    Empty new task added

  3. In the Choose action pane on the right, select Run Javascript.

    Select "Run JavaScript"

  4. Now is a good opportunity to give this task a better name by selecting the task name in the upper-right (similar to how we changed the workflow title) and changing it to ingest_custom_event.

    Change a task name

  5. As the name suggests, we want the second task to ingest a custom event to Grail using a Dynatrace API available for import in the Run Javascript action. To do that, copy the following JavaScript code and paste it into the Source code box to replace the default sample.

    import { execution } from "@dynatrace-sdk/automation-utils";
    import { eventsClient, EventIngestEventType } from '@dynatrace-sdk/client-classic-environment-v2';
    export default async function ({ execution_id }) {
    const my_event = {
    "eventType": EventIngestEventType.CustomInfo,
    "title": "Workflows quickstart sample event",
    "properties": {
    "triggeredBy": "tbd"
    }
    }
    const event_result = await eventsClient.createEvent({body: my_event})
    console.log(event_result)
    return event_result
    }

    This will create a CustomInfo event, log the API response, and return it. By returning data, it will be made available as the result of the task and can be used by subsequent tasks. For more information on the SDKs, see Dynatrace Developer.

  6. To verify your changes, save and run the workflow like before. Since we write new events to the Dynatrace API, your user requires the necessary permissions (storage:events:write) to do so and also allow the AutomationEngine to act with that permission (we did allow the AutomationEngine to act on our behalf in the Hello World example). A lack of permissions will be reflected in the ingest_custom_event task status and logs.

  7. Once the execution is successful, let's go back to editing the workflow and use the task results.

    Our sample run_javascript_1 returns the triggeredBy information, which we will now use as payload in our custom event. Select the ingest_custom_event task (our second task), copy the following JavaScript code, and paste it into Source code to replace the current contents of the ingest_custom_event task.

    import { execution } from "@dynatrace-sdk/automation-utils";
    import { eventsClient, EventIngestEventType } from '@dynatrace-sdk/client-classic-environment-v2';
    export default async function ({ execution_id }) {
    const exe = await execution(execution_id);
    const result = await exe.result("run_javascript_1");
    const my_event = {
    "eventType": EventIngestEventType.CustomInfo,
    "title": "Workflows quickstart sample event",
    "properties": {
    "triggeredBy": result.triggeredBy
    }
    }
    const event_result = await eventsClient.createEvent({body: my_event})
    console.log(event_result)
    return event_result
    }

    This time we grab the execution details, pull in the results of the run_javascript_1 task, and use the triggeredBy info as the event payload in line 13.

    "triggeredBy": result.triggeredBy
  8. Run the workflow again and then take a look at our custom events. Go to the Notebooks app and query Grail for our custom events.

    In Notebooks, select "Query Grail"

    fetch events
    | filter event.type == "CUSTOM_INFO"
    | filter event.name == "Workflows quickstart sample event"

    In Notebooks, run Grail query

  9. Finally, let's add another task at the end of our Quick Start workflow.

    Add one last task

    This time, select the Execute DQL Query action.

    Select "Execute DQL Query" task

    To use it to get the number of our Quick Start events, copy and paste the following DQL into DQL query box for the last task.

    fetch events, from:now() - 24h
    | filter event.type == "CUSTOM_INFO"
    | filter event.name == "Workflows quickstart sample event"
    | summarize EventCount = count()
    | fields EventCount

    Events might not show up immediately in Grail after ingesting them as they have to pass through several processing steps.

Automatic Triggers

The only thing left is add an automatic trigger to our workflow.

Goals
  • Learn how to trigger a workflow automatically instead of manually
  1. From the workflow monitor, select Edit workflow to return to the workflow editor.

    Select "Edit workflow"

  2. Select the trigger node in the graph (it should say On demand trigger in our example).

  3. In the trigger details on the right pane, select Change trigger and confirm that you want to change the trigger. This action discards the current trigger definition.

    Select "Change trigger"

  4. In the Select a trigger pane, select Time interval trigger.

    Select "Time interval trigger"

  5. Leave the interval value (Run every (mins)) as the default (30 minutes).

  6. Save the workflow.

With this change:

  • Instead of running on demand, your workflow will automatically start every 30 minutes.
  • Each execution (every 30 minutes) will be listed in the Executions table. Select Executions at the top of the editor to see the table.