mirror of
https://github.com/mudler/LocalAI.git
synced 2026-05-30 11:36:31 -04:00
Add a routing middleware stack and a cloud-proxy backend. * cloud-proxy: a Go gRPC backend that forwards OpenAI- and Anthropic-shaped chat requests to upstream providers, with an optional translate mode (OpenAI request -> Anthropic /v1/messages -> OpenAI response) and full tool-calling support. * routing: admission control, content-aware model routing (embedding cache + classifier + rerank + Arch-Router score), PII detection/redaction (regex + NER) with streaming filter and OpenAI/Anthropic adapters, and a per-user/per-key billing recorder backed by GORM or in-memory storage. * middleware: UsageMiddleware records usage via the billing recorder, plus admission, route-model, usage-stamp and trace middlewares. * observability: BackendTrace ring buffer stores full request bodies (capped), MITM proxy emits structured trace events, and router classifier decisions surface at /api/router/decide. * gallery: Arch-Router-1.5B (Q4_K_M and Q8_0). * UI: cloud-proxy model-editor fields, classifier system-prompt and score-normalization config, and a Traces page rendering request bodies. Assisted-by: claude-code:claude-opus-4-7 [Read] [Edit] [Bash] Signed-off-by: Richard Palethorpe <io@richiejp.com>
65 lines
2.1 KiB
Go
65 lines
2.1 KiB
Go
package monitoring
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/mudler/xlog"
|
|
"go.opentelemetry.io/otel"
|
|
"go.opentelemetry.io/otel/attribute"
|
|
"go.opentelemetry.io/otel/exporters/prometheus"
|
|
"go.opentelemetry.io/otel/metric"
|
|
metricApi "go.opentelemetry.io/otel/sdk/metric"
|
|
)
|
|
|
|
type LocalAIMetricsService struct {
|
|
Meter metric.Meter
|
|
Provider *metricApi.MeterProvider
|
|
ApiTimeMetric metric.Float64Histogram
|
|
}
|
|
|
|
func (m *LocalAIMetricsService) ObserveAPICall(method string, path string, duration float64) {
|
|
opts := metric.WithAttributes(
|
|
attribute.String("method", method),
|
|
attribute.String("path", path),
|
|
)
|
|
m.ApiTimeMetric.Record(context.Background(), duration, opts)
|
|
}
|
|
|
|
// setupOTelSDK bootstraps the OpenTelemetry pipeline.
|
|
// If it does not return an error, make sure to call shutdown for proper cleanup.
|
|
func NewLocalAIMetricsService() (*LocalAIMetricsService, error) {
|
|
exporter, err := prometheus.New()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
provider := metricApi.NewMeterProvider(metricApi.WithReader(exporter))
|
|
// Share the provider with the OTel global so packages outside this
|
|
// service (e.g., core/services/routing/billing) see the same Prom
|
|
// exporter when they call otel.Meter(...). Without this, the billing
|
|
// counters would route to the no-op global provider and never reach
|
|
// /metrics — which is exactly the silent-billing-loss class of bug
|
|
// the routing module is designed to surface.
|
|
otel.SetMeterProvider(provider)
|
|
meter := provider.Meter("github.com/mudler/LocalAI")
|
|
|
|
apiTimeMetric, err := meter.Float64Histogram("api_call", metric.WithDescription("api calls"))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &LocalAIMetricsService{
|
|
Meter: meter,
|
|
Provider: provider,
|
|
ApiTimeMetric: apiTimeMetric,
|
|
}, nil
|
|
}
|
|
|
|
func (lams LocalAIMetricsService) Shutdown() error {
|
|
// TODO: Not sure how to actually do this:
|
|
//// setupOTelSDK bootstraps the OpenTelemetry pipeline.
|
|
//// If it does not return an error, make sure to call shutdown for proper cleanup.
|
|
|
|
xlog.Warn("LocalAIMetricsService Shutdown called, but OTelSDK proper shutdown not yet implemented?")
|
|
return nil
|
|
}
|