Cloud Monitor (CMS) ServiceTaskController API'si (sürüm 2024-03-30) aracılığıyla Python uygulamaları için dinamik kod araştırma görevlerini yönetir. Beş araştırma türü (LOG, SNAPSHOT, METRIC, SPAN ve SPAN_TAG) desteklenir ve yakalama sonuçları Simple Log Service (SLS) içinde saklanır.
Ön Koşullar
Alibaba Cloud CLI — Bu konudaki
cms2veslskomutları Alibaba Cloud CLI (komut satırı arayüzü) gerektirir.Workspace ID — Application Real-Time Monitoring Service (ARMS) workspace kimliği; örneğin
default-cms-xxx-cn-hangzhou.Service ID — Uygulama veya hizmet kimliği; örneğin
ggxw4lnjuz@f2fd3a6265a254a052afb.SLS project — Uygulama için SLS projesi; örneğin
proj-xtrace-...-cn-hangzhou.Region — CLI'ın varsayılan bölgesine güvenmemek amacıyla hem CMS hem de SLS komutları için
--regionparametresini açıkça belirtin.
Bu konu yalnızca Python uygulamalarını (Araştırma görevleri) kapsar.
API İşlemleri
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Yol Parametreleri
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ortak CLI Parametreleri
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CreateServiceTask
Bir Live-Debug araştırma görevi oluşturur.
İstek
POST /serviceTask/{workspace}/{serviceId}/task
Content-Type: application/json
Gövde Parametreleri
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CLI Örneği
aliyun cms2 apm service-task create \
--workspace <workspace> --service-id <serviceId> \
--type <taskType> --ip '<targetIp>' \
--task-config '<taskConfigJson>' \
--region <regionId> -o json
Tüm örneklerle eşleşmek için
--ipparametresini*olarak ayarlayın.Ham JSON nesnesini doğrudan
--task-configparametresine iletin. JSON'u manuel olarak kaçış karakterleriyle düzenlemeniz gerekmez.
Yanıt
{
"success": true,
"data": {
"requestId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"taskId": "yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy"
}
}
|
|
|
|
|
|
|
|
|
HTTP Gövdesi Örneği (CLI Serileştirmesinden Sonra)
{
"type": "live_debug_log_probe",
"ip": "*",
"taskConfig": "{\"probeType\":\"LOG\",\"language\":\"python\",\"target\":{\"typeName\":\"app.service.order\",\"methodName\":\"OrderService.create_order\",\"location\":\"exit\",\"instanceIds\":[\"*\"]},\"action\":{\"type\":\"LOG\",\"template\":\"id={order_id}\"},\"ttl\":\"30m\",\"captureCount\":100}"
}
HTTP gövdesinde taskConfig kaçış karakterleri içeren bir JSON dizesidir. Parametreyi CLI --task-config üzerinden iletirken kaçış karakteri içermeyen JSON nesnesini iletin. Serileştirmeyi CLI halleder.
ListServiceTask
Live-Debug araştırma görevlerini listeler.
İstek
GET /serviceTask/{workspace}/{serviceId}/tasks?type={taskType}&maxResults={n}
Sorgu Parametreleri
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Yanıt Alanları
CLI, bir data.serviceTasks[] dizisi çıktılar. Her öğe aşağıdaki alanları içerir:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CLI Örneği
aliyun cms2 apm service-task list \
--workspace <workspace> --service-id <serviceId> \
--type <taskType> --max-results 100 \
--region <regionId> -o json
GetServiceTask
Tek bir Live-Debug araştırma görevinin ayrıntılarını sorgular.
İstek
GET /serviceTask/{workspace}/{serviceId}/task/{taskId}?type={taskType}
Sorgu Parametreleri
|
|
|
|
|
|
|
|
|
|
Yanıt
CLI, bir data.serviceTask nesnesi çıktılar. Alanlar, Liste yanıtındaki tek bir öğe ile aynıdır.
CLI Örneği
aliyun cms2 apm service-task get \
--workspace <workspace> --service-id <serviceId> \
--task-id <taskId> --type <taskType> \
--region <regionId> -o json
DeleteServiceTask
Bir Live-Debug araştırma görevini siler. Silme işleminden sonra sunucu görevi kaldırır ve değişikliği yapılandırma sunucusuyla senkronize eder. Aracı tarafındaki ilgili araştırma etkinliğini yitirir.
Dağıtılmış bir araştırmayı devre dışı bırakmak için ilgili görevi silin.
İstek
DELETE /serviceTask/{workspace}/{serviceId}/task/{taskId}?type={taskType}
Sorgu Parametreleri
|
|
|
|
|
|
|
|
|
|
CLI Örneği
aliyun cms2 apm service-task delete \
--workspace <workspace> --service-id <serviceId> \
--task-id <taskId> --type <taskType> \
--region <regionId> -o json
Toplu Silme Örneği
Bir hizmet için tüm araştırma görevlerini silmek amacıyla beş araştırma türünün her biri için görevleri listeleyin ve silin:
WS=<workspace>; SVC=<serviceId>; REGION=<regionId>
for t in live_debug_log_probe live_debug_snapshot_probe \
live_debug_metric_probe live_debug_span_probe live_debug_span_tag_probe; do
aliyun cms2 apm service-task list \
--workspace "$WS" --service-id "$SVC" --type "$t" --region "$REGION" -o json |
python3 -c 'import sys,json; [print(t["taskId"]) for t in (json.load(sys.stdin)["data"].get("serviceTasks") or [])]' |
while read -r id; do
aliyun cms2 apm service-task delete \
--workspace "$WS" --service-id "$SVC" \
--task-id "$id" --type "$t" --region "$REGION" -o json
done
done
Yakalama Sonuçlarını Sorgulama (SLS)
Yakalama durumu ve sonuçları Simple Log Service (SLS) üzerine yazılır ve CMS Get işlemi tarafından döndürülmez.
CLI Örneği
Son 10 dakikadaki verileri sorgular. Oluşturma işlemi tarafından döndürülen görev kimliği ile <taskId> değerini değiştirin.
FROM=$(( $(date +%s) - 600 )); TO=$(date +%s)
# Task status (installation status, funnel metrics)
aliyun sls get-logs-v2 --region <regionId> --accept-encoding gzip \
--project <slsProject> --logstore logstore-apm-logs \
--from "$FROM" --to "$TO" \
--query "* and \"<taskId>\" | SELECT content FROM log WHERE json_extract_scalar(attributes, '\$[\"livedebug.report_type\"]') = 'status'"
# Capture results (actual captured data)
aliyun sls get-logs-v2 --region <regionId> --accept-encoding gzip \
--project <slsProject> --logstore logstore-apm-logs \
--from "$FROM" --to "$TO" \
--query "* and \"<taskId>\" | SELECT content FROM log WHERE json_extract_scalar(attributes, '\$[\"livedebug.report_type\"]') != 'status'"
Sorgu Parametreleri
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Sorgu Türleri
|
|
|
|
|
|
|
|
|
|
|
|
SLS projeleri bölge bazında izole edilir. Yanlış bir region parametresi ProjectNotExist hatasına neden olur.
Görev Türü Numaralandırması
Aşağıdaki tabloda Python uygulamaları için desteklenen beş araştırma türü listelenmiştir. Adlandırma kuralı şöyledir: live_debug_ + küçük harfli araştırma anlamı + _probe.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Araştırma taskConfig Yapısı
--task-config parametresine geçirilen düz nesne:
{
"probeType": "LOG|SNAPSHOT|METRIC|SPAN|SPAN_TAG",
"language": "python",
"target": { },
"action": { },
"trigger": { },
"rateLimit": { },
"ttl": "1h",
"captureCount": 100,
"enabled": true
}
Üst Düzey Alanlar
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
target — Hedefi Konumlandırma
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Satır düzeyindeki araştırmalar öncelikle sourceFile + location:"line:N" kombinasyonuna dayanabilir.
trigger — Tetikleyici Koşulu (İsteğe Bağlı)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Koşul örnekleri: amount > 10000 veya @return is None or amount > 10000
İfade kuralları:
Parametre adlarını, yerel değişkenleri veya modül düzeyindeki global değişkenleri doğrudan kullanın.
exitveyaexceptionkonumlarında@return,@duration(milisaniye) veya@exceptionkullanın.OGNL,
args[0],returnValueveyadurationMskullanmayın.Koşul
@returnveya@exceptiondeğerlerine başvuruyorsa araştırmalocationdeğeriexitveyaexceptionolmalıdır.
rateLimit — Hız Sınırlama (İsteğe Bağlı)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Varsayılan hız sınırları: LOG, METRIC, SPAN ve SPAN_TAG araştırmaları saniyede yaklaşık 5.000 çalıştırmaya izin verir. SNAPSHOT araştırmaları saniyede yaklaşık 1 çalıştırmaya izin verir. Satır düzeyindeki araştırmalar için saniyede yaklaşık 100 çalıştırma ek bir genel koruma sınırı bulunmaktadır.
Türe Göre Araştırma Eylemi
LOG (taskType: live_debug_log_probe)
Nesne grafiği serileştirmesi olmadan hedef noktada dinamik günlükler çıktılar.
action alanları
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LOG araştırmaları yalnızca şablonu işler ve capture boyutunu yok sayar. Parametreleri, dönüş değerlerini veya yığın izlerini yakalamak için SNAPSHOT araştırması kullanın.
Örnek
{
"probeType": "LOG",
"language": "python",
"target": {
"typeName": "app.service.order",
"methodName": "OrderService.create_order",
"location": "exit",
"instanceIds": ["*"]
},
"action": {
"type": "LOG",
"template": "create_order id={order_id} amount={amount} ret={@return} cost={@duration}ms"
},
"ttl": "30m",
"captureCount": 100
}
SNAPSHOT (taskType: live_debug_snapshot_probe)
Yöntem anlık görüntülerini yakalar ve nesne grafiğini serileştirir.
action alanları
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
capture enum değerleri
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
captureConfig
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
captureExpressions
Sözdizimi trigger.condition ve LOG şablonlarıyla aynıdır (sınırlı eval). Sonuçlar context.evaluatedExpressions alanına yazılır. Her öğe aşağıdaki biçime sahiptir:
{"name": "order_id", "type": "...", "value": "...", "notCapturedReason": null}
Değerlendirme başarısız olursa value null olarak ayarlanır ve notCapturedReason nedeni kaydeder. Bu durum diğer boyutları etkilemez. captureExpressions ve capture birbirinden bağımsızdır.
Örnek
{
"probeType": "SNAPSHOT",
"language": "python",
"target": {
"typeName": "app.service.order",
"methodName": "OrderService.create_order",
"location": "exit",
"instanceIds": ["*"]
},
"trigger": {
"condition": "@return is None or amount > 10000"
},
"action": {
"type": "SNAPSHOT",
"capture": ["ARGS"],
"captureExpressions": ["order_id", "amount * count", "self.user_id", "@return"],
"captureConfig": {
"maxDepth": 3,
"maxCollectionSize": 100,
"maxStringLength": 1024
}
},
"ttl": "30m",
"captureCount": 50
}
METRIC (taskType: live_debug_metric_probe)
Hedef noktada özel ölçümler yayımlar.
action alanları
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Örnek
{
"probeType": "METRIC",
"language": "python",
"target": {
"typeName": "app.service.order",
"methodName": "OrderService.create_order",
"location": "exit",
"instanceIds": ["*"]
},
"action": {
"type": "METRIC",
"metricName": "livedebug.order.amount",
"metricType": "HISTOGRAM",
"valueExpression": "amount",
"tags": {
"is_vip": "str(user_id == 'vip')"
}
},
"ttl": "1h"
}
SPAN (taskType: live_debug_span_probe)
Hedef fonksiyon çalıştırması için bir OpenTelemetry span'i oluşturur. Bir istisna oluşursa span'i ERROR olarak işaretler ve bir istisna olayı kaydeder. Yalnızca fonksiyon düzeyinde.
action alanları
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Örnek
{
"probeType": "SPAN",
"language": "python",
"target": {
"typeName": "app.service.order",
"methodName": "OrderService.create_order",
"location": "enter",
"instanceIds": ["*"]
},
"action": {
"type": "SPAN",
"spanName": "dyn.create_order",
"spanTags": {
"order.id": "str(order_id)",
"order.amount": "str(amount)"
}
},
"ttl": "1h"
}
SPAN_TAG (taskType: live_debug_span_tag_probe)
Geçerli aktif span'e nitelikler ekler. Aktif bir span yoksa sessizce atlar.
action alanları
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Örnek
{
"probeType": "SPAN_TAG",
"language": "python",
"target": {
"typeName": "app.service.order",
"methodName": "OrderService.create_order",
"location": "exit",
"instanceIds": ["*"]
},
"action": {
"type": "SPAN_TAG",
"tags": [
{"key": "order.id", "value": "str(order_id)"},
{"key": "order.result", "value": "str(@return)"}
]
},
"ttl": "1h"
}
Hatalar ve Sorun Giderme
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Tam İş Akışı Örneği (LOG Araştırması)
# 0. Environment variables
REGION=cn-hangzhou
SLS_PROJECT=proj-xtrace-xxxxxxxxxxxxxxxxxxxxxx-cn-hangzhou
WORKSPACE=default-cms-xxxxxxxxxxxxxxxxxx-cn-hangzhou
SERVICE_ID='ggxw4lnjuz@f2fd3a6265a254a052afb'
# 1. Create a task
RESP=$(aliyun cms2 apm service-task create \
--workspace "$WORKSPACE" --service-id "$SERVICE_ID" \
--type live_debug_log_probe --ip '*' \
--task-config '{"probeType":"LOG","language":"python","target":{"typeName":"app.service.order","methodName":"OrderService.create_order","location":"exit","instanceIds":["*"]},"action":{"type":"LOG","template":"id={order_id} ret={@return}"},"ttl":"30m","captureCount":50}' \
--region "$REGION" -o json)
echo "$RESP"
TASK_ID=$(echo "$RESP" | python3 -c 'import sys,json; print(json.load(sys.stdin)["data"]["taskId"])')
# 2. Verify the task
aliyun cms2 apm service-task get \
--workspace "$WORKSPACE" --service-id "$SERVICE_ID" \
--task-id "$TASK_ID" --type live_debug_log_probe \
--region "$REGION" -o json
# 3. Generate traffic to the instrumented endpoint and query results (last 10 minutes)
FROM=$(( $(date +%s) - 600 )); TO=$(date +%s)
aliyun sls get-logs-v2 --region "$REGION" --accept-encoding gzip \
--project "$SLS_PROJECT" --logstore logstore-apm-logs \
--from "$FROM" --to "$TO" \
--query "* and \"$TASK_ID\" | SELECT content FROM log WHERE json_extract_scalar(attributes, '\$[\"livedebug.report_type\"]') != 'status'"
# 5. Delete the task
aliyun cms2 apm service-task delete \
--workspace "$WORKSPACE" --service-id "$SERVICE_ID" \
--task-id "$TASK_ID" --type live_debug_log_probe \
--region "$REGION" -o json