Skip to content

Commit 7df4f58

Browse files
committed
add docs
1 parent c5b8d88 commit 7df4f58

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

opentelemetry-exporter-gcp-logging/README.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,30 @@ Usage
6868
6969
logger1.warning("string log %s", "here")
7070
71+
If your code is running in a GCP environment with a supported Cloud Logging agent (like GKE,
72+
Cloud Run, GCE, etc.), you can write logs to stdout in Cloud Logging `structured JSON format
73+
<https://cloud.google.com/logging/docs/structured-logging>`_. Pass the ``structured_json_file``
74+
argument and use ``SimpleLogRecordProcessor``:
75+
76+
.. code:: python
77+
78+
import sys
79+
from opentelemetry.exporter.cloud_logging import (
80+
CloudLoggingExporter,
81+
)
82+
from opentelemetry._logs import set_logger_provider
83+
from opentelemetry.sdk._logs import LoggerProvider
84+
from opentelemetry.sdk._logs.export import SimpleLogRecordProcessor
85+
86+
logger_provider = LoggerProvider()
87+
set_logger_provider(logger_provider)
88+
exporter = CloudLoggingExporter(structured_json_file=sys.stdout)
89+
logger_provider.add_log_record_processor(SimpleLogRecordProcessor(exporter))
90+
91+
92+
otel_logger = logger_provider.get_logger(__name__)
93+
otel_logger.emit(attributes={"hello": "world"}, body={"foo": {"bar": "baz"}})
94+
7195
References
7296
----------
7397

opentelemetry-exporter-gcp-logging/src/opentelemetry/exporter/cloud_logging/__init__.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,25 @@ def __init__(
220220
client: Optional[LoggingServiceV2Client] = None,
221221
*,
222222
structured_json_file: Optional[TextIO] = None,
223-
):
223+
) -> None:
224+
"""Create a CloudLoggingExporter
225+
226+
Args:
227+
project_id: The GCP project ID to which the logs will be sent. If not
228+
provided, the exporter will infer it from Application Default Credentials.
229+
default_log_name: The default log name to use for log entries.
230+
If not provided, a default name will be used.
231+
client: An optional `LoggingServiceV2Client` instance to use for
232+
sending logs. If not provided and ``structured_json_file`` is not provided, a
233+
new client will be created. Passing both ``client`` and
234+
``structured_json_file`` is not supported.
235+
structured_json_file: An optional file-like object (like `sys.stdout`) to write
236+
logs to in Cloud Logging `structured JSON format
237+
<https://cloud.google.com/logging/docs/structured-logging>`_. If provided,
238+
``client`` must not be provided and logs will only be written to the file-like
239+
object.
240+
"""
241+
224242
self.project_id: str
225243
if not project_id:
226244
_, default_project_id = google.auth.default()

0 commit comments

Comments
 (0)