Skip to content

Commit

Permalink
Move tracing init etc. into dedicated file to follow DRY patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
proffalken committed Apr 27, 2024
1 parent 1b15492 commit d1d29cc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 49 deletions.
50 changes: 1 addition & 49 deletions memberportal/membermatters/asgi.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,6 @@
import os

## Tracing
from opentelemetry import trace, context, propagate
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.instrumentation.asgi import OpenTelemetryMiddleware

## Logging
from opentelemetry._logs import set_logger_provider
from opentelemetry.exporter.otlp.proto.http._log_exporter import OTLPLogExporter
from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler
from opentelemetry.sdk._logs.export import BatchLogRecordProcessor

## Databases
from opentelemetry.instrumentation.psycopg2 import Psycopg2Instrumentor

## DJANGO
from opentelemetry.instrumentation.django import DjangoInstrumentor
from otel import trace, OpenTelemetryMiddleware

# INTERNAL URLS
from django.conf.urls import url
Expand All @@ -32,36 +14,6 @@
from channels.routing import ProtocolTypeRouter, URLRouter
from membermatters.websocket_urls import urlpatterns

### OTEL EXPORTER SETUP ###

resource = Resource(
attributes={
"service.name": "member_portal_backend",
"deployment.environment": "production",
"service.namespace": "membermatters",
}
)

#### TRACES
trace.set_tracer_provider(TracerProvider(resource=resource))
tracer = trace.get_tracer(__name__)

otlp_trace_exporter = OTLPSpanExporter(endpoint="http://localhost:4318/v1/traces")

span_processor = BatchSpanProcessor(otlp_trace_exporter)

trace.get_tracer_provider().add_span_processor(span_processor)

#### LOGS
logger_provider = LoggerProvider(resource=resource)
otlp_logs_exporter = OTLPLogExporter(endpoint="http://localhost:4318/v1/logs")
logger_provider.add_log_record_processor(BatchLogRecordProcessor(otlp_logs_exporter))

#### Database
Psycopg2Instrumentor().instrument(enable_commenter=True, commenter_options={})

#### Django
DjangoInstrumentor().instrument(is_sql_commentor_enabled=True)

application = ProtocolTypeRouter(
{
Expand Down
40 changes: 40 additions & 0 deletions memberportal/otel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import os

## Tracing
from opentelemetry import trace, context, propagate
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.instrumentation.asgi import OpenTelemetryMiddleware

## Logging
from opentelemetry._logs import set_logger_provider
from opentelemetry.exporter.otlp.proto.http._log_exporter import OTLPLogExporter
from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler
from opentelemetry.sdk._logs.export import BatchLogRecordProcessor

### OTEL EXPORTER SETUP ###

resource = Resource(
attributes={
"service.name": os.environ.get("MM_OTEL_SVC_NAME", "member_portal_backend"),
"deployment.environment": os.environ.get("MM_ENV", "development"),
"service.namespace": "membermatters",
}
)

#### TRACES
trace.set_tracer_provider(TracerProvider(resource=resource))
tracer = trace.get_tracer(__name__)

otlp_trace_exporter = OTLPSpanExporter(endpoint="http://localhost:4318/v1/traces")

span_processor = BatchSpanProcessor(otlp_trace_exporter)

trace.get_tracer_provider().add_span_processor(span_processor)

#### LOGS
logger_provider = LoggerProvider(resource=resource)
otlp_logs_exporter = OTLPLogExporter(endpoint="http://localhost:4318/v1/logs")
logger_provider.add_log_record_processor(BatchLogRecordProcessor(otlp_logs_exporter))

0 comments on commit d1d29cc

Please sign in to comment.