feat: build vehicle data platform and production pipeline
This commit is contained in:
@@ -4,12 +4,16 @@ import (
|
||||
"fmt"
|
||||
"math"
|
||||
"net/http"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var processStartedAt = time.Now()
|
||||
|
||||
type Labels map[string]string
|
||||
|
||||
type Registry struct {
|
||||
@@ -33,6 +37,56 @@ func NewRegistry() *Registry {
|
||||
}
|
||||
}
|
||||
|
||||
func RegisterServiceInfo(registry *Registry, service string) {
|
||||
if registry == nil {
|
||||
return
|
||||
}
|
||||
service = strings.TrimSpace(service)
|
||||
if service == "" {
|
||||
service = "vehicle-service"
|
||||
}
|
||||
registry.SetGauge("vehicle_service_info", Labels{"service": service}, 1)
|
||||
RecordProcessRuntime(registry)
|
||||
}
|
||||
|
||||
func RecordProcessRuntime(registry *Registry) {
|
||||
if registry == nil {
|
||||
return
|
||||
}
|
||||
var mem runtime.MemStats
|
||||
runtime.ReadMemStats(&mem)
|
||||
registry.SetGauge("vehicle_process_start_time_unix_seconds", nil, float64(processStartedAt.Unix()))
|
||||
registry.SetGauge("vehicle_process_uptime_seconds", nil, time.Since(processStartedAt).Seconds())
|
||||
registry.SetGauge("vehicle_process_heap_alloc_bytes", nil, float64(mem.HeapAlloc))
|
||||
registry.SetGauge("vehicle_process_heap_sys_bytes", nil, float64(mem.HeapSys))
|
||||
registry.SetGauge("vehicle_process_goroutines", nil, float64(runtime.NumGoroutine()))
|
||||
}
|
||||
|
||||
func RegisterKafkaConsumerInfo(registry *Registry, service string, group string, topics []string) {
|
||||
if registry == nil {
|
||||
return
|
||||
}
|
||||
service = strings.TrimSpace(service)
|
||||
if service == "" {
|
||||
service = "vehicle-service"
|
||||
}
|
||||
group = strings.TrimSpace(group)
|
||||
if group == "" {
|
||||
return
|
||||
}
|
||||
for _, topic := range topics {
|
||||
topic = strings.TrimSpace(topic)
|
||||
if topic == "" {
|
||||
continue
|
||||
}
|
||||
registry.SetGauge("vehicle_kafka_consumer_info", Labels{
|
||||
"service": service,
|
||||
"group": group,
|
||||
"topic": topic,
|
||||
}, 1)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Registry) IncCounter(name string, labels Labels) {
|
||||
r.AddCounter(name, labels, 1)
|
||||
}
|
||||
@@ -90,6 +144,13 @@ func (r *Registry) SetKafkaLag(name string, topic string, partition int, offset
|
||||
}, float64(lag))
|
||||
}
|
||||
|
||||
func RecordLastActivity(registry *Registry, name string, labels Labels) {
|
||||
if registry == nil {
|
||||
return
|
||||
}
|
||||
registry.SetGauge(name, labels, float64(time.Now().Unix()))
|
||||
}
|
||||
|
||||
func (r *Registry) ObserveHistogram(name string, labels Labels, buckets []float64, value float64) {
|
||||
name = strings.TrimSpace(name)
|
||||
if r == nil || name == "" {
|
||||
@@ -265,6 +326,7 @@ func NewHandler(registry *Registry) http.Handler {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
RecordProcessRuntime(registry)
|
||||
w.Header().Set("Content-Type", "text/plain; version=0.0.4; charset=utf-8")
|
||||
_, _ = w.Write([]byte(registry.Render()))
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user