vectordotdev/vector · error

API key should be only valid ASCII characters

Error message

API key should be only valid ASCII characters

What it means

DatadogMetricsService::into_http_request builds the HTTP request with a DD-API-KEY header. When a per-event API key override exists, it is converted to a HeaderValue; HeaderValue::from_str only accepts visible ASCII, so a non-ASCII key panics with this message.

Solutions

  1. Verify the API key override field contains only printable ASCII
  2. Trim the key value before use
  3. Remove the per-event key override and use the configured static key
  4. Validate with key.is_ascii() before dispatch

Example fix

// before
HeaderValue::from_str(&key).expect("API key should be only valid ASCII characters")
// after
if key.is_ascii() { HeaderValue::from_str(&key).expect("valid ascii") } else { return Err(...) }
Defensive patterns

Strategy: validation

Validate before calling

// rust: check before calling into_http_request
if !key.is_ascii() || key.chars().any(|c| c.is_control()) {
    return Err(anyhow!("api_key override must be printable ASCII"));
}

Type guard

fn is_valid_header_value(s: &str) -> bool { HeaderValue::from_str(s).is_ok() }

Try / catch

let hv = HeaderValue::from_str(&key).map_err(|_| anyhow!("API key must be valid ASCII"))?;

Prevention

When it happens

Trigger: An event carries a datum 'api_key' override containing non-ASCII characters (e.g. UTF-8, control chars, spaces) which is passed as HeaderValue::from_str inside the sink service.

Common situations: Misconfigured template/field-based API keys that resolve to unicode or garbage values; copy-pasted keys with invisible non-ASCII characters.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of vectordotdev/vector@bdb87aeaa4 (2026-09-16). Data as JSON: /api/errors/e519531bc9c8fb8d. Report an issue: GitHub.

Appendix: source

Thrown at src/sinks/datadog/metrics/service.rs:64

    pub content_type: &'static str,
    pub content_encoding: &'static str,
    pub finalizers: EventFinalizers,
    pub metadata: RequestMetadata,
}

impl DatadogMetricsRequest {
    /// Converts this request to a `hyper`-compatible request.
    ///
    /// # Errors
    ///
    /// If any of the header names or values are invalid, or if the URI is invalid, an error variant
    /// will be returned.
    pub fn into_http_request(self, api_key: HeaderValue) -> http::Result<Request<Body>> {
        // use the API key from the incoming event if it is provided
        let api_key = self.api_key.map_or_else(
            || api_key,
            |key| {
                HeaderValue::from_str(&key).expect("API key should be only valid ASCII characters")
            },
        );
        let request = Request::post(self.uri)
            .header("DD-API-KEY", api_key)
            // TODO: The Datadog Agent sends this header to indicate the version of the Go library
            // it uses which contains the Protocol Buffers definitions used for the Sketches API.
            // We've copypasted the proto file for now -- `proto/ddsketch.rs`, a partial chunk of
            // `DataDog/agent-payload/proto/metrics/agent_payload.proto` -- and are thus hardcoding
            // the version that we copypasted from.
            //
            // In the future, we should likely figure out a way to depend on/submodule-ize the
            // `agent-payload` repo so we can always have an up-to-date proto definition, and be
            // able to programmatically set the version of the repo so we don't need to hardcode
            // this header.
            .header("DD-Agent-Payload", "4.87.0")
            .header(CONTENT_TYPE, self.content_type)
            .header(CONTENT_ENCODING, self.content_encoding);

View on GitHub (pinned to bdb87aeaa4)