Skip to content

Commit

Permalink
feat(tracing) - allow opting-out from tracing (#891)
Browse files Browse the repository at this point in the history
  • Loading branch information
harelmo-lumigo authored Sep 23, 2024
1 parent db5e3c6 commit ceb1309
Show file tree
Hide file tree
Showing 18 changed files with 54 additions and 32 deletions.
26 changes: 18 additions & 8 deletions src/lumigo_opentelemetry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ def init() -> Dict[str, Any]:
lumigo_report_dependencies = (
os.getenv("LUMIGO_REPORT_DEPENDENCIES", "true").lower() == "true"
)
logging_enabled = os.getenv("LUMIGO_ENABLE_LOGS", "").lower() == "true"
logging_enabled = os.getenv("LUMIGO_ENABLE_LOGS", "false").lower() == "true"
tracing_enabled = os.getenv("LUMIGO_ENABLE_TRACES", "true").lower() == "true"
spandump_file = os.getenv("LUMIGO_DEBUG_SPANDUMP")
logdump_file = os.getenv("LUMIGO_DEBUG_LOGDUMP")

Expand Down Expand Up @@ -167,14 +168,19 @@ def init() -> Dict[str, Any]:
logger_provider.add_log_record_processor(LumigoLogRecordProcessor())

if lumigo_token:
tracer_provider.add_span_processor(
LumigoSpanProcessor(
OTLPSpanExporter(
endpoint=lumigo_traces_endpoint,
headers={"Authorization": f"LumigoToken {lumigo_token}"},
),
if tracing_enabled:
tracer_provider.add_span_processor(
LumigoSpanProcessor(
OTLPSpanExporter(
endpoint=lumigo_traces_endpoint,
headers={"Authorization": f"LumigoToken {lumigo_token}"},
),
)
)
else:
logger.info(
'Tracing is disabled (the "LUMIGO_ENABLE_TRACES" environment variable is not set to "true"): no traces will be sent to Lumigo.'
)
)

if logging_enabled:
logger_provider.add_log_record_processor(
Expand All @@ -185,6 +191,10 @@ def init() -> Dict[str, Any]:
)
)
)
else:
logger.info(
'Logging is disabled (the "LUMIGO_ENABLE_LOGS" environment variable is not set to "true"): no logs will be sent to Lumigo.'
)

if lumigo_report_dependencies:
from lumigo_opentelemetry.dependencies import report
Expand Down
21 changes: 18 additions & 3 deletions src/lumigo_opentelemetry/instrumentations/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from abc import abstractmethod, ABC
import os


class AbstractInstrumentor(ABC):
Expand All @@ -12,10 +13,24 @@ class AbstractInstrumentor(ABC):
def __init__(self, instrumentation_id: str):
self._instrumentation_id = instrumentation_id

def is_applicable(self) -> bool:
tracing_enabled = (
os.environ.get("LUMIGO_ENABLE_TRACES", "true").lower() == "true"
)
if not tracing_enabled:
return False

try:
self.assert_instrumented_package_importable()
return True
except ImportError:
return False

@abstractmethod
def check_if_applicable(self) -> None:
# TODO Implement version lookup per instrumented package, and check that the version is supported
raise Exception("'check_if_applicable' method not implemented!")
def assert_instrumented_package_importable(self) -> None:
raise Exception(
"'assert_instrumented_package_importable' method not implemented!"
)

@abstractmethod
def install_instrumentation(self) -> None:
Expand Down
2 changes: 1 addition & 1 deletion src/lumigo_opentelemetry/instrumentations/boto/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class BotoInstrumentorWrapper(AbstractInstrumentor):
def __init__(self) -> None:
super().__init__("boto")

def check_if_applicable(self) -> None:
def assert_instrumented_package_importable(self) -> None:
import boto # noqa

def install_instrumentation(self) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class BotoCoreInstrumentorWrapper(AbstractInstrumentor):
def __init__(self) -> None:
super().__init__("botocore")

def check_if_applicable(self) -> None:
def assert_instrumented_package_importable(self) -> None:
from botocore.client import BaseClient # noqa
from botocore.endpoint import Endpoint # noqa
from botocore.exceptions import ClientError # noqa
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class DjangoInstrumentorWrapper(AbstractInstrumentor):
def __init__(self) -> None:
super().__init__("django")

