Skip to content

Commit

Permalink
add django, and modernize flask with content
Browse files Browse the repository at this point in the history
  • Loading branch information
saartochner-lumigo committed Sep 11, 2023
1 parent 8db625d commit 0cd1db8
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 17 deletions.
18 changes: 14 additions & 4 deletions src/lumigo_opentelemetry/instrumentations/django/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

from lumigo_opentelemetry.instrumentations import AbstractInstrumentor
from lumigo_opentelemetry.libs.general_utils import lumigo_safe_execute
from lumigo_opentelemetry.instrumentations.instrumentation_utils import add_body_attribute
from lumigo_opentelemetry.instrumentations.instrumentation_utils import (
add_body_attribute,
)
from lumigo_opentelemetry.libs.json_utils import dump_with_context


Expand All @@ -19,15 +21,23 @@ def install_instrumentation(self) -> None:

def request_hook(span: Span, request: HttpRequest):
with lumigo_safe_execute("django request_hook"):
span.set_attribute("http.request.headers", dump_with_context("requestHeaders", request.headers))
span.set_attribute(
"http.request.headers",
dump_with_context("requestHeaders", request.headers),
)
add_body_attribute(span, request.body, "http.request.body")

def response_hook(span: Span, request: HttpRequest, response: HttpResponse):
with lumigo_safe_execute("django response_hook"):
span.set_attribute("http.response.headers", dump_with_context("responseHeaders", response.headers))
span.set_attribute(
"http.response.headers",
dump_with_context("responseHeaders", response.headers),
)
add_body_attribute(span, response.content, "http.response.body")

DjangoInstrumentor().instrument(request_hook=request_hook, response_hook=response_hook)
DjangoInstrumentor().instrument(
request_hook=request_hook, response_hook=response_hook
)


instrumentor: AbstractInstrumentor = DjangoInstrumentorWrapper()
16 changes: 12 additions & 4 deletions src/lumigo_opentelemetry/instrumentations/flask/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,21 @@ def install_instrumentation(self) -> None:

def request_hook(span: Span, flask_request_environ: Dict[str, Any]):
with lumigo_safe_execute("flask_request_hook"):
span.set_attribute("http.request.headers", dump_with_context("requestHeaders", flask_request_environ))
span.set_attribute(
"http.request.headers",
dump_with_context("requestHeaders", flask_request_environ),
)

def response_hook(span: Span, status, response_headers):
with lumigo_safe_execute("flask_response_hook"):
span.set_attribute("http.response.headers", dump_with_context("responseHeaders", response_headers))

FlaskInstrumentor().instrument(request_hook=request_hook, response_hook=response_hook)
span.set_attribute(
"http.response.headers",
dump_with_context("responseHeaders", response_headers),
)

FlaskInstrumentor().instrument(
request_hook=request_hook, response_hook=response_hook
)


instrumentor: AbstractInstrumentor = FlaskInstrumentorWrapper()
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"An error occurred while applying the '%s' instrumentation: %s",
instrumentor.instrumentation_id,
str(e),
exc_info=True
exc_info=True,
)

logger.debug(
Expand Down
6 changes: 3 additions & 3 deletions src/test/integration/django/app/manage.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
"""Run administrative tasks."""
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.settings")
import lumigo_opentelemetry
import lumigo_opentelemetry # noqa: F401

try:
from django.core.management import execute_from_command_line
except ImportError as exc:
Expand All @@ -16,7 +16,7 @@ def main():
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(['manage.py', 'runserver', '5003'])
execute_from_command_line(["manage.py", "runserver", "5003"])


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion src/test/integration/django/scripts/start_django
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh

pushd app || exit 1
cd app || exit 1
python manage.py runserver 5003 &
9 changes: 5 additions & 4 deletions src/test/integration/django/tests/test_django.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
import unittest
from test.test_utils.span_exporter import wait_for_exporter
from test.test_utils.spans_parser import SpansContainer
Expand All @@ -25,9 +24,11 @@ def test_200_OK(self):
self.assertIsNotNone(root)
self.assertEqual(root["kind"], "SpanKind.SERVER")
self.assertEqual(root["attributes"]["http.status_code"], 200)
self.assertEqual(root["attributes"]["http.request.body"], '{"support": "django"}')
self.assertEqual(
root["attributes"]["http.request.body"], '{"support": "django"}'
)
self.assertIsNotNone(root["attributes"]["http.request.headers"])
self.assertIsNotNone(root["attributes"]['http.response.headers'])
self.assertIsNotNone(root["attributes"]['http.response.body'])
self.assertIsNotNone(root["attributes"]["http.response.headers"])
self.assertIsNotNone(root["attributes"]["http.response.body"])
self.assertEqual(root["attributes"]["http.method"], "GET")
self.assertEqual(root["attributes"]["http.host"], "localhost:5003")

0 comments on commit 0cd1db8

Please sign in to comment.