Dynatrace integration

Set a custom test header on all generated requests.

Using Gatling and Dynatrace to capture request attributes

Pass Gatling load test request attributes to Dynatrace using additional HTTP headers. Dynatrace can handle, extract, and tag information from incoming HTTP headers containing information such as:

  • script name,
  • test step name, and
  • virtual user ID.

You can then filter your monitoring data based on the defined tags.

Configure Dynatrace extraction rules

You can use any HTTP headers or HTTP parameters to pass contextual information. To configure the extraction rules in Dynatrace reference the extraction rules documentation.

Add contextual information to headers

The header x-dynatrace-test is used in the following example with the following set of key/value pairs for the header:

Acronym Full Term Description
VU Virtual User ID A unique identifier for the virtual user who sent the request.
SI Source ID Identifies the product that triggered the request (e.g., Gatling).
TSN Test Step Name Represents a logical test step within the load testing script (e.g., Login, Add to Cart).
LSN Load Script Name Name of the load testing script that groups test steps into a multistep transaction (e.g., Online Purchase).
LTN Load Test Name Uniquely identifies a test execution (e.g., 6h Load Test – June 25).
PC Page Context Provides information about the document loaded on the currently processed page.
Dynatrace Report

Defining a global signing function (example)

The idea here is to use sign on the HttpProtocol to define a global signing function to be applied on all generated requests.

       
private static final String Hostname;

static {
  try {
    Hostname = InetAddress.getLocalHost().getHostName();
  } catch (UnknownHostException e) {
    throw new ExceptionInInitializerError(e);
  }
}

// Source Id identifies the product that triggered the request
private static final String SI = "GATLING";

// The Load Test Name uniquely identifies a test execution
private final String LTN =
  getClass().getSimpleName() +
    "_" +
    LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);

HttpProtocolBuilder httpProtocol = http
  .sign((request, session) -> {
    // Virtual User ID of the unique user who sent the request
    String VU = Hostname + "_" + session.scenario() + "_" + session.userId();

    // Test Step Name is a logical test step within your load testing script
    String TSN = request.getName();

    // Load Script Name - name of the load testing script.
    String LSN = session.scenario();

    // Page Context provides information about the document
    String PC = String.join(",", session.groups());

    request.getHeaders()
      .set(
        "x-dynaTrace",
        "VU=" + VU + ";SI=" + SI + ";TSN=" + TSN + ";LSN=" + LSN + ";LTN=" + LTN + ";PC=" + PC
      );

    return request;
  });
Not supported by Gatling JS.
private val Hostname: String = InetAddress.getLocalHost().getHostName()

// Source Id identifies the product that triggered the request
private val SI = "GATLING"

// The Load Test Name uniquely identifies a test execution
private val LTN =
  javaClass.simpleName +
  "_" +
  LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)

val httpProtocol = http
  .sign { request: Request, session: Session ->
    // Virtual User ID of the unique user who sent the request
    val VU = "${Hostname}_${session.scenario()}_${session.userId()}"

    // Test Step Name is a logical test step within your load testing script
    val TSN = request.name

    // Load Script Name - name of the load testing script.
    val LSN = session.scenario()

    // Page Context provides information about the document
    val PC = session.groups().joinToString(",")

    request.headers["x-dynaTrace"] =
      "VU=$VU;SI=$SI;TSN=$TSN;LSN=$LSN;LTN=$LTN;PC=$PC"

    request
  }
private val Hostname = InetAddress.getLocalHost.getHostName

// Source Id identifies the product that triggered the request
private val SI = "GATLING"

// The Load Test Name uniquely identifies a test execution
private val LTN =
  getClass.getSimpleName +
    "_" +
    LocalDateTime.now.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)

val httpProtocol = http
  .sign { (request, session) =>
    // Virtual User ID of the unique user who sent the request
    val VU = s"${Hostname}_${session.scenario}_${session.userId}"

    // Test Step Name is a logical test step within your load testing script
    val TSN = request.getName

    // Load Script Name - name of the load testing script.
    val LSN = session.scenario

    // Page Context provides information about the document
    val PC = session.groups.mkString(",")

    request.getHeaders.set(
      "x-dynaTrace",
      s"VU=$VU;SI=$SI;TSN=$TSN;LSN=$LSN;LTN=$LTN;PC=$PC"
    )
    request
  }

Edit this page on GitHub