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.
High-level overview
Section titled “High-level overview”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 JSONotlpHttp- to export traces to an OpenTelemetry collector over HTTP otlptracehttpotlpGrpc- to export traces over gRPC otlptracegrpchttp- to export traces to a custom HTTP endpoint using otlptracehttp
TG_TELEMETRY_TRACE_EXPORTER_HTTP_ENDPOINT- in case ofhttpexporter, 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, formatTRACEPARENT=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 otlpmetrichttpgrpcHttp- 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 otlploghttpotlpGrpc- 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:4318forotlpHttporhttp://localhost:4317forotlpGrpc. The HTTP exporter adds the/v1/logspath.OTEL_EXPORTER_OTLP_LOGS_ENDPOINT- the collector address for logs only. ForotlpHttpit is used as is, so include the path, e.g.http://localhost:4318/v1/logs. ForotlpGrpc, usehttp://localhost:4317.OTEL_EXPORTER_OTLP_HEADERSorOTEL_EXPORTER_OTLP_LOGS_HEADERS- headers for every export, such as credentials, as comma-separatedkey=valuepairs with URL-encoded values (e.g.authorization=Bearer%20<token>).OTEL_EXPORTER_OTLP_CERTIFICATE,OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE, andOTEL_EXPORTER_OTLP_CLIENT_KEY(and their_LOGS_forms) - TLS certificate files for collectors that use a private CA or mutual TLS.OTEL_EXPORTER_OTLP_COMPRESSIONorOTEL_EXPORTER_OTLP_LOGS_COMPRESSION- set togzipto compress exports.OTEL_EXPORTER_OTLP_TIMEOUTorOTEL_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 to2048.OTEL_BLRP_MAX_EXPORT_BATCH_SIZE- the maximum number of records in one export. Defaults to512.OTEL_BLRP_SCHEDULE_DELAY- the delay in milliseconds between exports. Defaults to1000.OTEL_BLRP_EXPORT_TIMEOUT- the timeout in milliseconds for one export. Defaults to30000.
Resource attributes:
OTEL_SERVICE_NAME- overrides theservice.nameresource attribute attached to every span, metric, and log record. Defaults toterragrunt. 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 ofkey=valuepairs (e.g.deployment.environment=local,service.namespace=tensho). Values set here override Terragrunt’s defaults; if bothOTEL_SERVICE_NAMEandservice.nameinOTEL_RESOURCE_ATTRIBUTESare set,OTEL_SERVICE_NAMEtakes 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 on127.0.0.1only, since the connection is not encrypted:services:otel-collector:image: otel/opentelemetry-collector:0.164.0volumes:- ./otel-collector-config.yaml:/etc/otelcol/config.yamlports:- "127.0.0.1:4317:4317" # OTLP gRPC receiver- "127.0.0.1:4318:4318" # OTLP HTTP receiverquickwit:image: quickwit/quickwit:0.9.3command: runports:- "127.0.0.1:7280:7280" # Quickwit UI -
Create
otel-collector-config.yaml:receivers:otlp:protocols:grpc:endpoint: 0.0.0.0:4317http:endpoint: 0.0.0.0:4318processors:batch:exporters:debug:verbosity: detailedotlp_grpc/quickwit:endpoint: quickwit:7281tls:insecure: trueservice: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=otlpHttpexport TG_TELEMETRY_TRACE_EXPORTER_INSECURE_ENDPOINT=trueexport TG_TELEMETRY_METRIC_EXPORTER=otlpHttpexport TG_TELEMETRY_METRIC_EXPORTER_INSECURE_ENDPOINT=trueexport TG_TELEMETRY_LOGS_EXPORTER=otlpHttpexport TG_TELEMETRY_LOGS_EXPORTER_INSECURE_ENDPOINT=trueexport TG_EXPERIMENT=otel-logsexport OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318To use gRPC instead, set the trace and logs exporters to
otlpGrpc, the metrics exporter togrpcHttp, andOTEL_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-collectorEach span shows its name and the trace and span IDs, trimmed here to its main fields:
Span #28Trace ID : 15cb00e41f8533116fdd86b348e97054Parent ID : 366e2f8dd84da254ID : d523f63b92d576c9Name : unit_runEach metric shows its name and values:
Descriptor:-> Name: run_tofu_success_count-> DataType: SumNumberDataPoints #0Value: 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:errorshows errors only.attributes.prefix:appshows the logs of theappunit.trace_id:15cb00e41f8533116fdd86b348e97054shows 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.