diff --git a/docs/config-www.rst b/docs/config-www.rst index 163d7728e..34c414a98 100644 --- a/docs/config-www.rst +++ b/docs/config-www.rst @@ -44,7 +44,7 @@ from the built-in web server, run the following:: Using the default configuration, the web UI should now be reachable on port 5000. By default, the NIPAP web UI will look for nipap.conf in /etc/nipap. The -path can be changed setting the environment variable NIPPA_CONFIG_PATH as +path can be changed setting the environment variable NIPAP_CONFIG_PATH as such:: export NIPAP_CONFIG_PATH=~/.local/etc/nipap/nipap.conf diff --git a/nipap-www/nipapwww/__init__.py b/nipap-www/nipapwww/__init__.py index e7c569fa0..4e22db72e 100644 --- a/nipap-www/nipapwww/__init__.py +++ b/nipap-www/nipapwww/__init__.py @@ -12,7 +12,7 @@ def create_app(test_config=None): # dependencies installed. Relevant during initial package build. import os from flask import Flask, redirect, url_for - from nipap.nipapconfig import NipapConfig + from nipap.nipapconfig import NipapConfig, NipapConfigError import pynipap # create and configure the app @@ -43,7 +43,14 @@ def create_app(test_config=None): try: import nipap.tracing from opentelemetry.instrumentation.wsgi import OpenTelemetryMiddleware - nipap.tracing.init_tracing("nipap-www", nipap_config.get("tracing", "otlp_grpc_endpoint")) + + if nipap_config.has_option("tracing", "otlp_grpc_endpoint"): + nipap.tracing.init_tracing("nipap-www", nipap_config.get("tracing", "otlp_grpc_endpoint"), True) + elif nipap_config.has_option("tracing", "otlp_http_endpoint"): + nipap.tracing.init_tracing("nipap-www", nipap_config.get("tracing", "otlp_http_endpoint")) + else: + raise NipapConfigError("Tracing enabled but no OTLP endpoint configured") + app.wsgi_app = OpenTelemetryMiddleware(app.wsgi_app) except KeyError: pass diff --git a/nipap/nipap.conf.dist b/nipap/nipap.conf.dist index 358b13b33..d527103eb 100644 --- a/nipap/nipap.conf.dist +++ b/nipap/nipap.conf.dist @@ -206,5 +206,5 @@ secret_key = {{WWW_SECRET_KEY}} # [tracing] # Specify OTLP GRPC endpoint. Used to send traces to OpenTelemetry Collector # otlp_grpc_endpoint=http://opentelemetry-collector:4317 -# Specify OTLP HTTP endpoint. Used when proxying traces to OpenTelemetry-Collector from nipap-cli +# Specify OTLP HTTP endpoint. Used to send traces to OpenTelemetry Collector if GRPC endpoint is not configured but also used when proxying traces to OpenTelemetry-Collector from nipap-cli # otlp_http_endpoint=http://opentelemetry-collector:4318/v1/traces diff --git a/nipap/nipap/tracing.py b/nipap/nipap/tracing.py index 96b738f63..dd46ecabe 100644 --- a/nipap/nipap/tracing.py +++ b/nipap/nipap/tracing.py @@ -10,17 +10,21 @@ from opentelemetry.sdk.resources import SERVICE_NAME, Resource from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchSpanProcessor + import opentelemetry.exporter.otlp.proto.http.trace_exporter from requests import post tracer = trace.get_tracer("nipap") - def init_tracing(service_name, endpoint): + def init_tracing(service_name, endpoint, use_grpc=False): resource = Resource(attributes={ SERVICE_NAME: service_name }) provider = TracerProvider(resource=resource) - processor = BatchSpanProcessor(OTLPSpanExporter(endpoint=endpoint)) + if use_grpc: + processor = BatchSpanProcessor(OTLPSpanExporter(endpoint=endpoint)) + else: + processor = BatchSpanProcessor(opentelemetry.exporter.otlp.proto.http.trace_exporter.OTLPSpanExporter(endpoint=endpoint)) provider.add_span_processor(processor) trace.set_tracer_provider(provider) diff --git a/nipap/nipapd b/nipap/nipapd index 96db55537..3a8448f7b 100755 --- a/nipap/nipapd +++ b/nipap/nipapd @@ -233,15 +233,20 @@ if __name__ == '__main__': from nipap.tracing import init_tracing, setup from opentelemetry.instrumentation.psycopg2 import Psycopg2Instrumentor from opentelemetry.instrumentation.flask import FlaskInstrumentor - otlp_grpc_endpoint = cfg.get("tracing", "otlp_grpc_endpoint") - init_tracing("nipapd", otlp_grpc_endpoint) + + if cfg.has_option("tracing", "otlp_grpc_endpoint"): + init_tracing("nipapd", cfg.get("tracing", "otlp_grpc_endpoint"), True) + elif cfg.has_option("tracing", "otlp_http_endpoint"): + init_tracing("nipapd", cfg.get("tracing", "otlp_http_endpoint")) + else: + raise NipapConfigError("Tracing enabled but no OTLP endpoint configured") + FlaskInstrumentor.instrument_app(app, excluded_urls="/v1/traces") Psycopg2Instrumentor().instrument(enable_commenter=True, commenter_options={}) # Configure proxy of traces from nipap-cli to collector try: - otlp_http_endpoint = cfg.get("tracing", "otlp_http_endpoint") - setup(app, otlp_http_endpoint) + setup(app, cfg.get("tracing", "otlp_http_endpoint")) except configparser.NoOptionError: pass logger.debug('Tracing is enabled') diff --git a/pynipap/tracing.py b/pynipap/tracing.py index 0a779efaf..69ae5e458 100644 --- a/pynipap/tracing.py +++ b/pynipap/tracing.py @@ -22,7 +22,7 @@ tracer = trace.get_tracer("pynipap") - def init_tracing(service_name, endpoint, use_grpc=True): + def init_tracing(service_name, endpoint, use_grpc=False): resource = Resource(attributes={ SERVICE_NAME: service_name })