ملف تعريف حاويات Azure المباشرة لدى Application Insights

يمكنك تمكين محلل ملفات تعريف Application Insights لتطبيق ASP.NET Core الذي يعمل في حاويتك دون تعليمات برمجية تقريباً. لتمكين محلل ملفات تعريف Application Insights على مثيل الحاوية الخاص بك، تحتاج إلى:

  • أضِف المرجع إلى حزمة Microsoft.ApplicationInsights.Profiler.AspNetCore NuGet.
  • قم بتحديث التعليمات البرمجية لتمكين محلل ملفات التعريف.
  • إعداد مفتاح تقرير عن حالة النظام Application Insights.

في هذه المقالة، ستتعرف على الطرق المختلفة التي يمكنك من خلالها:

  • تثبيت حزمة NuGet في المشروع.
  • قم بتعيين متغير البيئة عبر المنسق (مثل نظام Kubernetes).
  • تعرف على اعتبارات الأمان حول توزيع الإنتاج، مثل حماية مفتاح تقرير عن حالة النظام في Application Insights.

المتطلبات الأساسية

إعداد البيئة

  1. قم بنسخ نموذج المشروع الآتي واستخدمه:

    git clone https://github.com/microsoft/ApplicationInsights-Profiler-AspNetCore.git
    
  2. انتقل إلى مثال تطبيق الحاوية:

    cd examples/EnableServiceProfilerForContainerAppNet6
    
  3. هذا المثال هو مشروع بلا بنية تم إنشاؤه عن طريق استدعاء أمر CLI التالي:

    dotnet new mvc -n EnableServiceProfilerForContainerApp
    

    لقد أضفنا تأخيرًا في Controllers/WeatherForecastController.cs المشروع لمحاكاة الازدحام.

    [HttpGet(Name = "GetWeatherForecast")]
    public IEnumerable<WeatherForecast> Get()
    {
        SimulateDelay();
        ...
        // Other existing code.
    }
    private void SimulateDelay()
    {
        // Delay for 500ms to 2s to simulate a bottleneck.
        Thread.Sleep((new Random()).Next(500, 2000));
    }
    
  4. إضافة حزمة NuGet لجمع تتبعات أداة تحليل الرؤى:

    dotnet add package Microsoft.ApplicationInsights.Profiler.AspNetCore
    
  5. تمكين Application Insights و Profiler.

    أضف builder.Services.AddApplicationInsightsTelemetry() الأسلوب وبعده WebApplication.CreateBuilder()builder.Services.AddServiceProfiler() في Program.cs:

    var builder = WebApplication.CreateBuilder(args);
    
    builder.Services.AddApplicationInsightsTelemetry(); // Add this line of code to enable Application Insights.
    builder.Services.AddServiceProfiler(); // Add this line of code to enable Profiler
    builder.Services.AddControllersWithViews();
    
    var app = builder.Build();
    

اسحب أحدث صور لوقت تشغيل/ تحديث إصدار الذاكر الأساسية لـ ASP.NET

  1. انتقل إلى دليل مثال .NET Core 6.0:

    cd examples/EnableServiceProfilerForContainerAppNet6
    
  2. اسحب أحدث صور ASP.NET Core:

    docker pull mcr.microsoft.com/dotnet/sdk:6.0
    docker pull mcr.microsoft.com/dotnet/aspnet:6.0
    

تلميح

ابحث عن الصور الرسمية ل Docker SDKووقت التشغيل.

أضف مفتاح Application Insights

  1. عبر مورد Application Insights في مدخل Azure، قم بتدوين مفتاح التقرير عن حالة نظام Application Insights.

    لقطة شاشة توضح العثور على مفتاح تقرير عن حالة النظام في مدخل Microsoft Azure.

  2. افتح appsettings.json وأضف مفتاح التقرير عن حالة نظام Application Insights إلى قسم التعليمات البرمجية هذا:

    {
        "ApplicationInsights":
        {
            "InstrumentationKey": "Your instrumentation key"
        }
    }
    

قم بإنشاء صورة Docker وتشغيلها

  1. راجع ملف Docker.

  2. قم بإنشاء صورة المثال:

    docker build -t profilerapp .
    
  3. قم بتشغيل الحاوية:

    docker run -d -p 8080:80 --name testapp profilerapp
    

اعرض الحاوية عبر مستعرض الويب

للضغط على نقطة النهاية، لديك خياران:

  • تفضل بزيارة http://localhost:8080/weatherforecast في المستعرض الخاص بك.

  • استخدام curl:

    curl http://localhost:8080/weatherforecast
    

افحص السجلات

اختيارياً، افحص السجل المحلي لمعرفة انتهاء جلسة عمل جمع المعلومات من عدم انتهائها:

docker logs testapp

في السجلات المحلية، لاحظ الأحداث الآتية:

Starting application insights profiler with instrumentation key: your-instrumentation key # Double check the instrumentation key
Service Profiler session started.               # Profiler started.
Finished calling trace uploader. Exit code: 0   # Uploader is called with exit code 0.
Service Profiler session finished.              # A profiling session is completed.

عرض تتبعات «محلل ملفات تعريف الخدمة»

  1. انتظر لمدة 2 إلى 5 دقائق بحيث يمكن تجميع الأحداث في Application Insights.

  2. افتح جزء Performance في مورد Application Insights.

  3. بعد الانتهاء من عملية التتبع، يظهر الزر تتبعات محلل ملفات التعريف .

    لقطة شاشة تعرض زر تتبعات محلل ملفات التعريف في جزء الأداء.

تنظيف الموارد

قم بتشغيل الأمر الآتي لإيقاف مثال المشروع:

docker rm -f testapp

الخطوات التالية