Checks

Learn how to check the records Gatling reads, and save values for the next requests.

Understand Kafka checks

Kafka checks are regular Gatling checks: they support find, findAll, count, saveAs, transform, and the usual validators such as is and gt. They run on the record that Gatling reads: the reply of a request-reply, the record of a consume, or the record of a subscription. send reads no record, so it has no checks.

Checks run once the record is read, outside of the response time. A failing check makes the request fail.

The checks of a subscription run on the session as it was when the subscription started. What they save, and their failures, apply to the session when the virtual user calls kafka.poll, see subscriptions.

Check the value

The generic checks on the body read the record’s value:

Check Reads the value as
bodyString, substring, regex Text, decoded as UTF-8
jsonPath, jmesPath JSON
bodyBytes, bodyLength, bodyStream Bytes
     
kafka("Check order")
  .requestReply("order-requests", "order-replies")
  .value(StringBody("{\"id\": \"#{orderId}\"}"))
  .key("#{orderId}")
  .check(
    // the value, as text
    bodyString().exists(),
    substring("ACCEPTED"),
    regex("\"status\": *\"(\\w+)\"").saveAs("status"),
    // the value, as JSON
    jsonPath("$.status").is("ACCEPTED"),
    jmesPath("total").ofInt().gt(0),
    // the value, as bytes
    bodyBytes().saveAs("replyBytes"),
    bodyLength().gt(0)
  );
kafka("Check order")
  .requestReply("order-requests", "order-replies")
  .value(StringBody("""{"id": "#{orderId}"}"""))
  .key("#{orderId}")
  .check(
    // the value, as text
    bodyString().exists(),
    substring("ACCEPTED"),
    regex(""""status": *"(\w+)"""").saveAs("status"),
    // the value, as JSON
    jsonPath("\$.status").shouldBe("ACCEPTED"),
    jmesPath("total").ofInt().gt(0),
    // the value, as bytes
    bodyBytes().saveAs("replyBytes"),
    bodyLength().gt(0)
  )
kafka("Check order")
  .requestReply("order-requests", "order-replies")
  .value(StringBody("""{"id": "#{orderId}"}"""))
  .key("#{orderId}")
  .check(
    // the value, as text
    bodyString.exists,
    substring("ACCEPTED"),
    regex(""""status": *"(\w+)"""").saveAs("status"),
    // the value, as JSON
    jsonPath("$.status").is("ACCEPTED"),
    jmesPath("total").ofType[Int].gt(0),
    // the value, as bytes
    bodyBytes.saveAs("replyBytes"),
    bodyLength.gt(0)
  )

A record without a value, such as a tombstone, fails these checks.

When a request-reply decodes its reply with a schema registry, the text and JSON checks read the decoded value as JSON, and the bytes checks still read the record’s bytes. See decode replies.

Check the key and the headers

kafkaKey extracts the record’s key, and kafkaHeader(name) the value of the record’s last header with this name, both decoded as UTF-8:

     
kafka("Order enriched")
  .consume("enriched-orders")
  .correlationId("#{orderId}")
  .check(
    // the key, decoded as UTF-8
    kafkaKey().is("#{orderId}"),
    // the last header with this name, decoded as UTF-8
    kafkaHeader("eventType").is("OrderEnriched"),
    kafkaHeader("traceId").saveAs("traceId")
  );
kafka("Order enriched")
  .consume("enriched-orders")
  .correlationId("#{orderId}")
  .check(
    // the key, decoded as UTF-8
    kafkaKey().shouldBe("#{orderId}"),
    // the last header with this name, decoded as UTF-8
    kafkaHeader("eventType").shouldBe("OrderEnriched"),
    kafkaHeader("traceId").saveAs("traceId")
  )
kafka("Order enriched")
  .consume("enriched-orders")
  .correlationId("#{orderId}")
  .check(
    // the key, decoded as UTF-8
    kafkaKey.is("#{orderId}"),
    // the last header with this name, decoded as UTF-8
    kafkaHeader("eventType").is("OrderEnriched"),
    kafkaHeader("traceId").saveAs("traceId")
  )

Edit this page on GitHub