def check_override_not_allowed(self):
     """set_log_emitter_provider should throw a warning when overridden"""
     provider = get_log_emitter_provider()
     with self.assertLogs(level=WARNING) as test:
         set_log_emitter_provider(LogEmitterProvider())
         self.assertEqual(
             test.output,
             [
                 (
                     "WARNING:opentelemetry.sdk._logs:Overriding of current "
                     "LogEmitterProvider is not allowed"
                 )
             ],
         )
     self.assertIs(provider, get_log_emitter_provider())
Beispiel #2
0
def _init_logging(
    exporters: Dict[str, Type[LogExporter]],
    auto_instrumentation_version: Optional[str] = None,
):
    # if env var OTEL_RESOURCE_ATTRIBUTES is given, it will read the service_name
    # from the env variable else defaults to "unknown_service"
    auto_resource = {}
    # populate version if using auto-instrumentation
    if auto_instrumentation_version:
        auto_resource[ResourceAttributes.
                      TELEMETRY_AUTO_VERSION] = auto_instrumentation_version
    provider = LogEmitterProvider(resource=Resource.create(auto_resource))
    set_log_emitter_provider(provider)

    for _, exporter_class in exporters.items():
        exporter_args = {}
        provider.add_log_processor(
            BatchLogProcessor(exporter_class(**exporter_args)))

    log_emitter = provider.get_log_emitter(__name__)
    handler = OTLPHandler(level=logging.NOTSET, log_emitter=log_emitter)

    logging.getLogger().addHandler(handler)
 def test_set_tracer_provider(self):
     reload(_logs)
     provider = LogEmitterProvider()
     set_log_emitter_provider(provider)
     retrieved_provider = get_log_emitter_provider()
     self.assertEqual(provider, retrieved_provider)
Beispiel #4
0
An example showing how to export exception telemetry using the AzureMonitorLogExporter.
"""
import os
import logging

from opentelemetry.sdk._logs import (
    LogEmitterProvider,
    OTLPHandler,
    set_log_emitter_provider,
)
from opentelemetry.sdk._logs.export import BatchLogProcessor

from azure.monitor.opentelemetry.exporter import AzureMonitorLogExporter

log_emitter_provider = LogEmitterProvider()
set_log_emitter_provider(log_emitter_provider)

exporter = AzureMonitorLogExporter.from_connection_string(
    os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"])

log_emitter_provider.add_log_processor(BatchLogProcessor(exporter))
handler = OTLPHandler()

# Attach OTel handler to namespaced logger
logger = logging.getLogger(__name__)
logger.addHandler(handler)
logger.setLevel(logging.NOTSET)

try:
    val = 1 / 0
    print(val)