Merge pull request #2750 from WesleyAC/get-audience-telemetry

Add more detailed telemetry for get_audience
This commit is contained in:
Mouse Reeve 2023-03-29 09:29:05 -07:00 committed by GitHub
commit 8cf7c5c392
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 2 deletions

View file

@ -4,10 +4,15 @@ from django.dispatch import receiver
from django.db import transaction from django.db import transaction
from django.db.models import signals, Q from django.db.models import signals, Q
from django.utils import timezone from django.utils import timezone
from opentelemetry import trace
from bookwyrm import models from bookwyrm import models
from bookwyrm.redis_store import RedisStore, r from bookwyrm.redis_store import RedisStore, r
from bookwyrm.tasks import app, LOW, MEDIUM, HIGH from bookwyrm.tasks import app, LOW, MEDIUM, HIGH
from bookwyrm.telemetry import open_telemetry
tracer = open_telemetry.tracer()
class ActivityStream(RedisStore): class ActivityStream(RedisStore):
@ -136,8 +141,10 @@ class ActivityStream(RedisStore):
) )
return audience.distinct() return audience.distinct()
def get_audience(self, status): # pylint: disable=no-self-use @tracer.start_as_current_span("ActivityStream.get_audience")
def get_audience(self, status):
"""given a status, what users should see it""" """given a status, what users should see it"""
trace.get_current_span().set_attribute("stream_id", self.key)
return [user.id for user in self._get_audience(status)] return [user.id for user in self._get_audience(status)]
def get_stores_for_object(self, obj): def get_stores_for_object(self, obj):
@ -160,7 +167,9 @@ class HomeStream(ActivityStream):
key = "home" key = "home"
@tracer.start_as_current_span("HomeStream.get_audience")
def get_audience(self, status): def get_audience(self, status):
trace.get_current_span().set_attribute("stream_id", self.key)
audience = super()._get_audience(status) audience = super()._get_audience(status)
if not audience: if not audience:
return [] return []

View file

@ -10,7 +10,7 @@ if settings.OTEL_EXPORTER_CONSOLE:
trace.get_tracer_provider().add_span_processor( trace.get_tracer_provider().add_span_processor(
BatchSpanProcessor(ConsoleSpanExporter()) BatchSpanProcessor(ConsoleSpanExporter())
) )
else: elif settings.OTEL_EXPORTER_OTLP_ENDPOINT:
trace.get_tracer_provider().add_span_processor( trace.get_tracer_provider().add_span_processor(
BatchSpanProcessor(OTLPSpanExporter()) BatchSpanProcessor(OTLPSpanExporter())
) )
@ -29,3 +29,7 @@ def instrumentCelery():
@worker_process_init.connect(weak=False) @worker_process_init.connect(weak=False)
def init_celery_tracing(*args, **kwargs): def init_celery_tracing(*args, **kwargs):
CeleryInstrumentor().instrument() CeleryInstrumentor().instrument()
def tracer():
return trace.get_tracer(__name__)