Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Otel instrumentation #121

Merged
merged 11 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,16 @@ Helm charts can be found at https://github.com/helxplatform/translator-devops/he
```bash
curl -X POST "http://localhost:6434/lookup?string=oxycod&offset=0&limit=10" -H "accept: application/json"
```

## Configuration

NameRes can be configured by setting environmental variables:

* `SOLR_HOST` and `SOLR_PORT`: Hostname and port for the Solr database containing NameRes information.
* `SERVER_NAME`: The name of this server (defaults to `infores:sri-name-resolver`)
* `SERVER_ROOT`: The server root (defaults to `/`)
* `MATURITY_VALUE`: How mature is this NameRes (defaults to `maturity`, e.g. `development`)
* `LOCATION_VALUE`: Where is this NameRes setup (defaults to `location`, e.g. `RENCI`)
* `OTEL_ENABLED`: Turn on Open TELemetry (default: `'false'`) -- only `'true'` will turn this on.
* `JAEGER_HOST` and `JAEGER_PORT`: Hostname and port for the Jaegar instance to provide telemetry to.
* `JAEGER_SERVICE_NAME`: The name of this service (defaults to the value of `SERVER_NAME`)
38 changes: 37 additions & 1 deletion api/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Matching names are returned first, followed by non-matching names
"""
import json
import logging
import logging, warnings
import os
import re
from typing import Dict, List, Union, Annotated
Expand Down Expand Up @@ -275,3 +275,39 @@ async def lookup(string: str,

# Override open api schema with custom schema
app.openapi_schema = construct_open_api_schema(app)

# Set up opentelemetry if enabled.
if os.environ.get('OTEL_ENABLED', 'false') == 'true':
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
from opentelemetry import trace
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
# from opentelemetry.sdk.trace.export import ConsoleSpanExporter

from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor

# 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)
plater_service_name = os.environ.get('SERVER_NAME', 'infores:sri-name-resolver')
assert plater_service_name and isinstance(plater_service_name, str)

jaeger_exporter = JaegerExporter(
agent_host_name=os.environ.get("JAEGER_HOST", "localhost"),
agent_port=int(os.environ.get("JAEGER_PORT", "6831")),
)
resource = Resource(attributes={
SERVICE_NAME: os.environ.get("JAEGER_SERVICE_NAME", plater_service_name),
})
provider = TracerProvider(resource=resource)
# processor = BatchSpanProcessor(ConsoleSpanExporter())
processor = BatchSpanProcessor(jaeger_exporter)
provider.add_span_processor(processor)
trace.set_tracer_provider(provider)
FastAPIInstrumentor.instrument_app(app, tracer_provider=provider, excluded_urls=
"docs,openapi.json")
HTTPXClientInstrumentor().instrument()
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ jsonlines

# For testing
pytest
opentelemetry-sdk==1.21.0
opentelemetry-exporter-jaeger==1.21.0
opentelemetry-instrumentation-fastapi==0.42b0
opentelemetry-instrumentation-httpx==0.42b0
Loading