$ mkdir workshop-opentelemetry
$ cd workshop-opentelemetry
$ cd intro-to-instrument-v1.2
$ podman build -t hello-otel:base -f Buildfile
Successfully tagged localhost/hello-otel:base \
516c5299a32b68e7a4634ce15d1fd659eed2164ebe945ef1673f7a55630e22c8
$ podman run -i -p 8001:8000 -e FLASK_RUN_PORT=8000 hello-otel:base
This webpage has been viewed 1 times
The demo application is a Flask web application written in Python.
Routes: * Debug mode: off
WARNING: This is a development server. Do not use it in a
production deployment. Use a production WSGI server instead.
* Running on all addresses (0.0.0.0)
* Running on http://127.0.0.1:8000
* Running on http://10.88.0.15:8000
Press CTRL+C to quit
10.88.0.18 - - [11/Jul/2024 10:24:55] "GET / HTTP/1.1" 200 -
$ podman build -t hello-otel:auto -f automatic/Buildfile-auto
Successfully tagged localhost/hello-otel:auto \
516c5299a32b68e7a4634ce15d1fd659eed2164ebe945ef1673f7a55630e22c8
flask run
command with the opentelemetry-instrument agent:
podman run -i -p 8001:8000 -e FLASK_RUN_PORT=8000 hello-otel:auto opentelemetry-instrument \
--traces_exporter console \
--metrics_exporter none \
--service_name hello-otel \
flask run --host=0.0.0.0
{
"name": "jinja2.compile",
"context": {
"trace_id": "0xcc03abd2bda0d500f650509da7aafd8e",
"span_id": "0xcc036db1701c8f49",
"trace_state": "[]"
},
"kind": "SpanKind.INTERNAL",
"parent_id": "0x1d73545725fd3568",
"start_time": "2024-04-18T17:57:16.631588Z",
"end_time": "2024-04-18T17:57:16.636529Z",
"status": {
"status_code": "UNSET"
},
"attributes": {
"jinja2.template_name": "random-pet-pic.html"
},
"events": [],
"links": [],
"resource": {
"attributes": {
"telemetry.sdk.language": "python",
"telemetry.sdk.name": "opentelemetry",
"telemetry.sdk.version": "1.25.0",
"service.name": "hello-otel",
"telemetry.auto.version": "0.46b0"
},
"schema_url": ""
}
}
opentelemetry-bootstrap
installed was
opentelemetry-instrumentation-flask
. This instruments web requests to the Flask
application. http.method
or net.host.port
but nothing about the
HITS
variable which is our custom code (scroll to view all the code below):
{
"name": "/",
"context": {
"trace_id": "0x42b9a36256ff46ba99060ddc45c02626",
"span_id": "0xbb725f3712a259f5",
"trace_state": "[]"
},
"kind": "SpanKind.SERVER",
"parent_id": null,
"start_time": "2024-04-18T21:53:52.327122Z",
"end_time": "2024-04-18T21:53:52.328112Z",
"status": {
"status_code": "UNSET"
},
"attributes": {
"http.method": "GET",
"http.server_name": "0.0.0.0",
"http.scheme": "http",
"net.host.port": 8000,
"http.host": "localhost:8001",
"http.target": "/",
"net.peer.ip": "10.88.0.25",
"http.user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36",
"net.peer.port": 52096,
"http.flavor": "1.1",
"http.route": "/",
"http.status_code": 200
},
"events": [],
"links": [],
"resource": {
"attributes": {
"telemetry.sdk.language": "python",
"telemetry.sdk.name": "opentelemetry",
"telemetry.sdk.version": "1.25.0",
"service.name": "hello-otel",
"telemetry.auto.version": "0.46b0"
},
"schema_url": ""
}
}
automatic/app.py
and import the OpenTelemetry SDK, which is already installed by opentelemetry-distro
:
import random
import re
import urllib3
import requests
from opentelemetry import trace
from flask import Flask, render_template, request
...
opentelemetry-instrument
agent creates one automatically,
so our code only needs to be responsible for accessing it.
import random
import re
import urllib3
import requests
from opentelemetry import trace
from flask import Flask, render_template, request
from breeds import breeds
app = Flask(__name__)
tracer = trace.get_tracer(app.name)
...
index()
method associated with the /
route and update to access the auto-instrumented span and add an attribute key-value pair for
"hits" and the value stored in HITS:
@app.route('/')
def index():
span = trace.get_current_span()
global HITS
HITS = HITS + 1
span.set_attribute("hits", HITS)
msg = f'This webpage has been viewed {HITS} times'
return msg
$ podman build -t hello-otel:auto-manual -f automatic/Buildfile-auto
Successfully tagged localhost/hello-otel:auto-manual /
516c5299a32b68e7a4634ce15d1fd659eed2164ebe945ef1673f7a55630e22c8
podman run -i -p 8001:8000 -e FLASK_RUN_PORT=8000 hello-otel:auto-manual opentelemetry-instrument \
--traces_exporter console \
--metrics_exporter none \
--service_name hello-otel \
flask run --host=0.0.0.0
{
"name": "/",
"context": {
"trace_id": "0x51f7a355b313fb63b733bd6aadbd5a46",
"span_id": "0xd4d29507a68dc3d1",
"trace_state": "[]"
},
"kind": "SpanKind.SERVER",
"parent_id": null,
"start_time": "2024-04-18T22:45:59.436677Z",
"end_time": "2024-04-18T22:45:59.445243Z",
"status": {
"status_code": "UNSET"
},
"attributes": {
"http.method": "GET",
"http.server_name": "0.0.0.0",
"http.scheme": "http",
"net.host.port": 8000,
"http.host": "localhost:8001",
"http.target": "/",
"net.peer.ip": "10.88.0.27",
"http.user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36",
"net.peer.port": 33384,
"http.flavor": "1.1",
"http.route": "/",
"hits": 2,
"http.status_code": 200
},
"events": [],
"links": [],
"resource": {
"attributes": {
"telemetry.sdk.language": "python",
"telemetry.sdk.name": "opentelemetry",
"telemetry.sdk.version": "1.25.0",
"service.name": "hello-otel",
"telemetry.auto.version": "0.46b0"
},
"schema_url": ""
}
}
Enter CTRL-C in the console to stop the container