<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>k6/x/dns on Grafana Labs</title><link>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-x-dns/</link><description>Recent content in k6/x/dns on Grafana Labs</description><generator>Hugo -- gohugo.io</generator><language>en</language><atom:link href="/docs/k6/v2.3.x/javascript-api/k6-x-dns/index.xml" rel="self" type="application/rss+xml"/><item><title>resolve( query, recordType, nameserver )</title><link>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-x-dns/resolve/</link><pubDate>Mon, 21 Sep 2026 15:16:28 +0000</pubDate><guid>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-x-dns/resolve/</guid><content><![CDATA[&lt;h1 id=&#34;resolve-query-recordtype-nameserver-&#34;&gt;resolve( query, recordType, nameserver )&lt;/h1&gt;
&lt;p&gt;The &lt;code&gt;dns.resolve&lt;/code&gt; function performs DNS resolution using a specified DNS server and returns a promise that resolves to an array of IP addresses. This function allows you to test specific DNS servers, validate DNS configurations, and measure DNS resolution performance under load.&lt;/p&gt;
&lt;h2 id=&#34;parameters&#34;&gt;Parameters&lt;/h2&gt;
&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th style=&#34;text-align: left&#34;&gt;Parameter&lt;/th&gt;
              &lt;th style=&#34;text-align: left&#34;&gt;Type&lt;/th&gt;
              &lt;th style=&#34;text-align: left&#34;&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td style=&#34;text-align: left&#34;&gt;query&lt;/td&gt;
              &lt;td style=&#34;text-align: left&#34;&gt;string&lt;/td&gt;
              &lt;td style=&#34;text-align: left&#34;&gt;The domain name to resolve. For example, &amp;ldquo;example.com&amp;rdquo; or &amp;ldquo;k6.io&amp;rdquo;.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td style=&#34;text-align: left&#34;&gt;recordType&lt;/td&gt;
              &lt;td style=&#34;text-align: left&#34;&gt;string&lt;/td&gt;
              &lt;td style=&#34;text-align: left&#34;&gt;The DNS record type to query. Supported values: &amp;ldquo;A&amp;rdquo; (IPv4 addresses), &amp;ldquo;AAAA&amp;rdquo; (IPv6 addresses)&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td style=&#34;text-align: left&#34;&gt;nameserver&lt;/td&gt;
              &lt;td style=&#34;text-align: left&#34;&gt;string&lt;/td&gt;
              &lt;td style=&#34;text-align: left&#34;&gt;The DNS server to use for resolution in the format &amp;ldquo;host:port&amp;rdquo;. For example, &amp;ldquo;8.8.8.8:53&amp;rdquo; or &amp;ldquo;[2606:4700:4700::1111]:53&amp;rdquo;&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;h2 id=&#34;returns&#34;&gt;Returns&lt;/h2&gt;
&lt;p&gt;A promise resolving to an array of strings, where each string is an IP address that the domain name resolves to. For A records, these will be IPv4 addresses. For AAAA records, these will be IPv6 addresses.&lt;/p&gt;
&lt;h2 id=&#34;examples&#34;&gt;Examples&lt;/h2&gt;
&lt;!-- md-k6:skipall --&gt;
&lt;h3 id=&#34;basic-a-record-resolution&#34;&gt;Basic A record resolution&lt;/h3&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;JavaScript&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-javascript&#34;&gt;import dns from &amp;#39;k6/x/dns&amp;#39;;

export default async function () {
  // Resolve IPv4 addresses using Google&amp;#39;s DNS server
  const ipv4Addresses = await dns.resolve(&amp;#39;k6.io&amp;#39;, &amp;#39;A&amp;#39;, &amp;#39;8.8.8.8:53&amp;#39;);
  console.log(&amp;#39;k6.io IPv4 addresses:&amp;#39;, ipv4Addresses);
  // Output: k6.io IPv4 addresses: [&amp;#34;104.21.7.127&amp;#34;, &amp;#34;172.67.154.74&amp;#34;]
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;h3 id=&#34;aaaa-record-resolution&#34;&gt;AAAA record resolution&lt;/h3&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;JavaScript&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-javascript&#34;&gt;import dns from &amp;#39;k6/x/dns&amp;#39;;

export default async function () {
  // Resolve IPv6 addresses using Cloudflare&amp;#39;s IPv6 DNS server
  const ipv6Addresses = await dns.resolve(&amp;#39;k6.io&amp;#39;, &amp;#39;AAAA&amp;#39;, &amp;#39;[2606:4700:4700::1111]:53&amp;#39;);
  console.log(&amp;#39;k6.io IPv6 addresses:&amp;#39;, ipv6Addresses);
  // Output: k6.io IPv6 addresses: [&amp;#34;2606:4700:3033::6815:77f&amp;#34;, &amp;#34;2606:4700:3030::ac43:9a4a&amp;#34;]
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;h3 id=&#34;testing-multiple-dns-servers&#34;&gt;Testing multiple DNS servers&lt;/h3&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;JavaScript&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-javascript&#34;&gt;import dns from &amp;#39;k6/x/dns&amp;#39;;
import { expect } from &amp;#39;https://jslib.k6.io/k6-testing/0.6.1/index.js&amp;#39;;

export const options = {
  vus: 5,
  duration: &amp;#39;30s&amp;#39;,
};

const dnsServers = [
  &amp;#39;8.8.8.8:53&amp;#39;,     // Google DNS
  &amp;#39;1.1.1.1:53&amp;#39;,     // Cloudflare DNS
  &amp;#39;9.9.9.9:53&amp;#39;,     // Quad9 DNS
];

export default async function () {
  const domain = &amp;#39;example.com&amp;#39;;

  for (const server of dnsServers) {
    try {
      const results = await dns.resolve(domain, &amp;#39;A&amp;#39;, server);

      expect(results.length).toBeGreaterThan(0);
      expect(results.every(ip =&amp;gt; /^\d&amp;#43;\.\d&amp;#43;\.\d&amp;#43;\.\d&amp;#43;$/.test(ip))).toBeTruthy();

      console.log(`${server} resolved ${domain} to:`, results);
    } catch (error) {
      console.error(`Failed to resolve ${domain} using ${server}:`, error);
    }
  }
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;h3 id=&#34;performance-comparison&#34;&gt;Performance comparison&lt;/h3&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;JavaScript&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-javascript&#34;&gt;import dns from &amp;#39;k6/x/dns&amp;#39;;
import { Trend } from &amp;#39;k6/metrics&amp;#39;;

const resolutionTime = new Trend(&amp;#39;custom_dns_resolution_time&amp;#39;);

export const options = {
  vus: 1,
  iterations: 100,
};

export default async function () {
  const startTime = Date.now();

  try {
    const results = await dns.resolve(&amp;#39;k6.io&amp;#39;, &amp;#39;A&amp;#39;, &amp;#39;8.8.8.8:53&amp;#39;);
    const duration = Date.now() - startTime;

    resolutionTime.add(duration);

    console.log(`Resolution took ${duration}ms, found ${results.length} addresses`);
  } catch (error) {
    console.error(&amp;#39;DNS resolution failed:&amp;#39;, error);
  }
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;h2 id=&#34;error-handling&#34;&gt;Error handling&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;resolve&lt;/code&gt; function may throw errors in the following cases:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Invalid domain name format&lt;/li&gt;
&lt;li&gt;Unsupported record type&lt;/li&gt;
&lt;li&gt;DNS server unreachable or timeout&lt;/li&gt;
&lt;li&gt;DNS server returns an error response&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;JavaScript&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-javascript&#34;&gt;import dns from &amp;#39;k6/x/dns&amp;#39;;

export default async function () {
  try {
    const results = await dns.resolve(&amp;#39;nonexistent.invalid&amp;#39;, &amp;#39;A&amp;#39;, &amp;#39;8.8.8.8:53&amp;#39;);
    console.log(&amp;#39;Unexpected success:&amp;#39;, results);
  } catch (error) {
    console.log(&amp;#39;Expected DNS resolution error:&amp;#39;, error.message);
  }
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;h2 id=&#34;metrics&#34;&gt;Metrics&lt;/h2&gt;
&lt;p&gt;When using &lt;code&gt;dns.resolve&lt;/code&gt;, the following metrics are automatically generated:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;dns_resolutions&lt;/code&gt;: Counter incremented for each resolution attempt&lt;/li&gt;
&lt;li&gt;&lt;code&gt;dns_resolution_duration&lt;/code&gt;: Trend measuring the time taken for DNS resolution&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These metrics help you monitor DNS performance and identify potential bottlenecks in your testing scenarios.&lt;/p&gt;
&lt;h2 id=&#34;best-practices&#34;&gt;Best practices&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Nameserver format&lt;/strong&gt;: Always specify the port number, typically 53 for DNS, in the nameserver parameter.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Timeout behavior&lt;/strong&gt;: DNS queries have built-in timeouts; consider this when designing load tests.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Load testing&lt;/strong&gt;: Use multiple VUs and iterations to properly test DNS server performance under load.&lt;/li&gt;
&lt;/ul&gt;
]]></content><description>&lt;h1 id="resolve-query-recordtype-nameserver-">resolve( query, recordType, nameserver )&lt;/h1>
&lt;p>The &lt;code>dns.resolve&lt;/code> function performs DNS resolution using a specified DNS server and returns a promise that resolves to an array of IP addresses. This function allows you to test specific DNS servers, validate DNS configurations, and measure DNS resolution performance under load.&lt;/p></description></item><item><title>lookup( hostname )</title><link>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-x-dns/lookup/</link><pubDate>Mon, 21 Sep 2026 15:16:28 +0000</pubDate><guid>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-x-dns/lookup/</guid><content><![CDATA[&lt;h1 id=&#34;lookup-hostname-&#34;&gt;lookup( hostname )&lt;/h1&gt;
&lt;p&gt;The &lt;code&gt;dns.lookup&lt;/code&gt; function performs DNS resolution using the system&amp;rsquo;s default DNS configuration and returns a promise that resolves to an array of IP addresses. This function is useful, for instance, for testing standard DNS resolution behavior and comparing it with custom DNS server results.&lt;/p&gt;
&lt;h2 id=&#34;parameters&#34;&gt;Parameters&lt;/h2&gt;
&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th style=&#34;text-align: left&#34;&gt;Parameter&lt;/th&gt;
              &lt;th style=&#34;text-align: left&#34;&gt;Type&lt;/th&gt;
              &lt;th style=&#34;text-align: left&#34;&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td style=&#34;text-align: left&#34;&gt;hostname&lt;/td&gt;
              &lt;td style=&#34;text-align: left&#34;&gt;string&lt;/td&gt;
              &lt;td style=&#34;text-align: left&#34;&gt;The domain name to resolve. For example, &amp;ldquo;example.com&amp;rdquo; or &amp;ldquo;k6.io&amp;rdquo;.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;h2 id=&#34;returns&#34;&gt;Returns&lt;/h2&gt;
&lt;p&gt;A promise resolving to an array of strings, where each string is an IP address that the domain name resolves to. The function returns the same IP addresses that would be returned by the system&amp;rsquo;s standard DNS resolution mechanism.&lt;/p&gt;
&lt;h2 id=&#34;metrics&#34;&gt;Metrics&lt;/h2&gt;
&lt;p&gt;When using &lt;code&gt;dns.lookup&lt;/code&gt;, the following metrics are automatically generated:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;dns_lookups&lt;/code&gt;: Counter incremented for each lookup attempt&lt;/li&gt;
&lt;li&gt;&lt;code&gt;dns_lookup_duration&lt;/code&gt;: Trend measuring the time taken for DNS lookup&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These metrics help you monitor DNS performance using your system&amp;rsquo;s DNS configuration.&lt;/p&gt;
&lt;h2 id=&#34;examples&#34;&gt;Examples&lt;/h2&gt;
&lt;!-- md-k6:skipall --&gt;
&lt;h3 id=&#34;basic-lookup&#34;&gt;Basic lookup&lt;/h3&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;JavaScript&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-javascript&#34;&gt;import dns from &amp;#39;k6/x/dns&amp;#39;;

export default async function () {
  // Resolve using system DNS servers
  const addresses = await dns.lookup(&amp;#39;k6.io&amp;#39;);
  console.log(&amp;#39;k6.io resolves to:&amp;#39;, addresses);
  // Output: k6.io resolves to: [&amp;#34;104.21.7.127&amp;#34;, &amp;#34;172.67.154.74&amp;#34;]
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;h3 id=&#34;comparing-system-vs-custom-dns&#34;&gt;Comparing system vs custom DNS&lt;/h3&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;JavaScript&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-javascript&#34;&gt;import dns from &amp;#39;k6/x/dns&amp;#39;;
import { expect } from &amp;#39;https://jslib.k6.io/k6-testing/0.6.1/index.js&amp;#39;;

export default async function () {
  const domain = &amp;#39;example.com&amp;#39;;

  // Get results from system DNS
  const systemResults = await dns.lookup(domain);

  // Get results from Google&amp;#39;s DNS
  const googleResults = await dns.resolve(domain, &amp;#39;A&amp;#39;, &amp;#39;8.8.8.8:53&amp;#39;);

  console.log(&amp;#39;System DNS results:&amp;#39;, systemResults);
  console.log(&amp;#39;Google DNS results:&amp;#39;, googleResults);

  // Check if both methods return results
  expect(systemResults.length).toBeGreaterThan(0);
  expect(googleResults.length).toBeGreaterThan(0);

  // Compare results (they might differ due to different DNS configurations)
  const hasCommonAddress = systemResults.some(ip =&amp;gt; googleResults.includes(ip));
  expect(hasCommonAddress).toBeTruthy();
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;h3 id=&#34;testing-dns-consistency&#34;&gt;Testing DNS consistency&lt;/h3&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;JavaScript&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-javascript&#34;&gt;import dns from &amp;#39;k6/x/dns&amp;#39;;
import { expect } from &amp;#39;https://jslib.k6.io/k6-testing/0.6.1/index.js&amp;#39;;

export const options = {
  vus: 1,
  iterations: 10,
};

export default async function () {
  const domain = &amp;#39;k6.io&amp;#39;;

  try {
    const results = await dns.lookup(domain);

    expect(results.length).toBeGreaterThan(0);
    expect(results.every(ip =&amp;gt;
      /^\d&amp;#43;\.\d&amp;#43;\.\d&amp;#43;\.\d&amp;#43;$/.test(ip) || /^[0-9a-fA-F:]&amp;#43;$/.test(ip)
    )).toBeTruthy();

    console.log(`Iteration ${__ITER}: ${domain} -&amp;gt; ${results.join(&amp;#39;, &amp;#39;)}`);
  } catch (error) {
    console.error(&amp;#39;DNS lookup failed:&amp;#39;, error);
  }
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;h3 id=&#34;load-testing-with-system-dns&#34;&gt;Load testing with system DNS&lt;/h3&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;JavaScript&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-javascript&#34;&gt;import dns from &amp;#39;k6/x/dns&amp;#39;;
import { sleep } from &amp;#39;k6&amp;#39;;
import { expect } from &amp;#39;https://jslib.k6.io/k6-testing/0.6.1/index.js&amp;#39;;
import { Trend, Rate } from &amp;#39;k6/metrics&amp;#39;;

const lookupDuration = new Trend(&amp;#39;dns_lookup_duration_custom&amp;#39;);
const successRate = new Rate(&amp;#39;dns_lookup_success_rate&amp;#39;);

export const options = {
  vus: 10,
  duration: &amp;#39;60s&amp;#39;,
};

const domains = [
  &amp;#39;k6.io&amp;#39;,
  &amp;#39;example.com&amp;#39;,
  &amp;#39;google.com&amp;#39;,
  &amp;#39;github.com&amp;#39;,
  &amp;#39;stackoverflow.com&amp;#39;,
];

export default async function () {
  const domain = domains[Math.floor(Math.random() * domains.length)];
  const startTime = Date.now();

  try {
    const results = await dns.lookup(domain);
    const duration = Date.now() - startTime;

    lookupDuration.add(duration);
    successRate.add(true);

    expect(results.length).toBeGreaterThan(0);

    console.log(`${domain} resolved in ${duration}ms to ${results.length} addresses`);
  } catch (error) {
    const duration = Date.now() - startTime;
    lookupDuration.add(duration);
    successRate.add(false);

    console.error(`Failed to resolve ${domain}: ${error.message}`);
  }

  sleep(1);
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;h3 id=&#34;validating-dns-configuration&#34;&gt;Validating DNS configuration&lt;/h3&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;JavaScript&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-javascript&#34;&gt;import dns from &amp;#39;k6/x/dns&amp;#39;;
import { expect } from &amp;#39;https://jslib.k6.io/k6-testing/0.6.1/index.js&amp;#39;;

export default async function () {
  const testDomains = [
    &amp;#39;k6.io&amp;#39;,
    &amp;#39;grafana.com&amp;#39;,
    &amp;#39;example.com&amp;#39;,
  ];

  for (const domain of testDomains) {
    try {
      const results = await dns.lookup(domain);

      expect(results.length).toBeGreaterThan(0);
      expect(results.every(ip =&amp;gt; {
        // Basic IPv4/IPv6 validation
        return /^\d&amp;#43;\.\d&amp;#43;\.\d&amp;#43;\.\d&amp;#43;$/.test(ip) || /^[0-9a-fA-F:]&amp;#43;$/.test(ip);
      })).toBeTruthy();

      console.log(`✓ ${domain}: ${results.join(&amp;#39;, &amp;#39;)}`);
    } catch (error) {
      console.error(`✗ ${domain}: ${error.message}`);
    }
  }
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;h2 id=&#34;error-handling&#34;&gt;Error handling&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;lookup&lt;/code&gt; function may throw errors in the following cases:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Invalid hostname format&lt;/li&gt;
&lt;li&gt;DNS resolution timeout&lt;/li&gt;
&lt;li&gt;No DNS servers configured on the system&lt;/li&gt;
&lt;li&gt;Network connectivity issues&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;JavaScript&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-javascript&#34;&gt;import dns from &amp;#39;k6/x/dns&amp;#39;;

export default async function () {
  try {
    const results = await dns.lookup(&amp;#39;nonexistent.invalid.domain.test&amp;#39;);
    console.log(&amp;#39;Unexpected success:&amp;#39;, results);
  } catch (error) {
    console.log(&amp;#39;Expected DNS lookup error:&amp;#39;, error.message);
  }

  // Test with invalid hostname format
  try {
    const results = await dns.lookup(&amp;#39;&amp;#39;);
    console.log(&amp;#39;Unexpected success with empty hostname:&amp;#39;, results);
  } catch (error) {
    console.log(&amp;#39;Expected error for empty hostname:&amp;#39;, error.message);
  }
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
]]></content><description>&lt;h1 id="lookup-hostname-">lookup( hostname )&lt;/h1>
&lt;p>The &lt;code>dns.lookup&lt;/code> function performs DNS resolution using the system&amp;rsquo;s default DNS configuration and returns a promise that resolves to an array of IP addresses. This function is useful, for instance, for testing standard DNS resolution behavior and comparing it with custom DNS server results.&lt;/p></description></item></channel></rss>