Search code examples
gatlingdynatrace

How can I add a default header to each HTTP request with Gatling?


I'm using Dynatrace and Gatling for performance analysis and testing. Dynatrace supports tracing test runs through the addition of a header to each HTTP request. I'd like to had that header with a dynamic test guid without adding it to every request individually in 100s of places.

An example test:

def GetLocationPage = exec(http(domain + "GetLocationPage")
.post("/location ")
.formParam("updateVersion", "1")

I understand that I could add the header individually in each request with...

.headers(gatlingHeaders)

... but my goal is to avoid doing that 100s of places in the code. Essentially, I'm looking for a Gatling equivalent to this functionality in Spring.

I found this issue on Gatling, but wasn't able to determine if it would be useful.

Any recommendations?


Solution

  • You can configure default headers directly when you are creating your http protocol, e.g.

    val httpConf = http
       // Here is the root for all relative URLs
       .baseURL("http://computer-database.gatling.io") 
       // Here are the common headers, via specialized methods
      .acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8") 
      .acceptEncodingHeader("gzip, deflate")
      .acceptLanguageHeader("en-US,en;q=0.5")
      .userAgentHeader("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:16.0) Gecko/20100101 Firefox/16.0")
      // More generic methods are available too
      .header("foo", "bar") // to set one header
      .headers(Map("foo" -> "bar", "baz" -> "qix")) // to set a bunch of headers
    
    val scn = scenario("Scenario Name")
      .exec(http("request_1").headers(...) // This is for single request, but you know it already
      .get("/")) // etc...
    
    setUp(scn.inject(atOnceUsers(1)).protocols(httpConf))
    

    For more info refer to documentation Http Headers