Skip to content

Commit

Permalink
Merge pull request #121 from TranslatorSRI/otel-instrumentation
Browse files Browse the repository at this point in the history
otel stuff in place, helm changes required are just passing the following env vars:
 - SERVER_NAME (defaults to it's infores)
 - JAEGER_HOST
 - JAEGER_PORT 
 - OTEL_ENABLED (set to false by default) 

Closes #105.
  • Loading branch information
gaurav authored Dec 13, 2023
2 parents 53d2f3e + 4e6e8ef commit a61f3f5
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
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 @@ -358,3 +358,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

0 comments on commit a61f3f5

Please sign in to comment.