Skip to content

Commit 2996ab4

Browse files
ymotongpoodandhleeleahecole
authored
trace: Update trace sample with OpenTelemetry (GoogleCloudPlatform#5706)
* Update product name from Stackdriver to Cloud Trace in README * Update dependent package versions * Remove unused and non-direct dependent packages from requirements.txt * Fix scope of span Co-authored-by: Dan Lee <[email protected]> Co-authored-by: Leah E. Cole <[email protected]>
1 parent 9899255 commit 2996ab4

File tree

3 files changed

+13
-20
lines changed

3 files changed

+13
-20
lines changed

trace/trace-python-sample-opentelemetry/README.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
.. This file is automatically generated. Do not edit this file directly.
22
3-
Stackdriver Trace Python Samples
3+
Google Cloud Trace Python Samples
44
===============================================================================
55

66
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
77
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=trace/README.rst
88

99

10-
This directory contains samples for Stackdriver Trace. `Stackdriver Trace`_ collects latency data from applications and displays it in near real time in the Google Cloud Platform Console.
10+
This directory contains samples for Google Cloud Trace. `Google Cloud Trace`_ collects latency data from applications and displays it in near real time in the Google Cloud Platform Console.
1111

1212

1313

1414

15-
.. _Stackdriver Trace: https://cloud.google.com/trace/docs
15+
.. _Google Cloud Trace: https://cloud.google.com/trace/docs
1616

1717
Setup
1818
-------------------------------------------------------------------------------

trace/trace-python-sample-opentelemetry/main.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@
2020
from flask import Flask, redirect, url_for
2121

2222
# [START trace_setup_python_configure]
23-
from opentelemetry import trace
23+
from opentelemetry import propagators, trace
2424
from opentelemetry.exporter.cloud_trace import CloudTraceSpanExporter
2525
from opentelemetry.sdk.trace import TracerProvider
2626
from opentelemetry.sdk.trace.export import SimpleExportSpanProcessor
27+
from opentelemetry.tools.cloud_trace_propagator import CloudTraceFormatPropagator
2728

2829

2930
def initialize_tracer(project_id):
@@ -32,6 +33,7 @@ def initialize_tracer(project_id):
3233
trace.get_tracer_provider().add_span_processor(
3334
SimpleExportSpanProcessor(cloud_trace_exporter)
3435
)
36+
propagators.set_global_textmap(CloudTraceFormatPropagator())
3537
opentelemetry_tracer = trace.get_tracer(__name__)
3638

3739
return opentelemetry_tracer
@@ -51,10 +53,10 @@ def root():
5153
@app.route("/index.html", methods=["GET"])
5254
def index():
5355
tracer = app.config["TRACER"]
54-
tracer.start_as_current_span(name="index")
55-
# Add up to 1 sec delay, weighted toward zero
56-
time.sleep(random.random() ** 2)
57-
result = "Tracing requests"
56+
with tracer.start_as_current_span(name="index"):
57+
# Add up to 1 sec delay, weighted toward zero
58+
time.sleep(random.random() ** 2)
59+
result = "Tracing requests"
5860

5961
return result
6062

@@ -74,7 +76,6 @@ def index():
7476
)
7577
args = parser.parse_args()
7678

77-
tracer = initialize_tracer(args.project_id)
78-
app.config["TRACER"] = tracer
79+
app.config["TRACER"] = initialize_tracer(args.project_id)
7980

8081
app.run()
Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
11
Flask==1.1.2
2-
google-cloud-trace==0.24.0
3-
opentelemetry-api==0.16b1
4-
opentelemetry-instrumentation==0.16b1
5-
opentelemetry-exporter-google-cloud==0.16b1
6-
opentelemetry-instrumentation-flask==0.16b1
7-
opentelemetry-instrumentation-grpc==0.16b1
8-
opentelemetry-exporter-jaeger==0.16b1
9-
opentelemetry-instrumentation-requests==0.16b1
10-
opentelemetry-instrumentation-wsgi==0.16b1
11-
opentelemetry-sdk==0.16b1
2+
opentelemetry-exporter-google-cloud==0.18b0
3+
opentelemetry-tools-google-cloud==0.18b0

0 commit comments

Comments
 (0)