Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove psycopg2-binary from dev_requirements.txt #38103

Merged
merged 10 commits into from
Oct 29, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
)
from opentelemetry.sdk._configuration import _OTelSDKConfigurator

from azure.monitor.opentelemetry.exporter import ApplicationInsightsSampler # pylint: disable=import-error,no-name-in-module
from azure.monitor.opentelemetry.exporter._utils import _is_attach_enabled # pylint: disable=import-error,no-name-in-module
from azure.monitor.opentelemetry.exporter import ( # pylint: disable=import-error,no-name-in-module
ApplicationInsightsSampler,
)
from azure.monitor.opentelemetry.exporter._utils import ( # pylint: disable=import-error,no-name-in-module
_is_attach_enabled,
)
from azure.monitor.opentelemetry._constants import (
_PREVIEW_ENTRY_POINT_WARNING,
LOG_EXPORTER_NAMES_ARG,
Expand Down Expand Up @@ -54,8 +58,7 @@ def _configure(self, **kwargs):
super()._configure(**kwargs)
AzureStatusLogger.log_status(True)
AzureDiagnosticLogging.info(
"Azure Monitor Configurator configured successfully.",
_ATTACH_SUCCESS_CONFIGURATOR
"Azure Monitor Configurator configured successfully.", _ATTACH_SUCCESS_CONFIGURATOR
)
except Exception as e:
AzureDiagnosticLogging.error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from os import environ
from warnings import warn

from opentelemetry.instrumentation.distro import ( # type: ignore
from opentelemetry.instrumentation.distro import ( # type: ignore
BaseDistro,
)
from opentelemetry.sdk.environment_variables import (
Expand All @@ -16,7 +16,9 @@

from azure.core.settings import settings
from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan
from azure.monitor.opentelemetry.exporter._utils import _is_attach_enabled # pylint: disable=import-error,no-name-in-module
from azure.monitor.opentelemetry.exporter._utils import ( # pylint: disable=import-error,no-name-in-module
_is_attach_enabled,
)
from azure.monitor.opentelemetry._constants import (
_AZURE_APP_SERVICE_RESOURCE_DETECTOR_NAME,
_AZURE_SDK_INSTRUMENTATION_NAME,
Expand All @@ -43,8 +45,7 @@ def _configure(self, **kwargs) -> None:
_configure_auto_instrumentation()
AzureStatusLogger.log_status(True)
AzureDiagnosticLogging.info(
"Azure Monitor OpenTelemetry Distro configured successfully.",
_ATTACH_SUCCESS_DISTRO
"Azure Monitor OpenTelemetry Distro configured successfully.", _ATTACH_SUCCESS_DISTRO
)
except Exception as e:
AzureStatusLogger.log_status(False, reason=str(e))
Expand All @@ -56,12 +57,8 @@ def _configure(self, **kwargs) -> None:


def _configure_auto_instrumentation() -> None:
environ.setdefault(
_OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED, "true"
)
environ.setdefault(
OTEL_EXPERIMENTAL_RESOURCE_DETECTORS, _AZURE_APP_SERVICE_RESOURCE_DETECTOR_NAME
)
environ.setdefault(_OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED, "true")
environ.setdefault(OTEL_EXPERIMENTAL_RESOURCE_DETECTORS, _AZURE_APP_SERVICE_RESOURCE_DETECTOR_NAME)
otel_disabled_instrumentations = _get_otel_disabled_instrumentations()
if _AZURE_SDK_INSTRUMENTATION_NAME not in otel_disabled_instrumentations:
settings.tracing_implementation = OpenTelemetrySpan
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from opentelemetry.instrumentation.dependencies import (
get_dist_dependency_conflicts,
)
from opentelemetry.instrumentation.instrumentor import ( # type: ignore
from opentelemetry.instrumentation.instrumentor import ( # type: ignore
BaseInstrumentor,
)
from opentelemetry.metrics import set_meter_provider
Expand Down Expand Up @@ -41,7 +41,9 @@
VIEWS_ARG,
)
from azure.monitor.opentelemetry._types import ConfigurationValue
from azure.monitor.opentelemetry.exporter._quickpulse import enable_live_metrics # pylint: disable=import-error,no-name-in-module
from azure.monitor.opentelemetry.exporter._quickpulse import ( # pylint: disable=import-error,no-name-in-module
enable_live_metrics,
)
from azure.monitor.opentelemetry.exporter._quickpulse._processor import ( # pylint: disable=import-error,no-name-in-module
_QuickpulseLogRecordProcessor,
_QuickpulseSpanProcessor,
Expand All @@ -52,7 +54,9 @@
AzureMonitorMetricExporter,
AzureMonitorTraceExporter,
)
from azure.monitor.opentelemetry.exporter._utils import _is_attach_enabled # pylint: disable=import-error,no-name-in-module
from azure.monitor.opentelemetry.exporter._utils import ( # pylint: disable=import-error,no-name-in-module
_is_attach_enabled,
)
from azure.monitor.opentelemetry._diagnostics.diagnostic_logging import (
_DISTRO_DETECTS_ATTACH,
AzureDiagnosticLogging,
Expand Down Expand Up @@ -127,14 +131,13 @@ def configure_azure_monitor(**kwargs) -> None: # pylint: disable=C4758


def _setup_tracing(configurations: Dict[str, ConfigurationValue]):
resource: Resource = configurations[RESOURCE_ARG] # type: ignore
resource: Resource = configurations[RESOURCE_ARG] # type: ignore
sampling_ratio = configurations[SAMPLING_RATIO_ARG]
tracer_provider = TracerProvider(
sampler=ApplicationInsightsSampler(sampling_ratio=cast(float, sampling_ratio)),
resource=resource
sampler=ApplicationInsightsSampler(sampling_ratio=cast(float, sampling_ratio)), resource=resource
)
for span_processor in configurations[SPAN_PROCESSORS_ARG]: # type: ignore
tracer_provider.add_span_processor(span_processor) # type: ignore
for span_processor in configurations[SPAN_PROCESSORS_ARG]: # type: ignore
tracer_provider.add_span_processor(span_processor) # type: ignore
if configurations.get(ENABLE_LIVE_METRICS_ARG):
qsp = _QuickpulseSpanProcessor()
tracer_provider.add_span_processor(qsp)
Expand All @@ -149,7 +152,7 @@ def _setup_tracing(configurations: Dict[str, ConfigurationValue]):


def _setup_logging(configurations: Dict[str, ConfigurationValue]):
resource: Resource = configurations[RESOURCE_ARG] # type: ignore
resource: Resource = configurations[RESOURCE_ARG] # type: ignore
logger_provider = LoggerProvider(resource=resource)
if configurations.get(ENABLE_LIVE_METRICS_ARG):
qlp = _QuickpulseLogRecordProcessor()
Expand All @@ -161,13 +164,13 @@ def _setup_logging(configurations: Dict[str, ConfigurationValue]):
logger_provider.add_log_record_processor(log_record_processor)
set_logger_provider(logger_provider)
handler = LoggingHandler(logger_provider=logger_provider)
logger_name: str = configurations[LOGGER_NAME_ARG] # type: ignore
logger_name: str = configurations[LOGGER_NAME_ARG] # type: ignore
getLogger(logger_name).addHandler(handler)


def _setup_metrics(configurations: Dict[str, ConfigurationValue]):
resource: Resource = configurations[RESOURCE_ARG] # type: ignore
views: List[View] = configurations[VIEWS_ARG] # type: ignore
resource: Resource = configurations[RESOURCE_ARG] # type: ignore
views: List[View] = configurations[VIEWS_ARG] # type: ignore
metric_exporter = AzureMonitorMetricExporter(**configurations)
reader = PeriodicExportingMetricReader(metric_exporter)
meter_provider = MeterProvider(
Expand All @@ -184,20 +187,16 @@ def _setup_live_metrics(configurations):

def _setup_instrumentations(configurations: Dict[str, ConfigurationValue]):
# use pkg_resources for now until https://github.com/open-telemetry/opentelemetry-python/pull/3168 is merged
for entry_point in iter_entry_points(
"opentelemetry_instrumentor"
):
for entry_point in iter_entry_points("opentelemetry_instrumentor"):
lib_name = entry_point.name
if lib_name not in _ALL_SUPPORTED_INSTRUMENTED_LIBRARIES:
continue
if not _is_instrumentation_enabled(configurations, lib_name):
_logger.debug(
"Instrumentation skipped for library %s", entry_point.name
)
_logger.debug("Instrumentation skipped for library %s", entry_point.name)
continue
try:
# Check if dependent libraries/version are installed
conflict = get_dist_dependency_conflicts(entry_point.dist) # type: ignore
conflict = get_dist_dependency_conflicts(entry_point.dist) # type: ignore
if conflict:
_logger.debug(
"Skipping instrumentation %s: %s",
Expand All @@ -222,4 +221,5 @@ def _send_attach_warning():
AzureDiagnosticLogging.warning(
"Distro detected that automatic attach may have occurred. Check your data to ensure "
"that telemetry is not being duplicated. This may impact your cost.",
_DISTRO_DETECTS_ATTACH)
_DISTRO_DETECTS_ATTACH,
)
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
_DIAGNOSTIC_LOGGER_FILE_NAME = "applicationinsights-extension.log"
_SITE_NAME = _env_var_or_default("WEBSITE_SITE_NAME")
_SUBSCRIPTION_ID_ENV_VAR = _env_var_or_default("WEBSITE_OWNER_NAME")
_SUBSCRIPTION_ID = (
_SUBSCRIPTION_ID_ENV_VAR.split("+")[0] if _SUBSCRIPTION_ID_ENV_VAR else None
)
_SUBSCRIPTION_ID = _SUBSCRIPTION_ID_ENV_VAR.split("+")[0] if _SUBSCRIPTION_ID_ENV_VAR else None
_logger = logging.getLogger(__name__)
_logger.propagate = False
_logger.setLevel(logging.INFO)
Expand Down Expand Up @@ -65,29 +63,23 @@ def _initialize(cls):
)
if not exists(_DIAGNOSTIC_LOG_PATH):
makedirs(_DIAGNOSTIC_LOG_PATH)
f_handler = logging.FileHandler(
join(
_DIAGNOSTIC_LOG_PATH, _DIAGNOSTIC_LOGGER_FILE_NAME
)
)
formatter = logging.Formatter(
fmt=log_format, datefmt="%Y-%m-%dT%H:%M:%S"
)
f_handler = logging.FileHandler(join(_DIAGNOSTIC_LOG_PATH, _DIAGNOSTIC_LOGGER_FILE_NAME))
formatter = logging.Formatter(fmt=log_format, datefmt="%Y-%m-%dT%H:%M:%S")
f_handler.setFormatter(formatter)
_logger.addHandler(f_handler)
AzureDiagnosticLogging._initialized = True

@classmethod
def info(cls, message: str, message_id: str):
AzureDiagnosticLogging._initialize()
_logger.info(message, extra={'msgId': message_id})
_logger.info(message, extra={"msgId": message_id})

@classmethod
def warning(cls, message: str, message_id: str):
AzureDiagnosticLogging._initialize()
_logger.warning(message, extra={'msgId': message_id})
_logger.warning(message, extra={"msgId": message_id})

@classmethod
def error(cls, message: str, message_id: str):
AzureDiagnosticLogging._initialize()
_logger.error(message, extra={'msgId': message_id})
_logger.error(message, extra={"msgId": message_id})
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@
def _get_status_logger_file_name(pid):
return f"status_{_MACHINE_NAME}_{pid}.json"


class AzureStatusLogger:
@classmethod
def _get_status_json(
cls, agent_initialized_successfully, pid, reason=None, sdk_present=None
):
def _get_status_json(cls, agent_initialized_successfully, pid, reason=None, sdk_present=None):
status_json = {
"AgentInitializedSuccessfully": agent_initialized_successfully,
"AppType": "python",
Expand All @@ -48,18 +47,12 @@ def _get_status_json(
def log_status(cls, agent_initialized_successfully, reason=None, sdk_present=None):
if _IS_DIAGNOSTICS_ENABLED and _STATUS_LOG_PATH:
pid = getpid()
status_json = AzureStatusLogger._get_status_json(
agent_initialized_successfully, pid, reason, sdk_present
)
status_json = AzureStatusLogger._get_status_json(agent_initialized_successfully, pid, reason, sdk_present)
if not exists(_STATUS_LOG_PATH):
makedirs(_STATUS_LOG_PATH)
# Change to be hostname and pid
status_logger_file_name = _get_status_logger_file_name(pid)
with open(
join(_STATUS_LOG_PATH, status_logger_file_name),
"w",
encoding="utf8"
) as f:
with open(join(_STATUS_LOG_PATH, status_logger_file_name), "w", encoding="utf8") as f:
f.seek(0)
f.write(dumps(status_json))
f.truncate()
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
from os import environ
from pathlib import Path

from azure.monitor.opentelemetry.exporter._connection_string_parser import ( # pylint: disable=import-error,no-name-in-module
from azure.monitor.opentelemetry.exporter._connection_string_parser import ( # pylint: disable=import-error,no-name-in-module
ConnectionStringParser,
)
from azure.monitor.opentelemetry.exporter._utils import _is_on_app_service # pylint: disable=import-error,no-name-in-module
from azure.monitor.opentelemetry.exporter._utils import ( # pylint: disable=import-error,no-name-in-module
_is_on_app_service,
)
from azure.monitor.opentelemetry._constants import (
_LOG_PATH_LINUX,
_LOG_PATH_WINDOWS,
Expand All @@ -35,9 +37,7 @@ def _get_customer_ikey_from_env_var():
if not _CUSTOMER_IKEY_ENV_VAR:
_CUSTOMER_IKEY_ENV_VAR = "unknown"
try:
_CUSTOMER_IKEY_ENV_VAR = (
ConnectionStringParser().instrumentation_key
)
_CUSTOMER_IKEY_ENV_VAR = ConnectionStringParser().instrumentation_key
except ValueError as e:
logger.error("Failed to parse Instrumentation Key: %s", e)
return _CUSTOMER_IKEY_ENV_VAR
Expand All @@ -62,6 +62,4 @@ def _env_var_or_default(var_name, default_val=""):
return default_val


_EXTENSION_VERSION = _env_var_or_default(
"ApplicationInsightsAgent_EXTENSION_VERSION", "disabled"
)
_EXTENSION_VERSION = _env_var_or_default("ApplicationInsightsAgent_EXTENSION_VERSION", "disabled")
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,7 @@ def _default_logger_name(configurations):


def _default_resource(configurations):
environ.setdefault(
OTEL_EXPERIMENTAL_RESOURCE_DETECTORS,
",".join(_SUPPORTED_RESOURCE_DETECTORS)
)
environ.setdefault(OTEL_EXPERIMENTAL_RESOURCE_DETECTORS, ",".join(_SUPPORTED_RESOURCE_DETECTORS))
if RESOURCE_ARG not in configurations:
configurations[RESOURCE_ARG] = Resource.create()
else:
Expand Down Expand Up @@ -162,16 +159,13 @@ def _default_views(configurations):


def _get_otel_disabled_instrumentations():
disabled_instrumentation = environ.get(
OTEL_PYTHON_DISABLED_INSTRUMENTATIONS, ""
)
disabled_instrumentation = environ.get(OTEL_PYTHON_DISABLED_INSTRUMENTATIONS, "")
disabled_instrumentation = disabled_instrumentation.split(",")
# to handle users entering "requests , flask" or "requests, flask" with spaces
disabled_instrumentation = [
x.strip() for x in disabled_instrumentation
]
disabled_instrumentation = [x.strip() for x in disabled_instrumentation]
return disabled_instrumentation


def _is_instrumentation_enabled(configurations, lib_name):
if INSTRUMENTATION_OPTIONS_ARG not in configurations:
return False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ pytest
django
fastapi-slim
flask
psycopg2-binary
psycopg2-binary; python_version >= '3.9'
requests
urllib3
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ def observable_gauge_func(options: CallbackOptions) -> Iterable[Observation]:
counter.add(1)

# Async Counter
observable_counter = meter.create_observable_counter(
"observable_counter", [observable_counter_func]
)
observable_counter = meter.create_observable_counter("observable_counter", [observable_counter_func])

# UpDownCounter
updown_counter = meter.create_up_down_counter("updown_counter")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
from opentelemetry.sdk.resources import Resource

configure_azure_monitor(
resource=Resource.create({
"service.name": "live_metrics_service",
"service.instance.id": "qp_instance_id",
}),
resource=Resource.create(
{
"service.name": "live_metrics_service",
"service.instance.id": "qp_instance_id",
}
),
logger_name=__name__,
enable_live_metrics=True, # Enable live metrics configuration
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,5 @@

tracer = trace.get_tracer(__name__)
with tracer.start_as_current_span(name="MyApplication"):
client = BlobServiceClient.from_connection_string(
environ["AZURE_STORAGE_ACCOUNT_CONNECTION_STRING"]
)
client = BlobServiceClient.from_connection_string(environ["AZURE_STORAGE_ACCOUNT_CONNECTION_STRING"])
client.create_container("mycontainer") # Call will be traced
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from opentelemetry.sdk.trace import SpanProcessor
from opentelemetry.trace import get_tracer, SpanContext, SpanKind, TraceFlags


# Define a custom processor to filter your spans
class SpanFilteringProcessor(SpanProcessor):

Expand All @@ -30,13 +31,12 @@ def on_start(self, span, parent_context):
span.context.trace_state,
)


# Create a SpanFilteringProcessor instance.
span_filter_processor = SpanFilteringProcessor()

# Pass in your processor to configuration options
configure_azure_monitor(
span_processors=[span_filter_processor]
)
configure_azure_monitor(span_processors=[span_filter_processor])

tracer = get_tracer(__name__)

Expand Down
Loading