Skip to content

Commit

Permalink
Initial instrumentation for the opentelemetry support
Browse files Browse the repository at this point in the history
  • Loading branch information
jschaff committed Dec 3, 2024
1 parent 562033e commit 4698e84
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
2 changes: 2 additions & 0 deletions biothings_annotator/application/cli/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from biothings_annotator.application.views import build_routes
from biothings_annotator.application.middleware import build_middleware
from biothings_annotator.application.exceptions import build_exception_handers
from biothings_annotator.application.telemetry import instrument_application_telemetry

logging.basicConfig()
logger = logging.getLogger("sanic-application")
Expand Down Expand Up @@ -94,4 +95,5 @@ class Sanic(
logger.error("Unable to add exception handler %s", exception_handler)
raise gen_exc

instrument_application_telemetry(application)
return application
6 changes: 6 additions & 0 deletions biothings_annotator/application/configuration/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
"REQUEST_MAX_SIZE": 100000000,
"CACHE_MAX_AGE": 604800
},
"opentelemetry": {
"OPENTELEMETRY_ENABLED": false,
"OPENTELEMETRY_SERVICE_NAME": "biothings-annotator",
"OPENTELEMETRY_JAEGER_HOST": "http://localhost",
"OPENTELEMETRY_JAEGER_PORT": 6381
},
"extension": {
"openapi": {
"OAS": true,
Expand Down
40 changes: 40 additions & 0 deletions biothings_annotator/application/telemetry/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
Integrates opentelemetry tracing with the biothings-annotator
web service
"""

import opentelemetry
from opentelemetry.exporter.jaeger.thrift import JaegerExporter
from opentelemetry.sdk.resources import SERVICE_NAME, Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
import sanic
from tracing import instrument_app


def instrument_application_telemetry(application_instance: sanic.Sanic):
"""
Method integrating the opentelemetry into the annotator
web service
"""
opentelemetry_configuration = application_instance.config.get("opentelemetry", {})
opentelemetry_enabled = opentelemetry_configuration.get("OPENTELEMETRY_ENABLED", False)

if opentelemetry_enabled:
opentelemetry_jaeger_host = opentelemetry_configuration["OPENTELEMETRY_JAEGER_HOST"]
opentelemetry_jaeger_port = opentelemetry_configuration["OPENTELEMETRY_JAEGER_PORT"]
opentelemetry_service_name = opentelemetry_configuration["OPENTELEMETRY_SERVICE_NAME"]

jaeger_trace_exporter = JaegerExporter(
agent_host_name=opentelemetry_jaeger_host,
agent_port=opentelemetry_jaeger_port,
udp_split_oversized_batches=True,
)
service_resource = Resource.create({SERVICE_NAME: opentelemetry_service_name})

trace_provider = TracerProvider(resource=service_resource)
batch_span_processor = BatchSpanProcessor(jaeger_trace_exporter)
trace_provider.add_span_processor(batch_span_processor)
opentelemetry.trace.set_tracer_provider(trace_provider)
tracer = opentelemetry.trace.get_tracer("biothings-annotator")
instrument_app(application_instance, tracer)
8 changes: 7 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ tests = [
"pytest-asyncio >= 0.23.8",
"sanic-testing >= 24.6.0"
]

telemetry = [
"opentelemetry-api >= 1.28.2",
"opentelemetry-sdk >= 1.28.2",
"opentelemetry-exporter-otlp >= 1.28.2",
"opentelemetry.exporter.jaeger.thrift >= 1.21.0",
"opentelemetry-instrumentation >= 0.49b2"
]

[project.urls]
"Homepage" = "https://github.com/biothings/biothings_annotator"
Expand Down

0 comments on commit 4698e84

Please sign in to comment.