azure-functions-python-worker icon indicating copy to clipboard operation
azure-functions-python-worker copied to clipboard

[BUG] Open Telemetry integration with Azure Functions - unnecessary telemetry sent

Open karpikpl opened this issue 8 months ago • 5 comments

When function is configured with OTEL using:

from azure.monitor.opentelemetry import configure_azure_monitor
configure_azure_monitor()

duplicated (4x) logs are sent to application insights. It seems like using env variable APPLICATIONINSIGHTS_CONNECTION_STRING is not supported because it configures both Application Insights and Open Telemetry?

When configured with:

configure_azure_monitor(connection_string=os.environ.get("APPINSIGHTS"))

it seems better but hundreds of extra logs are sent to Application Insights image

where all the calls to application insights are instrumented and logged in application insights

Investigative information

Please provide the following:
  • Timestamp: 2023-11-02T14:35:19.560076Z
  • Function App name: python-app-pka
  • Function name(s) (as appropriate): HttpTrigger
  • Core Tools version: 4.0.5390

Repro steps

Provide the steps required to reproduce the problem:
from logging import getLogger
import os

import azure.functions as func
from opentelemetry import trace
from opentelemetry.trace import SpanKind
import requests

from shared_code import helper

# Set up logging
configure_azure_monitor(os.environ.get("APPINSIGHTS"))

# Get a tracer for the current module.
tracer = trace.get_tracer(__name__)
logger = getLogger(__name__)


def main(req: func.HttpRequest) -> func.HttpResponse:
    """Http trigger function example using OpenTelemetry with Azure Monitor."""
    logger.info("logger.info: Python HTTP trigger function processed a request.")

    name = req.params.get("name")
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get("name")

    if not name:
        name = "no-name"

    # Start a new span with the name "hello".
    # This also sets this created span as the current span in this context.
    # This span will be exported to Azure Monitor as part of the trace.
    with tracer.start_as_current_span(name, kind=SpanKind.SERVER) as span:
        # log a message before making an HTTP call
        span.add_event("Making an HTTP call event")
        logger.info("Making an HTTP call")
        number = helper.double(2)
        url = os.environ["URL"]
        logger.warning("got a number!", extra={"number": number})

        response = requests.get(url, timeout=5)
        json_data = response.json()
        # log a message after making an HTTP call with URL in extra properties
        logger.info("HTTP call complete", extra={"url": url})

        title = json_data["title"]

        return func.HttpResponse(f"number: {number}. Title: {title}")

host.json:

{
  "version": "2.0",
  "logging": {
    // "applicationInsights": {
    //   "samplingSettings": {
    //     "isEnabled": false,
    //     "excludedTypes": "Request"
    //   }
    // },
    "logLevel": {
      "default": "Warning",
      "Host.Results": "Information",
      "Function": "Information",
      "Host": "Error",
      "Microsoft": "Error"
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[3.*, 4.0.0)"
  },
  "extensions": {
    "serviceBus": {
      "messageHandlerOptions": {
        "maxConcurrentCalls": 16
      }
    }
  },
  "concurrency": {
    "dynamicConcurrencyEnabled": true,
    "snapshotPersistenceEnabled": true
  }
}

Expected behavior

Provide a description of the expected behavior.

Each log message is logged ONCE in application insights. No logs related to application insights uploads are visible - only application logs.

Actual behavior

Provide a description of the actual behavior observed.

Ton of unnecessary logs in application insights - see screenshot above

Known workarounds

Provide a description of any known workarounds.

Not aware of any

Contents of the requirements.txt file:

Provide the requirements.txt file to help us find out module related issues.
azure-functions==1.17.0
azure-identity==1.14.1
azure-mgmt-resource==23.0.1
azure-monitor-opentelemetry==1.0.0
msrestazure==0.6.4
#opentelemetry-instrumentation-requests==0.41b0
requests==2.31.0

Related information

Provide any related information
  • Links to source
  • Bindings used - http and service bus (2 functions in the project)

karpikpl avatar Nov 02 '23 15:11 karpikpl