Skip to content

Commit

Permalink
fix opentelemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
vemonet committed Jan 10, 2024
1 parent 9286167 commit da8214f
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 44 deletions.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ dynamic = ["version"]

dependencies = [
"requests >=2.23.0",
"trapi-predict-kit >=0.2.2",
# "trapi-predict-kit @ git+https://github.com/MaastrichtU-IDS/trapi-predict-kit.git",
# "trapi-predict-kit[opentelemetry] >=0.2.3",
"trapi-predict-kit @ git+https://github.com/MaastrichtU-IDS/trapi-predict-kit.git",
# "trapi-predict-kit @ {root:uri}/trapi-predict-kit",

"pydantic >=1.9",
Expand Down
43 changes: 1 addition & 42 deletions src/trapi/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
],
info=openapi_info,
itrb_url_prefix=itrb_url_prefix,
opentelemetry=True,
dev_server_url="https://openpredict.semanticscience.org",
title='OpenPredict API',
version='1.0.0',
Expand Down Expand Up @@ -111,45 +112,3 @@

app.include_router(openpredict_api)
app.include_router(drkg_model_api)


# Configure 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.requests import RequestsInstrumentor
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor

service_name = os.environ.get('OTEL_SERVICE_NAME', itrb_url_prefix)
# 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")
RequestsInstrumentor().instrument()
HTTPXClientInstrumentor().instrument()

0 comments on commit da8214f

Please sign in to comment.