Skip to content

Commit 0d1efdb

Browse files
Add OTel configuration
1 parent 1cf58f9 commit 0d1efdb

File tree

4 files changed

+105
-7
lines changed

4 files changed

+105
-7
lines changed

Dockerfile.server

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ COPY examples/ ./examples/
1414
COPY chat-ui/ ./chat-ui/
1515
COPY scripts/provider-list.yaml ./scripts/
1616
COPY scripts/filter_guardrails.py ./scripts/
17+
COPY scripts/setup_otel.py ./scripts/
1718
COPY scripts/entrypoint.sh ./scripts/
1819
RUN chmod +x ./scripts/entrypoint.sh
1920

@@ -24,7 +25,11 @@ ENV POETRY_VIRTUALENVS_IN_PROJECT=1 \
2425
POETRY_NO_INTERACTION=1
2526

2627
RUN poetry install --no-ansi --extras="sdd jailbreak openai nvidia tracing" && \
27-
poetry run pip install "spacy>=3.4.4,<4.0.0" && \
28+
poetry run pip install \
29+
"spacy>=3.4.4,<4.0.0" \
30+
"opentelemetry-api>=1.27.0,<2.0.0" \
31+
"opentelemetry-sdk>=1.27.0,<2.0.0" \
32+
"opentelemetry-exporter-otlp>=1.27.0,<2.0.0" && \
2833
poetry run python -m spacy download en_core_web_lg
2934

3035
FROM registry.access.redhat.com/ubi9/python-312

poetry.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/entrypoint.sh

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,52 @@ if [[ ! -f "$CONFIG_DIR/rails.co" ]]; then
1919
exit 1
2020
fi
2121

22+
# Setup OpenTelemetry
23+
ENABLE_OTEL="${ENABLE_OTEL:-${AUTO_SETUP_OTEL:-false}}"
24+
25+
if [[ "${ENABLE_OTEL,,}" == "true" ]]; then
26+
echo "🔧 Configuring OpenTelemetry:"
27+
export OTEL_SERVICE_NAME="${OTEL_SERVICE_NAME:-guardrails}"
28+
export OTEL_SERVICE_VERSION="${OTEL_SERVICE_VERSION:-1.0.0}"
29+
export OTEL_ENVIRONMENT="${OTEL_ENVIRONMENT:-production}"
30+
export OTEL_ENABLE_CONSOLE="${OTEL_ENABLE_CONSOLE:-false}"
31+
export OTEL_EXPORTER_OTLP_INSECURE="${OTEL_EXPORTER_OTLP_INSECURE:-true}"
32+
export OTEL_EXPORTER_OTLP_ENDPOINT="${OTEL_EXPORTER_OTLP_ENDPOINT:-http://jaeger:4317}"
33+
34+
else
35+
echo "📝 OpenTelemetry disabled - starting without tracing"
36+
fi
37+
38+
echo ""
2239
echo "✅ Configuration validated. Starting server..."
23-
exec /app/.venv/bin/nemoguardrails server \
24-
--config "/app/config" \
25-
--port "$PORT" \
26-
--default-config-id "$CONFIG_ID" \
27-
--disable-chat-ui
40+
41+
exec /app/.venv/bin/python3 -c "
42+
import os
43+
import sys
44+
45+
# Setup OpenTelemetry if enabled
46+
if os.getenv('ENABLE_OTEL', '').lower() == 'true':
47+
print('🔍 Setting up OpenTelemetry within server process...')
48+
try:
49+
sys.path.insert(0, '/app/scripts')
50+
from setup_otel import setup_opentelemetry
51+
setup_opentelemetry()
52+
print('✅ OpenTelemetry configured in server process')
53+
except Exception as e:
54+
print(f'❌ OpenTelemetry setup failed: {e}')
55+
import traceback
56+
traceback.print_exc()
57+
sys.exit(1)
58+
59+
# Start the NeMo Guardrails server
60+
import uvicorn
61+
from nemoguardrails.server import api
62+
63+
# Set up the server configuration
64+
api.app.rails_config_path = '/app/config'
65+
api.app.disable_chat_ui = True
66+
api.set_default_config_id('${CONFIG_ID}')
67+
68+
# Start the server using uvicorn
69+
uvicorn.run(api.app, port=${PORT}, log_level='info', host='0.0.0.0')
70+
"

scripts/setup_otel.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import os
2+
3+
from opentelemetry import trace
4+
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
5+
from opentelemetry.sdk.resources import Resource
6+
from opentelemetry.sdk.trace import TracerProvider
7+
from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter
8+
9+
10+
def setup_opentelemetry():
11+
"""Configure OpenTelemetry SDK with OTLP and Console exporters."""
12+
# Get configuration from environment variables
13+
service_name = os.getenv("OTEL_SERVICE_NAME", "guardrails")
14+
service_version = os.getenv("OTEL_SERVICE_VERSION", "1.0.0")
15+
environment = os.getenv("OTEL_ENVIRONMENT", "production")
16+
otlp_endpoint = os.getenv("OTEL_EXPORTER_OTLP_ENDPOINT")
17+
enable_console = os.getenv("OTEL_ENABLE_CONSOLE", "true").lower() == "true"
18+
19+
# Configure resource (metadata about the service)
20+
resource = Resource.create(
21+
{
22+
"service.name": service_name,
23+
"service.version": service_version,
24+
"deployment.environment": environment,
25+
"service.namespace": "nemo-guardrails",
26+
}
27+
)
28+
29+
# Set up the tracer provider
30+
tracer_provider = TracerProvider(resource=resource)
31+
trace.set_tracer_provider(tracer_provider)
32+
33+
exporters_configured = []
34+
35+
# Configure OTLP exporter if endpoint is provided
36+
if otlp_endpoint:
37+
otlp_exporter = OTLPSpanExporter(
38+
endpoint=otlp_endpoint,
39+
insecure=os.getenv("OTEL_EXPORTER_OTLP_INSECURE", "true").lower() == "true",
40+
)
41+
42+
tracer_provider.add_span_processor(BatchSpanProcessor(otlp_exporter))
43+
exporters_configured.append("OTLP")
44+
45+
# Configure console exporter for debugging
46+
if enable_console:
47+
print("📝 Enabling console exporter for debugging")
48+
console_exporter = ConsoleSpanExporter()
49+
tracer_provider.add_span_processor(BatchSpanProcessor(console_exporter))
50+
exporters_configured.append("Console")

0 commit comments

Comments
 (0)