Instrument celery, move init into apps.py

This commit is contained in:
Joel Bradshaw 2022-01-01 14:11:45 -08:00
parent 40bec83833
commit 5b3ff0cf82
8 changed files with 48 additions and 18 deletions

View file

@ -85,7 +85,7 @@ PREVIEW_DEFAULT_COVER_COLOR=#002549
# data sent, used to distinguish different sources. Useful
# for sending prod and dev metrics to the same place and
# keeping them separate, for instance!
#
# OTEL_EXPORTER_OTLP_ENDPOINT=https://your.api.endpoint # API endpoint for your provider
# OTEL_EXPORTER_OTLP_HEADERS= # Any headers required, usually authentication info
# OTEL_SERVICE_NAME=your_service_name
OTEL_EXPORTER_OTLP_ENDPOINT= # API endpoint for your provider
OTEL_EXPORTER_OTLP_HEADERS= # Any headers required, usually authentication info
OTEL_SERVICE_NAME= # Service name to identify your app

10
bookwyrm/apps.py Normal file
View file

@ -0,0 +1,10 @@
from django.apps import AppConfig
from bookwyrm import settings
class BookwyrmConfig(AppConfig):
name = 'bookwyrm'
verbose_name = "BookWyrm"
def ready(self):
if settings.OTEL_EXPORTER_OTLP_ENDPOINT:
from bookwyrm.telemetry import open_telemetry
open_telemetry.instrumentDjango()

View file

@ -233,3 +233,7 @@ else:
MEDIA_FULL_URL = f"{PROTOCOL}://{DOMAIN}{MEDIA_URL}"
STATIC_FULL_URL = f"{PROTOCOL}://{DOMAIN}{STATIC_URL}"
MEDIA_ROOT = os.path.join(BASE_DIR, env("MEDIA_ROOT", "images"))
OTEL_EXPORTER_OTLP_ENDPOINT = env("OTEL_EXPORTER_OTLP_ENDPOINT")
OTEL_EXPORTER_OTLP_HEADERS = env("OTEL_EXPORTER_OTLP_HEADERS")
OTEL_SERVICE_NAME = env("OTEL_SERVICE_NAME")

View file

@ -1,10 +0,0 @@
def InstallOpenTelemetry():
from opentelemetry.instrumentation.django import DjangoInstrumentor
from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
DjangoInstrumentor().instrument()
trace.set_tracer_provider(TracerProvider())
trace.get_tracer_provider().add_span_processor(BatchSpanProcessor(OTLPSpanExporter()))

View file

@ -0,0 +1,19 @@
from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
trace.set_tracer_provider(TracerProvider())
trace.get_tracer_provider().add_span_processor(BatchSpanProcessor(OTLPSpanExporter()))
def instrumentDjango():
from opentelemetry.instrumentation.django import DjangoInstrumentor
DjangoInstrumentor().instrument()
def instrumentCelery():
from opentelemetry.instrumentation.celery import CeleryInstrumentor
from celery.signals import worker_process_init
@worker_process_init.connect(weak=False)
def init_celery_tracing(*args, **kwargs):
CeleryInstrumentor().instrument()

View file

@ -10,13 +10,9 @@ https://docs.djangoproject.com/en/2.0/howto/deployment/wsgi/
import os
from environs import Env
from django.core.wsgi import get_wsgi_application
from bookwyrm.telemetry import InstallOpenTelemetry
Env.read_env()
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "bookwyrm.settings")
if 'OTEL_EXPORTER_OTLP_ENDPOINT' in os.environ:
InstallOpenTelemetry()
application = get_wsgi_application()

10
celerywyrm/apps.py Normal file
View file

@ -0,0 +1,10 @@
from django.apps import AppConfig
from celerywyrm import settings
class CelerywyrmConfig(AppConfig):
name = 'celerywyrm'
verbose_name = "BookWyrm Celery"
def ready(self):
if settings.OTEL_EXPORTER_OTLP_ENDPOINT:
from bookwyrm.telemetry import open_telemetry
open_telemetry.instrumentCelery()

View file

@ -22,3 +22,4 @@ opentelemetry-api==1.8.0
opentelemetry-sdk==1.8.0
opentelemetry-exporter-otlp-proto-grpc==1.8.0
opentelemetry-instrumentation-django==0.27b0
opentelemetry-instrumentation-celery==0.27b0