feat: add debug log control and optimize error response and cors log

- add DebugLog global variable controlled by BYTEDANCE_TTS_DEBUG env
- enable debug logs for cors, tts upstream, response parsing etc when debug mode is on
- update tts error response to use standardized json error format
- add dashboard and metrics path to cache-control exempt list
This commit is contained in:
sun
2026-08-15 19:03:37 +08:00
parent 1455e4bf80
commit 155fab6b4b
7 changed files with 39 additions and 11 deletions
+16 -5
View File
@@ -10,6 +10,7 @@ import (
"log"
"time"
"github.com/volcano-tts/tts-api/common"
"github.com/volcano-tts/tts-api/dto"
)
@@ -40,7 +41,9 @@ func ParseStream(body io.Reader, started time.Time) (*ParsedStream, error) {
}
var resp dto.V3TTSResponse
if err := json.Unmarshal(line, &resp); err != nil {
log.Printf("volcano: 解析响应行失败: %v, line=%q", err, truncateForLog(line, 200))
if common.DebugLog {
log.Printf("volcano: 解析响应行失败: %v, line=%q", err, truncateForLog(line, 200))
}
continue
}
@@ -56,7 +59,9 @@ func ParseStream(body io.Reader, started time.Time) (*ParsedStream, error) {
if resp.Usage != nil {
out.TextWords = resp.Usage.TextWords
out.HasUsage = true
log.Printf("TTS 合成结束, usage: text_words=%d", out.TextWords)
if common.DebugLog {
log.Printf("TTS 合成结束, usage: text_words=%d", out.TextWords)
}
}
for scanner.Scan() {
}
@@ -66,9 +71,13 @@ func ParseStream(body io.Reader, started time.Time) (*ParsedStream, error) {
// 事件分发:显式匹配已知事件,绝不把未知事件当作音频。
switch resp.Event {
case "TTSSentenceStart":
log.Printf("Sentence start: sequence=%d, sentence=%s", resp.Sequence, resp.Sentence)
if common.DebugLog {
log.Printf("Sentence start: sequence=%d, sentence=%s", resp.Sequence, resp.Sentence)
}
case "TTSSentenceEnd":
log.Printf("Sentence end: sequence=%d", resp.Sequence)
if common.DebugLog {
log.Printf("Sentence end: sequence=%d", resp.Sequence)
}
case "TTSSubtitle":
if resp.Data != "" {
out.Subtitles = append(out.Subtitles, dto.SubtitleEntry{
@@ -98,7 +107,9 @@ func ParseStream(body io.Reader, started time.Time) (*ParsedStream, error) {
case "":
// 传输帧,跳过
default:
log.Printf("volcano: 忽略未识别事件 event=%q sequence=%d", resp.Event, resp.Sequence)
if common.DebugLog {
log.Printf("volcano: 忽略未识别事件 event=%q sequence=%d", resp.Event, resp.Sequence)
}
}
}
+8 -2
View File
@@ -8,6 +8,7 @@ import (
"log"
"time"
"github.com/volcano-tts/tts-api/common"
"github.com/volcano-tts/tts-api/dto"
)
@@ -79,8 +80,10 @@ func Synthesis(
"X-Control-Require-Usage-Tokens-Return": "*",
}
log.Printf("TTS upstream: resource_id=%s speaker=%s model=%q format=%s sample_rate=%d speech_rate=%d additions=%q",
opts.ResourceID, opts.Speaker, opts.Model, opts.Format, opts.SampleRate, opts.SpeechRate, extractAdditionsForLog(body))
if common.DebugLog {
log.Printf("TTS upstream: resource_id=%s speaker=%s model=%q format=%s sample_rate=%d speech_rate=%d additions=%q",
opts.ResourceID, opts.Speaker, opts.Model, opts.Format, opts.SampleRate, opts.SpeechRate, extractAdditionsForLog(body))
}
resp, err := client.PostStream(ctx, "https://openspeech.bytedance.com/api/v3/tts/unidirectional", headers, body)
if err != nil {
@@ -129,6 +132,9 @@ func Synthesis(
}
mtr.UpstreamFinished(opts.Speaker, opts.Model, opts.Format, "ok", duration, parsed.FirstChunk, parsed.Chunks, len(finalData), 0)
log.Printf("TTS 合成成功 - 音色=%s 格式=%s 文本=%d字 音频=%d字节 分片=%d 耗时=%v",
opts.Speaker, clientFormat, len(text), len(finalData), parsed.Chunks, duration)
return &dto.SynthesisResult{
AudioData: finalData,
Format: finalFormat,