Skip to content

Azure App Insights receiving duplicate logs #44235

@thabani-dev

Description

@thabani-dev

Similar to #37978 but solution provided there doesn't work.

Describe the bug
code 1:

import logging
import random
import asyncio
from fastapi import FastAPI
from azure.monitor.opentelemetry import configure_azure_monitor

CONNECTION_STRING = "XXX"

configure_azure_monitor(
    logger_name="dice-game-logger",
    connection_string=CONNECTION_STRING
)

logger = logging.getLogger("dice-game-logger")
logger.setLevel(logging.INFO)
logger.propagate = False

app = FastAPI()


print("Handlers on dice-game-logger:", logger.handlers)
print("Handlers on root logger:", logging.getLogger().handlers)


@app.get("/roll")
async def roll_dice():
    """Roll a dice, emit trace/logs/metrics"""
    dice1_value = random.randint(1, 6)
    dice2_value = random.randint(1, 6)

    value = dice1_value + dice2_value

    # Log
    logger.info(f"Rolled a {value}", extra={"dice.value": value})

    # Demonstrate async work under the same trace
    await async_log()

    # Put out error if someone rolls 2 sixes
    if (value == 12):
        logger.error("You rolled 2 sixes!!!")

    # Return response
    return {"dice_value": value}

async def async_log():
    await asyncio.sleep(1)
    logger.warning("Slowing down between rolls")


output of the prints is
"""
Handlers on dice-game-logger: [<LoggingHandler (NOTSET)>]
Handlers on root logger: []

"""

code 2:

import logging
import random
import asyncio
import time

from azure.monitor.opentelemetry import configure_azure_monitor

CONNECTION_STRING = "XXX"

configure_azure_monitor(
    logger_name="test-logger",
    connection_string=CONNECTION_STRING
)

logger = logging.getLogger("test-logger")
logger.setLevel(logging.INFO)

print("hello")

def roll_dice():
    """Roll a dice, emit trace/logs/metrics"""
    dice1_value = random.randint(1, 6)
    dice2_value = random.randint(1, 6)

    value = dice1_value + dice2_value

    # Log
    logger.info(f"Rolled a {value}", extra={"dice.value": value})

    # Demonstrate async work under the same trace
    async_log()

    # Put out error if someone rolls 2 sixes
    if (value == 12):
        logger.error("You rolled 2 sixes!!!")

    # Return response
    return {"dice_value": value}

def async_log():
    time.sleep(1)
    logger.warning("Slowing down between rolls")

print(roll_dice())
print(roll_dice())
print(roll_dice())

I'm sending logs to Azure app insights. when i run code 2, I can see the logs fine in app insights. when I run code 1, there's 2 sets of each log message. There's log duplicates

I've tried clearing root log handlers, but that didn't work

I ran code 2 using uvicorn test:app and also tried fastapi run test.py and same issue.

The duplicate logs are coming from same logger namespace and there's nothing different about them

Metadata

Metadata

Assignees

Labels

MonitorMonitor, Monitor Ingestion, Monitor Querycustomer-reportedIssues that are reported by GitHub users external to the Azure organization.needs-team-attentionWorkflow: This issue needs attention from Azure service team or SDK teamquestionThe issue doesn't require a change to the product in order to be resolved. Most issues start as that

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions