Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion azure-monitor-opentelemetry-distro/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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."
Expand Down
Original file line number Diff line number Diff line change
@@ -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="<your-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")
Original file line number Diff line number Diff line change
@@ -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="<your-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"})
Original file line number Diff line number Diff line change
@@ -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="<your-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)
Original file line number Diff line number Diff line change
@@ -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="<your-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")
12 changes: 2 additions & 10 deletions azure-monitor-opentelemetry-distro/samples/logging/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="<your-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")