Skip to content

Commit fb8ff1e

Browse files
committed
Add a parameter route_logs_to_stdout_as_json to the CloudLoggingExporter.
1 parent 3266917 commit fb8ff1e

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,9 @@ def __init__(
140140
project_id: Optional[str] = None,
141141
default_log_name: Optional[str] = None,
142142
client: Optional[LoggingServiceV2Client] = None,
143+
route_logs_to_stdout_as_json: bool = False,
143144
):
145+
self.route_logs_to_stdout_as_json = route_logs_to_stdout_as_json
144146
self.project_id: str
145147
if not project_id:
146148
_, default_project_id = google.auth.default()
@@ -151,13 +153,16 @@ def __init__(
151153
self.default_log_name = default_log_name
152154
else:
153155
self.default_log_name = "otel_python_inprocess_log_name_temp"
154-
self.client = client or LoggingServiceV2Client(
155-
transport=LoggingServiceV2GrpcTransport(
156-
channel=LoggingServiceV2GrpcTransport.create_channel(
157-
options=_OPTIONS,
156+
if route_logs_to_stdout_as_json and client:
157+
raise Exception("Cannot route logs to stdout and set a logging client to send logs to.")
158+
if not route_logs_to_stdout_as_json:
159+
self.client = client or LoggingServiceV2Client(
160+
transport=LoggingServiceV2GrpcTransport(
161+
channel=LoggingServiceV2GrpcTransport.create_channel(
162+
options=_OPTIONS,
163+
)
158164
)
159165
)
160-
)
161166

162167
def export(self, batch: Sequence[LogData]):
163168
now = datetime.datetime.now()
@@ -215,11 +220,16 @@ def export(self, batch: Sequence[LogData]):
215220
for k, v in attributes.items()
216221
}
217222
_set_payload_in_log_entry(log_entry, log_record.body)
218-
log_entries.append(log_entry)
223+
if self.route_logs_to_stdout_as_json:
224+
print(LogEntry.to_json(log_entry))
225+
else:
226+
log_entries.append(log_entry)
219227

220-
self._write_log_entries(log_entries)
228+
if not self.route_logs_to_stdout_as_json:
229+
self._write_log_entries_to_cloud_logging(log_entries)
230+
221231

222-
def _write_log_entries(self, log_entries: list[LogEntry]):
232+
def _write_log_entries_to_cloud_logging(self, log_entries: list[LogEntry]):
223233
batch: list[LogEntry] = []
224234
batch_byte_size = 0
225235
for entry in log_entries:

opentelemetry-exporter-gcp-logging/tests/test_cloud_logging.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,31 @@ def test_too_large_log_raises_warning(caplog) -> None:
6666
"exceeds Cloud Logging's maximum limit of 256000 bytes" in caplog.text
6767
)
6868

69+
def test_logs_printed_to_stdout_as_json(capsys) -> None:
70+
exporter = CloudLoggingExporter(
71+
project_id=PROJECT_ID, route_logs_to_stdout_as_json=True
72+
)
73+
exporter.export(
74+
[
75+
LogData(
76+
log_record=LogRecord(
77+
body="abc",
78+
resource=Resource({}),
79+
attributes={str(i): "i" * 5 for i in range(5)},
80+
),
81+
instrumentation_scope=InstrumentationScope("test"),
82+
)
83+
]
84+
)
85+
stdout = capsys.readouterr().out
86+
assert (
87+
'"logName": "projects/fakeproject/logs/otel_python_inprocess_log_name_temp"' in stdout
88+
)
89+
assert (
90+
'"textPayload": "abc"' in stdout
91+
)
92+
93+
6994

7095
def test_convert_otlp_dict_body(
7196
cloudloggingfake: CloudLoggingFake,

0 commit comments

Comments
 (0)