如何在Azure SpeechRecognizer中携带客户端ID统计服务使用时长?
在Azure Speech Services中携带客户端ID并统计使用时长
要实现按客户端ID统计语音服务使用时长,你可以通过以下步骤操作:
一、在代码中添加客户端ID标识
Azure Speech SDK允许通过SpeechConfig设置自定义属性,将客户端ID嵌入到服务请求中,后续可通过日志追踪该标识。修改你的代码如下:
const token = await this.GetToken(); const speechConfig = speechsdk.SpeechConfig.fromSubscription(token.Token, token.Region); // 添加客户端ID到SpeechConfig,使用自定义属性(便于日志直接提取) speechConfig.setProperty("clientId", token.ClientId); // 也可选择追加到UserAgent字段,同样能被日志捕获 // speechConfig.setProperty(speechsdk.PropertyId.SpeechServiceConnection_UserAgent, `YourAppName/${token.ClientId}`); const audioConfig = speechsdk.AudioConfig.fromDefaultMicrophoneInput(); this.speechRecognizer = new speechsdk.SpeechRecognizer(speechConfig, audioConfig); this.speechRecognizer.startContinuousRecognitionAsync(); this.speechRecognizer.recognizing = (s, e) => { console.log(`RECOGNIZING: Text=${e.result.text}`); };
二、在Azure门户启用诊断日志
要统计使用时长,需先开启Speech资源的诊断日志:
- 登录Azure门户,找到你的Speech Services资源
- 进入左侧菜单的监控 > 诊断设置
- 点击添加诊断设置,勾选
SpeechServicesMetrics和SpeechServicesLogs,选择发送到Log Analytics工作区(需提前创建) - 保存设置,等待日志数据流入(通常需要几分钟)
三、查询并统计各客户端的使用时长
在Log Analytics中执行Kusto查询,按clientId分组计算总使用秒数:
如果使用自定义属性方式:
SpeechServicesMetrics | where OperationName == "Speech.Recognition" | extend clientId = tostring(parse_json(CustomDimensions).clientId) | summarize TotalSeconds = sum(DurationMs)/1000 by clientId | order by TotalSeconds desc
如果使用UserAgent方式,查询语句调整为:
SpeechServicesMetrics | where OperationName == "Speech.Recognition" | extend clientId = extract(@"YourAppName/([^ ]+)", 1, UserAgent) | summarize TotalSeconds = sum(DurationMs)/1000 by clientId | order by TotalSeconds desc
这样就能得到每个客户端ID对应的语音服务总使用时长。
内容的提问来源于stack exchange,提问作者Qba
相关产品推荐
相关产品推荐

