### Deploy the monitoring example Source: https://grafana.com/docs/alloy/latest/monitor/monitor-logs-from-file Navigate to the specific scenario directory and start the required services using Docker Compose. ```shell cd alloy-scenarios/logs-file docker compose up -d ``` -------------------------------- ### Full OTLP exporter configuration example Source: https://grafana.com/docs/alloy/latest/collect/opentelemetry-data Demonstrates a complete setup with an authenticated OTLP exporter and a receiver forwarding data to it. ```alloy otelcol.exporter.otlp "default" { client { endpoint = "my-otlp-grpc-server:4317" auth = otelcol.auth.basic.credentials.handler } } otelcol.auth.basic "credentials" { // Retrieve credentials using environment variables. username = sys.env("BASIC_AUTH_USER") password = sys.env("API_KEY") } otelcol.receiver.otlp "example" { grpc { endpoint = "127.0.0.1:4317" } http { endpoint = "127.0.0.1:4318" } output { metrics = [otelcol.exporter.otlp.default.input] logs = [otelcol.exporter.otlp.default.input] traces = [otelcol.exporter.otlp.default.input] } } ``` -------------------------------- ### Deploy Grafana Tempo Source: https://grafana.com/docs/alloy/latest/monitor/monitor-kubernetes-logs Install Tempo in the prod namespace to generate logs for the example. ```shell helm install tempo grafana/tempo-distributed -n prod ``` -------------------------------- ### string.format usage examples Source: https://grafana.com/docs/alloy/latest/reference/stdlib/string Basic examples demonstrating string interpolation using %s and %d format verbs. ```alloy > string.format("Hello, %s!", "Ander") "Hello, Ander!" > string.format("There are %d lights", 4) "There are 4 lights" ``` -------------------------------- ### Full Kubernetes metrics collection example Source: https://grafana.com/docs/alloy/latest/collect/prometheus-metrics A complete configuration example for discovering production Pods in the default namespace and sending metrics to a remote write endpoint. ```alloy discovery.kubernetes "pods" { role = "pod" namespaces { own_namespace = false names = ["default"] } selectors { role = "pod" label = "environment in (production)" } } prometheus.scrape "pods" { targets = discovery.kubernetes.pods.targets forward_to = [prometheus.remote_write.default.receiver] } prometheus.remote_write "default" { endpoint { url = "http://localhost:9009/api/prom/push" } } ``` -------------------------------- ### Install and enable the Alloy service Source: https://grafana.com/docs/alloy/latest/set-up/install/chef Use these resources to install the alloy package and ensure the service is enabled and running. ```ruby package 'alloy' do action :install flush_cache [ :before ] if platform_family?('amazon', 'rhel', 'fedora') notifies :restart, 'service[alloy]', :delayed end service 'alloy' do service_name 'alloy' action [:enable, :start] end ``` -------------------------------- ### Iterate over Kubernetes targets with foreach Source: https://grafana.com/docs/alloy/latest/reference/config-blocks/foreach This example demonstrates using foreach to dynamically start a prometheus.exporter.redis instance for each discovered Kubernetes pod and apply custom labels. ```alloy discovery.kubernetes "default" { role = "pod" } discovery.relabel "redis" { targets = discovery.kubernetes.default.targets // Remove all targets except the Redis ones. rule { source_labels = ["__meta_kubernetes_pod_container_name"] regex = "redis-cont" action = "keep" } } // Collect metrics for each Redis instance. foreach "redis" { collection = discovery.relabel.redis.output var = "each" template { prometheus.exporter.redis "default" { // This is the "__address__" label from discovery.kubernetes. redis_addr = each["__address__"] } prometheus.scrape "default" { targets = prometheus.exporter.redis.default.targets forward_to = [prometheus.relabel.default.receiver] } // Add labels from discovery.kubernetes. prometheus.relabel "default" { rule { replacement = each["__meta_kubernetes_namespace"] target_label = "k8s_namespace" action = "replace" } rule { replacement = each["__meta_kubernetes_pod_container_name"] target_label = "k8s_pod_container_name" action = "replace" } forward_to = [prometheus.remote_write.mimir.receiver] } } } prometheus.remote_write "mimir" { endpoint { url = "https://prometheus-xxx.grafana.net/api/prom/push" basic_auth { username = sys.env("") password = sys.env("") } } } ``` -------------------------------- ### Full meta-monitoring configuration example Source: https://grafana.com/docs/alloy/latest/collect/metamonitoring A complete example showing the self-exporter, scrape component, and remote_write configuration to send metrics to a Mimir endpoint. ```alloy prometheus.exporter.self "default" { } prometheus.scrape "metamonitoring" { targets = prometheus.exporter.self.default.targets forward_to = [prometheus.remote_write.default.receiver] } prometheus.remote_write "default" { endpoint { url = "http://mimir:9009/api/v1/push" } } ``` -------------------------------- ### Docker log line example Source: https://grafana.com/docs/alloy/latest/reference/components/loki/loki.process.md Example of a Docker JSON log entry and the resulting extracted key-value pairs. ```text {"log":"log message\n","stream":"stderr","time":"2019-04-30T02:12:41.8443515Z"} output: log message\n stream: stderr timestamp: 2019-04-30T02:12:41.8443515 ``` -------------------------------- ### Clone the Alloy scenarios repository Source: https://grafana.com/docs/alloy/latest/monitor/monitor-syslog-messages Download the repository containing the example deployment configurations. ```shell git clone https://github.com/grafana/alloy-scenarios.git ``` -------------------------------- ### Install Grafana Alloy via Homebrew Source: https://grafana.com/docs/alloy/latest/set-up/install/macos Add the Grafana tap and install the Alloy package. ```shell brew tap grafana/grafana ``` ```shell brew install grafana/grafana/alloy ``` -------------------------------- ### Promtail configuration example Source: https://grafana.com/docs/alloy/latest/set-up/migrate/from-promtail A sample Promtail configuration file used as input for the conversion process. ```yaml clients: - url: http://localhost/loki/api/v1/push scrape_configs: - job_name: example static_configs: - targets: - localhost labels: __path__: /var/log/*.log ``` -------------------------------- ### Example logging configuration Source: https://grafana.com/docs/alloy/latest/collect/metamonitoring A practical example showing the logging block configured to send JSON-formatted logs to a Loki write component. ```alloy logging { level = "warn" format = "json" write_to = [loki.write.default.receiver] } loki.write "default" { endpoint { url = "http://loki:3100/loki/api/v1/push" } } ``` -------------------------------- ### GET /-/ready Source: https://grafana.com/docs/alloy/latest/reference/http Checks if the Alloy instance has loaded its initial configuration. ```APIDOC ## GET /-/ready ### Description Checks if the Alloy instance has loaded its initial configuration. Returns 200 OK if ready, or 503 Service Unavailable if not. ### Method GET ### Endpoint /-/ready ``` -------------------------------- ### Start the Alloy service Source: https://grafana.com/docs/alloy/latest/set-up/run/linux Initiates the Alloy service using systemd. ```shell sudo systemctl start alloy ``` -------------------------------- ### Start services with Podman Compose Source: https://grafana.com/docs/alloy/latest/set-up/install/podman Launches the containers defined in the compose.yaml file. ```shell podman-compose up -d ``` -------------------------------- ### Deploy Loki Source: https://grafana.com/docs/alloy/latest/monitor/monitor-kubernetes-logs Install Loki in the meta namespace using the specified configuration file. ```shell helm install --values loki-values.yml loki grafana/loki -n meta ``` -------------------------------- ### Navigate to the logs directory Source: https://grafana.com/docs/alloy/latest/monitor/monitor-kubernetes-logs Change the working directory to the Kubernetes logs example folder. ```shell cd alloy-scenarios/k8s/logs ``` -------------------------------- ### Deploy Grafana Source: https://grafana.com/docs/alloy/latest/monitor/monitor-kubernetes-logs Install Grafana in the meta namespace to visualize logs. ```shell helm install --values grafana-values.yml grafana grafana/grafana --namespace meta ``` -------------------------------- ### Deploy Alloy Source: https://grafana.com/docs/alloy/latest/monitor/monitor-kubernetes-logs Install the Kubernetes monitoring Helm chart to collect logs. ```shell helm install --values ./k8s-monitoring-values.yml k8s grafana/k8s-monitoring -n meta --create-namespace ``` -------------------------------- ### Deploy the monitoring stack Source: https://grafana.com/docs/alloy/latest/monitor/monitor-linux Navigate to the Linux scenario directory and start the containers in detached mode. ```shell cd alloy-scenarios/linux docker compose up -d ``` -------------------------------- ### Example JSON output Source: https://grafana.com/docs/alloy/latest/reference/cli/gql Sample output returned by the gql command. ```json { "data": { "alloy": { "isReady": true, "version": "v1.14.0" } } } ``` -------------------------------- ### Enable GraphQL API Source: https://grafana.com/docs/alloy/latest/reference/cli/gql Start Alloy with the necessary flags to enable the GraphQL API. ```shell alloy run --stability.level=experimental --server.http.enable-graphql ``` -------------------------------- ### Start Docker containers Source: https://grafana.com/docs/alloy/latest/tutorials/send-logs-to-loki Launch the defined services in detached mode. ```bash docker compose up -d ``` -------------------------------- ### Define Alloy configuration file Source: https://grafana.com/docs/alloy/latest/configure/kubernetes Example content for a standalone Alloy configuration file. ```alloy // Write your Alloy config here: logging { level = "info" format = "logfmt" } ``` -------------------------------- ### Deploy the Grafana stack Source: https://grafana.com/docs/alloy/latest/monitor/monitor-logs-over-tcp Navigate to the specific scenario directory and start the Docker containers. ```shell cd alloy-scenarios/logs-tcp docker compose up -d ``` -------------------------------- ### Start Redis container Source: https://grafana.com/docs/alloy/latest/tutorials/first-components-and-stdlib Run a Redis container in the background for metric scraping. ```bash docker container run -d --name alloy-redis -p 6379:6379 --rm redis ``` -------------------------------- ### Create a local.file component Source: https://grafana.com/docs/alloy/latest/get-started/components/configure-components An example of instantiating a local.file component with a specific label and filename. ```alloy local.file "config" { filename = "/etc/app/settings.yaml" } ``` -------------------------------- ### Example discovery.kubernetes output Source: https://grafana.com/docs/alloy/latest/reference/config-blocks/foreach A sample JSON representation of targets output by the discovery.kubernetes component. ```json [ { __address__ = "10.42.0.16:5432", __meta_kubernetes_namespace = "ns1", __meta_kubernetes_pod_container_id = "containerd://96b77d035d0bbe27bb173d8fc0c56d21965892a50e4e6eab9f6cffdb90b275fb", __meta_kubernetes_pod_container_image = "postgres:bullseye", __meta_kubernetes_pod_container_init = "false", __meta_kubernetes_pod_container_name = "pgcont", __meta_kubernetes_pod_container_port_name = "pg-db", __meta_kubernetes_pod_container_port_number = "5432", __meta_kubernetes_pod_container_port_protocol = "TCP", __meta_kubernetes_pod_controller_kind = "ReplicaSet", __meta_kubernetes_pod_controller_name = "postgres-db-cd54547b9", __meta_kubernetes_pod_host_ip = "172.25.0.2", __meta_kubernetes_pod_ip = "10.42.0.16", __meta_kubernetes_pod_label_name = "postgres-db", __meta_kubernetes_pod_label_pod_template_hash = "cd54547b9", __meta_kubernetes_pod_labelpresent_name = "true", __meta_kubernetes_pod_labelpresent_pod_template_hash = "true", __meta_kubernetes_pod_name = "postgres-db-cd54547b9-4zpds", __meta_kubernetes_pod_node_name = "k3d-asserts-test-server-0", __meta_kubernetes_pod_phase = "Running", __meta_kubernetes_pod_ready = "true", __meta_kubernetes_pod_uid = "7cdcacdc-4a2d-460a-b1fb-6340700c4cac", }, { __address__ = "10.42.0.20:6379", __meta_kubernetes_namespace = "ns1", __meta_kubernetes_pod_container_id = "containerd://68f2f0eacd880eb4a141d833aafc1f297f7d9bdf00f4c787f9fcc964a039d278", __meta_kubernetes_pod_container_image = "redis:latest", __meta_kubernetes_pod_container_init = "false", __meta_kubernetes_pod_container_name = "redis-cont", __meta_kubernetes_pod_container_port_name = "redis-db", __meta_kubernetes_pod_container_port_number = "6379", __meta_kubernetes_pod_container_port_protocol = "TCP", __meta_kubernetes_pod_controller_kind = "ReplicaSet", __meta_kubernetes_pod_controller_name = "redis-db-778b66cb7d", __meta_kubernetes_pod_host_ip = "172.25.0.2", __meta_kubernetes_pod_ip = "10.42.0.20", __meta_kubernetes_pod_label_name = "redis-db", __meta_kubernetes_pod_label_pod_template_hash = "778b66cb7d", __meta_kubernetes_pod_labelpresent_name = "true", __meta_kubernetes_pod_labelpresent_pod_template_hash = "true", __meta_kubernetes_pod_name = "redis-db-778b66cb7d-wxmf6", __meta_kubernetes_pod_node_name = "k3d-asserts-test-server-0", __meta_kubernetes_pod_phase = "Running", __meta_kubernetes_pod_ready = "true", __meta_kubernetes_pod_uid = "ae74e400-8eda-4b02-b4c8-669473fb001b", } ] ``` -------------------------------- ### Install systemd service file Source: https://grafana.com/docs/alloy/latest/set-up/install/podman Moves the generated service file to the user systemd directory. ```shell mv container-alloy.service ~/.config/systemd/user/ ``` -------------------------------- ### Enable the GraphQL Playground Source: https://grafana.com/docs/alloy/latest/reference/http/graphql Start the Alloy instance with both the GraphQL API and the interactive playground enabled. ```sh alloy run --server.http.enable-graphql --server.http.enable-graphql-playground config.alloy ``` -------------------------------- ### Example log entry input Source: https://grafana.com/docs/alloy/latest/reference/components/loki/loki.process.md Sample input data structure for testing truncation rules. ```text entry: "I'm a log message!" labels: { "label1": "hi", "label2": "hello world" } structured_metadata: { "metadata1": "here is some metadata", "metadata2": "and here is some more"} ``` -------------------------------- ### Initialize project directory Source: https://grafana.com/docs/alloy/latest/tutorials/send-logs-to-loki Create a new directory and an empty docker-compose.yml file to begin the setup. ```bash mkdir alloy-tutorial cd alloy-tutorial touch docker-compose.yml ``` -------------------------------- ### Define the Grafana Alloy Puppet class Source: https://grafana.com/docs/alloy/latest/set-up/install/puppet This class handles repository setup, package installation, and service management for Debian and RedHat OS families. ```ruby class grafana_alloy::grafana_alloy () { case $::os['family'] { 'debian': { apt::source { 'grafana': location => 'https://apt.grafana.com/', release => '', repos => 'stable main', key => { id => 'B53AE77BADB630A683046005963FA27710458545', source => 'https://apt.grafana.com/gpg.key', }, } -> package { 'alloy': require => Exec['apt_update'], } -> service { 'alloy': ensure => running, name => 'alloy', enable => true, subscribe => Package['alloy'], } } 'redhat': { yumrepo { 'grafana': ensure => 'present', name => 'grafana', descr => 'grafana', baseurl => 'https://rpm.grafana.com', gpgkey => 'https://rpm.grafana.com/gpg.key', enabled => '1', gpgcheck => '1', target => '/etc/yum.repos.d/grafana.repo', } -> package { 'alloy': } -> service { 'alloy': ensure => running, name => 'alloy', enable => true, subscribe => Package['alloy'], } } default: { fail("Unsupported OS family: (${$::os['family']})") } } } ``` -------------------------------- ### Start Grafana Alloy service Source: https://grafana.com/docs/alloy/latest/set-up/run/macos Starts the Alloy service as a launchd process. The service will automatically start on system boot. ```shell brew services start grafana/grafana/alloy ``` -------------------------------- ### Perform silent installation on Windows Source: https://grafana.com/docs/alloy/latest/set-up/install/windows Execute the installer in silent mode using either Command Prompt or PowerShell. Replace with the actual location of the installer executable. ```cmd \alloy-installer-windows-amd64.exe /S ``` ```powershell & \alloy-installer-windows-amd64.exe /S ``` -------------------------------- ### Create Kubernetes namespaces Source: https://grafana.com/docs/alloy/latest/monitor/monitor-kubernetes-logs Set up the meta and prod namespaces for the monitoring stack. ```shell kubectl create namespace meta && \ kubectl create namespace prod ``` -------------------------------- ### Install GPG on Linux Source: https://grafana.com/docs/alloy/latest/set-up/install/linux Required for Debian-based systems that do not have GPG installed by default. ```shell sudo apt install gpg ``` -------------------------------- ### Configure components with proper naming and labels Source: https://grafana.com/docs/alloy/latest/get-started/syntax Examples of valid component naming and labeling conventions. ```alloy // Component name: "local.file", Label: "api_key" local.file "api_key" { filename = sys.env("API_KEY_PATH") is_secret = true } // Component name: "prometheus.scrape", Label: "web_servers" prometheus.scrape "web_servers" { targets = discovery.kubernetes.services.targets metrics_path = "/metrics" } ``` -------------------------------- ### Enable and start systemd service Source: https://grafana.com/docs/alloy/latest/set-up/install/podman Reloads the systemd daemon and starts the Alloy service. ```shell systemctl --user daemon-reload systemctl --user enable --now container-alloy.service ``` -------------------------------- ### Initialize and start Podman machine Source: https://grafana.com/docs/alloy/latest/set-up/install/podman Required on macOS and Windows to set up the Linux virtual machine environment for Podman. ```shell podman machine init podman machine start ``` -------------------------------- ### Configuring log level and format Source: https://grafana.com/docs/alloy/latest/reference/config-blocks/logging Example of setting the log level to info and the format to logfmt. ```alloy logging { level = "info" format = "logfmt" } ``` -------------------------------- ### Example Custom Target Configuration Source: https://grafana.com/docs/alloy/latest/collect/prometheus-metrics Demonstrates defining multiple custom endpoints with specific schemes, paths, and labels. ```alloy prometheus.scrape "custom_targets" { targets = [ { __address__ = "prometheus:9090", }, { __address__ = "mimir:8080", __scheme__ = "https", }, { __address__ = "custom-application:80", __metrics_path__ = "/custom-metrics-path", }, { __address__ = "alloy:12345", application = "alloy", environment = "production", }, ] forward_to = [prometheus.remote_write.default.receiver] } prometheus.remote_write "default" { endpoint { url = "http://localhost:9009/api/prom/push" } } ``` -------------------------------- ### Install Alloy Package Source: https://grafana.com/docs/alloy/latest/set-up/install/linux Install the Grafana Alloy package using the system's package manager. ```debian-ubuntu sudo apt-get install alloy ``` ```rhel-fedora sudo dnf install alloy ``` ```suse-opensuse sudo zypper -r grafana install alloy ``` -------------------------------- ### CRI log line example Source: https://grafana.com/docs/alloy/latest/reference/components/loki/loki.process.md Example of a CRI log line and the resulting extracted key-value pairs. ```text "2019-04-30T02:12:41.8443515Z stdout F message" content: message stream: stdout time: 2019-04-30T02:12:41.8443515 ``` -------------------------------- ### Install Alloy via Helm Source: https://grafana.com/docs/alloy/latest/set-up/install/kubernetes Installs the Alloy Helm chart into the specified namespace with a custom release name. ```shell helm install --namespace grafana/alloy ``` -------------------------------- ### Configure vCenter receiver with batch processing Source: https://grafana.com/docs/alloy/latest/reference/components/otelcol/otelcol.receiver.vcenter This example demonstrates how to receive telemetry from vCenter, pass it through a batch processor, and export it via OTLP. ```alloy otelcol.receiver.vcenter "default" { endpoint = "http://localhost:15672" username = "otelu" password = "password" output { metrics = [otelcol.processor.batch.default.input] } } otelcol.processor.batch "default" { output { metrics = [otelcol.exporter.otlphttp.default.input] } } otelcol.exporter.otlphttp "default" { client { endpoint = sys.env("") } } ``` -------------------------------- ### Create config.alloy file Source: https://grafana.com/docs/alloy/latest/tutorials/send-logs-to-loki Initializes an empty configuration file in the current directory. ```bash touch config.alloy ``` -------------------------------- ### Decolorize log line example Source: https://grafana.com/docs/alloy/latest/reference/components/loki/loki.process.md Example showing the transformation of a log line containing ANSI color codes. ```text [2022-11-04 22:17:57.811] \033[0;32http\033[0m: GET /_health (0 ms) 204 ``` ```text [2022-11-04 22:17:57.811] http: GET /_health (0 ms) 204 ``` -------------------------------- ### Install Grafana Alloy via WinGet Source: https://grafana.com/docs/alloy/latest/set-up/install/windows Use the WinGet package manager to install Grafana Alloy on Windows systems. ```cmd winget install GrafanaLabs.Alloy ``` ```powershell winget install GrafanaLabs.Alloy ``` -------------------------------- ### Available template functions and complex examples Source: https://grafana.com/docs/alloy/latest/reference/components/loki/loki.process.md Lists supported template functions and provides examples of conditional logic within the replace field. ```text ToLower, ToUpper, Replace, Trim, TrimLeftTrimRight, TrimPrefix, TrimSuffix, TrimSpace, Hash, Sha2Hash, regexReplaceAll, regexReplaceAllLiteral | "{{ if eq .Value \"200\" }}{{ Replace .Value \"200\" \"HttpStatusOk\" -1 }}{{ else }}{{ .Value | ToUpper }}{{ end }}" | | "*IP4*{{ .Value | Hash \"salt\" }}*" | ``` -------------------------------- ### Run Alloy container with Podman Source: https://grafana.com/docs/alloy/latest/set-up/install/podman Starts the Alloy container with necessary volume mounts and port mappings. ```shell podman run -d --name alloy \ -v :/etc/alloy/config.alloy:Z \ -p 12345:12345 \ docker.io/grafana/alloy:latest \ run --server.http.listen-addr=0.0.0.0:12345 --storage.path=/var/lib/alloy/data \ /etc/alloy/config.alloy ``` -------------------------------- ### Install Alloy via Ansible Playbook Source: https://grafana.com/docs/alloy/latest/set-up/install/ansible Use the grafana.grafana.alloy role to install Alloy and define its configuration. Replace the placeholder with your actual Prometheus remote write endpoint. ```yaml - name: Install Alloy hosts: all become: true tasks: - name: Install Alloy ansible.builtin.include_role: name: grafana.grafana.alloy vars: alloy_config: | prometheus.scrape "default" { targets = [{"__address__" = "localhost:12345"}] forward_to = [prometheus.remote_write.prom.receiver] } prometheus.remote_write "prom" { endpoint { url = "" } } ``` -------------------------------- ### string.trim_prefix Source: https://grafana.com/docs/alloy/latest/reference/stdlib/string Removes the prefix from the start of a string. ```APIDOC ## string.trim_prefix(string, prefix) ### Description Removes the prefix from the start of a string. If the string doesn’t start with the prefix, the string is returned unchanged. ### Example string.trim_prefix("helloworld", "hello") // Returns "world" ``` -------------------------------- ### Install Alloy via Helm Source: https://grafana.com/docs/alloy/latest/collect/azure-event-hubs-logs Deploys the Alloy chart into the specified namespace using the provided configuration file. ```shell helm install alloy grafana/alloy \ --namespace alloy \ -f values.yaml ``` -------------------------------- ### Convert configuration with integrations-next Source: https://grafana.com/docs/alloy/latest/set-up/migrate/from-static Use the convert command with extra-args to enable integrations-next features during the migration process. ```shell alloy convert --source-format=static --extra-args="-enable-features=integrations-next" --output= ``` -------------------------------- ### string.trim_space Source: https://grafana.com/docs/alloy/latest/reference/stdlib/string Removes any whitespace characters from the start and end of a string. ```APIDOC ## string.trim_space(string) ### Description Removes any whitespace characters from the start and end of a string. ### Example string.trim_space(" hello\n\n") // Returns "hello" ``` -------------------------------- ### GET /-/healthy Source: https://grafana.com/docs/alloy/latest/reference/http Checks the health status of all Alloy components. ```APIDOC ## GET /-/healthy ### Description Checks if all Alloy components are working correctly. Returns 200 OK if healthy, or 500 Internal Server Error if any component is unhealthy. ### Method GET ### Endpoint /-/healthy ``` -------------------------------- ### Enable Alloy at boot Source: https://grafana.com/docs/alloy/latest/set-up/run/linux Configures the system to automatically start the Alloy service upon system boot. ```shell sudo systemctl enable alloy.service ``` -------------------------------- ### Configure loki.process and loki.write components Source: https://grafana.com/docs/alloy/latest/tutorials/processing-logs Full configuration example for receiving logs via API, processing them through multiple stages, and writing to a local Loki instance. ```alloy // Let's send and process more logs! loki.source.api "listener" { http { listen_address = "127.0.0.1" listen_port = 9999 } labels = { "source" = "api" } forward_to = [loki.process.process_logs.receiver] } loki.process "process_logs" { // Stage 1 stage.json { expressions = { log = "", ts = "timestamp", } } // Stage 2 stage.timestamp { source = "ts" format = "RFC3339" } // Stage 3 stage.json { source = "log" expressions = { is_secret = "", level = "", log_line = "message", } } // Stage 4 stage.drop { source = "is_secret" value = "true" } // Stage 5 stage.labels { values = { level = "", } } // Stage 6 stage.output { source = "log_line" } // This stage adds static values to the labels on the log line stage.static_labels { values = { source = "demo-api", } } forward_to = [loki.write.local_loki.receiver] } loki.write "local_loki" { endpoint { url = "http://localhost:3100/loki/api/v1/push" } } ``` -------------------------------- ### string.trim Source: https://grafana.com/docs/alloy/latest/reference/stdlib/string Removes the specified set of characters from the start and end of a string. ```APIDOC ## string.trim(string, str_character_set) ### Description Removes the specified set of characters from the start and end of a string. ### Example string.trim("?!hello?!", "!?") // Returns "hello" ``` -------------------------------- ### Duration metric example Source: https://grafana.com/docs/alloy/latest/reference/components/otelcol/otelcol.connector.spanmetrics Displays the format of the duration histogram metric. ```text duration { service.name="shipping", span.name="get_shipping/{shippingId}", span.kind="SERVER", status.code="Ok" } ``` -------------------------------- ### Define a PrometheusRule CRD Source: https://grafana.com/docs/alloy/latest/reference/components/mimir/mimir.rules.kubernetes Example of a PrometheusRule resource with specific labels. ```yaml apiVersion: monitoring.coreos.com/v1 kind: PrometheusRule metadata: labels: application.kubernetes.io/name: my-app ``` -------------------------------- ### Create log directory and file Source: https://grafana.com/docs/alloy/latest/tutorials/logs-and-relabeling-basics Use bash commands to prepare a directory and a log file for scraping. ```bash mkdir -p /tmp/alloy-logs echo "This is a log line" > /tmp/alloy-logs/log.log ``` -------------------------------- ### GET /debug/pprof Source: https://grafana.com/docs/alloy/latest/reference/http Returns a pprof Go profile for performance analysis. ```APIDOC ## GET /debug/pprof ### Description Returns a pprof Go profile that you can use to visualize and analyze profiling data. ### Method GET ### Endpoint /debug/pprof ``` -------------------------------- ### Run the convert command Source: https://grafana.com/docs/alloy/latest/reference/cli/convert Basic syntax for executing the convert command to transform configuration files. ```shell alloy convert [ ...] ``` -------------------------------- ### Create a local Kubernetes cluster Source: https://grafana.com/docs/alloy/latest/monitor/monitor-kubernetes-logs Initialize a kind cluster using the provided configuration file. ```shell kind create cluster --config kind.yml ``` -------------------------------- ### Example log line input Source: https://grafana.com/docs/alloy/latest/tutorials/processing-logs Sample JSON log structure used for demonstrating processing stages. ```json { "log": { "is_secret": "true", "level": "info", "message": "This is a secret message!", }, "timestamp": "2023-11-16T06:01:50Z", } ```