Skip to content

Commit

Permalink
Add fully support to export traces with http
Browse files Browse the repository at this point in the history
  • Loading branch information
erikoqvist committed Jan 22, 2024
1 parent 7e4a1bd commit a5264ca
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 11 deletions.
2 changes: 1 addition & 1 deletion docs/config-www.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions nipap-www/nipapwww/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"))
elif nipap_config.has_option("tracing", "otlp_http_endpoint"):
nipap.tracing.init_tracing("nipap-www", nipap_config.get("tracing", "otlp_http_endpoint"), False)
else:
raise NipapConfigError("Tracing enabled but no OTLP endpoint configured")

app.wsgi_app = OpenTelemetryMiddleware(app.wsgi_app)
except KeyError:
pass
Expand Down
4 changes: 2 additions & 2 deletions nipap/nipap.conf.dist
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ secret_key = {{WWW_SECRET_KEY}}
#welcome_message = Welcome to NIPAP at COMPANY. Please login using your XYZ credentials.
# Enable OpenTelemetry tracing by uncommenting section.
# [tracing]
# Specify OTLP GRPC endpoint. Used to send traces to OpenTelemetry Collector
# Specify OTLP GRPC endpoint. Used to send traces to OpenTelemetry Collector. Has precedence over OTLP HTTP endpoint.
# 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 but also used when proxying traces to OpenTelemetry-Collector from nipap-cli
# otlp_http_endpoint=http://opentelemetry-collector:4318/v1/traces
8 changes: 6 additions & 2 deletions nipap/nipap/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=True):
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)

Expand Down
13 changes: 9 additions & 4 deletions nipap/nipapd
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
elif cfg.has_option("tracing", "otlp_http_endpoint"):
init_tracing("nipapd", cfg.get("tracing", "otlp_http_endpoint"), False)
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')
Expand Down

0 comments on commit a5264ca

Please sign in to comment.