-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Trace python opentelemetry #4026
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
gguuss
merged 24 commits into
GoogleCloudPlatform:master
from
aravinsiva:trace-python-opentelemetry
Jun 24, 2020
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
f586771
creating new directory for opentelemetry version of quickstart
aravinsiva e1e0a3a
slight modification to README
aravinsiva d24cf8d
updating requirements.txt
aravinsiva 3df7e38
porting python trace sample docs to opentelemetry
aravinsiva 5269cad
modifying main.py for opentelemetry implementation
aravinsiva 07646c4
code cleanup
aravinsiva c10d83a
updating requiements.txt and code cleanup
aravinsiva 8e62689
refractor code from other PR
aravinsiva c895c7c
Merge branch 'master' into trace-python-opentelemetry
aravinsiva bc34095
Merge branch 'master' into trace-python-opentelemetry
aravinsiva 2d3ba90
making reccommended changes by Shawn and Gus
aravinsiva 4e55e2b
adding default variable
aravinsiva 994ce46
Merge branch 'trace-python-opentelemetry' of https://github.com/aravi…
aravinsiva d064132
fixing issue encountered when testing
aravinsiva 4010db2
Merge branch 'master' into trace-python-opentelemetry
aravinsiva 89ad05f
Merge branch 'master' into trace-python-opentelemetry
aravinsiva bc15bc6
Merge branch 'master' into trace-python-opentelemetry
aravinsiva f4db97e
adding production opentelemetry PyPi packages
aravinsiva 4403d32
fixing linter issue
aravinsiva cefea61
Revert "fixing linter issue"
aravinsiva 51c36fa
fixing actual linter issue
aravinsiva e279e72
Merge branch 'master' into trace-python-opentelemetry
busunkim96 3885f3c
Merge branch 'master' into trace-python-opentelemetry
busunkim96 f934de8
Merge branch 'master' into trace-python-opentelemetry
gguuss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
.. This file is automatically generated. Do not edit this file directly. | ||
|
||
Stackdriver Trace Python Samples | ||
=============================================================================== | ||
|
||
.. image:: https://gstatic.com/cloudssh/images/open-btn.png | ||
: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 | ||
|
||
|
||
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. | ||
|
||
|
||
|
||
|
||
.. _Stackdriver Trace: https://cloud.google.com/trace/docs | ||
|
||
Setup | ||
------------------------------------------------------------------------------- | ||
|
||
|
||
Authentication | ||
++++++++++++++ | ||
|
||
This sample requires you to have authentication setup. Refer to the | ||
`Authentication Getting Started Guide`_ for instructions on setting up | ||
credentials for applications (only for authentication). | ||
|
||
.. _Authentication Getting Started Guide: | ||
https://cloud.google.com/docs/authentication/getting-started | ||
|
||
Install Dependencies | ||
++++++++++++++++++++ | ||
|
||
#. Clone python-docs-samples: | ||
|
||
.. code-block:: bash | ||
|
||
$ git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git | ||
|
||
#. Change directory to the sample directory you want to use: | ||
|
||
.. code-block:: bash | ||
|
||
$ cd python-docs-samples/trace/trace-python-sample-opentelemetry | ||
|
||
#. Install `pip`_ and `virtualenv`_ if you do not already have them. You may want to refer to the `Python Development Environment Setup Guide`_ for Google Cloud Platform for instructions: | ||
|
||
.. _Python Development Environment Setup Guide: | ||
https://cloud.google.com/python/setup | ||
|
||
#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+: | ||
|
||
.. code-block:: bash | ||
|
||
$ virtualenv env | ||
$ source env/bin/activate | ||
|
||
#. Install the dependencies needed to run the samples: | ||
|
||
.. code-block:: bash | ||
|
||
$ pip install -r requirements.txt | ||
|
||
.. _pip: https://pip.pypa.io/ | ||
.. _virtualenv: https://virtualenv.pypa.io/ | ||
|
||
Samples | ||
------------------------------------------------------------------------------- | ||
|
||
Web Server | ||
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
|
||
.. image:: https://gstatic.com/cloudssh/images/open-btn.png | ||
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=trace/main.py,trace/README.rst | ||
|
||
|
||
|
||
|
||
To run this sample: | ||
|
||
.. code-block:: bash | ||
|
||
$ python main.py | ||
|
||
|
||
|
||
|
||
The client library | ||
------------------------------------------------------------------------------- | ||
|
||
This sample uses the `Google Cloud Client Library for Python`_. | ||
You can read the documentation for more details on API usage and use GitHub | ||
to `browse the source`_ and `report issues`_. | ||
|
||
.. _Google Cloud Client Library for Python: | ||
https://googlecloudplatform.github.io/google-cloud-python/ | ||
.. _browse the source: | ||
https://github.com/GoogleCloudPlatform/google-cloud-python | ||
.. _report issues: | ||
https://github.com/GoogleCloudPlatform/google-cloud-python/issues | ||
|
||
|
||
.. _Google Cloud SDK: https://cloud.google.com/sdk/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# This file is used to generate README.rst | ||
|
||
product: | ||
name: Stackdriver Trace | ||
short_name: Stackdriver Trace | ||
url: https://cloud.google.com/trace/docs | ||
description: > | ||
`Stackdriver Trace`_ collects latency data from applications and displays | ||
it in near real time in the Google Cloud Platform Console. | ||
|
||
setup: | ||
- auth | ||
- install_deps | ||
|
||
samples: | ||
- name: Web Server | ||
file: main.py | ||
|
||
cloud_client_library: true | ||
|
||
folder: trace |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# Copyright 2020 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import argparse | ||
import os | ||
import random | ||
import time | ||
|
||
from flask import Flask, redirect, url_for | ||
|
||
# [START trace_setup_python_configure] | ||
from opentelemetry import trace | ||
from opentelemetry.exporter.cloud_trace import CloudTraceSpanExporter | ||
from opentelemetry.sdk.trace import TracerProvider | ||
from opentelemetry.sdk.trace.export import SimpleExportSpanProcessor | ||
|
||
|
||
def initialize_tracer(project_id): | ||
trace.set_tracer_provider(TracerProvider()) | ||
cloud_trace_exporter = CloudTraceSpanExporter(project_id) | ||
trace.get_tracer_provider().add_span_processor( | ||
SimpleExportSpanProcessor(cloud_trace_exporter) | ||
) | ||
opentelemetry_tracer = trace.get_tracer(__name__) | ||
|
||
return opentelemetry_tracer | ||
|
||
|
||
# [END trace_setup_python_configure] | ||
|
||
app = Flask(__name__) | ||
|
||
|
||
@app.route("/", methods=["GET"]) | ||
def root(): | ||
return redirect(url_for("index")) | ||
|
||
|
||
# [START trace_setup_python_quickstart] | ||
@app.route("/index.html", methods=["GET"]) | ||
def index(): | ||
tracer = app.config["TRACER"] | ||
tracer.start_as_current_span(name="index") | ||
# Add up to 1 sec delay, weighted toward zero | ||
time.sleep(random.random() ** 2) | ||
result = "Tracing requests" | ||
|
||
return result | ||
|
||
|
||
# [END trace_setup_python_quickstart] | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser( | ||
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter | ||
) | ||
parser.add_argument( | ||
"--project_id", | ||
help="Project ID you want to access.", | ||
default=os.environ["GOOGLE_CLOUD_PROJECT"], | ||
required=True, | ||
) | ||
args = parser.parse_args() | ||
|
||
tracer = initialize_tracer(args.project_id) | ||
app.config["TRACER"] = tracer | ||
|
||
app.run() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Copyright 2020 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import os | ||
|
||
import main | ||
|
||
|
||
def test_index(): | ||
project_id = os.environ['GOOGLE_CLOUD_PROJECT'] | ||
main.app.testing = True | ||
main.app.config['TRACER'] = main.initialize_tracer(project_id) | ||
client = main.app.test_client() | ||
|
||
resp = client.get('/index.html') | ||
assert resp.status_code == 200 | ||
assert 'Tracing requests' in resp.data.decode('utf-8') | ||
|
||
|
||
def test_redirect(): | ||
project_id = os.environ['GOOGLE_CLOUD_PROJECT'] | ||
main.app.testing = True | ||
main.app.config['TRACER'] = main.initialize_tracer(project_id) | ||
client = main.app.test_client() | ||
|
||
resp = client.get('/') | ||
assert resp.status_code == 302 | ||
assert '/index.html' in resp.headers.get('location', '') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pytest==5.3.2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Flask==1.1.2 | ||
google-cloud-trace==0.23.0 | ||
opentelemetry-api==0.9b0 | ||
opentelemetry-auto-instrumentation==0.8b0 | ||
opentelemetry-exporter-cloud-trace==0.9b0 | ||
opentelemetry-ext-flask==0.8b0 | ||
opentelemetry-ext-grpc==0.9b0 | ||
opentelemetry-ext-jaeger==0.8b0 | ||
opentelemetry-ext-requests==0.8b0 | ||
opentelemetry-ext-wsgi==0.8b0 | ||
opentelemetry-sdk==0.9b0 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.