Jump to content

Connect with Application Insights in 'not Local auth mode' using OpenTelemetry

Featured Replies

Posted

TOC

 

  1. What is it
  2. How to use it
  3. References

 

 

 

What is it

 

Azure Web Apps or Azure Function Apps frequently communicate with Application Insights to log various levels of data, which can later be reviewed and filtered in the Log Analytics Workspace.

 

 

Taking Python as an example, the official documentation mentions that the OpenCensus package will no longer be supported after 2024-09-30.

 

largevv2px999.png.693b2b75d9e2936cb8d2551600fe61a7.png

 

 

 

 

The article suggests OpenTelemetry as the latest alternative. In response to the growing cybersecurity awareness among many companies, many users have disabled the 'Local Authentication' feature in Application Insights to enhance security.

 

largevv2px999.png.3e7a6db82e279653200abd629967f559.png

 

 

 

 

Therefore, this article will focus on how Web Apps/Function Apps can use Managed Identity to communicate with Application Insights and utilize the latest OpenTelemetry package to avoid the predicament of unsupported packages.

 

 

 

How to use it

 

According to Microsoft Entra authentication for Application Insights - Azure Monitor | Microsoft Learn, sample code with "OpenCensus" will EOS after 2024-09-30 which means this method is deprecatedfrom now. (will show up in further code snippet with method 1)

 

Currently, Microsoft officially suggest user apply OpenTelemetry as the new method. (will show up in further code snippet with method 2).

 

 

Step 1:

 

Function App should use system/user assigned managed identity to issue credential for accessing AI (i.e., Application Insights), I choose system assigned managed identity in this sample.

 

largevv2px999.png.4b931de28ebfdfac65b56e6d5c6de01c.png

 

 

 

In the "Role Assignment", please add the "Monitoring Metrics Publisher" to the target AI resource, I add the parent RG (i.e., resource group) from that AI in this experiment.

 

largevv2px999.png.95e8e90d24c5e4df5292205dda5ede1d.png

 

 

 

Step 2:

 

In code level, I use Function App python V1 architecture from the python code, but I think V1 and V2 could achieve the same goal.

 

largevv2px999.png.c55d12c607e34fea295064f873a3abc6.png

 

[requirements.txt]

 

 

 

# Method 2: opentelemetry
azure-monitor-opentelemetry
azure-identity

 

 

 

 

 

largevv2px999.png.8da71aae9cd2b9e2f57e7561a0eec655.png

 

[<TriggerName>/__init__.py]

 

 

 

# Method 2: opentelemetry
from azure.monitor.opentelemetry import configure_azure_monitor
from logging import INFO, getLogger
from azure.identity import ManagedIdentityCredential
credential = ManagedIdentityCredential()
configure_azure_monitor(
   connection_string='InstrumentationKey=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX;IngestionEndpoint=https://XXXXXX-X.in.applicationinsights.azure.com/;LiveEndpoint=https://XXXXXX.livediagnostics.monitor.azure.com/;ApplicationId=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
   credential=credential
)

   # Method 2: opentelemetry
   logger2 = getLogger(__name__)
   logger2.setLevel(INFO)
   logger2.info("Method 2: opentelemetry")
   logger2.handlers.clear()

 

 

 

 

The connection_string mentioned in the code can be obtained through the AI's overview page.

 

largevv2px999.png.753f630ee3726428cdc1fb107f7b773a.png

 

 

 

 

Step 3:

 

After the deployment to the Function App, we could use online Code+Test from Azure portal

 

largevv2px999.thumb.png.3cf465e55196789c1d05b3df4c31173c.png

 

 

 

 

And the corresponding AI will got the log.

 

largevv2px999.thumb.png.921d0bff94ac3e194ecc2b5d0bca35d1.png

 

 

 

References:

 

azure-monitor-opentelemetry · PyPI

 

Enable Azure Monitor OpenTelemetry for .NET, Java, Node.js, and Python applications - Azure Monitor | Microsoft Learn

 

azure-sdk-for-python/sdk/monitor/azure-monitor-opentelemetry/samples/metrics/instruments.py at main · Azure/azure-sdk-for-python (github.com)

 

Enable Azure Monitor OpenTelemetry for .NET, Java, Node.js, and Python applications - Azure Monitor | Microsoft Learn

 

Continue reading...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...