Skip to content

Commit

Permalink
chore: add OTEL (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
brunopacheco1 authored Nov 6, 2024
1 parent 202273e commit a87d022
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
7 changes: 7 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# SPDX-FileCopyrightText: 2024 PNED G.I.E.
#
# SPDX-License-Identifier: Apache-2.0

OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED=true
OTEL_SDK_DISABLED=true
OTEL_EXPORTER_OTLP_ENDPOINT=http://openobserve-dev-env.ss-dev.lnds.internal:4319
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,5 @@ Temporary Items
__pycache__

*.egg-info

venv
40 changes: 39 additions & 1 deletion ckanext/gdi_userportal/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,34 @@
from ckanext.gdi_userportal.logic.auth.get import config_option_show
from ckanext.gdi_userportal.validation import scheming_isodatetime_flex

from ckan import model
from opentelemetry import metrics
from opentelemetry.exporter.otlp.proto.http.metric_exporter import OTLPMetricExporter
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader
from opentelemetry.sdk.resources import SERVICE_NAME, Resource
from opentelemetry.instrumentation.flask import FlaskInstrumentor

from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor

import logging

log = logging.getLogger(__name__)


def setup_opentelemetry():
resource = Resource(attributes={SERVICE_NAME: "ckan"})

traceProvider = TracerProvider(resource=resource)
processor = BatchSpanProcessor(OTLPSpanExporter())
traceProvider.add_span_processor(processor)
trace.set_tracer_provider(traceProvider)

reader = PeriodicExportingMetricReader(OTLPMetricExporter())
provider = MeterProvider(resource=resource, metric_readers=[reader])
metrics.set_meter_provider(provider)


class GdiUserPortalPlugin(plugins.SingletonPlugin):
Expand All @@ -23,6 +50,8 @@ class GdiUserPortalPlugin(plugins.SingletonPlugin):
plugins.implements(plugins.IActions)
plugins.implements(plugins.IPackageController)
plugins.implements(plugins.IValidators)
plugins.implements(plugins.IMiddleware, inherit=True)
plugins.implements(plugins.IConfigurable, inherit=True)

_dcatap_fields_to_normalize = [
"access_rights",
Expand Down Expand Up @@ -147,3 +176,12 @@ def update_facet_titles(self, facet_titles):

def get_validators(self):
return {"scheming_isodatetime_flex": scheming_isodatetime_flex}

# IConfigurable
def configure(self, config):
setup_opentelemetry()

# IMiddleware
def make_middleware(self, app, config):
FlaskInstrumentor().instrument_app(app)
return app
11 changes: 8 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
#
# SPDX-License-Identifier: Apache-2.0

# CKAN requires 2.9.0, we are slightly less picky
python-dateutil >= 2.8.1
pytz
python-dateutil==2.9.0.post0
pytz==2024.2
opentelemetry-api==1.27.0
opentelemetry-sdk==1.27.0
opentelemetry-instrumentation==0.48b0
opentelemetry-instrumentation-flask==0.48b0
opentelemetry-exporter-otlp-proto-http==1.27.0
ckantoolkit==0.0.7

0 comments on commit a87d022

Please sign in to comment.