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
oauth2option inloki.write.endpointto exchange service account credentials for short-lived JWT access tokens automatically. - Instance Access Token: Uses the
bearer_tokenoption inloki.write.endpointwith a static bearer token created in your Logs instance.
Prerequisites
Section titled “Prerequisites”- STACKIT Logs instance: Create your first STACKIT Logs instance
- Data source URL: Retrieve API URLs for your STACKIT Logs instance
- Credentials:
- For Service Account authentication (recommended):
- A STACKIT Service Account: Create a service account
- A Service Account Key pair: Create a service account key
- An Access Policy configured on your STACKIT Logs instance granting the Service Account write permissions.
- For Access Token authentication:
- An access token with write permissions: Retrieve credentials for STACKIT Logs instance
- For Service Account authentication (recommended):
Authenticate with a STACKIT Service Account (Recommended)
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.
Configuration example
Section titled “Configuration example”// 1. Load the STACKIT Service Account private key filelocal.file "stackit_service_account_private_key" { filename = "<path-to-sa-private-key.pem>" is_secret = true}
// 2. Configure Loki write receiver with oauth2loki.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 receiverloki.source.file "example_log_files" { targets = {__path__ = "/logs/foo.txt", "color" = "pink"}, // reference the loki write receiver forward_to = [loki.write.stackit_logs.receiver]}Setup steps
Section titled “Setup steps”- Open or create the Grafana Alloy configuration file (typically named
config.alloy). - Add a
local.fileblock to load the STACKIT Service Account private key file:- Set
filenameto the file path of your private key PEM file (for example,<path-to-sa-private-key.pem>). - Set
is_secrettotrueto mark the file content as secret.
- Set
- Add a
loki.writeblock (for example, named"stackit_logs") with anendpointblock:- Set
urlto your STACKIT Logs instance Ingest URL. - Configure the
oauth2block insideendpointwith 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: Thesubfield 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 inlocal.file(local.file.stackit_service_account_private_key.content).
- Set
- 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.
Configuration example
Section titled “Configuration example”// 1. Load the STACKIT Logs access token from a filelocal.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 tokenloki.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 receiverloki.source.file "example_log_files" { targets = {__path__ = "/logs/foo.txt", "color" = "pink"}, // Reference the loki write receiver forward_to = [loki.write.stackit_logs.receiver]}Setup steps
Section titled “Setup steps”- Open or create the Grafana Alloy configuration file (typically named
config.alloy). - Provide your STACKIT Logs instance access token using one of the following methods:
- From a file: Add a
local.fileblock (for example, namedstackit_logs_secret), specify the token file path infilename, and setis_secret = true. - From an environment variable: Access the token using
sys.env("LOKI_ACCESS_TOKEN").
- From a file: Add a
- Add a
loki.writeblock (for example, named"stackit_logs") with anendpointblock:- Set
urlto your STACKIT Logs instance Ingest URL. - Set
bearer_tokento reference either the file content (local.file.stackit_logs_secret.content) or the environment variable (sys.env("LOKI_ACCESS_TOKEN")).
- Set
- Forward log sources (such as
loki.source.file) to the Loki write receiver (loki.write.stackit_logs.receiver).