diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c60346c..e9fc96be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,8 @@ ([#237](https://github.com/microsoft/ApplicationInsights-Python/pull/237)) - Add ability to specify custom metric readers ([#241](https://github.com/microsoft/ApplicationInsights-Python/pull/241)) +- Defaulting logging env var for auto-instrumentation. Added logging samples. + ([#240](https://github.com/microsoft/ApplicationInsights-Python/pull/240)) ## [1.0.0b8](https://github.com/microsoft/ApplicationInsights-Python/releases/tag/v1.0.0b8) - 2022-09-26 diff --git a/azure-monitor-opentelemetry-distro/README.md b/azure-monitor-opentelemetry-distro/README.md index 6ba715bf..c7b27076 100644 --- a/azure-monitor-opentelemetry-distro/README.md +++ b/azure-monitor-opentelemetry-distro/README.md @@ -49,7 +49,7 @@ You can use `configure_azure_monitor` to set up instrumentation for your app to * disable_logging - If set to `True`, disables collection and export of logging telemetry. * disable_metrics - If set to `True`, disables collection and export of metric telemetry. * disable_tracing - If set to `True`, disables collection and export of distributed tracing telemetry. -* logging_level - Specifies the [logging level][logging_level] of the Opentelemetry Logging Handler. Ex: logging.WARNING. +* logging_level - Specifies the [logging level][logging_level] of the Opentelemetry Logging Handler. Defaults to logging.NOTSET. * logger_name = Specifies the [logger name][logger_name_hierarchy_doc] under which all logging will be instrumented. Defaults to "" which corresponds to the root logger. * logging_export_interval_millis - Specifies the logging export interval in milliseconds. Defaults to 5000. * metric_readers - Specifies the [metric readers][ot_metric_reader] that you would like to use for your metrics pipeline. Accepts a list of [metric readers][ot_sdk_python_metric_reader]. diff --git a/azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/distro.py b/azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/distro.py index 7be1a631..e084c41f 100644 --- a/azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/distro.py +++ b/azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/distro.py @@ -18,6 +18,9 @@ OTEL_TRACES_EXPORTER, ) from opentelemetry.instrumentation.distro import BaseDistro +from opentelemetry.sdk.environment_variables import ( + _OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED, +) _logger = logging.getLogger(__name__) _opentelemetry_logger = logging.getLogger("opentelemetry") @@ -59,6 +62,9 @@ def _configure_auto_instrumentation() -> None: environ.setdefault( OTEL_LOGS_EXPORTER, "azure_monitor_opentelemetry_exporter" ) + environ.setdefault( + _OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED, "true" + ) AzureStatusLogger.log_status(True) _logger.info( "Azure Monitor OpenTelemetry Distro configured successfully." diff --git a/azure-monitor-opentelemetry-distro/samples/logging/correlated_logs.py b/azure-monitor-opentelemetry-distro/samples/logging/correlated_logs.py new file mode 100644 index 00000000..acd943bc --- /dev/null +++ b/azure-monitor-opentelemetry-distro/samples/logging/correlated_logs.py @@ -0,0 +1,30 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- + +from logging import WARNING, getLogger + +from azure.monitor.opentelemetry.distro import configure_azure_monitor +from opentelemetry import trace + +configure_azure_monitor( + connection_string="", + service_name="foo_service", + logging_level=WARNING, + disable_metrics=True, + disable_tracing=True, +) + +logger = getLogger(__name__) +tracer = trace.get_tracer(__name__) + +logger.info("Uncorrelated info log") +logger.warning("Uncorrelated warning log") +logger.error("Uncorrelated error log") + +with tracer.start_as_current_span("Span for correlated logs"): + logger.info("Correlated info log") + logger.warning("Correlated warning log") + logger.error("Correlated error log") diff --git a/azure-monitor-opentelemetry-distro/samples/logging/custom_properties.py b/azure-monitor-opentelemetry-distro/samples/logging/custom_properties.py new file mode 100644 index 00000000..4e91b212 --- /dev/null +++ b/azure-monitor-opentelemetry-distro/samples/logging/custom_properties.py @@ -0,0 +1,24 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- + +from logging import DEBUG, getLogger + +from azure.monitor.opentelemetry.distro import configure_azure_monitor + +configure_azure_monitor( + connection_string="", + service_name="foo_service", + logger_name=__name__, + logging_level=DEBUG, + disable_metrics=True, + disable_tracing=True, +) + +logger = getLogger(__name__) +logger.setLevel(DEBUG) + +# Pass custom properties in a dictionary with the extra argument +logger.debug("DEBUG: Debug with properties", extra={"debug": "true"}) diff --git a/azure-monitor-opentelemetry-distro/samples/logging/exception_logs.py b/azure-monitor-opentelemetry-distro/samples/logging/exception_logs.py new file mode 100644 index 00000000..d562fdf1 --- /dev/null +++ b/azure-monitor-opentelemetry-distro/samples/logging/exception_logs.py @@ -0,0 +1,33 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- + +from logging import WARNING, getLogger + +from azure.monitor.opentelemetry.distro import configure_azure_monitor + +configure_azure_monitor( + connection_string="", + service_name="foo_service", + logging_level=WARNING, + disable_metrics=True, + disable_tracing=True, +) + +logger = getLogger(__name__) + +# The following code will generate two pieces of exception telemetry +# that are identical in nature +try: + val = 1 / 0 + print(val) +except ZeroDivisionError: + logger.exception("Error: Division by zero") + +try: + val = 1 / 0 + print(val) +except ZeroDivisionError: + logger.error("Error: Division by zero", stack_info=True, exc_info=True) diff --git a/azure-monitor-opentelemetry-distro/samples/logging/logs_with_traces.py b/azure-monitor-opentelemetry-distro/samples/logging/logs_with_traces.py new file mode 100644 index 00000000..04575ddb --- /dev/null +++ b/azure-monitor-opentelemetry-distro/samples/logging/logs_with_traces.py @@ -0,0 +1,53 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- + +from logging import WARNING, getLogger + +import flask +from azure.monitor.opentelemetry.distro import configure_azure_monitor +from opentelemetry import trace + +configure_azure_monitor( + connection_string="", + service_name="flask_service_name", + logging_level=WARNING, + disable_metrics=True, + instrumentations=["flask"], + tracing_export_interval_millis=15000, +) + +logger = getLogger(__name__) + +app = flask.Flask(__name__) + + +@app.route("/info_log") +def info_log(): + message = "Correlated info log" + logger.info(message) + return message + + +@app.route("/error_log") +def error_log(): + message = "Correlated error log" + logger.error(message) + return message + + +@app.route("/error_log") +def error_log(): + message = "Correlated error log" + logger.error(message) + return message + + +if __name__ == "__main__": + app.run(host="localhost", port=8080) + + logger.info("Correlated info log") + logger.warning("Correlated warning log") + logger.error("Correlated error log") diff --git a/azure-monitor-opentelemetry-distro/samples/logging/simple.py b/azure-monitor-opentelemetry-distro/samples/logging/simple.py index 07ca5a7a..d3d92bc5 100644 --- a/azure-monitor-opentelemetry-distro/samples/logging/simple.py +++ b/azure-monitor-opentelemetry-distro/samples/logging/simple.py @@ -4,28 +4,20 @@ # license information. # -------------------------------------------------------------------------- -from logging import WARN, getLogger +from logging import WARNING, getLogger from azure.monitor.opentelemetry.distro import configure_azure_monitor -from opentelemetry import trace configure_azure_monitor( connection_string="", service_name="foo_service", - logging_level=WARN, + logging_level=WARNING, disable_metrics=True, disable_tracing=True, ) logger = getLogger(__name__) -tracer = trace.get_tracer(__name__) logger.info("info log") logger.warning("warning log") logger.error("error log") - -with tracer.start_as_current_span("hello"): - print("Hello, World!") - logger.info("Correlated info log") - logger.warning("Correlated warning log") - logger.error("Correlated error log")