Aller au contenu
DocsTry Aspire
DocsTry

Set up Grafana k6 in the Aspire AppHost

Ce contenu n’est pas encore disponible dans votre langue.

⭐ Community Toolkit k6 logo

This reference describes the Community Toolkit k6 hosting integration APIs for the Aspire AppHost. If you’re new to the integration, start with Get started with the k6 integration.

Add 📦 CommunityToolkit.Aspire.Hosting.k6 to the AppHost:

Terminal
aspire add k6

Or add the package manually:

AppHost.cs
#:package CommunityToolkit.Aspire.Hosting.k6@*

Learn more about aspire add in the command reference.

Use AddK6 / addK6 to add a Grafana k6 container resource. The resource uses the docker.io/grafana/k6 image, exposes its HTTP API as the http endpoint, and is visible in the dashboard.

AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var k6 = builder.AddK6("k6");
builder.Build().Run();

name identifies the resource and is used as its connection name when another resource references it.

AddK6 / addK6 configures these behaviors:

  • An internal http endpoint targets port 6565. Specify port to bind a fixed host port; otherwise Aspire allocates one.
  • Aspire checks GET /health on that endpoint.
  • The resource is a container resource, so its logs, lifecycle actions, endpoint link, and health state appear in the dashboard.
  • The integration configures an OTLP exporter for the container. Use WithK6OtlpEnvironment / withK6OtlpEnvironment when k6 should consume the corresponding k6 environment variables.

The examples read PrimaryEndpoint / primaryEndpoint() when an AppHost API needs the k6 HTTP endpoint directly.

The optional enableBrowserExtensions parameter selects the k6 image variant with browser support. The optional port parameter selects the host port for the HTTP API.

AppHost.cs
var k6 = builder.AddK6(
"browser-tests",
enableBrowserExtensions: true,
port: 6565);
var endpoint = k6.Resource.PrimaryEndpoint;

The default image does not include browser support. Enable it only for tests that use the k6 browser module.

WithScript / withScript configures the container command as k6 run, binds it to 0.0.0.0:6565, and sets the virtual-user and duration arguments. virtualUsers defaults to 10 and duration defaults to 30s.

Mount the script directory with the standard container WithBindMount / withBindMount API before selecting a path in the container:

AppHost.cs
var k6 = builder.AddK6("load-test")
.WithBindMount("./scripts", "/scripts", isReadOnly: true)
.WithScript("/scripts/main.js", virtualUsers: 25, duration: "45s");

scriptPath must be a path the container can read. The integration doesn’t set a container working directory, so use an absolute container path or a path valid for the configured image. WithScript / withScript rejects a null script path and a zero or negative virtual-user count.

WithK6OtlpEnvironment / withK6OtlpEnvironment copies every existing OTEL_* environment variable for the resource to a corresponding K6_OTEL_* variable. This enables the k6 OpenTelemetry output to use the OTLP configuration that Aspire has already set for the resource.

AppHost.cs
var k6 = builder.AddK6("load-test")
.WithK6OtlpEnvironment();

Use the standard WithReference / withReference and WaitFor / waitFor APIs to model the relationship between the test and the resources it exercises. Referenced resources provide their connection information through Aspire’s normal resource configuration.

The integration doesn’t add k6-specific publish or export callbacks. It remains a normal Aspire container resource with its configured image, endpoint, and container settings available to publishers.