Add full monorepo: virtual-banker, backend, frontend, docs, scripts, deployment
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
73
backend/observability/metrics.go
Normal file
73
backend/observability/metrics.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package observability
|
||||
|
||||
import (
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Metrics collects system metrics
|
||||
type Metrics struct {
|
||||
SessionCreations int64
|
||||
ActiveSessions int64
|
||||
MessagesProcessed int64
|
||||
ASRLatency int64 // microseconds
|
||||
TTSLatency int64 // microseconds
|
||||
LLMLatency int64 // microseconds
|
||||
Errors int64
|
||||
}
|
||||
|
||||
var globalMetrics = &Metrics{}
|
||||
|
||||
// GetMetrics returns current metrics
|
||||
func GetMetrics() *Metrics {
|
||||
return &Metrics{
|
||||
SessionCreations: atomic.LoadInt64(&globalMetrics.SessionCreations),
|
||||
ActiveSessions: atomic.LoadInt64(&globalMetrics.ActiveSessions),
|
||||
MessagesProcessed: atomic.LoadInt64(&globalMetrics.MessagesProcessed),
|
||||
ASRLatency: atomic.LoadInt64(&globalMetrics.ASRLatency),
|
||||
TTSLatency: atomic.LoadInt64(&globalMetrics.TTSLatency),
|
||||
LLMLatency: atomic.LoadInt64(&globalMetrics.LLMLatency),
|
||||
Errors: atomic.LoadInt64(&globalMetrics.Errors),
|
||||
}
|
||||
}
|
||||
|
||||
// IncrementSessionCreations increments session creation count
|
||||
func IncrementSessionCreations() {
|
||||
atomic.AddInt64(&globalMetrics.SessionCreations, 1)
|
||||
}
|
||||
|
||||
// IncrementActiveSessions increments active session count
|
||||
func IncrementActiveSessions() {
|
||||
atomic.AddInt64(&globalMetrics.ActiveSessions, 1)
|
||||
}
|
||||
|
||||
// DecrementActiveSessions decrements active session count
|
||||
func DecrementActiveSessions() {
|
||||
atomic.AddInt64(&globalMetrics.ActiveSessions, -1)
|
||||
}
|
||||
|
||||
// IncrementMessagesProcessed increments message count
|
||||
func IncrementMessagesProcessed() {
|
||||
atomic.AddInt64(&globalMetrics.MessagesProcessed, 1)
|
||||
}
|
||||
|
||||
// RecordASRLatency records ASR latency
|
||||
func RecordASRLatency(duration time.Duration) {
|
||||
atomic.StoreInt64(&globalMetrics.ASRLatency, duration.Microseconds())
|
||||
}
|
||||
|
||||
// RecordTTSLatency records TTS latency
|
||||
func RecordTTSLatency(duration time.Duration) {
|
||||
atomic.StoreInt64(&globalMetrics.TTSLatency, duration.Microseconds())
|
||||
}
|
||||
|
||||
// RecordLLMLatency records LLM latency
|
||||
func RecordLLMLatency(duration time.Duration) {
|
||||
atomic.StoreInt64(&globalMetrics.LLMLatency, duration.Microseconds())
|
||||
}
|
||||
|
||||
// IncrementErrors increments error count
|
||||
func IncrementErrors() {
|
||||
atomic.AddInt64(&globalMetrics.Errors, 1)
|
||||
}
|
||||
|
||||
48
backend/observability/tracing.go
Normal file
48
backend/observability/tracing.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package observability
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Tracer provides distributed tracing
|
||||
type Tracer interface {
|
||||
StartSpan(ctx context.Context, name string) (context.Context, Span)
|
||||
}
|
||||
|
||||
// Span represents a tracing span
|
||||
type Span interface {
|
||||
End()
|
||||
SetAttribute(key string, value interface{})
|
||||
SetError(err error)
|
||||
}
|
||||
|
||||
// MockTracer is a mock tracer for development
|
||||
type MockTracer struct{}
|
||||
|
||||
// StartSpan starts a new span
|
||||
func (t *MockTracer) StartSpan(ctx context.Context, name string) (context.Context, Span) {
|
||||
return ctx, &MockSpan{}
|
||||
}
|
||||
|
||||
// MockSpan is a mock span
|
||||
type MockSpan struct{}
|
||||
|
||||
// End ends the span
|
||||
func (m *MockSpan) End() {}
|
||||
|
||||
// SetAttribute sets an attribute
|
||||
func (m *MockSpan) SetAttribute(key string, value interface{}) {}
|
||||
|
||||
// SetError sets an error
|
||||
func (m *MockSpan) SetError(err error) {}
|
||||
|
||||
// TraceConversation traces a conversation turn
|
||||
func TraceConversation(ctx context.Context, tracer Tracer, sessionID, userID string, input string) (context.Context, Span) {
|
||||
ctx, span := tracer.StartSpan(ctx, "conversation.turn")
|
||||
span.SetAttribute("session_id", sessionID)
|
||||
span.SetAttribute("user_id", userID)
|
||||
span.SetAttribute("input_length", len(input))
|
||||
return ctx, span
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user