Skip to content

Commit 9e381cc

Browse files
New directory for OpenTelemetry QuickStart (#4016)
* creating new directory for opentelemetry version of quickstart * slight modification to README * updating requirements.txt * Making modifications suggested by Shawn * adding Shawn suggested documentation changes * making suggested edits * making more suggested changes to README.md * adding production PyPi librarires * fixing undefined issues * fixing test issues and simplyfying example * fixing mismatched region tag issue * reworking service for GKE * linting and adding more descriptive README.md * fixing test issues * linting * adding suggested changes by Shawn * linting and adding Shawn suggested changes Co-authored-by: Leah E. Cole <[email protected]>
1 parent a518851 commit 9e381cc

File tree

10 files changed

+280
-0
lines changed

10 files changed

+280
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# cloud-trace-demo-app-opentelemetry
2+
3+
Open this demo app in [Google Cloud Shell](https://cloud.google.com/shell/docs/). This includes necessary tools.
4+
5+
6+
[![Open Cloud Trace Demo APP in Cloud Shell](http://gstatic.com/cloudssh/images/open-btn.svg)](https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=trace/cloud-trace-demo-app/README.md&amp;cloudshell_tutorial=trace/cloud-trace-demo-app/README.md)
7+
8+
#### Demo Requirements
9+
If you are using Cloud Shell, skip to the next section.
10+
11+
1. Install gcloud <https://cloud.google.com/sdk/install>
12+
2. Install kubectl <https://kubernetes.io/docs/tasks/tools/install-kubectl/>
13+
3. Install docker <https://docs.docker.com/install/>
14+
15+
16+
#### Create a GKE cluster
17+
18+
4. Enable Google Cloud and set up region and zone.
19+
20+
`gcloud init`
21+
5. Enable the GKE API & billing:
22+
23+
`gcloud services enable container.googleapis.com`
24+
6. Create a GKE cluster named "cloud-trace-demo", replacing `your-gcp-zone` below
25+
with the
26+
[GCP Zone](https://cloud.google.com/compute/docs/regions-zones) closest in proximity to you:
27+
28+
```
29+
gcloud container clusters create cloud-trace-demo\`
30+
--num-nodes 1 \
31+
--enable-basic-auth \
32+
--issue-client-certificate \
33+
--zone your-gcp-zone
34+
```
35+
7. Verify that you have access to the cluster:
36+
37+
`kubectl get nodes`
38+
39+
#### Deploy The Cloud Trace Demo App
40+
41+
8. Build and tag the docker image for demo app:
42+
43+
`docker build -t gcr.io/${PROJECT_ID}/cloud-trace-demo:v1 .`
44+
9. Deploy resource to the cluster:
45+
46+
`kubectl apply -f deployment.yaml`
47+
10. Track the status of the deployment:
48+
49+
`kubectl get deployments`
50+
51+
Deployment is complete when all of the available deployments are ready.
52+
11. Run the following command to see the pods the deployment created:
53+
54+
`kubectl get pods`
55+
56+
#### Deploy The Cloud Trace Demo Service
57+
58+
12. Create the cloud trace demo service:
59+
60+
`kubectl apply -f service.yaml`
61+
13. Get the services IP address by running the following command:
62+
63+
`kubectl get services`
64+
14. Send a curl request to the EXTERNAL_IP, replacing `EXTERNAL_IP` with the external IP address found
65+
in step 13:
66+
67+
`curl EXTERNAL_IP`
68+
15. Visit [Trace List](https://console.cloud.google.com/traces/list) to check traces generated.
69+
Click on any trace in the graph to see the Waterfall View.
70+
71+
![Screenshot](example-trace.png)
72+
16. Clean up GKE cluster/pods/services:
73+
74+
`gcloud container clusters delete cloud-trace-demo`
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Use the official lightweight Python image.
2+
# https://hub.docker.com/_/python
3+
FROM python:3.7-slim
4+
5+
# Copy local code to the container image.
6+
ENV APP_HOME /app
7+
WORKDIR $APP_HOME
8+
COPY . ./
9+
COPY requirements.txt /app/
10+
RUN pip install Flask gunicorn
11+
RUN pip install -r requirements.txt
12+
13+
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 app:app
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Copyright 2020 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+
# https://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+
A sample app demonstrating CloudTraceSpanExporter
16+
"""
17+
import random
18+
import time
19+
20+
# [START trace_demo_imports]
21+
import flask
22+
23+
from opentelemetry import trace
24+
from opentelemetry.exporter.cloud_trace import CloudTraceSpanExporter
25+
from opentelemetry.sdk.trace import TracerProvider
26+
from opentelemetry.sdk.trace.export import SimpleExportSpanProcessor
27+
28+
# [END trace_demo_imports]
29+
30+
app = flask.Flask(__name__)
31+
32+
33+
def configure_exporter(exporter):
34+
trace.set_tracer_provider(TracerProvider())
35+
36+
trace.get_tracer_provider().add_span_processor(SimpleExportSpanProcessor(exporter))
37+
38+
39+
tracer = trace.get_tracer(__name__)
40+
41+
42+
@app.route("/")
43+
def template_test():
44+
# Sleep for a random time to imitate a random processing time
45+
time.sleep(random.uniform(0, 0.5))
46+
47+
with tracer.start_as_current_span("span1"):
48+
with tracer.start_as_current_span("span2"):
49+
with tracer.start_as_current_span("span3"):
50+
print("Hello world from Cloud Trace Exporter!")
51+
52+
return "Hello World"
53+
54+
55+
if __name__ == "__main__":
56+
app.run(debug=True, host="0.0.0.0", port=8080)
57+
configure_exporter(CloudTraceSpanExporter())
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright 2020 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+
# https://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+
A sample app demonstrating Stackdriver Trace
16+
"""
17+
import mock
18+
19+
import app
20+
21+
22+
def test_traces():
23+
exporter = mock.Mock()
24+
app.configure_exporter(exporter)
25+
client = app.app.test_client()
26+
resp = client.get("/")
27+
assert resp.status_code == 200
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# This file configures the hello-world app which serves public web traffic.
2+
apiVersion: extensions/v1beta1
3+
kind: Deployment
4+
metadata:
5+
name: cloud-trace-demo
6+
spec:
7+
replicas: 1
8+
selector:
9+
matchLabels:
10+
app: trace-demo
11+
template:
12+
metadata:
13+
labels:
14+
app: trace-demo
15+
spec:
16+
containers:
17+
- name: trace-demo-app
18+
# Replace $GCLOUD_PROJECT with your project ID
19+
image: gcr.io/$GCLOUD_PROJECT/cloud-trace-demo:latest
20+
# This app listens on port 8080 for web traffic by default.
21+
ports:
22+
- containerPort: 8080
23+
env:
24+
- name: PORT
25+
value: "8080"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pytest==5.3.2
2+
mock==3.0.5
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Flask==1.1.2
2+
opentelemetry-api==0.9b0
3+
opentelemetry-auto-instrumentation==0.8b0
4+
opentelemetry-exporter-cloud-trace==0.9b0
5+
opentelemetry-ext-flask==0.8b0
6+
opentelemetry-ext-grpc==0.9b0
7+
opentelemetry-ext-jaeger==0.8b0
8+
opentelemetry-ext-requests==0.8b0
9+
opentelemetry-ext-wsgi==0.8b0
10+
opentelemetry-sdk==0.9b0
11+
grpcio==1.29.0
12+
httpretty==1.0.2
13+
google-cloud-monitoring==1.0.0
14+
google-cloud-trace==0.23.0
15+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# The hello service provides a load-balancing proxy over the hello-app
2+
# pods. By specifying the type as a 'LoadBalancer', Kubernetes Engine will
3+
# create an external HTTP load balancer.
4+
apiVersion: v1
5+
kind: Service
6+
metadata:
7+
name: trace-demo
8+
spec:
9+
type: LoadBalancer
10+
selector:
11+
app: trace-demo
12+
ports:
13+
- port: 80
14+
targetPort: 8080
Loading
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/bin/bash
2+
################## Set up service a ###########################
3+
4+
echo "Creating service a"
5+
kubectl apply -f app/demo-service-a.yaml
6+
7+
################## Set up service b ###########################
8+
echo "Fetching the external IP of service a"
9+
endpoint=""
10+
for run in {1..20}
11+
do
12+
echo "Attempt #${run} to fetch the external IP of service a..."
13+
sleep 5
14+
endpoint=`kubectl get svc cloud-trace-demo-a -ojsonpath='{.status.loadBalancer.ingress[0].ip}'`
15+
if [[ "$endpoint" != "" ]]; then
16+
break
17+
fi
18+
done
19+
20+
if [[ "$endpoint" == "" ]]; then
21+
echo "Unable to get external IP for service cloud-trace-demo-a"
22+
exit 1
23+
fi
24+
25+
echo "Passing external IP for the first service ${endpoint} to the second service template"
26+
sed "s/{{ endpoint }}/${endpoint}/g" app/demo-service-b.yaml.template > app/demo-service-b.yaml
27+
kubectl apply -f app/demo-service-b.yaml
28+
rm app/demo-service-b.yaml
29+
30+
################## Set up service c ###########################
31+
echo "Fetching the external IP of service b"
32+
endpoint=""
33+
for run in {1..20}
34+
do
35+
echo "Attempt #${run} to fetch the external IP of service b..."
36+
sleep 5
37+
endpoint=`kubectl get svc cloud-trace-demo-b -ojsonpath='{.status.loadBalancer.ingress[0].ip}'`
38+
if [[ "$endpoint" != "" ]]; then
39+
break
40+
fi
41+
done
42+
43+
if [[ "$endpoint" == "" ]]; then
44+
echo "Unable to get external IP for service cloud-trace-demo-a"
45+
exit 1
46+
fi
47+
48+
echo "Passing external IP for the service b ${endpoint} to the service c"
49+
sed "s/{{ endpoint }}/${endpoint}/g" app/demo-service-c.yaml.template > app/demo-service-c.yaml
50+
kubectl apply -f app/demo-service-c.yaml
51+
rm app/demo-service-c.yaml
52+
53+
echo "Successfully deployed all services"

0 commit comments

Comments
 (0)