Skip to content

Commit

Permalink
add jaeger opentelementry
Browse files Browse the repository at this point in the history
  • Loading branch information
vemonet committed Dec 18, 2023
1 parent 8263d69 commit f9fe44a
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 3 deletions.
45 changes: 45 additions & 0 deletions backend/app/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logging
import os
from fastapi.responses import RedirectResponse
from starlette.middleware.cors import CORSMiddleware
from starlette.middleware.sessions import SessionMiddleware
Expand Down Expand Up @@ -46,3 +48,46 @@ def health_check():
def redirect_root_to_docs():
"""Redirect the route / to /docs"""
return RedirectResponse(url="/docs")


def configure_otel(app):
# open telemetry https://github.com/ranking-agent/aragorn/blob/main/src/otel_config.py#L4
# https://ncatstranslator.github.io/TranslatorTechnicalDocumentation/deployment-guide/monitoring/
# https://github.com/TranslatorSRI/Jaeger-demo
if not os.environ.get('NO_JAEGER'):
logging.info("starting up jaeger telemetry")
import warnings
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
from opentelemetry import trace
from opentelemetry.exporter.jaeger.thrift import JaegerExporter
from opentelemetry.sdk.resources import SERVICE_NAME as telemetery_service_name_key, Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor

service_name = os.environ.get('OTEL_SERVICE_NAME', 'OPENPREDICT')
# httpx connections need to be open a little longer by the otel decorators
# but some libs display warnings of resource being unclosed.
# these supresses such warnings.
logging.captureWarnings(capture=True)
warnings.filterwarnings("ignore",category=ResourceWarning)
trace.set_tracer_provider(
TracerProvider(
resource=Resource.create({telemetery_service_name_key: service_name})
)
)
jaeger_host = os.environ.get('JAEGER_HOST', 'jaeger-otel-agent.sri')
jaeger_port = int(os.environ.get('JAEGER_PORT', '6831'))
jaeger_exporter = JaegerExporter(
agent_host_name=jaeger_host,
agent_port=jaeger_port,
)
trace.get_tracer_provider().add_span_processor(
BatchSpanProcessor(jaeger_exporter)
)
# tracer = trace.get_tracer(__name__)
FastAPIInstrumentor.instrument_app(app, tracer_provider=trace, excluded_urls="docs,openapi.json")
HTTPXClientInstrumentor().instrument()

# Configure open telemetry if enabled
configure_otel(app)
12 changes: 9 additions & 3 deletions backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ dependencies = [
# "kgx >=1.6.0",
"rdflib >=6.1.1",
"SPARQLWrapper >=1.8.5",

"opentelemetry-sdk",
"opentelemetry-exporter-otlp-proto-http",
"opentelemetry-instrumentation-fastapi",
"opentelemetry-instrumentation-httpx",
]

[project.optional-dependencies]
Expand Down Expand Up @@ -121,12 +126,13 @@ itrb-test = "pytest tests/production --server https://collaboratory-api.test.tra
itrb-prod = "pytest tests/production --server https://collaboratory-api.transltr.io {args}"


# OTHER TOOLS
[tool.hatch.build.targets.wheel]
packages = ["app"]

[tool.hatch.metadata]
allow-direct-references = true


# OTHER TOOLS

[tool.coverage.run]
source = ["app"]
branch = true
Expand Down
1 change: 1 addition & 0 deletions docker-compose.override.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ services:
- FRONTEND_URL=http://localhost:19006
- INSTALL_DEV=true
- DEV_MODE=true
- NO_JAEGER=true
build:
context: ./backend
dockerfile: Dockerfile
Expand Down
1 change: 1 addition & 0 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ services:
VIRTUAL_PORT: 80
VIRTUAL_HOST: api.collaboratory.semanticscience.org
LETSENCRYPT_HOST: api.collaboratory.semanticscience.org
NO_JAEGER: "true"
volumes:
- /data/knowledge-collaboratory/ner-models:/data/ner-models
- /data/knowledge-collaboratory/nanopub-keystore:/data/nanopub-keystore
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ services:
INSTALL_DEV: ${INSTALL_DEV-false}
env_file:
- .env
environment:
- NO_JAEGER=true
volumes:
- /data/knowledge-collaboratory/ner-models:/data/ner-models
- /data/knowledge-collaboratory/nanopub-keystore:/data/nanopub-keystore

0 comments on commit f9fe44a

Please sign in to comment.