How do I set the timeout with url_get()?

As shown in the documentation for url_get(), you set url.timeout in the data container passed for url_get() options.

This example policy shows how to configure the timeout and demonstrates it by making a request through www.delay.me.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
  bundle agent main
  {
    vars:
        "options" data => '{
    "url.timeout": "2",
    "url.verbose": "1"
  }';

      "response" data => url_get(  "http://www.deelay.me/29999/google", options );

    reports:
        "$(with)"
          with => string_mustache( "{{$-top-}}", response );
  }

This output shows the effect of setting the timeout to 1.

  *   Trying 54.174.228.92...
  * TCP_NODELAY set
  * Connected to www.deelay.me (54.174.228.92) port 80 (#0)
  > GET /29999/google HTTP/1.1
  Host: www.deelay.me
  Accept: */*

  * Operation timed out after 1000 milliseconds with 0 bytes received
  * Closing connection 0
  *   Trying 54.174.228.92...
  * TCP_NODELAY set
  * Connected to www.deelay.me (54.174.228.92) port 80 (#0)
  > GET /29999/google HTTP/1.1
  Host: www.deelay.me
  Accept: */*

  * Operation timed out after 1000 milliseconds with 0 bytes received
  * Closing connection 0
  R: {"content":"","error_message":"Timeout was reached","headers":"","rc":28,"returncode":0,"success":false}

This output shows the effect of setting the timeout to 2.

  *   Trying 52.1.117.85...
  * TCP_NODELAY set
  * Connected to www.deelay.me (52.1.117.85) port 80 (#0)
  > GET /29999/google HTTP/1.1
  Host: www.deelay.me
  Accept: */*

  * Operation timed out after 2001 milliseconds with 0 bytes received
  * Closing connection 0
  *   Trying 52.1.117.85...
  * TCP_NODELAY set
  * Connected to www.deelay.me (52.1.117.85) port 80 (#0)
  > GET /29999/google HTTP/1.1
  Host: www.deelay.me
  Accept: */*

  * Operation timed out after 2001 milliseconds with 0 bytes received
  * Closing connection 0
  R: {"content":"","error_message":"Timeout was reached","headers":"","rc":28,"returncode":0,"success":false}