Skip to content

New directory for OpenTelemetry QuickStart #4016

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
38 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
5878bdb
Merge branch 'master' into porting_to_opentelemetry
aravinsiva Jun 9, 2020
2e7fea8
Making modifications suggested by Shawn
aravinsiva Jun 10, 2020
5b80ad4
Merge branch 'porting_to_opentelemetry' of https://github.com/aravins…
aravinsiva Jun 10, 2020
2b78b62
Merge branch 'master' into porting_to_opentelemetry
aravinsiva Jun 10, 2020
c8f6b81
Merge branch 'master' into porting_to_opentelemetry
aravinsiva Jun 10, 2020
10dc3fb
adding Shawn suggested documentation changes
aravinsiva Jun 11, 2020
66b6007
Merge branch 'master' into porting_to_opentelemetry
aravinsiva Jun 15, 2020
4fcaddb
making suggested edits
aravinsiva Jun 15, 2020
c81c293
Merge branch 'porting_to_opentelemetry' of https://github.com/aravins…
aravinsiva Jun 15, 2020
9c5cfb3
Merge branch 'master' into porting_to_opentelemetry
aravinsiva Jun 15, 2020
c62cf97
Merge branch 'master' into porting_to_opentelemetry
aravinsiva Jun 19, 2020
83b1628
making more suggested changes to README.md
aravinsiva Jun 19, 2020
ab5483b
Merge branch 'master' into porting_to_opentelemetry
aravinsiva Jun 19, 2020
e90e001
adding production PyPi librarires
aravinsiva Jun 20, 2020
ad9f0c7
Merge branch 'porting_to_opentelemetry' of https://github.com/aravins…
aravinsiva Jun 20, 2020
181e5d4
Merge branch 'master' into porting_to_opentelemetry
aravinsiva Jun 23, 2020
a79c2c9
Merge branch 'master' into porting_to_opentelemetry
aravinsiva Aug 12, 2020
d1b188f
fixing undefined issues
aravinsiva Aug 13, 2020
99e8999
fixing test issues and simplyfying example
aravinsiva Aug 18, 2020
06189f2
fixing mismatched region tag issue
aravinsiva Aug 18, 2020
283ca73
Merge branch 'master' into porting_to_opentelemetry
leahecole Aug 21, 2020
c94cff9
reworking service for GKE
aravinsiva Aug 24, 2020
58469aa
Merge branch 'porting_to_opentelemetry' of https://github.com/aravins…
aravinsiva Aug 24, 2020
f5be627
linting and adding more descriptive README.md
aravinsiva Aug 24, 2020
dc722c9
fixing test issues
aravinsiva Aug 24, 2020
2c66f96
Merge branch 'master' into porting_to_opentelemetry
leahecole Aug 25, 2020
e5b280f
Merge branch 'master' into porting_to_opentelemetry
leahecole Aug 26, 2020
634986e
linting
aravinsiva Aug 26, 2020
7dd263e
Merge branch 'porting_to_opentelemetry' of https://github.com/aravins…
aravinsiva Aug 26, 2020
288d302
adding suggested changes by Shawn
aravinsiva Aug 27, 2020
4b3681e
Merge branch 'master' into porting_to_opentelemetry
aravinsiva Aug 27, 2020
37f6f3f
Merge branch 'master' into porting_to_opentelemetry
leahecole Aug 27, 2020
c76a120
linting and adding Shawn suggested changes
aravinsiva Aug 27, 2020
ef779bf
Merge branch 'porting_to_opentelemetry' of https://github.com/aravins…
aravinsiva Aug 27, 2020
8e1c97a
Merge branch 'master' into porting_to_opentelemetry
aravinsiva Aug 27, 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
74 changes: 74 additions & 0 deletions trace/cloud-trace-demo-app-opentelemetry/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# cloud-trace-demo-app-opentelemetry

