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%
```
7 files changed
tree: 1914332cade3c93536cba680c80f1662d6c5411e
  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.