عملکردهای نظارت مصنوعی لاجورد با نمایشنامه نویس: راهنمای کامل – قسمت 1

مقدمه
نظارت مصنوعی یک رویکرد فعال برای نظارت بر برنامه های وب با شبیه سازی تعامل کاربران و اندازه گیری بهره وری از دیدگاه کاربر نهایی است. این مقاله نحوه ساخت یک راه حل پایدار برای مشاهده مصنوعی با استفاده از آن را نشان می دهد ویژگی های لاجورد وت نمایشنامه نویس این می تواند به طور خودکار برنامه های وب شما را آزمایش کند ، نتایج بینش برنامه Azure و آثار باستانی آزمایش را در ذخیره سازی Blob Azure در نظر بگیرد.
چرا نظارت مصنوعی اهمیت دارد
نظارت سنتی فقط به شما می گوید وقتی چیزی از قبل شکسته شده است. نظارت مصنوعی به شما کمک می کند:
- قبل از اینکه کاربران این کار را انجام دهند ، مشکلات را کشف کنید با راه اندازی مداوم تست های خودکار
- سفر بحرانی کاربران را رعایت کنید مانند ورودی ، جعبه یا فرآیندهای کار کلیدی
- اعتبار سنجی اجرای با ارائه عملکردهای اساسی پس از نسخه ها
مشاهده معماری
راه حل ما چندین سرویس لاجورد را برای ایجاد یک سیستم نظارت جامع ترکیب می کند:
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Azure Timer │ │ Azure Function │ │ Playwright │
│ Trigger │───▶│ (Runtime) │───▶│ Test Runner │
│ (Scheduled) │ │ │ │ │
└─────────────────┘ └─────────────────┘ └─────────────────┘
│
▼
┌─────────────────┐
│ Test Results │
│ Processing │
└─────────────────┘
│
┌─────────┴─────────┐
▼ ▼
┌─────────────────┐ ┌─────────────────┐
│ Application │ │ Azure Blob │
│ Insights │ │ Storage │
│ (Telemetry) │ │ (Artifacts) │
└─────────────────┘ └─────────────────┘
پیش نیازهای
قبل از شروع ، مطمئن شوید که:
- node.js 18+ نصب شده
- لاجورد کلی نصب و پیکربندی شده
- ویژگی های لاجورد ابزارهای اساسی v4
- در اشتراک لاجورد با:
- یک نسخه از بینش برنامه
- حساب ذخیره سازی
- برنامه کاربردی (حق بیمه یا برنامه ویژه توصیه شده)
طرح
1. پروژه را آغاز کنید
# Create project directory
mkdir synthetic-monitoring
cd synthetic-monitoring
# Initialize Node.js project
npm init -y
# Install core dependencies
npm install @azure/functions @playwright/test playwright
npm install @azure/storage-blob applicationinsights archiver dotenv
# Install development dependencies
npm install -D typescript @types/node @types/archiver
2 ساختار پروژه
ساختار دایرکتوری زیر را ایجاد کنید:
synthetic-monitoring/
├── src/
│ ├── functions/
│ │ └── synthetic-monitor.ts # Timer-triggered function
│ ├── support/
│ │ ├── azure/
│ │ │ ├── app-insights-client.ts
│ │ │ ├── app-insights.ts
│ │ │ ├── blob-storage-client.ts
│ │ │ └── blob-storage.ts
│ │ ├── logger/
│ │ │ └── logger.ts
│ │ ├── pages/ # Page Object Models
│ │ ├── reporter/
│ │ │ ├── appinsights-reporter.ts
│ │ │ └── utils.ts
│ │ └── services/ # Authentication services
│ ├── tests/
│ │ └── marketplace-integration.spec.ts
│ └── index.ts # Main entry point
├── host.json # Azure Functions configuration
├── local.settings.json # Local environment variables
├── package.json
├── playwright.config.ts # Playwright configuration
└── tsconfig.json # TypeScript configuration
تحقق اساسی
1. تایمر تایمر لاجورد
ویژگی اصلی ایجاد شده توسط تایمر را ایجاد کنید (src/functions/synthetic-monitor.ts) ::
import { app, InvocationContext, Timer } from '@azure/functions';
import { runPlaywrightTests } from '../index';
app.timer('syntheticMonitor', {
schedule: process.env.SYNTHETIC_MONITOR_SCHEDULE || '|| '0 0 * * * *',
handler: async (myTimer: Timer, context: InvocationContext) => {
try {
context.log("🚀 Executing synthetic monitoring tests...");
await runPlaywrightTests(context);
context.log("✅ Synthetic monitoring tests completed successfully!");
} catch (error) {
context.log("❌ Error in synthetic monitoring tests:", error);
throw error; // Ensure the function fails for monitoring purposes
} finally {
context.log("🔄 Synthetic monitoring cycle completed.");
}
}
});
2. یک دونده بزرگ آزمون
منطق اصلی عملکرد آزمون را پیاده سازی کنید (src/index.ts) ::
import { spawn } from 'child_process';
import { setLoggerContext } from './support/logger/logger';
import { InvocationContext } from '@azure/functions';
import path from 'path';
// Import the function to register it
import './functions/synthetic-monitor';
export async function runPlaywrightTests(context: InvocationContext): Promisevoid> {
setLoggerContext(context);
return new Promise((resolve, reject) => {
context.log('▶️ Running Playwright tests via CLI...');
// Use full path to playwright binary
const playwrightPath = path.join(process.cwd(), 'node_modules', '.bin', 'playwright.cmd');
const command = \`"\${playwrightPath}" test\`;
context.log(\`📍 Using command: \${command}\`);
const child = spawn(command, {
shell: true,
env: {
...process.env,
// Add any environment variables your tests need
NODE_ENV: 'production'
}
});
child.stdout.on('data', (data) => {
context.log(data.toString());
});
child.stderr.on('data', (data) => {
context.log(data.toString());
});
child.on('error', (error) => {
context.log(\`❌ Playwright tests failed: \${error.message}\`);
reject(error);
});
child.on('close', (code) => {
if (code === 0) {
context.log('✅ Playwright tests completed.');
resolve();
} else {
context.log(\`❌ Playwright tests exited with code \${code}\`);
reject(new Error(\`Playwright tests failed with code \${code}\`));
}
});
});
}
3. پیکربندی نمایشنامه نویس
یک نمایشنامه نویس را برای ویژگی های لاجورد پیکربندی کنید (playwright.config.ts) ::
import { defineConfig, devices } from '@playwright/test';
import fs from 'fs';
import os from 'os';
import path from 'path';
const tmpDir = os.tmpdir();
const outputPath = process.env.PLAYWRIGHT_OUTPUT_DIR || path.join(tmpDir, 'playwright-artifacts');
const htmlReportPath = process.env.PLAYWRIGHT_HTML_REPORT || path.join(tmpDir, 'playwright-html-report');
// Ensure directories exist
if (!fs.existsSync(outputPath)) {
fs.mkdirSync(outputPath, { recursive: true });
}
if (!fs.existsSync(htmlReportPath)) {
fs.mkdirSync(htmlReportPath, { recursive: true });
}
export default defineConfig({
testDir: './src/tests',
timeout: 180 * 1000, // 3 minutes per test
retries: 1, // Retry failed tests once
fullyParallel: true,
workers: 3, // Adjust based on your Azure Function plan
outputDir: outputPath,
use: {
headless: true, // Always run headless in Azure Functions
viewport: { width: 1280, height: 720 },
baseURL: process.env.baseUrl,
screenshot: 'only-on-failure',
trace: 'retain-on-failure',
actionTimeout: 30000,
},
reporter: [
['html', { outputFolder: htmlReportPath, open: 'never' }],
['junit', { outputFile: path.join(outputPath, 'junit-report.xml') }],
['list'],
['./src/support/reporter/appinsights-reporter.ts'] // Custom reporter
],
projects: [
{
name: 'chromium',
use: {
...devices['Desktop Chrome']
},
}
],
});
4. پیکربندی توابع لاجورد
پیکربندی برنامه عملکرد (host.json) ::
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
},
"logLevel": {
"default": "Information"
}
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[4.*, 5.0.0)"
}
}
عملکرد آزمون مثال زدنی
یک آزمایش جامع ایجاد کنید (src/tests/do-somethinng.spec.ts) ::
import { test, expect, BrowserContext, Page } from '@playwright/test';
const aadUsername: string = process.env['username'] || '';
const aadPassword: string = process.env['password'] || '';
const baseURL: string = process.env['baseUrl'] || '';
let context: BrowserContext;
let page: Page;
test.beforeEach(async ({ browser: bro }) => {
context = await bro.newContext();
page = await context.newPage();
// Init all pages here
await page.goto(baseURL);
});
test('User should be able to do somethinng', { tag: ['@some-tag', ] }, async () => {
// All the test implemention here
//Arrange
//Act
//Assert
});
test.afterEach(async () => {
await page.close();
await context.close();
});
استقرار و پیکربندی
1. متغیرهای محیطی
متغیرهای متوسط خود را در تنظیمات برنامه عملکرد Azure تنظیم کنید:
{
"AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=;AccountKey=" ,
"FUNCTIONS_WORKER_RUNTIME": "node",
"FUNCTIONS_EXTENSION_VERSION": "~4",
//ScheduleTime
"SYNTHETIC_MONITOR_SCHEDULE": "0 */10 * * * *",
// Test Configuration
"username": "" ,
"password": "" ,
"baseUrl": "" ,
// Azure Services
"APPLICATIONINSIGHTS_CONNECTION_STRING": "" ,
"APPINSIGHTS_INSTRUMENTATIONKEY": "" ,
"AZURE_BLOB_CONNECTION_STRING": "" ,
"AZURE_BLOB_CONTAINER_NAME": "" ,
}
2. اسکریپت های بسته بندی
اسکریپت ها را برای ساخت و اجرای پیکربندی کنید:
{
"scripts": {
"build": "tsc",
"start": "func start --typescript",
"test:local": "npx playwright test",
"install:browsers": "npx playwright install"
}
}
پایان
کاربرد نظارت مصنوعی با توابع لاجورد و نمایشنامه نویس یک راه حل قدرتمند و مقیاس برای نظارت بر برنامه های کاربردی ارائه می دهد. این رویکرد ارائه می دهد:
- تست خودکار برنامه
- همه چیز گزارش می شود توسط بینش برنامه
- انباره برای خطاها در خطاها
- سودمندی با ویژگی های لاجورد
- شبیه سازی واقعی کاربران نمایشنامه نویس
نکته اصلی موفقیت ، انتخاب برنامه عملکرد لاجورد راست ، بهینه سازی پیکربندی نمایشنامه نویس برای اجرای یک ابر و اعمال پایدار پردازش و نظارت بر خطا است.
با استفاده از این مبنای ، می توانید نظارت خود را برای پوشش سفر مهم کاربران ، نقاط پایانی API ، عملکرد عملکرد و آزمایشات برای بسیاری از مناطق گسترش دهید تا اطمینان حاصل شود که برنامه های شما تجربه کاربر بهینه را در طول ساعت ارائه می دهد.
منابع اضافی
این تحقق مبتنی بر سیستمی برای نظارت مصنوعی بر تولید در دنیای واقعی است که نظارت بر برنامه ها و فرآیندهای مهم کار مصرف کنندگان است.




