k6 环境变量导致 GO 错误 "invalid character"

k6 environmental variables results in GO error "invalid character"

k6 文档使环境变量的使用变得非常简单,我尝试按照他们的说明进行操作,但是当我尝试 运行 时出现 GO 错误:

ERRO[0000] GoError: parse https://${__ENV.TARGET_ENV}-api.mycompany.com/v1/managers/259999/properties": invalid character "{" in host name".

我在任何地方都没有看到额外的括号。当我将 url 作为 https://green-api.mycompany.com/v1/managers/259999/properties. Am I possibly missing an import or dependency somewhere? All I am trying to do is get it working to where when i type k6 run --env TARGET_ENV=green propertiesScript.js, it executes against http://green-api.mycompany.com/v1/managers/259999/properties 时,此脚本运行良好。这是文件:

import { check } from "k6";

export let options = {
  thresholds: {
    http_req_duration: ["p(90)<300"], // 95% of requests should be below 200ms
    Errors: ["count<100"],
  },
};

export default function () {
  var url =
    "https://${__ENV.TARGET_ENV}-api.mycompany.com/v1/managers/259999/properties";

  const params = {
    headers: {
      "X-App-Token": "<our app token>",
      "X-Auth-Token":
        "<our auth token>",
      accept: "application/json",
    },
  };

  let res = http.get(url, params);
  console.log(res.body);
  console.log(JSON.stringify(res.headers));

  check(res, {
    "status is 200": (r) => r.status === 200,
  });
}

我还尝试添加场景并调整我的文件主体。这些是场景:

  thresholds: {
    http_req_duration: ["p(90)<300"], // 95% of requests should be below 200ms
    Errors: ["count<100"],
  },
  scenarios: {
    pod_green: {
      tags: { my_custom_tag: "green" },
      env: { MYVAR: "green" },
      executor: "shared-iterations",
    },
    pod_red: {
      tags: { my_custom_tag: "red" },
      env: { MYVAR: "red" },
      executor: "shared-iterations",
    },
    staging: {
      tags: { my_custom_tag: "staging" },
      env: { MYVAR: "staging" },
      executor: "shared-iterations",
    },
  },
};

然后我编辑了我的导出默认函数,我能够将该脚本添加到 运行,但它 运行 对每个环境都是如此。

当您设置 url 变量以使字符串插值在模板文字中起作用时,您需要使用反引号。参见 the documentation

所以在你的情况下你应该:

var url =
    `https://${__ENV.TARGET_ENV}-api.mycompany.com/v1/managers/259999/properties`;