Skip to content

Commit

Permalink
add python example
Browse files Browse the repository at this point in the history
  • Loading branch information
zeitlinger committed Feb 6, 2024
1 parent 166757a commit dd10b81
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea
venv/
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ The Docker image is available on Docker hub: https://hub.docker.com/r/grafana/ot
## Run the Docker image

```sh
docker run -p 3000:3000 -p 4317:4317 -p 4318:4318 --rm -ti grafana/otel-lgtm
./run-lgtm.sh
```

## Send OpenTelemetry Data
Expand Down Expand Up @@ -72,4 +72,5 @@ Each example uses a different application port (to be able to run all applicatio
|---------|---------------------------------------|
| Java | `curl http://localhost:8080/rolldice` |
| Go | `curl http://localhost:8081/rolldice` |
| Python | `curl http://localhost:8082/rolldice` |

11 changes: 0 additions & 11 deletions examples/go/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
# syntax=docker/dockerfile:1

FROM golang:1.21

# Set destination for COPY
WORKDIR /app

# Download Go modules
COPY go.mod go.sum ./
RUN go mod download

# Copy the source code. Note the slash at the end, as explained in
# https://docs.docker.com/engine/reference/builder/#copy
COPY *.go ./

# Build
RUN CGO_ENABLED=0 GOOS=linux go build -o /rolldice

# Optional:
# To bind to a TCP port, runtime parameters must be supplied to the docker command.
# But we can document in the Dockerfile what ports
# the application is going to listen on by default.
# https://docs.docker.com/engine/reference/builder/#expose
EXPOSE 8081

# Run
Expand Down
25 changes: 25 additions & 0 deletions examples/python/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
FROM python:alpine3.19

WORKDIR /app

COPY requirements.txt .

# How to get the requirements.txt file?
# 1. Follow https://opentelemetry.io/docs/languages/python/getting-started/
# 2. Run `pip freeze > requirements.txt` in the same directory as your app.py file
RUN pip install -r requirements.txt

RUN pip install opentelemetry-distro[otlp]
RUN opentelemetry-bootstrap -a install

COPY app.py .

# Logging support is still in alpha, so we need to enable it explicitly
ENV OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED=true

EXPOSE 8082

CMD ["opentelemetry-instrument", "flask", "run", "--host", "0.0.0.0", "--port", "8082"]



22 changes: 22 additions & 0 deletions examples/python/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from random import randint
from flask import Flask, request
import logging

app = Flask(__name__)
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


@app.route("/rolldice")
def roll_dice():
player = request.args.get('player', default=None, type=str)
result = str(roll())
if player:
logger.warning("%s is rolling the dice: %s", player, result)
else:
logger.warning("Anonymous player is rolling the dice: %s", result)
return result


def roll():
return randint(1, 6)
13 changes: 13 additions & 0 deletions examples/python/docker-compose.oats.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# OATS is an acceptance testing framework for OpenTelemetry - https://github.com/grafana/oats/tree/main/yaml
version: '3.4'

services:
go:
build:
context: .
dockerfile: Dockerfile
environment:
OTEL_EXPORTER_OTLP_ENDPOINT: http://collector:4318
OTEL_METRIC_EXPORT_INTERVAL: "5000" # so we don't have to wait 60s for metrics
ports:
- "8080:8082"
17 changes: 17 additions & 0 deletions examples/python/oats.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# OATS is an acceptance testing framework for OpenTelemetry - https://github.com/grafana/oats/tree/main/yaml
docker-compose:
generator: lgtm
files:
- ./docker-compose.oats.yml
input:
- path: /rolldice
expected:
traces:
- traceql: '{ span.http.route = "/rolldice" }'
spans:
- name: '/rolldice' # should be "GET /rolldice"
attributes:
otel.library.name: opentelemetry.instrumentation.flask
metrics:
- promql: 'http_server_active_requests{http_method="GET"}'
value: ">= 0"
7 changes: 7 additions & 0 deletions examples/python/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
blinker==1.7.0
click==8.1.7
Flask==2.3.3
itsdangerous==2.1.2
Jinja2==3.1.3
MarkupSafe==2.1.5
Werkzeug==2.3.8
20 changes: 20 additions & 0 deletions examples/python/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

set -euo pipefail

export OTEL_METRIC_EXPORT_INTERVAL="5000" # so we don't have to wait 60s for metrics
export OTEL_RESOURCE_ATTRIBUTES="service.name=rolldice,service.instance.id=localhost:8082"
export OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED=true

python3 -m venv venv
source ./venv/bin/activate

# How to get the requirements.txt file?
# 1. Follow https://opentelemetry.io/docs/languages/python/getting-started/
# 2. Run `pip freeze > requirements.txt` in the same directory as your app.py file
pip install -r requirements.txt

pip install opentelemetry-distro[otlp]
opentelemetry-bootstrap -a install

opentelemetry-instrument flask run -p 8082
2 changes: 1 addition & 1 deletion generate-traffic.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash

watch 'curl -s http://localhost:8080/rolldice; curl -s http://localhost:8081/rolldice'
watch 'curl -s http://localhost:8080/rolldice; curl -s http://localhost:8081/rolldice; curl -s http://localhost:8082/rolldice'
3 changes: 3 additions & 0 deletions run-lgtm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

docker run -p 3000:3000 -p 4317:4317 -p 4318:4318 --rm -ti grafana/otel-lgtm

0 comments on commit dd10b81

Please sign in to comment.