Open this demo app in [Google Cloud Shell](https://cloud.google.com/shell/docs/). This includes necessary tools.


[![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&cloudshell_tutorial=trace/cloud-trace-demo-app/README.md)

#### Demo Requirements
If you are using Cloud Shell, skip to the next section.

1. Install gcloud <https://cloud.google.com/sdk/install>
2. Install kubectl <https://kubernetes.io/docs/tasks/tools/install-kubectl/>
3. Install docker <https://docs.docker.com/install/>


#### Create a GKE cluster

4. Enable Google Cloud and set up region and zone.

`gcloud init`
5. Enable the GKE API & billing:

`gcloud services enable container.googleapis.com`
6. Create a GKE cluster named "cloud-trace-demo", replacing `your-gcp-zone` below
with the
[GCP Zone](https://cloud.google.com/compute/docs/regions-zones) closest in proximity to you:

```
gcloud container clusters create cloud-trace-demo\`
--num-nodes 1 \
--enable-basic-auth \
--issue-client-certificate \
--zone your-gcp-zone
```
7. Verify that you have access to the cluster:

`kubectl get nodes`

#### Deploy The Cloud Trace Demo App

8. Build and tag the docker image for demo app:

`docker build -t gcr.io/${PROJECT_ID}/cloud-trace-demo:v1 .`
9. Deploy resource to the cluster:

`kubectl apply -f deployment.yaml`
10. Track the status of the deployment:

`kubectl get deployments`

Deployment is complete when all of the available deployments are ready.
11. Run the following command to see the pods the deployment created:

`kubectl get pods`

#### Deploy The Cloud Trace Demo Service

12. Create the cloud trace demo service:

`kubectl apply -f service.yaml`
13. Get the services IP address by running the following command:

`kubectl get services`
14. Send a curl request to the EXTERNAL_IP, replacing `EXTERNAL_IP` with the external IP address found
in step 13:

`curl EXTERNAL_IP`
15. Visit [Trace List](https://console.cloud.google.com/traces/list) to check traces generated.
Click on any trace in the graph to see the Waterfall View.

![Screenshot](example-trace.png)
16. Clean up GKE cluster/pods/services:

`gcloud container clusters delete cloud-trace-demo`
13 changes: 13 additions & 0 deletions trace/cloud-trace-demo-app-opentelemetry/app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Use the official lightweight Python image.
# https://hub.docker.com/_/python
FROM python:3.7-slim

# Copy local code to the container image.
ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . ./
COPY requirements.txt /app/
RUN pip install Flask gunicorn
RUN pip install -r requirements.txt

CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 app:app
57 changes: 57 additions & 0 deletions trace/cloud-trace-demo-app-opentelemetry/app/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# 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
#
# https://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.
"""
A sample app demonstrating CloudTraceSpanExporter
"""
import random
import time

# [START trace_demo_imports]
import flask

from opentelemetry import trace
from opentelemetry.exporter.cloud_trace import CloudTraceSpanExporter
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleExportSpanProcessor

# [END trace_demo_imports]

app = flask.Flask(__name__)


def configure_exporter(exporter):
trace.set_tracer_provider(TracerProvider())

trace.get_tracer_provider().add_span_processor(SimpleExportSpanProcessor(exporter))


tracer = trace.get_tracer(__name__)


@app.route("/")
def template_test():
# Sleep for a random time to imitate a random processing time
time.sleep(random.uniform(0, 0.5))

with tracer.start_as_current_span("span1"):
with tracer.start_as_current_span("span2"):
with tracer.start_as_current_span("span3"):
print("Hello world from Cloud Trace Exporter!")

return "Hello World"


if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=8080)
configure_exporter(CloudTraceSpanExporter())
27 changes: 27 additions & 0 deletions trace/cloud-trace-demo-app-opentelemetry/app/app_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 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
#
# https://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.
"""
A sample app demonstrating Stackdriver Trace
"""
import mock

import app


def test_traces():
exporter = mock.Mock()
app.configure_exporter(exporter)
client = app.app.test_client()
resp = client.get("/")
assert resp.status_code == 200
25 changes: 25 additions & 0 deletions trace/cloud-trace-demo-app-opentelemetry/app/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# This file configures the hello-world app which serves public web traffic.
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: cloud-trace-demo
spec:
replicas: 1
selector:
matchLabels:
app: trace-demo
template:
metadata:
labels:
app: trace-demo
spec:
containers:
- name: trace-demo-app
# Replace $GCLOUD_PROJECT with your project ID
image: gcr.io/$GCLOUD_PROJECT/cloud-trace-demo:latest
# This app listens on port 8080 for web traffic by default.
ports:
- containerPort: 8080
env:
- name: PORT
value: "8080"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pytest==5.3.2
mock==3.0.5
15 changes: 15 additions & 0 deletions trace/cloud-trace-demo-app-opentelemetry/app/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Flask==1.1.2
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
grpcio==1.29.0
httpretty==1.0.2
google-cloud-monitoring==1.0.0
google-cloud-trace==0.23.0

14 changes: 14 additions & 0 deletions trace/cloud-trace-demo-app-opentelemetry/app/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# The hello service provides a load-balancing proxy over the hello-app
# pods. By specifying the type as a 'LoadBalancer', Kubernetes Engine will
# create an external HTTP load balancer.
apiVersion: v1
kind: Service
metadata:
name: trace-demo
spec:
type: LoadBalancer
selector:
app: trace-demo
ports:
- port: 80
targetPort: 8080
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
53 changes: 53 additions & 0 deletions trace/cloud-trace-demo-app-opentelemetry/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/bash
################## Set up service a ###########################

echo "Creating service a"
kubectl apply -f app/demo-service-a.yaml

################## Set up service b ###########################
echo "Fetching the external IP of service a"
endpoint=""
for run in {1..20}
do
echo "Attempt #${run} to fetch the external IP of service a..."
sleep 5
endpoint=`kubectl get svc cloud-trace-demo-a -ojsonpath='{.status.loadBalancer.ingress[0].ip}'`
if [[ "$endpoint" != "" ]]; then
break
fi
done

if [[ "$endpoint" == "" ]]; then
echo "Unable to get external IP for service cloud-trace-demo-a"
exit 1
fi

echo "Passing external IP for the first service ${endpoint} to the second service template"
sed "s/{{ endpoint }}/${endpoint}/g" app/demo-service-b.yaml.template > app/demo-service-b.yaml
kubectl apply -f app/demo-service-b.yaml
rm app/demo-service-b.yaml

################## Set up service c ###########################
echo "Fetching the external IP of service b"
endpoint=""
for run in {1..20}
do
echo "Attempt #${run} to fetch the external IP of service b..."
sleep 5
endpoint=`kubectl get svc cloud-trace-demo-b -ojsonpath='{.status.loadBalancer.ingress[0].ip}'`
if [[ "$endpoint" != "" ]]; then
break
fi
done

if [[ "$endpoint" == "" ]]; then
echo "Unable to get external IP for service cloud-trace-demo-a"
exit 1
fi

echo "Passing external IP for the service b ${endpoint} to the service c"
sed "s/{{ endpoint }}/${endpoint}/g" app/demo-service-c.yaml.template > app/demo-service-c.yaml
kubectl apply -f app/demo-service-c.yaml
rm app/demo-service-c.yaml

echo "Successfully deployed all services"