Skip to content

Instantly share code, notes, and snippets.

@PeterJCLaw
Last active May 13, 2023 14:12
Show Gist options
  • Save PeterJCLaw/5e6fd585d0c620496c82ca926ce50f67 to your computer and use it in GitHub Desktop.
Save PeterJCLaw/5e6fd585d0c620496c82ca926ce50f67 to your computer and use it in GitHub Desktop.
Toy WSGI app for testing Open Telemetry `BatchSpanProcessor`

Demo wsgi app

This is a toy WSGI app under Gunicorn and uWSGI, used to explore forking behaviours interacting with Open Telemetry (and BatchSpanProcessor in particular).

gunicorn wsgi:app --workers 3

Optionally plus --preload, so that gunicorn will import Python into the main process before forking the workers.

uwsgi --http :8000 --master --workers 3 --import uwsgi-tasks.py --module wsgi:app

Optionally plus --lazy-apps, so that uWSGI will delay importing Python until after the workers have been forked.

Note: the default loading of Python under gunicorn and uWSGI are inverted relative to each other.

import os
def pre_fork(server, worker):
print(f'pre_fork({server!r}, {worker!r}) in', os.getpid())
def post_fork(server, worker):
print(f'post_fork({server!r}, {worker!r}) in', os.getpid())
gunicorn==20.1.0
opentelemetry-sdk==1.17.0
uWSGI==2.0.21
import os
import uwsgidecorators
@uwsgidecorators.postfork
def post_fork():
print(f'post_fork in', os.getpid())
import os
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import (
BatchSpanProcessor,
ConsoleSpanExporter,
)
provider = TracerProvider()
processor = BatchSpanProcessor(ConsoleSpanExporter())
provider.add_span_processor(processor)
trace.set_tracer_provider(provider)
tracer = trace.get_tracer("my.tracer.name")
print('imported in', os.getpid())
def app(environ, start_response):
with tracer.start_as_current_span("span-name") as span:
print('app in', os.getpid())
data = b"Hello, World!\n"
start_response("200 OK", [
("Content-Type", "text/plain"),
("Content-Length", str(len(data)))
])
return iter([data])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment