-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initialise opentelemetry in the gunicorn post_fork hook for prod. Includes an `OTEL_CONSOLE` env variable that can be used to send traces to the console in local dev environments.
- Loading branch information
Showing
5 changed files
with
82 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,17 @@ | ||
import tracing | ||
|
||
|
||
# workers | ||
workers = 4 | ||
|
||
# listen | ||
port = 8000 | ||
bind = "0.0.0.0" | ||
|
||
|
||
# Because of Gunicorn's pre-fork web server model, we need to initialise opentelemetry | ||
# in gunicorn's post_fork method in order to instrument our application process, see: | ||
# https://opentelemetry-python.readthedocs.io/en/latest/examples/fork-process-model/README.html | ||
def post_fork(server, worker): | ||
server.log.info("Worker spawned (pid: %s)", worker.pid) | ||
tracing.setup_default_tracing() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import os | ||
|
||
from opentelemetry import trace | ||
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter | ||
from opentelemetry.sdk.trace import TracerProvider | ||
from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter | ||
|
||
|
||
def get_provider(): | ||
provider = TracerProvider() | ||
trace.set_tracer_provider(provider) | ||
return provider | ||
|
||
|
||
def add_exporter(provider, exporter, processor=BatchSpanProcessor): | ||
"""Utility method to add an exporter. | ||
We use the BatchSpanProcessor by default, which is the default for | ||
production. This is asynchronous, and queues and retries sending telemetry. | ||
In testing, we instead use SimpleSpanProcessor, which is synchronous and | ||
easy to inspect the output of within a test. | ||
""" | ||
# Note: BatchSpanProcessor is configured via env vars: | ||
# https://opentelemetry-python.readthedocs.io/en/latest/sdk/trace.export.html#opentelemetry.sdk.trace.export.BatchSpanProcessor | ||
provider.add_span_processor(processor(exporter)) | ||
|
||
|
||
def setup_default_tracing(): | ||
provider = get_provider() | ||
|
||
"""Inspect environment variables and set up exporters accordingly.""" | ||
if "OTEL_EXPORTER_OTLP_HEADERS" in os.environ: | ||
if "OTEL_SERVICE_NAME" not in os.environ: | ||
raise Exception( | ||
"OTEL_EXPORTER_OTLP_HEADERS is configured, but missing OTEL_SERVICE_NAME" | ||
) | ||
if "OTEL_EXPORTER_OTLP_ENDPOINT" not in os.environ: | ||
os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] = "https://api.honeycomb.io" | ||
|
||
add_exporter(provider, OTLPSpanExporter()) | ||
|
||
if "OTEL_EXPORTER_CONSOLE" in os.environ: | ||
add_exporter(provider, ConsoleSpanExporter()) | ||
|
||
from opentelemetry.instrumentation.auto_instrumentation import ( # noqa: F401 | ||
sitecustomize, | ||
) |