def check_if_applicable(self) -> None:
def assert_instrumented_package_importable(self) -> None:
import django # noqa

def install_instrumentation(self) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class FastApiInstrumentorWrapper(AbstractInstrumentor):
def __init__(self) -> None:
super().__init__("fast-api")

def check_if_applicable(self) -> None:
def assert_instrumented_package_importable(self) -> None:
import fastapi # noqa

def install_instrumentation(self) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class FlaskInstrumentorWrapper(AbstractInstrumentor):
def __init__(self) -> None:
super().__init__("flask")

def check_if_applicable(self) -> None:
def assert_instrumented_package_importable(self) -> None:
import flask # noqa

def install_instrumentation(self) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class GRPCInstrumentor(AbstractInstrumentor):
def __init__(self) -> None:
super().__init__("grpc")

def check_if_applicable(self) -> None:
def assert_instrumented_package_importable(self) -> None:
import grpc # noqa

@staticmethod
Expand Down
10 changes: 3 additions & 7 deletions src/lumigo_opentelemetry/instrumentations/instrumentations.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,8 @@
redis_instrumentor,
requests_instrumentor,
]
for instrumentor in instrumentors:
try:
instrumentor.check_if_applicable()
except ImportError:
continue

applicable_instumentors = [inst for inst in instrumentors if inst.is_applicable()]
for instrumentor in applicable_instumentors:
try:
instrumentor.install_instrumentation()
installed_instrumentations.append(instrumentor.instrumentation_id)
Expand All @@ -56,7 +52,7 @@
)

logger.debug(
"Installed instrumentations: %s", ", ".join(list(installed_instrumentations))
"Installed instrumentations: [%s]", ", ".join(list(installed_instrumentations))
)

frameworks = list(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class KafkaPythonInstrumentor(AbstractInstrumentor):
def __init__(self) -> None:
super().__init__("kafka_python")

def check_if_applicable(self) -> None:
def assert_instrumented_package_importable(self) -> None:
import kafka # noqa

def install_instrumentation(self) -> None:
Expand Down
2 changes: 1 addition & 1 deletion src/lumigo_opentelemetry/instrumentations/pika/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class PikaInstrumentor(AbstractInstrumentor):
def __init__(self) -> None:
super().__init__("pika")

def check_if_applicable(self) -> None:
def assert_instrumented_package_importable(self) -> None:
import pika # noqa

def install_instrumentation(self) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class PsycopgInstrumentor(AbstractInstrumentor):
def __init__(self) -> None:
super().__init__("psycopg")

def check_if_applicable(self) -> None:
def assert_instrumented_package_importable(self) -> None:
import psycopg # noqa

def install_instrumentation(self) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Psycopg2Instrumentor(AbstractInstrumentor):
def __init__(self) -> None:
super().__init__("psycopg2")

def check_if_applicable(self) -> None:
def assert_instrumented_package_importable(self) -> None:
import psycopg2 # noqa

def install_instrumentation(self) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class PymongoInstrumentor(AbstractInstrumentor):
def __init__(self) -> None:
super().__init__("pymongo")

def check_if_applicable(self) -> None:
def assert_instrumented_package_importable(self) -> None:
import pymongo # noqa

def install_instrumentation(self) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class PyMySqlInstrumentor(AbstractInstrumentor):
def __init__(self) -> None:
super().__init__("pymysql")

def check_if_applicable(self) -> None:
def assert_instrumented_package_importable(self) -> None:
import pymysql # noqa

def install_instrumentation(self) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class RedisInstrumentor(AbstractInstrumentor):
def __init__(self) -> None:
super().__init__("redis")

def check_if_applicable(self) -> None:
def assert_instrumented_package_importable(self) -> None:
import redis # noqa

def install_instrumentation(self) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class RequestsInstrumentor(AbstractInstrumentor):
def __init__(self) -> None:
super().__init__("requests")

def check_if_applicable(self) -> None:
def assert_instrumented_package_importable(self) -> None:
from requests.models import Response # noqa
from requests.sessions import Session # noqa
from requests.structures import CaseInsensitiveDict # noqa
Expand Down
1 change: 1 addition & 0 deletions src/test/integration/motor/requirements_others.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pytest==7.1.1
psutil==5.9.1
testcontainers==3.7.0
pymongo==4.4.0 # make sure the version used in the test is compatible with motor

0 comments on commit ceb1309

Please sign in to comment.