azureazure-application-insightsazure-log-analyticsazure-managed-identity

azure.monitor.opentelemetry - not logging anything


I have a simple python script and are following this sample: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry/samples/metrics/attributes.py

Code:

import logging
from azure.monitor.opentelemetry import configure_azure_monitor
from azure.identity import DefaultAzureCredential
from opentelemetry import metrics

large_attribute_set = {}
for i in range(20):
    key = "key{}".format(i)
    val = "val{}".format(i)
    large_attribute_set[key] = val

credential = DefaultAzureCredential()

configure_azure_monitor(
    logger_name=__name__,
    credential=credential,
    connection_string="InstrumentationKey=xx-xx-xx-xx-xx;IngestionEndpoint=https://westeurope-5.in.applicationinsights.azure.com/;LiveEndpoint=https://westeurope.livediagnostics.monitor.azure.com/;ApplicationId=x-x-x-x-x",
    )

logger = logging.getLogger(__name__)

meter = metrics.get_meter_provider().get_meter("sample")
counter = meter.create_counter("my_counter")
counter.add(1)

logger.info("info1", extra=large_attribute_set)
logger.warning("warning1", extra=large_attribute_set)
logger.error("error1", extra=large_attribute_set)
logger.critical("critical1", extra=large_attribute_set)

In visual studio code I have run: azd auth login

I do not get any errors. However no logs either: enter image description here

I have following access to application insigths: enter image description here


Solution

  • To create counter. I have used AzureMonitorMetricExporter and for sending data to traces, I have used AzureMonitorLogExporter as below:

    import logging 
    from opentelemetry.sdk._logs.export import BatchLogRecordProcessor as rith
    from azure.monitor.opentelemetry.exporter import AzureMonitorLogExporter as testPack
    from opentelemetry._logs import (get_logger_provider as glp,set_logger_provider as rc,)
    from opentelemetry.sdk._logs import (LoggerProvider,LoggingHandler,)
    from opentelemetry import metrics
    from opentelemetry.sdk.metrics import Counter, MeterProvider
    from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader
    from azure.monitor.opentelemetry.exporter import AzureMonitorMetricExporter
    
    exp = AzureMonitorMetricExporter.from_connection_string("InstrumentationKey=faderithwik02;IngestionEndpoint=https://eastus-8.in.applicationinsights.azure.com/;LiveEndpoint=https://eastus.livediagnostics.monitor.azure.com/;ApplicationId=a6rithwikb1"
    )
    rr = PeriodicExportingMetricReader(exp, export_interval_millis=5000)
    metrics.set_meter_provider(MeterProvider(metric_readers=[rr]))
    ri_out = {
        "Name": "Chotu"
    }
    ch_meter = metrics.get_meter("testname")
    ch_cnter = ch_meter.create_counter("testcounter")
    ch_cnter.add(8, ri_out)
    
    rc(LoggerProvider())
    ri_ex = testPack(
        connection_string="InstrumentationKey=fadrithwik02;IngestionEndpoint=https://eastus-8.in.applicationinsights.azure.com/;LiveEndpoint=https://eastus.livediagnostics.monitor.azure.com/;ApplicationIdrithwikc4b1"
    )
    glp().add_log_record_processor(rith(ri_ex))
    
    cho = LoggingHandler()
    chlg = logging.getLogger(__name__)
    chlg.addHandler(cho)
    chlg.setLevel(logging.INFO)
    
    
    chlg.info("Rithwik from Info")
    chlg.error("Rithwik from Error")
    chlg.warning("Rithwik from WARNING ")
    

    Output:

    enter image description here

    Counter is exported to CustomMetrics:

    enter image description here

    enter image description here