Skip to content

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
Merged
Show file tree
Hide file tree
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 Jun 8, 2020
e1e0a3a
slight modification to README
aravinsiva Jun 8, 2020
d24cf8d
updating requirements.txt
aravinsiva Jun 9, 2020
3df7e38
porting python trace sample docs to opentelemetry
aravinsiva Jun 9, 2020
5269cad
modifying main.py for opentelemetry implementation
aravinsiva Jun 9, 2020
07646c4
code cleanup
aravinsiva Jun 9, 2020
c10d83a
updating requiements.txt and code cleanup
aravinsiva Jun 9, 2020
8e62689
refractor code from other PR
aravinsiva Jun 9, 2020
c895c7c
Merge branch 'master' into trace-python-opentelemetry
aravinsiva Jun 9, 2020
bc34095
Merge branch 'master' into trace-python-opentelemetry
aravinsiva Jun 10, 2020
2d3ba90
making reccommended changes by Shawn and Gus
aravinsiva Jun 10, 2020
4e55e2b
adding default variable
aravinsiva Jun 10, 2020
994ce46
Merge branch 'trace-python-opentelemetry' of https://github.com/aravi…
aravinsiva Jun 10, 2020
d064132
fixing issue encountered when testing
aravinsiva Jun 10, 2020
4010db2
Merge branch 'master' into trace-python-opentelemetry
aravinsiva Jun 10, 2020
89ad05f
Merge branch 'master' into trace-python-opentelemetry
aravinsiva Jun 16, 2020
bc15bc6
Merge branch 'master' into trace-python-opentelemetry
aravinsiva Jun 19, 2020
f4db97e
adding production opentelemetry PyPi packages
aravinsiva Jun 20, 2020
4403d32
fixing linter issue
aravinsiva Jun 23, 2020
cefea61
Revert "fixing linter issue"
aravinsiva Jun 23, 2020
51c36fa
fixing actual linter issue
aravinsiva Jun 23, 2020
e279e72
Merge branch 'master' into trace-python-opentelemetry
busunkim96 Jun 23, 2020
3885f3c
Merge branch 'master' into trace-python-opentelemetry
busunkim96 Jun 23, 2020
f934de8
Merge branch 'master' into trace-python-opentelemetry
gguuss Jun 24, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions trace/trace-python-sample-opentelemetry/README.rst
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/
21 changes: 21 additions & 0 deletions trace/trace-python-sample-opentelemetry/README.rst.in
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
80 changes: 80 additions & 0 deletions trace/trace-python-sample-opentelemetry/main.py
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()
39 changes: 39 additions & 0 deletions trace/trace-python-sample-opentelemetry/main_test.py
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', '')
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest==5.3.2
11 changes: 11 additions & 0 deletions trace/trace-python-sample-opentelemetry/requirements.txt
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