|
14 | 14 | # See the License for the specific language governing permissions and |
15 | 15 | # limitations under the License. |
16 | 16 |
|
| 17 | +import logging |
17 | 18 | import os |
18 | 19 | from logging import getLogger |
19 | 20 |
|
| 21 | +from opentelemetry._logs import set_logger_provider |
| 22 | +from opentelemetry._events import set_event_logger_provider |
20 | 23 | from opentelemetry.environment_variables import ( |
21 | 24 | OTEL_METRICS_EXPORTER, |
22 | 25 | OTEL_TRACES_EXPORTER, |
|
27 | 30 | _DEFAULT_CONFIG as SYSTEM_METRICS_DEFAULT_CONFIG, |
28 | 31 | SystemMetricsInstrumentor, |
29 | 32 | ) |
30 | | -from opentelemetry.sdk._configuration import _OTelSDKConfigurator |
| 33 | +from opentelemetry.semconv.resource import ResourceAttributes |
| 34 | +from opentelemetry.sdk._configuration import ( |
| 35 | + _OTelSDKConfigurator, |
| 36 | + _import_exporters, |
| 37 | + _get_exporter_names, |
| 38 | + _get_sampler, |
| 39 | + _import_sampler, |
| 40 | + _get_id_generator, |
| 41 | + _import_id_generator, |
| 42 | + _init_tracing, |
| 43 | + _init_metrics, |
| 44 | +) |
| 45 | +from opentelemetry.sdk._events import EventLoggerProvider |
| 46 | +from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler |
| 47 | +from opentelemetry.sdk._logs.export import BatchLogRecordProcessor |
31 | 48 | from opentelemetry.sdk.environment_variables import ( |
32 | 49 | OTEL_EXPERIMENTAL_RESOURCE_DETECTORS, |
33 | 50 | OTEL_EXPORTER_OTLP_PROTOCOL, |
| 51 | + _OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED, |
34 | 52 | ) |
| 53 | +from opentelemetry.sdk.resources import Resource |
35 | 54 | from pkg_resources import EntryPoint |
36 | 55 |
|
37 | 56 | from elasticotel.distro.environment_variables import ELASTIC_OTEL_SYSTEM_METRICS_ENABLED |
|
41 | 60 |
|
42 | 61 |
|
43 | 62 | class ElasticOpenTelemetryConfigurator(_OTelSDKConfigurator): |
44 | | - pass |
| 63 | + def _configure(self, **kwargs): |
| 64 | + """This is overriden to enable the log machinery (and thus log events) without |
| 65 | + attaching the OTel handler to the Python logging module. |
| 66 | +
|
| 67 | + This code is a simplified version of _initialize_components plus the changes |
| 68 | + required to have log events enabled out of the box""" |
| 69 | + span_exporters, metric_exporters, log_exporters = _import_exporters( |
| 70 | + _get_exporter_names("traces"), |
| 71 | + _get_exporter_names("metrics"), |
| 72 | + _get_exporter_names("logs"), |
| 73 | + ) |
| 74 | + sampler_name = _get_sampler() |
| 75 | + sampler = _import_sampler(sampler_name) |
| 76 | + id_generator_name = _get_id_generator() |
| 77 | + id_generator = _import_id_generator(id_generator_name) |
| 78 | + |
| 79 | + resource_attributes = {} |
| 80 | + # populate version if using auto-instrumentation |
| 81 | + auto_instrumentation_version = kwargs.get("auto_instrumentation_version") |
| 82 | + if auto_instrumentation_version: |
| 83 | + resource_attributes[ResourceAttributes.TELEMETRY_AUTO_VERSION] = auto_instrumentation_version |
| 84 | + # if env var OTEL_RESOURCE_ATTRIBUTES is given, it will read the service_name |
| 85 | + # from the env variable else defaults to "unknown_service" |
| 86 | + resource = Resource.create(resource_attributes) |
| 87 | + |
| 88 | + _init_tracing( |
| 89 | + exporters=span_exporters, |
| 90 | + id_generator=id_generator, |
| 91 | + sampler=sampler, |
| 92 | + resource=resource, |
| 93 | + ) |
| 94 | + _init_metrics(metric_exporters, resource) |
| 95 | + |
| 96 | + # from here we change the semantics of _OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED from |
| 97 | + # disable the logs to do not setup the logging handler so we can use log events |
| 98 | + logger_provider = LoggerProvider(resource=resource) |
| 99 | + set_logger_provider(logger_provider) |
| 100 | + |
| 101 | + for _, exporter_class in log_exporters.items(): |
| 102 | + exporter_args = {} |
| 103 | + logger_provider.add_log_record_processor(BatchLogRecordProcessor(exporter_class(**exporter_args))) |
| 104 | + |
| 105 | + setup_logging_handler = ( |
| 106 | + os.getenv(_OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED, "false").strip().lower() == "true" |
| 107 | + ) |
| 108 | + if setup_logging_handler: |
| 109 | + handler = LoggingHandler(level=logging.NOTSET, logger_provider=logger_provider) |
| 110 | + logging.getLogger().addHandler(handler) |
| 111 | + |
| 112 | + # now setup the event logger |
| 113 | + event_logger_provider = EventLoggerProvider(logger_provider=logger_provider) |
| 114 | + set_event_logger_provider(event_logger_provider) |
45 | 115 |
|
46 | 116 |
|
47 | 117 | class ElasticOpenTelemetryDistro(BaseDistro): |
|
0 commit comments