Skip to content

Grafana Alloy: Write log data to Logs instance

Last updated on

Grafana Alloy is a telemetry collector which supports collecting logs and exporting them to multiple destinations. It has built-in support for Loki as an exporter which can be configured to target STACKIT Logs instance.

To authenticate requests, STACKIT Logs supports two authentication methods:

  • STACKIT Service Account (recommended): Uses the oauth2 option in loki.write.endpoint to exchange service account credentials for short-lived JWT access tokens automatically.
  • Instance Access Token: Uses the bearer_token option in loki.write.endpoint with a static bearer token created in your Logs instance.

Section titled “Authenticate with a STACKIT Service Account (Recommended)”

Using a STACKIT Service Account is the recommended approach for automated and production workloads. The oauth2 configuration in loki.write automatically obtains short-lived JWTs from the STACKIT IAM endpoint, removing the need to manage and rotate long-lived access tokens.

config.alloy
// 1. Load the STACKIT Service Account private key file
local.file "stackit_service_account_private_key" {
filename = "<path-to-sa-private-key.pem>"
is_secret = true
}
// 2. Configure Loki write receiver with oauth2
loki.write "stackit_logs" {
endpoint {
// Set here the complete 'Ingest URL' of STACKIT Logs instance
url = "<ingest-url-of-logs-instance>"
oauth2 {
grant_type = "urn:ietf:params:oauth:grant-type:jwt-bearer" // For using Service Account key authentication
audience = "https://accounts.stackit.cloud"
token_url = "https://accounts.stackit.cloud/oauth/v2/token"
client_id = "<sa-credentials-sub-claim>" // The `sub` field from service account key JSON file
iss = "<service-account-id>" // Service account ID or email
client_certificate_key = local.file.stackit_service_account_private_key.content
}
}
}
// 3. Example: read logs from file and forward to loki write's receiver
loki.source.file "example_log_files" {
targets = {__path__ = "/logs/foo.txt", "color" = "pink"},
// reference the loki write receiver
forward_to = [loki.write.stackit_logs.receiver]
}
  1. Open or create the Grafana Alloy configuration file (typically named config.alloy).
  2. Add a local.file block to load the STACKIT Service Account private key file:
    • Set filename to the file path of your private key PEM file (for example, <path-to-sa-private-key.pem>).
    • Set is_secret to true to mark the file content as secret.
  3. Add a loki.write block (for example, named "stackit_logs") with an endpoint block:
    • Set url to your STACKIT Logs instance Ingest URL.
    • Configure the oauth2 block inside endpoint with your service account credentials:
      • grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer"
      • audience: "https://accounts.stackit.cloud"
      • token_url: "https://accounts.stackit.cloud/oauth/v2/token"
      • client_id: The sub field from your service account key JSON file.
      • iss: The service account ID or service account email.
      • client_certificate_key: Reference the private key file loaded in local.file (local.file.stackit_service_account_private_key.content).
  4. Forward log sources (such as loki.source.file) to the Loki write receiver (loki.write.stackit_logs.receiver).

Authenticate with an Instance Access Token

Section titled “Authenticate with an Instance Access Token”

You can also authenticate using a static access token generated directly from your STACKIT Logs instance. The bearer_token option in loki.write.endpoint attaches this token to the Authorization: Bearer <token> header of every exported HTTP request.

config.alloy
// 1. Load the STACKIT Logs access token from a file
local.file "stackit_logs_secret" {
// File containing STACKIT Logs access token
filename = "/var/secrets/stackit_logs.token"
is_secret = true
}
// 2. Configure Loki write receiver with bearer token
loki.write "stackit_logs" {
endpoint {
// Set here the complete 'Ingest URL' of STACKIT Logs instance
url = "<ingest-url-of-logs-instance>"
// Reference the token file
bearer_token = local.file.stackit_logs_secret.content
// Or access the token if provided via an environment variable:
// bearer_token = sys.env("LOKI_ACCESS_TOKEN")
}
}
// 3. Example: read logs from file and forward to loki write's receiver
loki.source.file "example_log_files" {
targets = {__path__ = "/logs/foo.txt", "color" = "pink"},
// Reference the loki write receiver
forward_to = [loki.write.stackit_logs.receiver]
}
  1. Open or create the Grafana Alloy configuration file (typically named config.alloy).
  2. Provide your STACKIT Logs instance access token using one of the following methods:
    • From a file: Add a local.file block (for example, named stackit_logs_secret), specify the token file path in filename, and set is_secret = true.
    • From an environment variable: Access the token using sys.env("LOKI_ACCESS_TOKEN").
  3. Add a loki.write block (for example, named "stackit_logs") with an endpoint block:
    • Set url to your STACKIT Logs instance Ingest URL.
    • Set bearer_token to reference either the file content (local.file.stackit_logs_secret.content) or the environment variable (sys.env("LOKI_ACCESS_TOKEN")).
  4. Forward log sources (such as loki.source.file) to the Loki write receiver (loki.write.stackit_logs.receiver).