stdoutlog: use non-OTLP representation for log bodies and attributes (#9150)
NaN and infinities in log bodies or attributes currently stop
stdoutlog's JSON encoding and leave the rest of the batch unwritten.
Share the attribute package's non-OTLP formatting for special floats,
bytes, arrays, and nested maps.
Encode log bodies directly as JSON values: strings, booleans, finite
numbers, arrays, objects, and `null` for empty values. For example, a
string body is `"Body":"test"` and a nested map is a JSON object. Scalar
strings and Base64 bytes receive normal JSON transport quoting; special
floats become `"NaN"`, `"Infinity"`, and `"-Infinity"`. JSON-looking
strings stay strings, and user map entries named `Type` or `Value`
remain ordinary data.
Log attributes remain an ordered array of single-attribute objects;
resource and scope attribute sets become JSON objects. Empty sets are
`{}`, nil resources remain `null`, and populated scope attributes are
retained.
Use `json.RawMessage` for the private body field and conversion helper
so every body is encoded JSON before the record is marshaled. Formatting
remains local to stdoutlog; shared attribute marshaling and other signal
exporters are unaffected. Ordinary export errors still reach the caller.
Fixes #9127.
Fixes #9128.
Six-sample benchmarks below isolate the `json.RawMessage` refactor
against `deb62de8`, which already emits direct non-OTLP bodies. Scalar
strings, bytes, special floats, and empty bodies gain one allocation;
finite float and float-array cases use three fewer. Short strings
measured 7.5% slower and escaped strings 16.7% slower; the finite float
and small-array cases measured 8–11% faster in this run. This is an
explicit-type refactor with a performance tradeoff.
Benchmark command: `GOMAXPROCS=2 make
benchmark/./exporters/stdout/stdoutlog ARGS='-bench=BenchmarkExporter
-benchmem -count=6 -benchtime=100ms'`.
```text
goos: linux
goarch: amd64
pkg: go.opentelemetry.io/otel/exporters/stdout/stdoutlog
cpu: AMD EPYC 7571
│ before (deb62de8) │ RawMessage Body │
│ sec/op │ sec/op vs base │
ExporterObservability/Disabled-2 8.639µ ± 5% 8.845µ ± 2% ~ (p=0.143 n=6)
ExporterObservability/UploadSuccess-2 10.95µ ± 1% 11.07µ ± 1% +1.07% (p=0.009 n=6)
ExporterBody/short_string-2 4.312µ ± 1% 4.634µ ± 2% +7.47% (p=0.002 n=6)
ExporterBody/escaped_string-2 4.562µ ± 1% 5.322µ ± 1% +16.66% (p=0.002 n=6)
ExporterBody/byte_slice-2 4.426µ ± 8% 4.686µ ± 1% ~ (p=0.065 n=6)
ExporterBody/NaN-2 4.245µ ± 1% 4.593µ ± 0% +8.20% (p=0.002 n=6)
ExporterBody/finite_float-2 4.892µ ± 1% 4.363µ ± 1% -10.82% (p=0.002 n=6)
ExporterBody/empty-2 3.850µ ± 5% 3.975µ ± 1% ~ (p=0.061 n=6)
ExporterFloat64Slice/finite/8-2 6.476µ ± 1% 5.934µ ± 2% -8.36% (p=0.002 n=6)
ExporterFloat64Slice/finite/1024-2 175.8µ ± 2% 170.7µ ± 1% -2.90% (p=0.002 n=6)
ExporterFloat64Slice/nonfinite/8-2 6.247µ ± 1% 5.611µ ± 1% -10.19% (p=0.002 n=6)
ExporterFloat64Slice/nonfinite/1024-2 172.6µ ± 1% 172.0µ ± 1% ~ (p=0.310 n=6)
geomean 9.824µ 9.895µ +0.72%
│ before (deb62de8) │ RawMessage Body │
│ B/op │ B/op vs base │
ExporterObservability/Disabled-2 1.242Ki ± 0% 1.312Ki ± 0% +5.66% (p=0.002 n=6)
ExporterObservability/UploadSuccess-2 1.243Ki ± 0% 1.313Ki ± 0% +5.66% (p=0.002 n=6)
ExporterBody/short_string-2 632.0 ± 0% 704.0 ± 0% +11.39% (p=0.002 n=6)
ExporterBody/escaped_string-2 632.0 ± 0% 760.0 ± 0% +20.25% (p=0.002 n=6)
ExporterBody/byte_slice-2 640.0 ± 0% 712.0 ± 0% +11.25% (p=0.002 n=6)
ExporterBody/NaN-2 632.0 ± 0% 704.0 ± 0% +11.39% (p=0.002 n=6)
ExporterBody/finite_float-2 688.0 ± 0% 680.0 ± 0% -1.16% (p=0.002 n=6)
ExporterBody/empty-2 600.0 ± 0% 672.0 ± 0% +12.00% (p=0.002 n=6)
ExporterFloat64Slice/finite/8-2 928.0 ± 0% 920.0 ± 0% -0.86% (p=0.002 n=6)
ExporterFloat64Slice/finite/1024-2 33.32Ki ± 0% 33.34Ki ± 0% ~ (p=1.000 n=6)
ExporterFloat64Slice/nonfinite/8-2 944.0 ± 0% 936.0 ± 0% -0.85% (p=0.002 n=6)
ExporterFloat64Slice/nonfinite/1024-2 33.32Ki ± 0% 33.35Ki ± 0% ~ (p=0.290 n=6)
geomean 1.445Ki 1.532Ki +6.03%
│ before (deb62de8) │ RawMessage Body │
│ allocs/op │ allocs/op vs base │
ExporterObservability/Disabled-2 25.00 ± 0% 26.00 ± 0% +4.00% (p=0.002 n=6)
ExporterObservability/UploadSuccess-2 25.00 ± 0% 26.00 ± 0% +4.00% (p=0.002 n=6)
ExporterBody/short_string-2 9.000 ± 0% 10.000 ± 0% +11.11% (p=0.002 n=6)
ExporterBody/escaped_string-2 9.000 ± 0% 10.000 ± 0% +11.11% (p=0.002 n=6)
ExporterBody/byte_slice-2 10.00 ± 0% 11.00 ± 0% +10.00% (p=0.002 n=6)
ExporterBody/NaN-2 9.000 ± 0% 10.000 ± 0% +11.11% (p=0.002 n=6)
ExporterBody/finite_float-2 12.000 ± 0% 9.000 ± 0% -25.00% (p=0.002 n=6)
ExporterBody/empty-2 7.000 ± 0% 8.000 ± 0% +14.29% (p=0.002 n=6)
ExporterFloat64Slice/finite/8-2 12.000 ± 0% 9.000 ± 0% -25.00% (p=0.002 n=6)
ExporterFloat64Slice/finite/1024-2 12.000 ± 0% 9.000 ± 0% -25.00% (p=0.002 n=6)
ExporterFloat64Slice/nonfinite/8-2 12.000 ± 0% 9.000 ± 0% -25.00% (p=0.002 n=6)
ExporterFloat64Slice/nonfinite/1024-2 12.000 ± 0% 9.000 ± 0% -25.00% (p=0.002 n=6)
geomean 11.88 11.10 -6.57%
```
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.
| Signal | Status |
|---|---|
| Traces | Stable |
| Metrics | Stable |
| Logs | Stable |
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.
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:
Currently, this project supports the following environments.
| OS | Go Version | Architecture |
|---|---|---|
| Ubuntu | 1.27 | amd64 |
| Ubuntu | 1.26 | amd64 |
| Ubuntu | 1.27 | 386 |
| Ubuntu | 1.26 | 386 |
| Ubuntu | 1.27 | arm64 |
| Ubuntu | 1.26 | arm64 |
| macOS | 1.27 | amd64 |
| macOS | 1.26 | amd64 |
| macOS | 1.27 | arm64 |
| macOS | 1.26 | arm64 |
| Windows | 1.27 | amd64 |
| Windows | 1.26 | amd64 |
| Windows | 1.27 | 386 |
| Windows | 1.26 | 386 |
While this project should work for other systems, no compatibility guarantees are made for those systems currently.
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.
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.
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.
| Exporter | Logs | Metrics | Traces |
|---|---|---|---|
| OTLP | ✓ | ✓ | ✓ |
| Prometheus | ✓ | ||
| stdout | ✓ | ✓ | ✓ |
| Zipkin | ✓ |
See the contributing documentation.