Skip to content

Commit 2a0539a

Browse files
gkevinzhengaabmass
andauthored
docs: Documentation update for OpenTelemetry (#915)
* docs: Documentation update for OpenTelemetry * updated documentation * Update docs/auto-trace-span-extraction.rst Co-authored-by: Aaron Abbott <[email protected]> --------- Co-authored-by: Aaron Abbott <[email protected]>
1 parent 6266f39 commit 2a0539a

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

docs/auto-trace-span-extraction.rst

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Automatic Trace/Span ID Extraction
2+
==================================
3+
4+
.. note::
5+
All `LogEntry fields`_ populated :ref:`manually<Manual-Metadata>` will override those populated via methods referred to in this
6+
section.
7+
8+
The Google Cloud Logging library can automatically populate `LogEntry fields`_
9+
`trace`, `span_id`, and `trace_sampled` via OpenTelemetry integration, or extracting header information from an HTTP request.
10+
11+
OpenTelemetry Integration
12+
-------------------------
13+
14+
If you have the OpenTelemetry SDK package installed and are logging from within an active OpenTelemetry span, that log entry will automatically
15+
have the `trace`, `span_id`, and `trace_sampled` fields populated from that span. More information about OpenTelemetry can be found
16+
`here <https://opentelemetry.io/docs/languages/python/>`_.
17+
18+
HTTP headers
19+
------------
20+
21+
Another possible method of automatic `trace` / `span_id` is via extraction from HTTP headers.
22+
This is prioritized after OpenTelemetry and requires a :doc:`supported Python web framework </web-framework-integration>`.
23+
Trace information is automatically populated from either the `W3C Traceparent <https://www.w3.org/TR/trace-context>`_
24+
or `X-Cloud-Trace-Context <https://cloud.google.com/trace/docs/trace-context#legacy-http-header>`_ headers.
25+
Populating trace information this way also automatically populates the `http_request` field in the `LogEntry` as well.
26+
27+
.. _LogEntry fields: https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry

docs/std-lib-integration.rst

+13-7
Original file line numberDiff line numberDiff line change
@@ -102,31 +102,35 @@ The Google Cloud Logging library attempts to detect and attach additional
102102
The following fields are currently supported:
103103

104104
- labels
105-
- trace*
106-
- span_id*
107-
- trace_sampled*
108-
- http_request*
105+
- trace
106+
- span_id
107+
- trace_sampled
108+
- http_request
109109
- source_location
110110
- resource
111111
- :ref:`json_fields<JSON>`
112112

113113
.. note::
114-
Fields marked with "*" require a :doc:`supported Python web framework </web-framework-integration>`.
114+
| More information about `trace`, `span_id`, and `trace_sampled` can be found :doc:`here </auto-trace-span-extraction>`.
115+
| `http_request` requires a :doc:`supported Python web framework </web-framework-integration>`.
116+
115117

116118
Manual Metadata Using the `extra` Argument
117119
--------------------------------------------
118120

121+
.. _Manual-Metadata:
122+
119123
The Python :mod:`logging` standard library accepts `an "extra" argument <https://docs.python.org/3/library/logging.html#logging.Logger.debug>`_ when
120124
writing logs. You can use this argument to populate LogRecord objects with user-defined
121125
key-value pairs. Google Cloud Logging uses the `extra` field as a way to pass in additional
122-
metadata to populate `LogEntry fields <https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry>`_.
126+
metadata to populate `LogEntry fields`_.
123127

124128
.. literalinclude:: ../samples/snippets/usage_guide.py
125129
:start-after: [START logging_extras]
126130
:end-before: [END logging_extras]
127131
:dedent: 4
128132

129-
All of the `LogEntry fields <https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry>`_
133+
All of the `LogEntry fields`_
130134
that can be :ref:`autodetected<Autodetection>` can also be set manually through the `extra` argument. Fields sent explicitly through the `extra`
131135
argument override any :ref:`automatically detected<Autodetection>` fields.
132136

@@ -153,3 +157,5 @@ You can use both transport options over :doc:`gRPC or HTTP</grpc-vs-http>`.
153157
.. note::
154158
:class:`~google.cloud.logging_v2.handlers.structured_log.StructuredLogHandler`
155159
prints logs as formatted JSON to standard output, and does not use a Transport class.
160+
161+
.. _LogEntry fields: https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry

docs/usage.rst

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Usage Guide
44
:maxdepth: 2
55

66
std-lib-integration
7+
auto-trace-span-extraction
78
web-framework-integration
89
direct-lib-usage
910
grpc-vs-http

0 commit comments

Comments
 (0)