-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(telemetry): Move Prometheus metrics to dedicated package and…
… add pprof debug
- Loading branch information
Showing
7 changed files
with
192 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// debug.go: pprof debug routes and helpers for telemetry package | ||
package telemetry | ||
|
||
import ( | ||
"net/http" | ||
"net/http/pprof" | ||
) | ||
|
||
const debugPath = "/debug/pprof/" | ||
|
||
// RegisterDebugHandlers adds pprof debugging routes to the provided mux | ||
func RegisterDebugHandlers(mux *http.ServeMux) { | ||
mux.HandleFunc(debugPath, pprof.Index) | ||
mux.HandleFunc(debugPath+"cmdline", pprof.Cmdline) | ||
mux.HandleFunc(debugPath+"profile", pprof.Profile) | ||
mux.HandleFunc(debugPath+"symbol", pprof.Symbol) | ||
mux.HandleFunc(debugPath+"trace", pprof.Trace) | ||
mux.Handle(debugPath+"allocs", pprof.Handler("allocs")) | ||
mux.Handle(debugPath+"goroutine", pprof.Handler("goroutine")) | ||
mux.Handle(debugPath+"heap", pprof.Handler("heap")) | ||
mux.Handle(debugPath+"threadcreate", pprof.Handler("threadcreate")) | ||
mux.Handle(debugPath+"block", pprof.Handler("block")) | ||
mux.Handle(debugPath+"mutex", pprof.Handler("mutex")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// endpoint.go: Prohmetheus compatible telemetry endpoint | ||
package telemetry | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"sync" | ||
"time" | ||
|
||
"github.com/tphakala/birdnet-go/internal/conf" | ||
) | ||
|
||
// Endpoint handles all operations related to Prometehus compatible telemetry | ||
type Endpoint struct { | ||
server *http.Server | ||
ListenAddress string | ||
} | ||
|
||
// New creates a new instance of telemetry Endpoint | ||
func NewEndpoint(settings *conf.Settings) (*Endpoint, error) { | ||
if !settings.Realtime.Telemetry.Enabled { | ||
return nil, fmt.Errorf("metrics not enabled") | ||
} | ||
|
||
return &Endpoint{ | ||
ListenAddress: settings.Realtime.Telemetry.Listen, | ||
}, nil | ||
} | ||
|
||
// Start the HTTP server for telemetry endpoint and listen for the quit signal to shut down. | ||
func (e *Endpoint) Start(metrics *Metrics, wg *sync.WaitGroup, quitChan <-chan struct{}) { | ||
mux := http.NewServeMux() | ||
RegisterMetricsHandlers(mux) // Registering metrics handlers | ||
RegisterDebugHandlers(mux) // Registering debug handlers | ||
|
||
e.server = &http.Server{ | ||
Addr: e.ListenAddress, | ||
Handler: mux, | ||
} | ||
|
||
// Run the server in a separate goroutine so that it doesn't block. | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
log.Printf("Telemetry endpoint starting at %s", e.ListenAddress) | ||
if err := e.server.ListenAndServe(); err != nil && err != http.ErrServerClosed { | ||
log.Fatalf("Failed to start telemetry HTTP server at %s: %v", e.ListenAddress, err) | ||
} | ||
}() | ||
|
||
// Listen for quit signal | ||
go func() { | ||
<-quitChan | ||
log.Println("Quit signal received, stopping telemetry server.") | ||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
if err := e.server.Shutdown(ctx); err != nil { | ||
log.Printf("Failed to shutdown telemetry server gracefully: %v", err) | ||
} | ||
}() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// metrics.go: Prometheus metrics setup and manipulation for telemetry | ||
package telemetry | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
) | ||
|
||
type Metrics struct { | ||
DetectionCounter *prometheus.CounterVec | ||
// Additional metrics can be added here | ||
} | ||
|
||
const metricsPath = "/metrics" | ||
|
||
// NewMetrics initializes and registers all Prometheus metrics used in the telemetry system. | ||
func NewMetrics() (*Metrics, error) { | ||
metrics := &Metrics{} | ||
|
||
// Setup DetectionCounter | ||
counterOpts := prometheus.CounterOpts{ | ||
Name: "birdnet_detections", | ||
Help: "Counts of BirdNET detections partitioned by common name.", | ||
} | ||
labels := []string{"name"} | ||
metrics.DetectionCounter = prometheus.NewCounterVec(counterOpts, labels) | ||
|
||
if err := prometheus.Register(metrics.DetectionCounter); err != nil { | ||
return nil, err | ||
} | ||
|
||
// Additional metrics can be initialized here | ||
|
||
return metrics, nil | ||
} | ||
|
||
// RegisterMetricsHandlers adds metrics routes to the provided mux | ||
func RegisterMetricsHandlers(mux *http.ServeMux) { | ||
mux.Handle(metricsPath, promhttp.Handler()) | ||
} | ||
|
||
// IncrementDetectionCounter increments the detection counter for a given species | ||
func (m *Metrics) IncrementDetectionCounter(speciesName string) { | ||
m.DetectionCounter.WithLabelValues(speciesName).Inc() | ||
} |