Skip to content

OpenTelemetry

Terragrunt can be configured to emit telemetry in OpenTelemetry format in the form of traces, metrics, and logs.

OpenTelemetry tracing is typically used in Terragrunt to analyze performance. For more details on analyzing and optimizing performance, read the dedicated Performance documentation.

The following are concepts and technologies that are important to be aware of when using OpenTelemetry with Terragrunt.

Tracing configuration:

  • TG_TELEMETRY_TRACE_EXPORTER - traces exporter type to be used. Currently supported values are:
    • none - no trace exporting, default value.
    • console - to export traces to console as JSON
    • otlpHttp - to export traces to an OpenTelemetry collector over HTTP otlptracehttp
    • otlpGrpc - to export traces over gRPC otlptracegrpc
    • http - to export traces to a custom HTTP endpoint using otlptracehttp
  • TG_TELEMETRY_TRACE_EXPORTER_HTTP_ENDPOINT - in case of http exporter, this is the endpoint to which traces will be sent.
  • TG_TELEMETRY_TRACE_EXPORTER_INSECURE_ENDPOINT - if set to true, the exporter will not validate the server’s certificate, helpful for local traces collection.
  • TRACEPARENT - if set, the value will be used as a parent trace context, format TRACEPARENT=00-<hex_trace_id>-<hex_span_id>-<trace_flags>, example: TRACEPARENT=00-xxx-yyy-01

Metrics configuration:

  • TG_TELEMETRY_METRIC_EXPORTER - metrics exporter type to be used. Currently supported values are:
    • none - no metric exporting, default value.
    • console - write metrics to console as JSONs.
    • otlpHttp - export metrics to an OpenTelemetry collector over HTTP otlpmetrichttp
    • grpcHttp - export metrics to an OpenTelemetry collector over gRPC otlpmetricgrpc
  • TG_TELEMETRY_METRIC_EXPORTER_INSECURE_ENDPOINT - if set to true, the exporter will not validate the server’s certificate, helpful for local metrics collection.

Logs configuration:

  • TG_TELEMETRY_LOGS_EXPORTER - logs exporter type to be used. Currently supported values are:
    • none - no log exporting, default value.
    • console - write log records to console as JSONs.
    • otlpHttp - export logs to an OpenTelemetry collector over HTTP otlploghttp
    • otlpGrpc - export logs to an OpenTelemetry collector over gRPC otlploggrpc
  • TG_TELEMETRY_LOGS_EXPORTER_INSECURE_ENDPOINT - if set to true, the exporter disables TLS and sends logs over an insecure connection, for local logs collection only.

The OTLP log exporters read the standard OTLP exporter environment variables. The OTEL_EXPORTER_OTLP_LOGS_* variables apply to logs only and override the shared OTEL_EXPORTER_OTLP_* ones:

  • OTEL_EXPORTER_OTLP_ENDPOINT - the collector address, e.g. http://localhost:4318 for otlpHttp or http://localhost:4317 for otlpGrpc. The HTTP exporter adds the /v1/logs path.
  • OTEL_EXPORTER_OTLP_LOGS_ENDPOINT - the collector address for logs only. For otlpHttp it is used as is, so include the path, e.g. http://localhost:4318/v1/logs. For otlpGrpc, use http://localhost:4317.
  • OTEL_EXPORTER_OTLP_HEADERS or OTEL_EXPORTER_OTLP_LOGS_HEADERS - headers for every export, such as credentials, as comma-separated key=value pairs with URL-encoded values (e.g. authorization=Bearer%20<token>).
  • OTEL_EXPORTER_OTLP_CERTIFICATE, OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE, and OTEL_EXPORTER_OTLP_CLIENT_KEY (and their _LOGS_ forms) - TLS certificate files for collectors that use a private CA or mutual TLS.
  • OTEL_EXPORTER_OTLP_COMPRESSION or OTEL_EXPORTER_OTLP_LOGS_COMPRESSION - set to gzip to compress exports.
  • OTEL_EXPORTER_OTLP_TIMEOUT or OTEL_EXPORTER_OTLP_LOGS_TIMEOUT - the export timeout in milliseconds.

Each record carries the trace and span IDs of the span it was logged in, so logs link to traces in the backend. Records logged outside a unit, such as by the run queue, link to the root command span. Records carry these IDs only when a trace exporter is also set.

Only records at or above the log level are exported, so --log-level also controls how many records reach the backend. Record bodies are plain text, without ANSI color codes. Records are sent in batches, and buffered records are flushed when the command exits. This also happens when a run is interrupted, and the flush waits at most 30 seconds. Records that do not fit in the buffer are dropped. These variables tune the buffer:

  • OTEL_BLRP_MAX_QUEUE_SIZE - the number of records buffered before new records are dropped. Defaults to 2048.
  • OTEL_BLRP_MAX_EXPORT_BATCH_SIZE - the maximum number of records in one export. Defaults to 512.
  • OTEL_BLRP_SCHEDULE_DELAY - the delay in milliseconds between exports. Defaults to 1000.
  • OTEL_BLRP_EXPORT_TIMEOUT - the timeout in milliseconds for one export. Defaults to 30000.

