Loading...
Loading...
Compare original and translation side by side
Setup: See tencentcloud authentication. The SDK reads/TENCENTCLOUD_SECRET_ID/TENCENTCLOUD_SECRET_KEYfrom the environment.TENCENTCLOUD_REGIONCompanion skill: Usefor alarm policy / notice group / shield management. This skill is only about searching log content.tencentcloud-cls-alarm
设置说明: 请查看腾讯云认证。SDK会从环境变量中读取、TENCENTCLOUD_SECRET_ID和TENCENTCLOUD_SECRET_KEY。TENCENTCLOUD_REGION配套技能: 使用进行告警策略、通知组、屏蔽规则管理。本技能仅专注于日志内容搜索。tencentcloud-cls-alarm
scripts/cls.pyCLS=$SKILL_DIR/scripts/cls.py
python3 $CLS topics # list topics
python3 $CLS search --topic <topic-id> --query 'level:ERROR' --time 1h
python3 $CLS search --topic <topic-id> --trace-id <uuid>
python3 $CLS search --topic <topic-id> --time 1d \
--query '* | SELECT api_name, count(*) AS cnt GROUP BY api_name ORDER BY cnt DESC LIMIT 20' \
--format json--time30m / 1h / 6h / 1d / 7d--query--lucene| SELECT ... GROUP BY ...Contextscripts/cls.pyCLS=$SKILL_DIR/scripts/cls.py
python3 $CLS topics # 列出所有日志主题
python3 $CLS search --topic <topic-id> --query 'level:ERROR' --time 1h
python3 $CLS search --topic <topic-id> --trace-id <uuid>
python3 $CLS search --topic <topic-id> --time 1d \
--query '* | SELECT api_name, count(*) AS cnt GROUP BY api_name ORDER BY cnt DESC LIMIT 20' \
--format json--time30m / 1h / 6h / 1d / 7d--query--lucene| SELECT ... GROUP BY ...Contextstatus_code:>=500 AND service:"openai"SELECT api_name, COUNT(*) GROUP BY api_namestatus_code:>=500 AND service:"openai"SELECT api_name, COUNT(*) GROUP BY api_namepip install tencentcloud-sdk-pythonpip install tencentcloud-sdk-pythonimport os
import json
from tencentcloud.common import credential
from tencentcloud.cls.v20201016 import cls_client, models
cred = credential.EnvironmentVariableCredential().get_credential()
client = cls_client.ClsClient(cred, os.environ["TENCENTCLOUD_REGION"])import os
import json
from tencentcloud.common import credential
from tencentcloud.cls.v20201016 import cls_client, models
cred = credential.EnvironmentVariableCredential().get_credential()
client = cls_client.ClsClient(cred, os.environ["TENCENTCLOUD_REGION"])req = models.DescribeTopicsRequest()
resp = client.DescribeTopics(req)
for t in resp.Topics:
print(t.TopicId, t.TopicName, t.LogsetId)Tip: ask the user for the topic ID up front. Topic IDs look likeand aren't guessable from a service name.751a7350-dc5d-41c3-a6ca-c178bae05807
req = models.DescribeTopicsRequest()
resp = client.DescribeTopics(req)
for t in resp.Topics:
print(t.TopicId, t.TopicName, t.LogsetId)提示:建议先向用户获取主题ID。主题ID格式类似,无法通过服务名称猜测。751a7350-dc5d-41c3-a6ca-c178bae05807
import time
req = models.SearchLogRequest()
req.TopicId = "<topic-id>"
req.From = int((time.time() - 3600) * 1000) # 1 hour ago, ms epoch
req.To = int(time.time() * 1000)
req.Query = 'status_code:>=500 AND service:"openai"'
req.Limit = 100
req.Sort = "desc"
req.SyntaxRule = 1 # 1 = CQL, 0 = Lucene
resp = client.SearchLog(req)
for line in resp.Results:
fields = {f.Key: f.Value for f in line.LogJson and []} # see below
print(line.Time, line.PkgLogId, fields.get("status_code"), fields.get("api_name"))CLS hands youwithResultsas a JSON string per record. Decode withLogJsonto get a flat dict of all the indexed fields the topic stores.json.loads(line.LogJson)
import time
req = models.SearchLogRequest()
req.TopicId = "<topic-id>"
req.From = int((time.time() - 3600) * 1000) # 1小时前,毫秒级时间戳
req.To = int(time.time() * 1000)
req.Query = 'status_code:>=500 AND service:"openai"'
req.Limit = 100
req.Sort = "desc"
req.SyntaxRule = 1 # 1 = CQL语法,0 = Lucene语法
resp = client.SearchLog(req)
for line in resp.Results:
fields = {f.Key: f.Value for f in line.LogJson and []} # 见下方说明
print(line.Time, line.PkgLogId, fields.get("status_code"), fields.get("api_name"))CLS返回的中,每条记录的Results是一个JSON字符串。使用LogJson解码后可得到该主题存储的所有索引字段的扁平字典。json.loads(line.LogJson)
TRACE_ID = "34341776-0835-422a-956d-ac8d5b404db1"
TOPICS = {
"trace": "<trace-topic-id>",
"api-usages": "<api-usages-topic-id>",
}
for label, topic_id in TOPICS.items():
req = models.SearchLogRequest()
req.TopicId = topic_id
req.From = int((time.time() - 86400) * 1000)
req.To = int(time.time() * 1000)
req.Query = f'trace_id:"{TRACE_ID}"'
req.Limit = 100
req.SyntaxRule = 1
resp = client.SearchLog(req)
print(f"--- {label}: {len(resp.Results)} records ---")
for line in resp.Results:
print(line.Time, line.LogJson)TRACE_ID = "34341776-0835-422a-956d-ac8d5b404db1"
TOPICS = {
"trace": "<trace-topic-id>",
"api-usages": "<api-usages-topic-id>",
}
for label, topic_id in TOPICS.items():
req = models.SearchLogRequest()
req.TopicId = topic_id
req.From = int((time.time() - 86400) * 1000)
req.To = int(time.time() * 1000)
req.Query = f'trace_id:"{TRACE_ID}"'
req.Limit = 100
req.SyntaxRule = 1
resp = client.SearchLog(req)
print(f"--- {label}: {len(resp.Results)} 条记录 ---")
for line in resp.Results:
print(line.Time, line.LogJson)| <SQL>req.Query = '* | SELECT api_name, count(*) AS cnt GROUP BY api_name ORDER BY cnt DESC LIMIT 20'
req.SyntaxRule = 1
resp = client.SearchLog(req)| <SQL>req.Query = '* | SELECT api_name, count(*) AS cnt GROUP BY api_name ORDER BY cnt DESC LIMIT 20'
req.SyntaxRule = 1
resp = client.SearchLog(req)||undefinedundefinedundefinedundefinedundefinedundefined| Pattern | Meaning |
|---|---|
| exact match |
| range |
| range with bounds |
| quoted phrase (use for values containing spaces) |
| negation |
| conjunction |
| grouping |
| `* | SELECT ... GROUP BY ...` |
| 语法模式 | 含义 |
|---|---|
| 精确匹配 |
| 范围匹配 |
| 带边界的范围匹配 |
| 带引号的短语匹配(适用于包含空格的值) |
| 否定匹配 |
| 逻辑与匹配 |
| 分组逻辑或匹配 |
| `* | SELECT ... GROUP BY ...` |
Contextall_records = []
context = None
while True:
req = models.SearchLogRequest()
req.TopicId = topic_id
req.From = ...
req.To = ...
req.Query = '...'
req.Limit = 1000
req.SyntaxRule = 1
if context:
req.Context = context
resp = client.SearchLog(req)
all_records.extend(resp.Results)
context = resp.Context
if not context or len(resp.Results) < 1000:
breakContextall_records = []
context = None
while True:
req = models.SearchLogRequest()
req.TopicId = topic_id
req.From = ...
req.To = ...
req.Query = '...'
req.Limit = 1000
req.SyntaxRule = 1
if context:
req.Context = context
resp = client.SearchLog(req)
all_records.extend(resp.Results)
context = resp.Context
if not context or len(resp.Results) < 1000:
break| Symptom | Likely cause |
|---|---|
| Quote phrases that contain |
| Concurrent |
| CLS service is suspended for billing — check the console |
Empty | Time range is wrong ( |
| 症状 | 可能原因 |
|---|---|
| 对包含 |
| 并发 |
| CLS服务因欠费暂停——请查看控制台 |
控制台可见日志但 | 时间范围错误( |