Skip to content

Commit 71455bc

Browse files
cojencogcf-owl-bot[bot]frankyn
authored
samples: add OTel Tracing quickstart (#1371)
* samples: add otel tracing quickstart * update test * fix lint * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * review comments * align samples to use ALWAYS_ON * address comments --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Frank Natividad <[email protected]>
1 parent 2945853 commit 71455bc

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

samples/snippets/requirements.txt

+3
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ google-cloud-storage==2.19.0
33
pandas===1.3.5; python_version == '3.7'
44
pandas===2.0.3; python_version == '3.8'
55
pandas==2.2.3; python_version >= '3.9'
6+
opentelemetry-exporter-gcp-trace
7+
opentelemetry-propagator-gcp
8+
opentelemetry-instrumentation-requests

samples/snippets/snippets_test.py

+13
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
import storage_set_client_endpoint
7676
import storage_set_object_retention_policy
7777
import storage_set_metadata
78+
import storage_trace_quickstart
7879
import storage_transfer_manager_download_bucket
7980
import storage_transfer_manager_download_chunks_concurrently
8081
import storage_transfer_manager_download_many
@@ -850,3 +851,15 @@ def test_create_bucket_hierarchical_namespace(test_bucket_create, capsys):
850851
)
851852
out, _ = capsys.readouterr()
852853
assert f"Created bucket {test_bucket_create.name} with hierarchical namespace enabled" in out
854+
855+
856+
def test_storage_trace_quickstart(test_bucket, capsys):
857+
blob_name = f"trace_quickstart_{uuid.uuid4().hex}"
858+
contents = "The quick brown fox jumps over the lazy dog."
859+
storage_trace_quickstart.run_quickstart(test_bucket.name, blob_name, contents)
860+
out, _ = capsys.readouterr()
861+
862+
assert f"{blob_name} uploaded to {test_bucket.name}" in out
863+
assert (
864+
f"Downloaded storage object {blob_name} from bucket {test_bucket.name}" in out
865+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2024 Google LLC. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the 'License');
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import sys
18+
19+
"""
20+
Sample that exports OpenTelemetry Traces collected from the Storage client to Cloud Trace.
21+
"""
22+
23+
24+
def run_quickstart(bucket_name, blob_name, data):
25+
# [START storage_enable_otel_tracing]
26+
27+
from opentelemetry import trace
28+
from opentelemetry.exporter.cloud_trace import CloudTraceSpanExporter
29+
from opentelemetry.resourcedetector.gcp_resource_detector import (
30+
GoogleCloudResourceDetector,
31+
)
32+
from opentelemetry.sdk.trace import TracerProvider
33+
from opentelemetry.sdk.trace.export import BatchSpanProcessor
34+
from opentelemetry.sdk.trace.sampling import ALWAYS_ON
35+
# Optional: Enable traces emitted from the requests HTTP library.
36+
from opentelemetry.instrumentation.requests import RequestsInstrumentor
37+
38+
from google.cloud import storage
39+
40+
# The ID of your GCS bucket
41+
# bucket_name = "your-bucket-name"
42+
# The ID of your GCS object
43+
# blob_name = "your-object-name"
44+
# The contents to upload to the file
45+
# data = "The quick brown fox jumps over the lazy dog."
46+
47+
# In this sample, we use Google Cloud Trace to export the OpenTelemetry
48+
# traces: https://cloud.google.com/trace/docs/setup/python-ot
49+
# Choose and configure the exporter for your environment.
50+
51+
tracer_provider = TracerProvider(
52+
# Sampling is set to ALWAYS_ON.
53+
# It is recommended to sample based on a ratio to control trace ingestion volume,
54+
# for instance, sampler=TraceIdRatioBased(0.2)
55+
sampler=ALWAYS_ON,
56+
resource=GoogleCloudResourceDetector().detect(),
57+
)
58+
59+
# Export to Google Cloud Trace.
60+
tracer_provider.add_span_processor(BatchSpanProcessor(CloudTraceSpanExporter()))
61+
trace.set_tracer_provider(tracer_provider)
62+
63+
# Optional: Enable traces emitted from the requests HTTP library.
64+
RequestsInstrumentor().instrument(tracer_provider=tracer_provider)
65+
66+
# Get the tracer and create a new root span.
67+
tracer = tracer_provider.get_tracer("My App")
68+
with tracer.start_as_current_span("trace-quickstart"):
69+
# Instantiate a storage client and perform a write and read workload.
70+
storage_client = storage.Client()
71+
bucket = storage_client.bucket(bucket_name)
72+
blob = bucket.blob(blob_name)
73+
blob.upload_from_string(data)
74+
print(f"{blob_name} uploaded to {bucket_name}.")
75+
76+
blob.download_as_bytes()
77+
print("Downloaded storage object {} from bucket {}.".format(blob_name, bucket_name))
78+
79+
# [END storage_enable_otel_tracing]
80+
81+
82+
if __name__ == "__main__":
83+
run_quickstart(bucket_name=sys.argv[1], blob_name=sys.argv[2], data=sys.argv[3])

0 commit comments

Comments
 (0)