Resource attributes:

  • OTEL_SERVICE_NAME - overrides the service.name resource attribute attached to every span, metric, and log record. Defaults to terragrunt. Useful for distinguishing multiple Terragrunt invocations in the same backend (e.g. per-environment or per-user).
  • OTEL_RESOURCE_ATTRIBUTES - additional resource attributes to attach to every span, metric, and log record, formatted as a comma-separated list of key=value pairs (e.g. deployment.environment=local,service.namespace=tensho). Values set here override Terragrunt’s defaults; if both OTEL_SERVICE_NAME and service.name in OTEL_RESOURCE_ATTRIBUTES are set, OTEL_SERVICE_NAME takes precedence per the OpenTelemetry specification.

Example collection of traces, metrics, and logs with Docker Compose

Section titled “Example collection of traces, metrics, and logs with Docker Compose”

This example runs an OpenTelemetry collector that receives Terragrunt’s traces, metrics, and logs. The collector prints traces and metrics, and forwards logs to Quickwit, which stores them and shows them in a web UI.

  • Create docker-compose.yml. The ports are published on 127.0.0.1 only, since the connection is not encrypted:

    services:
    otel-collector:
    image: otel/opentelemetry-collector:0.164.0
    volumes:
    - ./otel-collector-config.yaml:/etc/otelcol/config.yaml
    ports:
    - "127.0.0.1:4317:4317" # OTLP gRPC receiver
    - "127.0.0.1:4318:4318" # OTLP HTTP receiver
    quickwit:
    image: quickwit/quickwit:0.9.3
    command: run
    ports:
    - "127.0.0.1:7280:7280" # Quickwit UI
  • Create otel-collector-config.yaml:

    receivers:
    otlp:
    protocols:
    grpc:
    endpoint: 0.0.0.0:4317
    http:
    endpoint: 0.0.0.0:4318
    processors:
    batch:
    exporters:
    debug:
    verbosity: detailed
    otlp_grpc/quickwit:
    endpoint: quickwit:7281
    tls:
    insecure: true
    service:
    pipelines:
    traces:
    receivers: [otlp]
    processors: [batch]
    exporters: [debug]
    metrics:
    receivers: [otlp]
    processors: [batch]
    exporters: [debug]
    logs:
    receivers: [otlp]
    processors: [batch]
    exporters: [otlp_grpc/quickwit]
  • Start both services:

    Terminal window
    docker compose up -d
  • Define environment variables for Terragrunt to send traces, metrics, and logs to the collector over HTTP:

    Terminal window
    export TG_TELEMETRY_TRACE_EXPORTER=otlpHttp
    export TG_TELEMETRY_TRACE_EXPORTER_INSECURE_ENDPOINT=true
    export TG_TELEMETRY_METRIC_EXPORTER=otlpHttp
    export TG_TELEMETRY_METRIC_EXPORTER_INSECURE_ENDPOINT=true
    export TG_TELEMETRY_LOGS_EXPORTER=otlpHttp
    export TG_TELEMETRY_LOGS_EXPORTER_INSECURE_ENDPOINT=true
    export TG_EXPERIMENT=otel-logs
    export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318

    To use gRPC instead, set the trace and logs exporters to otlpGrpc, the metrics exporter to grpcHttp, and OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317.

  • Run terragrunt, for example terragrunt run --all plan.

  • Check the traces and metrics the collector printed:

    Terminal window
    docker compose logs otel-collector

    Each span shows its name and the trace and span IDs, trimmed here to its main fields:

    Span #28
    Trace ID : 15cb00e41f8533116fdd86b348e97054
    Parent ID : 366e2f8dd84da254
    ID : d523f63b92d576c9
    Name : unit_run

    Each metric shows its name and values:

    Descriptor:
    -> Name: run_tofu_success_count
    -> DataType: Sum
    NumberDataPoints #0
    Value: 4
  • Check the logs in the Quickwit UI at http://localhost:7280/ui. New records can take about 10 seconds to appear. In Query editor, pick the index whose name starts with otel-logs-, type a query, and select Run:

    • * shows all log records.
    • severity_text:error shows errors only.
    • attributes.prefix:app shows the logs of the app unit.
    • trace_id:15cb00e41f8533116fdd86b348e97054 shows the logs of one run. Use a trace ID from the collector output.

    The same queries work from the command line, where otel-logs-* matches the index by name:

    Terminal window
    curl -G 'http://localhost:7280/api/v1/otel-logs-*/search' --data-urlencode 'query=severity_text:error'
  • Stop both services with docker compose down.

To print traces, metrics, and logs to the terminal instead, set the exporters to console.