fix(otlptrace,otlpmetric): reject negative and overflowing timeout env values (#9338)

`OTEL_EXPORTER_OTLP_TIMEOUT=-1` gives the trace and metric OTLP
exporters a timeout of -1ms, and so does `9223372036854775807`, because
multiplying it by `time.Millisecond` wraps around. The clients only set
an export deadline when `exportTimeout > 0`, so either value quietly
removes the timeout and an export can wait until the caller's context
ends. Nothing is logged.

`WithDuration` in `internal/shared/otlp/envconfig/envconfig.go.tmpl` now
rejects negative values and values above `math.MaxInt64 /
time.Millisecond`. It reports them through `global.Error` and skips the
value, so the default 10s timeout or a valid generic
`OTEL_EXPORTER_OTLP_TIMEOUT` stays in effect. This matches `envDuration`
in `sdk/metric/env.go`. Zero is still accepted as before.

I switched the parse from `strconv.Atoi` to `strconv.ParseInt(v, 10,
64)` so the overflow bound behaves the same on 32-bit targets. The
template was regenerated with `make generate` into `otlptracegrpc`,
`otlptracehttp`, `otlpmetricgrpc` and `otlpmetrichttp`.

This is the trace and metric counterpart of the log exporter fixes in
#9254 and #9155, which only touch `otlploggrpc` and `otlploghttp` and do
not share this code.

Testing: the shared `envconfig_test.go.tmpl` now covers 0, -1, the
largest accepted value 9223372036854, the first overflowing value
9223372036855, and the max int64. The shared trace and metric
`options_test.go.tmpl` check that a negative or overflowing
`OTEL_EXPORTER_OTLP_TIMEOUT` leaves `DefaultTimeout` in place, and that
a negative signal-specific timeout keeps the generic one. The new
rejection cases fail on main in all four modules (got -1ms, want 10s)
and pass with the change.

Reverting only the non-test change makes them fail again.

`go test -race ./...`, `go vet`, `gofmt` and `golangci-lint` v2.15.0 are
clean for all four modules.
17 files changed
tree: 493679a561db4cfa7a49993f0963218916d4351b
  1. .github/
  2. attribute/
  3. baggage/
  4. bridge/
  5. codes/
  6. exporters/
  7. internal/
  8. log/
  9. metric/
  10. propagation/
  11. schema/
  12. sdk/
  13. semconv/
  14. trace/
  15. .clomonitor.yml
  16. .codespellignore
  17. .codespellrc
  18. .gitattributes
  19. .gitignore
  20. .golangci.yml
  21. .lycheeignore
  22. .markdownlint.yaml
  23. AGENTS.md
  24. CHANGELOG.md
  25. CODEOWNERS
  26. CONTRIBUTING.md
  27. dependencies.Dockerfile
  28. doc.go
  29. error_handler.go
  30. go.mod
  31. go.sum
  32. handler.go
  33. handler_test.go
  34. internal_logging.go
  35. internal_logging_test.go
  36. LICENSE
  37. log.go
  38. log_test.go
  39. Makefile
  40. metric.go
  41. metric_test.go
  42. propagation.go
  43. README.md
  44. RELEASING.md
  45. renovate.json
  46. requirements.txt
  47. SECURITY-INSIGHTS.yml
  48. trace.go
  49. trace_test.go
  50. verify_internal_boundary_compat.sh
  51. verify_released_changelog.sh
  52. version.go
  53. version_test.go
  54. VERSIONING.md
  55. versions.yaml
README.md

OpenTelemetry-Go

ci codecov.io PkgGoDev OpenSSF Scorecard OpenSSF Best Practices Fuzzing Status FOSSA Status Slack

OpenTelemetry-Go is the Go implementation of OpenTelemetry. It provides a set of APIs to directly measure performance and behavior of your software and send this data to observability platforms.

Project Status

SignalStatus
TracesStable
MetricsStable
LogsStable

Progress and status specific to this repository is tracked in our project boards and milestones.

Project versioning information and stability guarantees can be found in the versioning documentation.

Compatibility

OpenTelemetry-Go ensures compatibility with the current supported versions of the Go language:

Each major Go release is supported until there are two newer major releases. For example, Go 1.5 was supported until the Go 1.7 release, and Go 1.6 was supported until the Go 1.8 release.

For versions of Go that are no longer supported upstream, opentelemetry-go will stop ensuring compatibility with these versions in the following manner:

  • A minor release of opentelemetry-go will be made to add support for the new supported release of Go.
  • The following minor release of opentelemetry-go will remove compatibility testing for the oldest (now archived upstream) version of Go. This, and future, releases of opentelemetry-go may include features only supported by the currently supported versions of Go.

Currently, this project supports the following environments.

OSGo VersionArchitecture
Ubuntu1.27amd64
Ubuntu1.26amd64
Ubuntu1.27386
Ubuntu1.26386
Ubuntu1.27arm64
Ubuntu1.26arm64
macOS1.27amd64
macOS1.26amd64
macOS1.27arm64
macOS1.26arm64
Windows1.27amd64
Windows1.26amd64
Windows1.27386
Windows1.26386

While this project should work for other systems, no compatibility guarantees are made for those systems currently.

Getting Started

You can find a getting started guide on opentelemetry.io.

OpenTelemetry's goal is to provide a single set of APIs to capture distributed traces and metrics from your application and send them to an observability platform. This project allows you to do just that for applications written in Go. There are two steps to this process: instrument your application, and configure an exporter.

Instrumentation

To start capturing distributed traces and metric events from your application it first needs to be instrumented. The easiest way to do this is by using an instrumentation library for your code. Be sure to check out the officially supported instrumentation libraries.

If you need to extend the telemetry an instrumentation library provides or want to build your own instrumentation for your application directly you will need to use the Go otel package. The examples are a good way to see some practical uses of this process.

Export

Now that your application is instrumented to collect telemetry, it needs an export pipeline to send that telemetry to an observability platform.

All officially supported exporters for the OpenTelemetry project are contained in the exporters directory.

ExporterLogsMetricsTraces
OTLP✓✓✓
Prometheus✓
stdout✓✓✓
Zipkin✓

Contributing

See the contributing documentation.