Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 732916e

Browse filesBrowse files
authored
Add Trace code samples (GoogleCloudPlatform#1941)
* Skeletal trace example * Initial sample code * Completed sample * Simple way to run the server * New library version available
1 parent 0c4bae5 commit 732916e
Copy full SHA for 732916e

File tree

Expand file treeCollapse file tree

7 files changed

+235
-0
lines changed
Filter options
Expand file treeCollapse file tree

7 files changed

+235
-0
lines changed

‎.kokoro/presubmit_tests_trace.cfg

Copy file name to clipboard
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Format: //devtools/kokoro/config/proto/build.proto
2+
3+
# Download secrets from Cloud Storage.
4+
gfile_resources: "/bigstore/cloud-devrel-kokoro-resources/python-docs-samples"
5+
6+
# Tell the trampoline which build file to use.
7+
env_vars: {
8+
key: "TRAMPOLINE_BUILD_FILE"
9+
value: "github/python-docs-samples/.kokoro/system_tests.sh"
10+
}
11+
12+
env_vars: {
13+
key: "NOX_SESSION"
14+
value: "trace and py36 and not appengine"
15+
}

‎.kokoro/system_tests_trace.cfg

Copy file name to clipboard
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Format: //devtools/kokoro/config/proto/build.proto
2+
3+
# Download secrets from Cloud Storage.
4+
gfile_resources: "/bigstore/cloud-devrel-kokoro-resources/python-docs-samples"
5+
6+
# Tell the trampoline which build file to use.
7+
env_vars: {
8+
key: "TRAMPOLINE_BUILD_FILE"
9+
value: "github/python-docs-samples/.kokoro/system_tests.sh"
10+
}
11+
12+
env_vars: {
13+
key: "NOX_SESSION"
14+
value: "trace and py36 and not appengine"
15+
}

‎trace/README.rst

Copy file name to clipboard
+97Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
.. This file is automatically generated. Do not edit this file directly.
2+
3+
Stackdriver Trace Python Samples
4+
===============================================================================
5+
6+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
7+
: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
8+
9+
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.
11+
12+
13+
14+
15+
.. _Stackdriver Trace: https://cloud.google.com/trace/docs
16+
17+
Setup
18+
-------------------------------------------------------------------------------
19+
20+
21+
Authentication
22+
++++++++++++++
23+
24+
This sample requires you to have authentication setup. Refer to the
25+
`Authentication Getting Started Guide`_ for instructions on setting up
26+
credentials for applications.
27+
28+
.. _Authentication Getting Started Guide:
29+
https://cloud.google.com/docs/authentication/getting-started
30+
31+
Install Dependencies
32+
++++++++++++++++++++
33+
34+
#. Clone python-docs-samples and change directory to the sample directory you want to use.
35+
36+
.. code-block:: bash
37+
38+
$ git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
39+
40+
#. 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.
41+
42+
.. _Python Development Environment Setup Guide:
43+
https://cloud.google.com/python/setup
44+
45+
#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+.
46+
47+
.. code-block:: bash
48+
49+
$ virtualenv env
50+
$ source env/bin/activate
51+
52+
#. Install the dependencies needed to run the samples.
53+
54+
.. code-block:: bash
55+
56+
$ pip install -r requirements.txt
57+
58+
.. _pip: https://pip.pypa.io/
59+
.. _virtualenv: https://virtualenv.pypa.io/
60+
61+
Samples
62+
-------------------------------------------------------------------------------
63+
64+
Web Server
65+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
66+
67+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
68+
: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
69+
70+
71+
72+
73+
To run this sample:
74+
75+
.. code-block:: bash
76+
77+
$ python main.py
78+
79+
80+
81+
82+
The client library
83+
-------------------------------------------------------------------------------
84+
85+
This sample uses the `Google Cloud Client Library for Python`_.
86+
You can read the documentation for more details on API usage and use GitHub
87+
to `browse the source`_ and `report issues`_.
88+
89+
.. _Google Cloud Client Library for Python:
90+
https://googlecloudplatform.github.io/google-cloud-python/
91+
.. _browse the source:
92+
https://github.com/GoogleCloudPlatform/google-cloud-python
93+
.. _report issues:
94+
https://github.com/GoogleCloudPlatform/google-cloud-python/issues
95+
96+
97+
.. _Google Cloud SDK: https://cloud.google.com/sdk/

‎trace/README.rst.in

Copy file name to clipboard
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# This file is used to generate README.rst
2+
3+
product:
4+
name: Stackdriver Trace
5+
short_name: Stackdriver Trace
6+
url: https://cloud.google.com/trace/docs
7+
description: >
8+
`Stackdriver Trace`_ collects latency data from applications and displays
9+
it in near real time in the Google Cloud Platform Console.
10+
11+
setup:
12+
- auth
13+
- install_deps
14+
15+
samples:
16+
- name: Web Server
17+
file: main.py
18+
19+
cloud_client_library: true
20+
21+
folder: trace

‎trace/main.py

Copy file name to clipboard
+51Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Copyright 2018 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import random
16+
import time
17+
18+
from flask import Flask, redirect, url_for
19+
20+
# [START trace-setup-python-configure]
21+
from opencensus.trace.exporters import stackdriver_exporter
22+
import opencensus.trace.tracer
23+
24+
exporter = stackdriver_exporter.StackdriverExporter()
25+
tracer = opencensus.trace.tracer.Tracer(exporter=exporter)
26+
# [END trace-setup-python-configure]
27+
28+
app = Flask(__name__)
29+
30+
31+
@app.route('/', methods=['GET'])
32+
def root():
33+
return redirect(url_for('index'))
34+
35+
36+
# [START trace_setup_python_quickstart]
37+
@app.route('/index.html', methods=['GET'])
38+
def index():
39+
tracer.start_span(name='index')
40+
41+
# Add up to 1 sec delay, weighted toward zero
42+
time.sleep(random.random() ** 2)
43+
result = "Tracing requests"
44+
45+
tracer.end_span()
46+
return result
47+
# [END trace_setup_python_quickstart]
48+
49+
50+
if __name__ == '__main__':
51+
app.run()

‎trace/main_test.py

Copy file name to clipboard
+33Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright 2018 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import main
16+
17+
18+
def test_index():
19+
main.app.testing = True
20+
client = main.app.test_client()
21+
22+
resp = client.get('/index.html')
23+
assert resp.status_code == 200
24+
assert 'Tracing requests' in resp.data.decode('utf-8')
25+
26+
27+
def test_redirect():
28+
main.app.testing = True
29+
client = main.app.test_client()
30+
31+
resp = client.get('/')
32+
assert resp.status_code == 302
33+
assert '/index.html' in resp.headers.get('location', '')

‎trace/requirements.txt

Copy file name to clipboard
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
google-cloud-trace==0.20.2
2+
opencensus==0.1.10
3+
Flask==1.0.2

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.