Skip to content

Commit

Permalink
maint: dockerizing flask app (#83)
Browse files Browse the repository at this point in the history
## Which problem is this PR solving?

- Closes #72 

## Short description of the changes

- add Dockerfile for flask app
- update `opentelemetry-api`, `opentelemetry-sdk`, and
`opentelemetry-exporter-otlp` from `1.14.0` to `1.16.0` to help deal
with version conflicts
- update `opentelemetry-instrumentation` and
`opentelemetry-instrumentation-flask` from `0.36b0` to `0.37b0` to help
deal with version conflicts
- update `pylint`
- add flask app to docker-compose
- whitespace cleanup

## How to verify that this has the expected result

Build the flask app, and locally `curl localhost:5000`:

`cd smoke-tests && docker-compose up --build app-sdk-grpc-flask`

---------

Co-authored-by: Robb Kidd <robbkidd@honeycomb.io>
Co-authored-by: Emily Ashley <15912063+emilyashley@users.noreply.github.com>
  • Loading branch information
3 people committed Mar 1, 2023
1 parent 84a5dd2 commit cbbc998
Show file tree
Hide file tree
Showing 8 changed files with 978 additions and 903 deletions.
31 changes: 31 additions & 0 deletions examples/hello-world-flask/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
FROM python:3.10-alpine

WORKDIR /app

RUN apk add --no-cache gcc musl-dev python3-dev libffi-dev openssl-dev cargo g++

ENV PYTHONFAULTHANDLER=1 \
PYTHONUNBUFFERED=1 \
PYTHONHASHSEED=random \
PIP_NO_CACHE_DIR=off \
PIP_DISABLE_PIP_VERSION_CHECK=on \
PIP_DEFAULT_TIMEOUT=100 \
POETRY_VERSION=1.3.2

# This project is poetic.
RUN pip install "poetry==$POETRY_VERSION"
# This image is single purpose, so we won't need to compartmentalize Py deps in virtualenvs.
RUN poetry config virtualenvs.create false

# Copy the distro into the image and install deps.
COPY README.md pyproject.toml poetry.lock ./
COPY src/ ./src/
RUN poetry install

# Copy the examples into the image and install deps.
COPY examples/ ./examples/
RUN cd ./examples/hello-world-flask && poetry install

# From this point forward, we're operating on the flask example.
WORKDIR /app/examples/hello-world-flask
CMD ["poetry", "run", "opentelemetry-instrument", "flask", "run", "--host", "0.0.0.0"]
15 changes: 10 additions & 5 deletions examples/hello-world-flask/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,26 @@

# Recommended: use attach and detach tokens for Context management with Baggage


@app.route("/")
def hello_world():
token = attach(baggage.set_baggage("queen", "bee"))

with tracer.start_as_current_span(name="honey"):
token_honey = attach(baggage.set_baggage("honey", "bee"))

with tracer.start_as_current_span(name="child"):
# this goes nowhere if it is the final span in a trace
child_token = attach(baggage.set_baggage("bee", "that_maybe_doesnt_propagate"))
child_token = attach(
baggage.set_baggage("bee", "that_maybe_doesnt_propagate")
)
detach(child_token)
detach(token_honey)
detach(token)
bee_counter.add(1, {'app.route': '/'})
return "Hello World"

# For manually passing around the Context for baggage

@app.route("/ctx")
def hello_ctx_world():
ctx = baggage.set_baggage("worker", "bees")
Expand All @@ -47,6 +49,9 @@ def hello_ctx_world():
ctx = baggage.set_baggage("additional", "bees", ctx)
with tracer.start_as_current_span(name="last", context=ctx):
# this goes nowhere if it is the final span in a trace
ctx = baggage.set_baggage("last", "bee", ctx)
ctx = baggage.set_baggage("last", "bee", ctx)
bee_counter.add(1, {'app.route': '/ctx'})
return "Hello Context World"
return "Hello Context World"

if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)
719 changes: 359 additions & 360 deletions examples/hello-world-flask/poetry.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion examples/hello-world-flask/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ readme = "README.md"
python = "^3.10"
flask = "^2.2.2"
honeycomb-opentelemetry = {path = "../../", develop = true}
opentelemetry-instrumentation-flask = "^0.36b0"
opentelemetry-api = "^1.16.0"
opentelemetry-instrumentation-flask = "^0.37b0"

