Skip to content
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

add python example #15

Merged
merged 3 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
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
2 changes: 1 addition & 1 deletion examples/go/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ set -euo pipefail

export OTEL_EXPORTER_OTLP_INSECURE="true" # needed because of https://github.com/open-telemetry/opentelemetry-go/issues/4834
export OTEL_METRIC_EXPORT_INTERVAL="5000" # so we don't have to wait 60s for metrics
export OTEL_RESOURCE_ATTRIBUTES="service.name=example-app,service.instance.id=localhost:8081"
export OTEL_RESOURCE_ATTRIBUTES="service.name=rolldice,service.instance.id=localhost:8081"
go run .
2 changes: 1 addition & 1 deletion examples/java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>

<groupId>com.grafana.example</groupId>
<artifactId>example-app</artifactId>
<artifactId>rolldice</artifactId>
<version>1.0.0-SNAPSHOT</version>

<name>Hello World REST Service</name>
Expand Down
6 changes: 3 additions & 3 deletions examples/java/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

set -euo pipefail

if [[ ! -f ./target/example-app.jar ]] ; then
if [[ ! -f ./target/rolldice.jar ]] ; then
./mvnw clean package
fi
version=v2.0.0
jar=opentelemetry-javaagent-${version}.jar
if [[ ! -f ./${jar} ]] ; then
curl -sL https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/download/v2.0.0/opentelemetry-javaagent.jar -o ${jar}
fi
export OTEL_RESOURCE_ATTRIBUTES="service.name=example-app,service.instance.id=localhost:8080"
java -Dotel.metric.export.interval=500 -Dotel.bsp.schedule.delay=500 -javaagent:${jar} -jar ./target/example-app.jar
export OTEL_RESOURCE_ATTRIBUTES="service.name=rolldice,service.instance.id=localhost:8080"
java -Dotel.metric.export.interval=500 -Dotel.bsp.schedule.delay=500 -javaagent:${jar} -jar ./target/rolldice.jar
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
Loading