better handling

This commit is contained in:
2026-04-05 09:47:25 +02:00
parent 0fd17e4944
commit 988ea1d919
5 changed files with 357 additions and 282 deletions
+21 -277
View File
@@ -2,14 +2,10 @@ package main
import (
"context"
"encoding/json"
"io"
"log"
"net"
"net/http"
"strconv"
"strings"
"time"
"git.tornberg.me/go-otel/internal/collector"
"git.tornberg.me/go-otel/internal/storage"
@@ -21,8 +17,8 @@ import (
metricsgrpc "go.opentelemetry.io/proto/otlp/collector/metrics/v1"
tracegrpc "go.opentelemetry.io/proto/otlp/collector/trace/v1"
"google.golang.org/grpc"
"google.golang.org/grpc/status"
"google.golang.org/grpc/stats"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
)
@@ -32,9 +28,10 @@ func loggingInterceptor(ctx context.Context, req interface{}, info *grpc.UnarySe
if err != nil {
st, _ := status.FromError(err)
log.Printf("gRPC ERROR [%s] code=%s message=%s", info.FullMethod, st.Code(), st.Message())
} else {
log.Printf("gRPC SUCCESS [%s]", info.FullMethod)
}
//else {
// log.Printf("gRPC SUCCESS [%s]", info.FullMethod)
//}
return resp, err
}
@@ -55,7 +52,9 @@ func (h *connStatsHandler) HandleConn(ctx context.Context, s stats.ConnStats) {
}
}
func (h *connStatsHandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo) context.Context { return ctx }
func (h *connStatsHandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo) context.Context {
return ctx
}
func (h *connStatsHandler) HandleRPC(ctx context.Context, s stats.RPCStats) {}
type traceService struct {
@@ -181,23 +180,25 @@ func main() {
// OTLP HTTP mux (standard OTel port 4318)
otelMux := http.NewServeMux()
otelMux.HandleFunc("/v1/traces", handleTraces(coll))
otelMux.HandleFunc("/v1/metrics", handleMetrics(coll))
otelMux.HandleFunc("/v1/logs", handleLogs(coll))
otelMux.HandleFunc("/v1/traces", coll.HandleTraces)
otelMux.HandleFunc("/v1/metrics", coll.HandleMetrics)
otelMux.HandleFunc("/v1/logs", coll.HandleLogs)
otelMux.HandleFunc("/health", coll.HandleHealth)
// UI and API mux (port 8080)
uiMux := http.NewServeMux()
// Also register OTLP endpoints on 8080 for backwards compatibility
uiMux.HandleFunc("/v1/traces", handleTraces(coll))
uiMux.HandleFunc("/v1/metrics", handleMetrics(coll))
uiMux.HandleFunc("/v1/logs", handleLogs(coll))
uiMux.HandleFunc("/v1/traces", coll.HandleTraces)
uiMux.HandleFunc("/v1/metrics", coll.HandleMetrics)
uiMux.HandleFunc("/v1/logs", coll.HandleLogs)
uiMux.HandleFunc("/health", coll.HandleHealth)
uiMux.HandleFunc("/api/history", handleHistory(coll))
uiMux.HandleFunc("/api/history/traces", handleHistoryByType(coll, "traces"))
uiMux.HandleFunc("/api/history/metrics", handleHistoryByType(coll, "metrics"))
uiMux.HandleFunc("/api/history/logs", handleHistoryByType(coll, "logs"))
uiMux.HandleFunc("/api/history/search", handleSearch(coll))
uiMux.HandleFunc("/api/history/wait", handleWait(coll))
uiMux.HandleFunc("/api/history", coll.HandleHistory)
uiMux.HandleFunc("/api/history/traces", coll.HandleHistoryByType("traces"))
uiMux.HandleFunc("/api/history/metrics", coll.HandleHistoryByType("metrics"))
uiMux.HandleFunc("/api/history/logs", coll.HandleHistoryByType("logs"))
uiMux.HandleFunc("/api/history/search", coll.HandleSearch)
uiMux.HandleFunc("/api/history/wait", coll.HandleWait)
uiMux.HandleFunc("/ws", wsServer.HandleConnections)
uiMux.Handle("/", spaHandler(http.Dir("./dist")))
@@ -222,263 +223,6 @@ func main() {
log.Fatalf("failed to start HTTP server: %v", err)
}
}
func handleTraces(coll *collector.Collector) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Printf("Received traces request: method=%s, content-type=%s", r.Method, r.Header.Get("Content-Type"))
if r.Method != http.MethodPost {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
body, err := io.ReadAll(r.Body)
if err != nil {
log.Printf("failed to read request body: %v", err)
http.Error(w, "failed to read request body", http.StatusInternalServerError)
return
}
log.Printf("Received traces body, size=%d", len(body))
var unmarshaler ptrace.Unmarshaler
contentType := r.Header.Get("Content-Type")
switch contentType {
case "application/x-protobuf":
unmarshaler = &ptrace.ProtoUnmarshaler{}
case "application/json":
unmarshaler = &ptrace.JSONUnmarshaler{}
default:
unmarshaler = &ptrace.JSONUnmarshaler{} // default to JSON
}
traces, err := unmarshaler.UnmarshalTraces(body)
if err != nil {
log.Printf("failed to unmarshal traces: %v", err)
http.Error(w, "failed to unmarshal traces", http.StatusBadRequest)
return
}
log.Printf("Successfully unmarshaled traces, span_count=%d", traces.SpanCount())
if err := coll.ConsumeTraces(context.Background(), traces); err != nil {
log.Printf("failed to consume traces: %v", err)
http.Error(w, "failed to consume traces", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
}
func handleMetrics(coll *collector.Collector) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
body, err := io.ReadAll(r.Body)
if err != nil {
log.Printf("failed to read request body: %v", err)
http.Error(w, "failed to read request body", http.StatusInternalServerError)
return
}
var unmarshaler pmetric.Unmarshaler
contentType := r.Header.Get("Content-Type")
switch contentType {
case "application/x-protobuf":
unmarshaler = &pmetric.ProtoUnmarshaler{}
case "application/json":
unmarshaler = &pmetric.JSONUnmarshaler{}
default:
unmarshaler = &pmetric.JSONUnmarshaler{} // default to JSON
}
metrics, err := unmarshaler.UnmarshalMetrics(body)
if err != nil {
log.Printf("failed to unmarshal metrics: %v", err)
http.Error(w, "failed to unmarshal metrics", http.StatusBadRequest)
return
}
if err := coll.ConsumeMetrics(context.Background(), metrics); err != nil {
log.Printf("failed to consume metrics: %v", err)
http.Error(w, "failed to consume metrics", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
}
func handleLogs(coll *collector.Collector) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
body, err := io.ReadAll(r.Body)
if err != nil {
log.Printf("failed to read request body: %v", err)
http.Error(w, "failed to read request body", http.StatusInternalServerError)
return
}
var unmarshaler plog.Unmarshaler
contentType := r.Header.Get("Content-Type")
switch contentType {
case "application/x-protobuf":
unmarshaler = &plog.ProtoUnmarshaler{}
case "application/json":
unmarshaler = &plog.JSONUnmarshaler{}
default:
unmarshaler = &plog.JSONUnmarshaler{} // default to JSON
}
logs, err := unmarshaler.UnmarshalLogs(body)
if err != nil {
log.Printf("failed to unmarshal logs: %v", err)
http.Error(w, "failed to unmarshal logs", http.StatusBadRequest)
return
}
if err := coll.ConsumeLogs(context.Background(), logs); err != nil {
log.Printf("failed to consume logs: %v", err)
http.Error(w, "failed to consume logs", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
}
func handleHistory(coll *collector.Collector) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
history := coll.GetHistory()
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(history); err != nil {
log.Printf("failed to encode history: %v", err)
http.Error(w, "failed to encode history", http.StatusInternalServerError)
return
}
}
}
func handleSearch(coll *collector.Collector) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
query := r.URL.Query().Get("q")
limitStr := r.URL.Query().Get("limit")
limit := 100
if limitStr != "" {
if l, err := strconv.Atoi(limitStr); err == nil {
limit = l
}
}
history, err := coll.SearchHistory(r.Context(), query, limit)
if err != nil {
log.Printf("failed to search history: %v", err)
http.Error(w, "failed to search history", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(history); err != nil {
log.Printf("failed to encode search results: %v", err)
http.Error(w, "failed to encode history", http.StatusInternalServerError)
return
}
}
}
func handleWait(coll *collector.Collector) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
query := r.URL.Query().Get("q")
timeoutSec := 30
if t := r.URL.Query().Get("timeout"); t != "" {
if ts, err := strconv.Atoi(t); err == nil {
timeoutSec = ts
}
}
ctx, cancel := context.WithTimeout(r.Context(), time.Duration(timeoutSec)*time.Second)
defer cancel()
// First, check if result already exists in recent history
matches, err := coll.SearchHistory(ctx, query, 1)
if err == nil && len(matches) > 0 {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(matches[0])
return
}
// Otherwise, subscribe and wait
lowerQuery := strings.ToLower(query)
ch, cleanup := coll.Subscribe(func(entry websocket.HistoryEntry) bool {
if query == "" {
return true
}
data, _ := json.Marshal(entry.Data)
return strings.Contains(strings.ToLower(string(data)), lowerQuery)
})
defer cleanup()
select {
case entry := <-ch:
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(entry); err != nil {
log.Printf("failed to encode signal: %v", err)
}
case <-ctx.Done():
http.Error(w, "timeout waiting for signal", http.StatusGatewayTimeout)
}
}
}
func handleHistoryByType(coll *collector.Collector, dataType string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
history := coll.GetHistory()
// Filter by type
var filteredHistory []websocket.HistoryEntry
for _, entry := range history {
if string(entry.Type) == dataType {
filteredHistory = append(filteredHistory, entry)
}
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(filteredHistory); err != nil {
log.Printf("failed to encode filtered history: %v", err)
http.Error(w, "failed to encode history", http.StatusInternalServerError)
return
}
}
}
func spaHandler(root http.FileSystem) http.Handler {
fileServer := http.FileServer(root)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {