Skip to content

Commit

Permalink
fix logging, add additional warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
seemk committed Oct 30, 2023
1 parent aca9fa0 commit f52bb88
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 26 deletions.
3 changes: 3 additions & 0 deletions splunk_otel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
"""

from .defaults import _set_otel_defaults
from .util import _init_logger

_init_logger("splunk_otel")

_set_otel_defaults()

Expand Down
5 changes: 3 additions & 2 deletions splunk_otel/distro.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
import os
from typing import Any, Dict

Expand All @@ -23,9 +24,9 @@
from splunk_otel.profiling import _start_profiling
from splunk_otel.profiling.options import _Options as ProfilingOptions
from splunk_otel.tracing import _configure_tracing
from splunk_otel.util import _get_logger, _is_truthy
from splunk_otel.util import _is_truthy

logger = _get_logger(__name__)
logger = logging.getLogger(__name__)


class _SplunkDistro(BaseDistro):
Expand Down
5 changes: 3 additions & 2 deletions splunk_otel/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
import os

from opentelemetry.exporter.otlp.proto.grpc.metric_exporter import OTLPMetricExporter
Expand All @@ -20,9 +21,9 @@
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader

from splunk_otel.util import _get_logger, _is_truthy
from splunk_otel.util import _is_truthy

logger = _get_logger(__name__)
logger = logging.getLogger(__name__)


def start_metrics() -> MeterProvider:
Expand Down
45 changes: 31 additions & 14 deletions splunk_otel/profiling/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import base64
import gzip
import logging
import os
import sys
import threading
Expand All @@ -36,13 +37,19 @@
import splunk_otel
from splunk_otel.profiling import profile_pb2
from splunk_otel.profiling.options import _Options
from splunk_otel.util import _get_logger
from splunk_otel.version import __version__

logger = _get_logger(__name__)
logger = logging.getLogger(__name__)

thread_states = {}
batch_processor = None

class Profiler:
def __init__(self):
self.running = False
self.thread_states = {}
self.batch_processor = None


profiler = Profiler()


class StringTable:
Expand Down Expand Up @@ -128,7 +135,7 @@ def get_location(frame):

labels = [timestamp_label, event_period_label, thread_id_label]

trace_context = thread_states.get(thread_id)
trace_context = profiler.thread_states.get(thread_id)
if trace_context:
(trace_id, span_id) = trace_context

Expand Down Expand Up @@ -167,8 +174,7 @@ def _profiler_loop(options: _Options):

exporter = OTLPLogExporter(options.endpoint)
# pylint: disable-next=global-statement
global batch_processor
batch_processor = BatchLogRecordProcessor(exporter)
profiler.batch_processor = BatchLogRecordProcessor(exporter)

while True:
profiling_stacktraces = []
Expand Down Expand Up @@ -208,7 +214,7 @@ def _profiler_loop(options: _Options):
),
instrumentation_scope=InstrumentationScope("otel.profiling", "0.1.0"),
)
batch_processor.emit(log_data)
profiler.batch_processor.emit(log_data)
time.sleep(interval / 1e3)


Expand All @@ -222,7 +228,10 @@ def _wrapped_context_attach(wrapped, _instance, args, kwargs):

if span:
thread_id = threading.get_ident()
thread_states[thread_id] = (span.context.trace_id, span.context.span_id)
profiler.thread_states[thread_id] = (
span.context.trace_id,
span.context.span_id,
)

return token

Expand All @@ -237,20 +246,28 @@ def _wrapped_context_detach(wrapped, _instance, args, kwargs):
span = prev.get(_SPAN_KEY)

if span:
thread_states[thread_id] = (span.context.trace_id, span.context.span_id)
profiler.thread_states[thread_id] = (
span.context.trace_id,
span.context.span_id,
)
else:
thread_states[thread_id] = None
profiler.thread_states[thread_id] = None
else:
thread_states[thread_id] = None
profiler.thread_states[thread_id] = None
return wrapped(*args, **kwargs)


def _force_flush():
if batch_processor:
batch_processor.force_flush()
if profiler.batch_processor:
profiler.batch_processor.force_flush()


def _start_profiling(options):
if profiler.running:
logger.warning("profiler already running")
return

profiler.running = True
logger.debug(
"starting profiling call_stack_interval=%s endpoint=%s",
options.call_stack_interval,
Expand Down
29 changes: 26 additions & 3 deletions splunk_otel/profiling/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,34 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
from os import environ
from typing import Optional

from opentelemetry.sdk.resources import Resource

logger = logging.getLogger(__name__)
_DEFAULT_CALL_STACK_INTERVAL = 1_000


def _sanitize_interval(interval):
if isinstance(interval, int):
if interval < 1:
logger.warning(
"call stack interval has to be positive, got %s, defaulting to %s",
interval,
_DEFAULT_CALL_STACK_INTERVAL,
)
return _DEFAULT_CALL_STACK_INTERVAL

return interval

logger.warning(
"call stack interval not an integer, defaulting to %s",
_DEFAULT_CALL_STACK_INTERVAL,
)
return _DEFAULT_CALL_STACK_INTERVAL


class _Options:
resource: Resource
Expand Down Expand Up @@ -49,8 +72,8 @@ def _get_call_stack_interval(interval: Optional[int]) -> int:
interval = environ.get("SPLUNK_PROFILER_CALL_STACK_INTERVAL")

if interval:
return int(interval)
return _sanitize_interval(int(interval))

return 1_000
return _DEFAULT_CALL_STACK_INTERVAL

return interval
return _sanitize_interval(interval)
5 changes: 3 additions & 2 deletions splunk_otel/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
import os
import sys
from typing import Collection, Dict, Optional, Union
Expand All @@ -26,9 +27,9 @@
from pkg_resources import iter_entry_points

from splunk_otel.options import _Options, _SpanExporterFactory
from splunk_otel.util import _get_logger, _is_truthy
from splunk_otel.util import _is_truthy

logger = _get_logger(__name__)
logger = logging.getLogger(__name__)


def start_tracing(
Expand Down
4 changes: 1 addition & 3 deletions splunk_otel/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ def _get_log_level(level):
return levels[level.lower()]


def _get_logger(name):
def _init_logger(name):
level = _get_log_level(os.environ.get("OTEL_LOG_LEVEL", "info"))
logger = logging.getLogger(name)
logger.setLevel(level)
logger.addHandler(logging.StreamHandler())

return logger

0 comments on commit f52bb88

Please sign in to comment.