This example demonstrates how to publish application metrics to Datadog during a Multiple load test, specifically the API response time of a POST request to the /chat endpoint. In vuInit, the Datadog client is initialized with the appropriate credentials and axios is used as the HTTP client.

Prerequisites

  1. An active Datadog account
  2. The following environment variables from your Datadog account:
    • DD_API_KEY: Found in your Datadog account under Organization Settings > Access > API Keys
    • DD_SITE: Your Datadog domain, e.g., us5.datadoghq.com

Code

Environment Variables
# Datadog Credentials
DD_SITE=us5.datadoghq.com
DD_API_KEY=YOUR_DD_API_KEY

# API Base URL
API_BASE_URL=https://example-api.multiple.dev
Test Spec
// Datadog API client
import dd from 'dogapi';

// axios for making HTTP requests
import axios from 'axios';

class DatadogTestSpec {
  npmDeps = {
    axios: '1.6.0',
    dogapi: '2.8.4',
  };

  async vuInit(ctx) {
    // Initialize the Datadog client
    dd.initialize({
      api_host: process.env.DD_SITE,
      api_key: process.env.DD_API_KEY,
    });

    // Create an axios API client with the API_BASE_URL environment variable
    const apiClient = axios.create({ baseURL: process.env.API_BASE_URL });

    // Return the API client so we can use it in the VU loop
    return { apiClient };
  }

  async vuLoop(ctx) {
    // Retrieve the apiClient from VU Init
    const { apiClient } = ctx.vuInitData;

    const startTime = Date.now();

    // Send a POST request to the chat endpoint
    await apiClient.post('chat', {
      message: 'Hello World!',
    });

    // Measure the API response time
    const metricValue = Date.now() - startTime;
    const metricLabel = 'POST /chat';

    // Capture the metric in Multiple
    ctx.metric(metricLabel, metricValue, 'ms (API)');

    // Publish the metric to Datadog
    dd.metric.send(metricLabel, metricValue);
  }
}