Skip to content

Commit 8cf89a3

Browse files
authored
docs(logging): Documentation for automatic trace/span ID extraction (#10536)
* docs(logging): Documentation for automatic trace/span ID extraction * updated documentation * addressed feedback * Changed OpenTelemetry to otelhttp * updated opentelemetry/otelhttp documentation
1 parent 978d4a1 commit 8cf89a3

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

logging/doc.go

+29
Original file line numberDiff line numberDiff line change
@@ -146,5 +146,34 @@ parent.Timestamp marks the end of the request.)
146146
You should observe the child log entries grouped under the parent on the console. The
147147
parent entry will not inherit the severity of its children; you must update the
148148
parent severity yourself.
149+
150+
# Automatic Trace/Span ID Extraction
151+
152+
You can automatically populate the Trace, SpanID, and TraceSampled fields of an Entry object by providing an [http.Request] object
153+
within the Entry's HTTPRequest field:
154+
155+
logging.Entry{
156+
HTTPRequest: &logging.HTTPRequest{
157+
Request: // Reference to your http.Request here
158+
}
159+
}
160+
161+
When Entry with an [http.Request] is logged, its Trace, SpanID, and TraceSampled fields may be automatically populated as follows:
162+
163+
1. If you are instrumenting your application with [OpenTelemetry], more specifically [otelhttp],
164+
the Entry's Trace, SpanID, and TraceSampled will be populated with information from the [http.Request]'s span context.
165+
2. Trace, SpanID, and TraceSampled fields will be populated from information from the http.Request's [W3C Traceparent]
166+
or [X-Cloud-Trace-Context] headers, if those headers exist.
167+
168+
Note that if Trace, SpanID, or TraceSampled are explicitly provided within an Entry object, then those values take precedence over values automatically
169+
extracted values.
170+
171+
[http.Request]: https://pkg.go.dev/net/http#Request
172+
[OpenTelemetry]: https://opentelemetry.io/docs/languages/go/
173+
[otelhttp]: https://pkg.go.dev/go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp
174+
[W3C Traceparent]: https://www.w3.org/TR/trace-context
175+
[X-Cloud-Trace-Context]: https://cloud.google.com/trace/docs/trace-context#legacy-http-header
176+
177+
[OpenTelemetry span context]: https://pkg.go.dev/go.opentelemetry.io/otel/trace#SpanContext
149178
*/
150179
package logging // import "cloud.google.com/go/logging"

0 commit comments

Comments
 (0)