grafana/k6 · error

invalid hostname pattern '%s'

Error message

invalid hostname pattern '%s'

What it means

Raised by isValidHostnamePattern when a hostnames/blockHostnames entry does not match the allowed hostname wildcard pattern (optional *. prefix plus dot-separated labels). It is a regex-based format guard for hostname trie entries.

Source

Thrown at lib/types/hostnametrie.go:117

	}
	for _, s := range h.source {
		if err := h.insert(s); err != nil {
			return nil, err
		}
	}
	return h, nil
}

// Regex description of hostname pattern to enforce blocks by.
// Global var to avoid compilation penalty at runtime.
// Based on regex from https://stackoverflow.com/a/106223/5427244
//
//nolint:lll
var validHostnamePattern = regexp.MustCompile(`^(\*\.?)?((([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9]))?$`)

func isValidHostnamePattern(s string) error {
	if len(validHostnamePattern.FindString(s)) != len(s) {
		return fmt.Errorf("invalid hostname pattern '%s'", s)
	}
	return nil
}

// insert inserts a hostname pattern into the HostnameTrie. It returns an error
// if the hostname pattern is invalid.
func (t *HostnameTrie) insert(s string) error {
	s = strings.ToLower(s)
	if err := isValidHostnamePattern(s); err != nil {
		return err
	}

	t.trieNode.insert(s)
	return nil
}

// Contains returns whether s matches a pattern in the HostnameTrie
// along with the matching pattern, if one was found.

View on GitHub (pinned to 01ffac6f24)

Solutions

  1. Use a plain hostname or a wildcard like *.example.com
  2. Avoid patterns with multiple wildcards, paths, ports or invalid characters in hostnames
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at lib/types/hostnametrie.go:117 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of grafana/k6@01ffac6f24 (2026-08-18). Data as JSON: /api/errors/f36ee89baccb302f. Report an issue: GitHub.