10000 chore(functions/logging): exclude trace code from sample (#6648) · riteshverma/python-docs-samples@e931200 · GitHub
[go: up one dir, main page]

Skip to content

Commit e931200

Browse files
Ace Nassridinagraves
andauthored
chore(functions/logging): exclude trace code from sample (GoogleCloudPlatform#6648)
* Apply Adam's suggested fix * locals -> globals * Add tests + confirm it works on GCF * Run black * Remove unused region tag Co-authored-by: Dina Graves Portman <dinagraves@google.com>
1 parent 1b73be5 commit e931200

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

run/logging-manual/e2e_test.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def test_end_to_end(service_url_auth_token, deployed_service):
152152
total=3,
153153
status_forcelist=[400, 401, 403, 500, 502, 503, 504],
154154
allowed_methods=["GET", "POST"],
155-
backoff_factor=3
155+
backoff_factor=3,
156156
)
157157
adapter = HTTPAdapter(max_retries=retry_strategy)
158158

@@ -187,7 +187,9 @@ def test_end_to_end(service_url_auth_token, deployed_service):
187187
# Retry a maximum number of 10 times to find results in stackdriver
188188
found = False
189189
for x in range(10):
190-
iterator = client.list_log_entries({"resource_names": resource_names, "filter": filters})
190+
iterator = client.list_log_entries(
191+
{"resource_names": resource_names, "filter": filters}
192+
)
191193
for entry in iterator:
192194
found = True
193195
# If there are any results, exit loop

run/logging-manual/main.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
# [START cloudrun_manual_logging]
1516
import json
1617
import os
1718

19+
# [END cloudrun_manual_logging]
20+
1821
from flask import Flask, request
1922

2023

@@ -29,21 +32,24 @@ def index():
2932
PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"]
3033

3134
# [START cloudrun_manual_logging]
32-
# [START run_manual_logging]
3335
# Uncomment and populate this variable in your code:
3436
# PROJECT = 'The project ID of your Cloud Run service';
3537

3638
# Build structured log messages as an object.
3739
global_log_fields = {}
3840

3941
# Add log correlation to nest all log messages.
40-
trace_header = request.headers.get("X-Cloud-Trace-Context")
41-
42-
if trace_header and PROJECT:
43-
trace = trace_header.split("/")
44-
global_log_fields[
45-
"logging.googleapis.com/trace"
46-
] = f"projects/{PROJECT}/traces/{trace[0]}"
42+
# This is only relevant in HTTP-based contexts, and is ignored elsewhere.
43+
# (In particular, non-HTTP-based Cloud Functions.)
44+
request_is_defined = "request" in globals() or "request" in locals()
45+
if request_is_defined and request:
46+
trace_header = request.headers.get("X-Cloud-Trace-Context")
47+
48+
if trace_header and PROJECT:
49+
trace = trace_header.split("/")
50+
global_log_fields[
51+
"logging.googleapis.com/trace"
52+
] = f"projects/{PROJECT}/traces/{trace[0]}"
4753

4854
# Complete a structured log entry.
4955
entry = dict(
@@ -55,7 +61,6 @@ def index():
5561
)
5662

5763
print(json.dumps(entry))
58-
# [END run_manual_logging]
5964
# [END cloudrun_manual_logging]
6065

6166
return "Hello Logger!"

run/logging-manual/main_test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,11 @@ def test_with_cloud_headers(client, capsys):
4242
out, _ = capsys.readouterr()
4343
print(out)
4444
assert "trace" in out
45+
46+
47+
def test_no_http_request(capsys):
48+
main.index()
49+
50+
out, _ = capsys.readouterr()
51+
print(out)
52+
assert "trace" not in out

0 commit comments

Comments
 (0)
0