fix
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
// Package collector implements an OpenTelemetry consumer that stores telemetry in history
|
||||
// and broadcasts it to connected clients.
|
||||
package collector
|
||||
|
||||
import (
|
||||
@@ -18,6 +20,7 @@ import (
|
||||
"go.opentelemetry.io/collector/pdata/ptrace"
|
||||
)
|
||||
|
||||
// Collector handles the collection, storage and broadcasting of telemetry data.
|
||||
type Collector struct {
|
||||
broadcaster websocket.Broadcaster
|
||||
historySize int
|
||||
@@ -28,6 +31,7 @@ type Collector struct {
|
||||
subMutex sync.Mutex
|
||||
}
|
||||
|
||||
// New creates a new Collector instance.
|
||||
func New(broadcaster websocket.Broadcaster, historySize int, store storage.Store) *Collector {
|
||||
return &Collector{
|
||||
broadcaster: broadcaster,
|
||||
@@ -38,6 +42,7 @@ func New(broadcaster websocket.Broadcaster, historySize int, store storage.Store
|
||||
}
|
||||
}
|
||||
|
||||
// SetBroadcaster updates the broadcaster used by the collector.
|
||||
func (c *Collector) SetBroadcaster(broadcaster websocket.Broadcaster) {
|
||||
c.broadcaster = broadcaster
|
||||
}
|
||||
@@ -82,6 +87,8 @@ func (c *Collector) notifySubscribers(entry websocket.HistoryEntry) {
|
||||
}
|
||||
}
|
||||
|
||||
// Subscribe adds a new history subscriber with the given filter.
|
||||
// It returns a channel for receiving filtered history entries and a cleanup function.
|
||||
func (c *Collector) Subscribe(filter func(websocket.HistoryEntry) bool) (chan websocket.HistoryEntry, func()) {
|
||||
ch := make(chan websocket.HistoryEntry, 1)
|
||||
c.subMutex.Lock()
|
||||
@@ -98,6 +105,7 @@ func (c *Collector) Subscribe(filter func(websocket.HistoryEntry) bool) (chan we
|
||||
return ch, cleanup
|
||||
}
|
||||
|
||||
// GetHistory returns the last 'limit' history entries.
|
||||
func (c *Collector) GetHistory(limit int) []websocket.HistoryEntry {
|
||||
if limit <= 0 {
|
||||
limit = c.historySize
|
||||
@@ -121,6 +129,7 @@ func (c *Collector) GetHistory(limit int) []websocket.HistoryEntry {
|
||||
return history
|
||||
}
|
||||
|
||||
// SearchHistory searches through stored telemetry data using the provided query.
|
||||
func (c *Collector) SearchHistory(ctx context.Context, query string, limit int) ([]websocket.HistoryEntry, error) {
|
||||
if limit <= 0 {
|
||||
limit = 50
|
||||
@@ -162,6 +171,7 @@ func (c *Collector) SearchHistory(ctx context.Context, query string, limit int)
|
||||
return filtered, nil
|
||||
}
|
||||
|
||||
// ConsumeTraces implements the consumer.Traces interface.
|
||||
func (c *Collector) ConsumeTraces(ctx context.Context, td ptrace.Traces) error {
|
||||
marshaler := &ptrace.JSONMarshaler{}
|
||||
buf, err := marshaler.MarshalTraces(td)
|
||||
@@ -187,6 +197,7 @@ func (c *Collector) ConsumeTraces(ctx context.Context, td ptrace.Traces) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ConsumeMetrics implements the consumer.Metrics interface.
|
||||
func (c *Collector) ConsumeMetrics(ctx context.Context, md pmetric.Metrics) error {
|
||||
marshaler := &pmetric.JSONMarshaler{}
|
||||
buf, err := marshaler.MarshalMetrics(md)
|
||||
@@ -212,6 +223,7 @@ func (c *Collector) ConsumeMetrics(ctx context.Context, md pmetric.Metrics) erro
|
||||
return nil
|
||||
}
|
||||
|
||||
// ConsumeLogs implements the consumer.Logs interface.
|
||||
func (c *Collector) ConsumeLogs(ctx context.Context, ld plog.Logs) error {
|
||||
marshaler := &plog.JSONMarshaler{}
|
||||
buf, err := marshaler.MarshalLogs(ld)
|
||||
@@ -238,7 +250,7 @@ func (c *Collector) ConsumeLogs(ctx context.Context, ld plog.Logs) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Implement consumer interfaces
|
||||
// Capabilities returns the capabilities of the collector.
|
||||
func (c *Collector) Capabilities() consumer.Capabilities {
|
||||
return consumer.Capabilities{MutatesData: false}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
// Package collector implements an OpenTelemetry consumer that stores telemetry in history
|
||||
// and broadcasts it to connected clients.
|
||||
package collector
|
||||
|
||||
import (
|
||||
@@ -16,6 +18,7 @@ import (
|
||||
"go.opentelemetry.io/collector/pdata/ptrace"
|
||||
)
|
||||
|
||||
// HandleTraces handles OTLP trace requests.
|
||||
func (c *Collector) HandleTraces(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 {
|
||||
@@ -61,6 +64,7 @@ func (c *Collector) HandleTraces(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
// HandleMetrics handles OTLP metric requests.
|
||||
func (c *Collector) HandleMetrics(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
@@ -101,6 +105,7 @@ func (c *Collector) HandleMetrics(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
// HandleLogs handles OTLP log requests.
|
||||
func (c *Collector) HandleLogs(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
@@ -141,6 +146,7 @@ func (c *Collector) HandleLogs(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
// HandleHistory returns the telemetry history as JSON.
|
||||
func (c *Collector) HandleHistory(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
@@ -165,6 +171,7 @@ func (c *Collector) HandleHistory(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
// HandleSearch searches the telemetry history.
|
||||
func (c *Collector) HandleSearch(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
@@ -195,6 +202,7 @@ func (c *Collector) HandleSearch(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
// HandleWait waits for a specific telemetry signal.
|
||||
func (c *Collector) HandleWait(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
@@ -216,7 +224,9 @@ func (c *Collector) HandleWait(w http.ResponseWriter, r *http.Request) {
|
||||
matches, err := c.SearchHistory(ctx, query, 1)
|
||||
if err == nil && len(matches) > 0 {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(matches[0])
|
||||
if err := json.NewEncoder(w).Encode(matches[0]); err != nil {
|
||||
log.Printf("failed to encode match: %v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -242,6 +252,7 @@ func (c *Collector) HandleWait(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
// HandleHistoryByType returns telemetry history filtered by type.
|
||||
func (c *Collector) HandleHistoryByType(dataType string) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
@@ -283,8 +294,11 @@ func (c *Collector) HandleHistoryByType(dataType string) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
// HandleHealth returns the health status of the collector.
|
||||
func (c *Collector) HandleHealth(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
|
||||
if err := json.NewEncoder(w).Encode(map[string]string{"status": "ok"}); err != nil {
|
||||
log.Printf("failed to encode health check: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user