استخدم go get لتثبيت وحدات عميل Azure Monitor Ingestion Logs وAzure Identity ل Go. الوحدة النمطية Azure Identity مطلوبة للمصادقة المستخدمة في هذا النموذج.
go get github.com/Azure/azure-sdk-for-go/sdk/monitor/ingestion/azlogs
go get github.com/Azure/azure-sdk-for-go/sdk/azidentity
إنشاء متغيرات البيئة التالية مع قيم لتطبيق Microsoft Entra الخاص بك. يتم استخدام هذه القيم من قبل DefaultAzureCredential في الوحدة النمطية Azure Identity.
AZURE_TENANT_ID
AZURE_CLIENT_ID
AZURE_CLIENT_SECRET
استبدل المتغيرات في نموذج التعليمات البرمجية التالي بقيم من DCR الخاص بك. قد تحتاج أيضا إلى استبدال بيانات العينة بالبيانات الخاصة بك.
package main
import (
"context"
"encoding/json"
"strconv"
"time"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/monitor/ingestion/azlogs"
)
// logs ingestion URI
const endpoint = "https://logs-ingestion-rzmk.eastus2-1.ingest.monitor.azure.com"
// data collection rule (DCR) immutable ID
const ruleID = "dcr-00000000000000000000000000000000"
// stream name in the DCR that represents the destination table
const streamName = "Custom-MyTableRawData"
type Computer struct {
Time time.Time
Computer string
AdditionalContext string
}
func main() {
// creating the client using DefaultAzureCredential
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
//TODO: handle error
}
client, err := azlogs.NewClient(endpoint, cred, nil)
if err != nil {
//TODO: handle error
}
// generating logs
// logs should match the schema defined by the provided stream
var data []Computer
for i := 0; i < 10; i++ {
data = append(data, Computer{
Time: time.Now().UTC(),
Computer: "Computer" + strconv.Itoa(i),
AdditionalContext: "context",
})
}
// marshal data into []byte
logs, err := json.Marshal(data)
if err != nil {
panic(err)
}
// upload logs
_, err = client.Upload(context.TODO(), ruleID, streamName, logs, nil)
if err != nil {
//TODO: handle error
}
}
قم بتنفيذ التعليمات البرمجية، ويجب أن تصل البيانات إلى مساحة عمل Log Analytics في غضون بضع دقائق.
قم بتنفيذ التعليمات البرمجية، ويجب أن تصل البيانات إلى مساحة عمل Log Analytics في غضون بضع دقائق.
يرسل رمز PowerShell التالي البيانات إلى نقطة النهاية باستخدام أساسيات HTTP REST.
إشعار
يتطلب هذا النموذج PowerShell v7.0 أو أحدث.
قم بتشغيل نموذج أمر PowerShell التالي، الذي يضيف تجميعا مطلوبا للبرنامج النصي.
Add-Type -AssemblyName System.Web
استبدل المعلمات في قسم الخطوة 0 بقيم من التطبيق الخاص بك وDCR. قد تحتاج أيضا إلى استبدال نموذج البيانات في قسم الخطوة 2 بالبيانات الخاصة بك.
### Step 0: Set variables required for the rest of the script.
# information needed to authenticate to AAD and obtain a bearer token
$tenantId = "00000000-0000-0000-00000000000000000" #Tenant ID the data collection endpoint resides in
$appId = " 000000000-0000-0000-00000000000000000" #Application ID created and granted permissions
$appSecret = "0000000000000000000000000000000000000000" #Secret created for the application
# information needed to send data to the DCR endpoint
$endpoint_uri = "https://logs-ingestion-rzmk.eastus2-1.ingest.monitor.azure.com" #Logs ingestion URI for the DCR
$dcrImmutableId = "dcr-00000000000000000000000000000000" #the immutableId property of the DCR object
$streamName = "Custom-MyTableRawData" #name of the stream in the DCR that represents the destination table
### Step 1: Obtain a bearer token used later to authenticate against the DCR.
$scope= [System.Web.HttpUtility]::UrlEncode("https://monitor.azure.com//.default")
$body = "client_id=$appId&scope=$scope&client_secret=$appSecret&grant_type=client_credentials";
$headers = @{"Content-Type"="application/x-www-form-urlencoded"};
$uri = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token"
$bearerToken = (Invoke-RestMethod -Uri $uri -Method "Post" -Body $body -Headers $headers).access_token
### Step 2: Create some sample data.
$currentTime = Get-Date ([datetime]::UtcNow) -Format O
$staticData = @"
[
{
"Time": "$currentTime",
"Computer": "Computer1",
"AdditionalContext": {
"InstanceName": "user1",
"TimeZone": "Pacific Time",
"Level": 4,
"CounterName": "AppMetric1",
"CounterValue": 15.3
}
},
{
"Time": "$currentTime",
"Computer": "Computer2",
"AdditionalContext": {
"InstanceName": "user2",
"TimeZone": "Central Time",
"Level": 3,
"CounterName": "AppMetric1",
"CounterValue": 23.5
}
}
]
"@;
### Step 3: Send the data to the Log Analytics workspace.
$body = $staticData;
$headers = @{"Authorization"="Bearer $bearerToken";"Content-Type"="application/json"};
$uri = "$endpoint_uri/dataCollectionRules/$dcrImmutableId/streams/$($streamName)?api-version=2023-01-01"
$uploadResponse = Invoke-RestMethod -Uri $uri -Method "Post" -Body $body -Headers $headers
إشعار
إذا تلقيت خطأ Unable to find type [System.Web.HttpUtility]. ، فقم بتشغيل السطر الأخير في القسم 1 من البرنامج النصي لإصلاحه وتنفيذه. لن يؤدي تنفيذه دون تعليق كجزء من البرنامج النصي إلى حل المشكلة. يجب تنفيذ الأمر بشكل منفصل.
تنفيذ البرنامج النصي، ويجب أن تشاهد استجابة HTTP - 204 . يجب أن تصل البيانات إلى مساحة عمل Log Analytics في غضون بضع دقائق.
إنشاء متغيرات البيئة التالية مع قيم لتطبيق Microsoft Entra الخاص بك. يتم استخدام هذه القيم من قبل DefaultAzureCredential في مكتبة Azure Identity.
AZURE_TENANT_ID
AZURE_CLIENT_ID
AZURE_CLIENT_SECRET
استبدل المتغيرات في نموذج التعليمات البرمجية التالي بقيم من DCR الخاص بك. قد تحتاج أيضا إلى استبدال نموذج البيانات في قسم الخطوة 2 بالبيانات الخاصة بك.
# information needed to send data to the DCR endpoint
endpoint_uri = "https://logs-ingestion-rzmk.eastus2-1.ingest.monitor.azure.com" # logs ingestion endpoint of the DCR
dcr_immutableid = "dcr-00000000000000000000000000000000" # immutableId property of the Data Collection Rule
stream_name = "Custom-MyTableRawData" #name of the stream in the DCR that represents the destination table
# Import required modules
import os
from azure.identity import DefaultAzureCredential
from azure.monitor.ingestion import LogsIngestionClient
from azure.core.exceptions import HttpResponseError
credential = DefaultAzureCredential()
client = LogsIngestionClient(endpoint=endpoint_uri, credential=credential, logging_enable=True)
body = [
{
"Time": "2023-03-12T15:04:48.423211Z",
"Computer": "Computer1",
"AdditionalContext": {
"InstanceName": "user1",
"TimeZone": "Pacific Time",
"Level": 4,
"CounterName": "AppMetric2",
"CounterValue": 35.3
}
},
{
"Time": "2023-03-12T15:04:48.794972Z",
"Computer": "Computer2",
"AdditionalContext": {
"InstanceName": "user2",
"TimeZone": "Central Time",
"Level": 3,
"CounterName": "AppMetric2",
"CounterValue": 43.5
}
}
]
try:
client.upload(rule_id=dcr_immutableid, stream_name=stream_name, logs=body)
except HttpResponseError as e:
print(f"Upload failed: {e}")
قم بتنفيذ التعليمات البرمجية، ويجب أن تصل البيانات إلى مساحة عمل Log Analytics في غضون بضع دقائق.
استكشاف الأخطاء وإصلاحها
يصف هذا القسم حالات الخطأ المختلفة التي قد تتلقاها وكيفية تصحيحها.
البرنامج النصي يرجع رمز الخطأ 403
تأكد من أن لديك الأذونات الصحيحة لتطبيقك إلى DCR. قد تحتاج أيضا إلى الانتظار لمدة تصل إلى 30 دقيقة حتى يتم نشر الأذونات.
يقوم البرنامج النصي بإرجاع رمز الخطأ 413 أو تحذير من TimeoutExpired مع الرسالة ReadyBody_Client الاتصال ionAbort في الاستجابة
الرسالة كبيرة جداً. يبلغ الحد الأقصى لحجم الرسالة حاليا 1 ميغابايت لكل مكالمة.
البرنامج النصي يرجع رمز الخطأ 429
تم تجاوز حدود معدل واجهة برمجة التطبيقات. يتم تعيين الحدود حاليا إلى 500 ميغابايت من البيانات في الدقيقة لكل من البيانات المضغوطة وغير المضغوطة و300000 طلب في الدقيقة. أعِد المحاولة بعد المدة المدرجة في العنوان Retry-After في الاستجابة.
البرنامج النصي يرجع رمز الخطأ 503
تأكد من أن لديك الأذونات الصحيحة لتطبيقك إلى DCR. قد تحتاج أيضا إلى الانتظار لمدة تصل إلى 30 دقيقة حتى يتم نشر الأذونات.
لا تتلقى خطأ، ولكن البيانات لا تظهر في مساحة العمل
قد يستغرق استيعاب البيانات بعض الوقت، خاصة في المرة الأولى التي يتم فيها إرسال البيانات إلى جدول معين. لا ينبغي أن يستغرق الأمر أكثر من 15 دقيقة.
لا يتعرف IntelliSense في Log Analytics على الجدول الجديد
قد يستغرق تحديث ذاكرة التخزين المؤقت التي تقود IntelliSense ما يصل إلى 24 ساعة.