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

Too much traces in AppInsights #317

Closed
WaterKnight1998 opened this issue Sep 29, 2023 · 8 comments
Closed

Too much traces in AppInsights #317

WaterKnight1998 opened this issue Sep 29, 2023 · 8 comments

Comments

@WaterKnight1998
Copy link

I have the following function code:

from azure.monitor.opentelemetry import configure_azure_monitor
from opentelemetry import trace

logging.getLogger().handlers = []

configure_azure_monitor(connection_string=os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"])
tracer = trace.get_tracer(__name__)

app = fastapi.FastAPI()

@app.get("/x")
async def x():
   with tracer.start_as_current_span("aaaaa"):
      logging.info("a",extras={"b":1})

def main(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:
    return func.AsgiMiddleware(app).handle(req, context)

With this code I removed the duplicated logs and I can see custom dimensions. However I am seeing 3 taces each 5s creating too much logs:

Request URL: 'https://westeurope-5.in.applicationinsights.azure.com//v2.1/track' Request method: 'POST' Request header
Response status: 200 Response headers: 'Transfer-Encoding': 'chunked' 'Content-Type': 'application/json; charset=utf-8
Transmission succeeded: Item received: 3. Items accepted: 3

I tried the following env vars:

OTEL_PYTHON_EXCLUDED_URLS=.*applicationinsights\.azure\.com/.*
OTEL_PYTHON_REQUESTS_EXCLUDED_URLS=.*applicationinsights\.azure\.com/.*
OTEL_PYTHON_DISABLED_INSTRUMENTATIONS=django,asgi,wsgi,fastapi,flask,requests,urllib,urllib3
OTEL_TRACES_EXPORTER=none

However, I am still seeing this. I have seen that your sdk calls this endpoint. https://github.com/Azure/azure-sdk-for-python/blob/11437bbf7be952401581641f556ac8c897667e59/sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/_generated/operations/_azure_monitor_client_operations.py#L39

But i don't know which library you use for http requests, probably this one: https://github.com/Azure/azure-sdk-for-python/blob/11437bbf7be952401581641f556ac8c897667e59/sdk/core/azure-core/azure/core/utils/_pipeline_transport_rest_shared.py#L27.

Any idea on how to disable this logs being sent to AI?

@WaterKnight1998
Copy link
Author

I have seen that those logs come from:


Request URL: 'https://westeurope-5.in.applicationinsights.azure.com//v2.1/track'
Request method: 'POST'
Request headers:
'Content-Type': 'application/json'
'Content-Length': '12891'
'Accept': 'application/json'
'User-Agent': 'azsdk-python-azuremonitorclient/unknown Python/3.9.17 (Linux-5.15.116.1-1.cm2-x86_64-with-glibc2.31)'
A body is sent with the request

sdkVersion
py3.9.17:otel1.20.0:ext1.0.0b17

Same as the explicit log that I do.

@WaterKnight1998
Copy link
Author

I have tried raising logs to:

logging.basicConfig(level=logging.WARNING)

But I still see them

@WaterKnight1998
Copy link
Author

Fixed with following code:

from azure.monitor.opentelemetry import configure_azure_monitor
from opentelemetry import trace
import logging

logging.getLogger().handlers = []
configure_azure_monitor(connection_string=os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"])
logging.getLogger().setLevel(logging.WARNING)

tracer = trace.get_tracer(__name__)

app = fastapi.FastAPI()

@app.get("/x")
async def x():
   with tracer.start_as_current_span("aaaaa"):
      logging.warning("a",extras={"b":1})

def main(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:
    return func.AsgiMiddleware(app).handle(req, context)

@macieyng
Copy link

macieyng commented Oct 1, 2023

Do this and it should suppress those verbose logs.

configure_azure_monitor(connection_string=os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"])
logging.getLogger("azure.core.pipeline.policies.http_logging_policy").setLevel(logging.WARNING)
logging.getLogger("azure.monitor.opentelemetry.exporter.export").setLevel(logging.WARNING)

@WaterKnight1998
Copy link
Author

This helped, thank you very much @macieyng . The only thing that I don't understand is why disabling the automatic instrumentations and excluding urls didn't help

@macieyng
Copy link

macieyng commented Oct 2, 2023

It didn't work because Azure SDK is instrumenting itself while suppressing any other instrumentation. Read this comment: Azure/azure-sdk-for-python#31292 (comment)

@lzchen
Copy link
Contributor

lzchen commented Oct 2, 2023

@WaterKnight1998

These additional logs are appearing in the "traces" table in your Application Insights resource correct? We have recently implemented a feature to define the specific logger that you want to collect traces telemetry. This should allow you to define the logger to your application logger instead of the default which is the root logger. This is why you are seeing logs collected from the SDK itself. This feature should be available soon in the next release.

@jeremydvoss
Copy link
Member

Migrating to Azure sdk: Azure/azure-sdk-for-python#32503

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants