Telemetry does not record errors on spans/metrics per OpenTelemetry conventions
Describe the bug
This summary heavily relies on the "Recording error" OpenTelemetry documentation, hence it's crucial to read it before diving into the text below.
When an operation fails, Terragrunt attaches an exception event to the span but never sets the span status, and never sets error.type on spans or metrics. As a result a failed run is indistinguishable from a successful one in any OpenTelemetry-native backend (New Relic), because backends classify errors by span status, not by the presence of an exception event.
The presence of a span exception event doesn't qualify the span as an error on its own. Only spans with span status set to ERROR are classified as errors. https://docs.newrelic.com/docs/opentelemetry/get-started/apm-monitoring/opentelemetry-apm-ui/#distributed-tracing-page
Current behaviour
Telemeter.Collect records the error and returns:
// internal/telemetry/tracer.go
if err := fn(ctx); err != nil {
// record error in span
span.RecordError(err)
return err
}There is no span.SetStatus(codes.Error, ...), and no error.type attribute is added. On the metric side, failures are reported as separate counters (unit_run_errors_count, apply_errors_count, errors_count, …) rather than as an attribute on the corresponding duration histogram.
Why this is a problem
The OpenTelemetry "Recording error" document states that when an operation ends with an error, instrumentation:
- SHOULD set the span status code to Error
- SHOULD set the
error.typeattribute - SHOULD set the span status description to the exception message
and, for metrics, that the operation duration histogram SHOULD include error.type so users can derive error rates, rather than reporting separate success and failure metrics.
Backends follow that contract literally. New Relic, for example, documents that:
span is classified as an error when its status is set to ERROR" and, explicitly, that "the presence of a span exception event doesn't qualify the span as an error on its own" (docs). Error rate, the errors inbox and error alerting are all driven by span status.
Evidence
From a failed run --all apply in our estate (Terragrunt 1.1.4, OTLP/HTTP exporter to New Relic), for a single pipeline run:
FROM Span SELECT count(*) FACET name, span.kind, otel.status_code["apply ","internal",null]– 1 ← apply command root span["unit_run","internal",null]– 7 unit run spans["run_terraform","internal",null]– 40 ... 2478 spans,otel.status_codeis null on every one.
The failure is real and visible elsewhere in the same run: unit_run_errors_count = 1 as a metric, and exit_code = 1 as an attribute on the run_terraform span. Only the status - the field backends actually read - is missing.
Incidentally, the only spans in that our dataset that do carry ERROR status come from google.cloud.kms.v1.KeyManagementService/Decrypt from sops_decrypt_file function call, i.e. the Google Cloud SDK's own gRPC instrumentation, which picks up Terragrunt's globally registered tracer provider and follows the convention correctly.
Proposed change
In Telemeter.Collect/ Tracer.Collect, on the error path:
span.SetStatus(codes.Error, err.Error())alongside the existingRecordError.- Set
error.typefrom the error's type (its canonical Go type name is a good low-cardinality default, e.g.run.DownloadingTerraformSourceErr), keepingRecordErrorfor the full message and stack. - On the metric side, add
error.typeto the existing*_durationhistograms on failure, per the same document. The existing*_errors_countcounters could stay for backwards compatibility.
Happy to open a PR for 1 and 2 if the approach looks right.
Steps To Reproduce
# modules/broken/main.tf
output "boom" {
value = nonexistent_function("boom") # Fails during evaluation: Error: Call to unknown function: There is no function named "nonexistent_function".
}# terragrunt.hcl
terraform {
source = "../../modules/broken"
}$ export OTEL_EXPORTER_OTLP_ENDPOINT="https://otlp.nr-data.net"
$ export OTEL_EXPORTER_OTLP_HEADERS="<REDACTED>"
$ export TG_TELEMETRY_TRACE_EXPORTER=otlpHttp
$ export TG_TELEMETRY_METRIC_EXPORTER=otlpHttp
$ terragrunt run --all -- plan
...
* Failed to execute "terraform plan -input=false" in ./.terragrunt-cache/jkB-ZNizuQnQrOvukyLa0Ls1tqo/VXx5GLWGJaBDdM7cJUK7InD4R60
╷
│ Error: Call to unknown function
│
│ on main.tf line 6, in output "boom":
│ 6: value = nonexistent_function("boom")
│
│ There is no function named "nonexistent_function".
╵
exit status 1Could be verified locally as well:
$ export TG_TELEMETRY_TRACE_EXPORTER=console
$ terragrunt run --all -- plan
...Status: { Code: Unset }...Expected behavior
Spans and histogram metrics adhere Recording error OpenTelemetry semantic, hence backend can mark signals correctly.
Versions
- Terragrunt version: 1.1.4
- OpenTofu/Terraform version: 1.7.0
Additional context
Related findings
tracestateis dropped. The incoming header is parsed for trace id, span id and flags only (trace.SpanContextConfigininternal/telemetry/tracer.go), and onlyTRACEPARENTis propagated to child processes, so vendor sampling and priority state does not survive the Terragrunt hop. The W3C spec expects propagators to carrytracestatethrough unmodified.- Trace flags are parsed as a decimal number:
strconv.Atoi(traceFlagsStr). The W3C field is two hex digits, so a valid value containing a hex letter (0a,ff) fails with invalid trace flags, and02(the random flag from trace context level 2) is silently treated as sampled.
Resources
Source: gruntwork-io/terragrunt