|
| 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