[build-system]
requires = ["poetry-core"]
Expand Down
1,061 changes: 536 additions & 525 deletions poetry.lock

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ packages = [{include = "honeycomb", from = "src" }]

[tool.poetry.dependencies]
python = "^3.7, >= 3.7.2"
opentelemetry-api = "^1.14.0"
opentelemetry-sdk = "^1.14.0"
opentelemetry-exporter-otlp = "^1.14.0"
opentelemetry-instrumentation = "^0.36b0"
opentelemetry-api = "^1.16.0"
opentelemetry-sdk = "^1.16.0"
opentelemetry-exporter-otlp = "^1.16.0"
opentelemetry-instrumentation = "^0.37b0"

[tool.poetry.group.dev.dependencies]
coverage = "^6.5.0"
pytest = "^7.2.0"
pylint = "^2.15.7"
pylint = "^2.16.0"
pycodestyle = "^2.10.0"
importlib-metadata = { version = ">=0.12", python = "<3.8" }
requests-mock = "^1.10.0"
Expand Down
40 changes: 34 additions & 6 deletions smoke-tests/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
version: '3.0'

x-env-base: &env_base
x-env-base:
&env_base
HONEYCOMB_API_ENDPOINT: http://collector:4317
HONEYCOMB_API_KEY: bogus_key
HONEYCOMB_DATASET: bogus_dataset
Expand All @@ -9,35 +10,62 @@ x-env-base: &env_base
OTEL_EXPORTER_OTLP_INSECURE: "true"
DEBUG: "true"

x-app-base: &app_base
x-app-base:
&app_base
build:
context: ../
dockerfile: ./examples/hello-world/Dockerfile
image: honeycomb/hello-world
depends_on:
- collector

x-flask-app-base:
&flask_app_base
build:
context: ../
dockerfile: ./examples/hello-world-flask/Dockerfile
image: honeycomb/hello-world-flask
depends_on:
- collector

services:
collector:
image: otel/opentelemetry-collector:0.52.0
command: ["--config=/etc/otel-collector-config.yaml"]
command: [ "--config=/etc/otel-collector-config.yaml" ]
volumes:
- "./collector/otel-collector-config.yaml:/etc/otel-collector-config.yaml"
- "./collector:/var/lib"

app-sdk-grpc:
<<: *app_base
ports:
- "127.0.0.1:5000:5000"
environment:
<<: *env_base
OTEL_EXPORTER_OTLP_PROTOCOL: grpc
ports:
- "127.0.0.1:5000:5000"

app-sdk-http:
<<: *app_base
ports:
- "127.0.0.1:5000:5000"
environment:
<<: *env_base
HONEYCOMB_API_ENDPOINT: http://collector:4318
OTEL_EXPORTER_OTLP_PROTOCOL: http/protobuf

app-sdk-grpc-flask:
<<: *flask_app_base
ports:
- "127.0.0.1:5000:5000"
- "127.0.0.1:5000:5000"
environment:
<<: *env_base
OTEL_EXPORTER_OTLP_PROTOCOL: grpc

app-sdk-http-flask:
<<: *flask_app_base
ports:
- "127.0.0.1:5000:5000"
environment:
<<: *env_base
HONEYCOMB_API_ENDPOINT: http://collector:4318
OTEL_EXPORTER_OTLP_PROTOCOL: http/protobuf
2 changes: 1 addition & 1 deletion smoke-tests/smoke-sdk-http.bats
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ setup_file() {
}

teardown_file() {
cp collector/data.json collector/data-results/data-${CONTAINER_NAME}.json
cp collector/data.json collector/data-results/data-${CONTAINER_NAME}.json
docker-compose stop ${CONTAINER_NAME}
docker-compose restart collector
wait_for_flush
Expand Down

0 comments on commit cbbc998

Please sign in to comment.