From 5612026506a16274400ce2422369181739306d78 Mon Sep 17 00:00:00 2001 From: Lukas Garberg Date: Sun, 26 May 2024 21:18:31 +0200 Subject: [PATCH] nipapd: Reset Host header in proxy, improve logs Fix the tracing proxy function by removing the Host header from the headers received from tracing client before POST:ing it to the tracing receiver. Improve logging of how the setup of tracing is progressing. --- nipap/nipap/tracing.py | 20 +++++++++++++++++--- nipap/nipapd | 9 +++++---- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/nipap/nipap/tracing.py b/nipap/nipap/tracing.py index 510a773b4..1b4b2bde0 100644 --- a/nipap/nipap/tracing.py +++ b/nipap/nipap/tracing.py @@ -2,7 +2,7 @@ import inspect from functools import wraps - from flask import request + from flask import request, abort from opentelemetry import trace, context from opentelemetry.trace import SpanKind, StatusCode @@ -34,9 +34,23 @@ def init_tracing(service_name, endpoint, sampler, use_grpc=True): def setup(app, endpoint): + """ Set up an endpoint which proxies traces to the OpenTelemetry receiver. + + This can be used by for example the NIPAP CLI. + """ @app.route('/v1/traces/', defaults={'path': ''}, methods=["POST"]) def proxy(path): - return post(f'{endpoint}{path}', data=request.data, headers=request.headers).content + + # Remove Host-header as it's set to the host running nipapd. + headers = {k: v for k, v in request.headers} + headers.pop("Host", None) + res = post(f'{endpoint}{path}', data=request.data, headers=headers) + + # Check result and abort with error if not successful + if res.status_code >= 400: + abort(res.status_code, description=res.text) + + return res.content def create_span(f): @@ -159,4 +173,4 @@ def create_span_authenticate(f): @wraps(f) def decorated(*args, **kwargs): return f(*args, **kwargs) - return decorated \ No newline at end of file + return decorated diff --git a/nipap/nipapd b/nipap/nipapd index 991743dc4..d381ea0d6 100755 --- a/nipap/nipapd +++ b/nipap/nipapd @@ -257,13 +257,14 @@ if __name__ == '__main__': try: setup(app, cfg.get("tracing", "otlp_http_endpoint")) except configparser.NoOptionError: + logger.info('Found no OTLP HTTP endpoint, OTLP proxy disabled') pass - logger.debug('Tracing is enabled') + logger.debug('Tracing is enabled and successfully set up') except KeyError: - logger.error('Error in tracing configuration. No tracing enabled') + logger.error('Error in tracing configuration, tracing not enabled') pass - except ImportError: - logger.error('Failed to import tracing libraries. Check dependencies. No tracing enabled') + except ImportError as err: + logger.error('Failed to import tracing library %s, tracing not enabled', err.name) pass else: logger.debug('Tracing is disabled')