Align origin/main with upstream/main (GitHub). The two branches diverged due to pre-rebase vs post-rebase merge commits for the k8s-amd64-dockerfiles feature. Includes: multi-stage Go compilation Dockerfiles, pan-bolt inventory features, CRM relations, Excel import tooling, proto/gRPC updates, migration scripts, and tools/ directory. Excludes deploy/bin/ pre-compiled binaries (arm64, not needed for amd64 K3s cluster; builds use multi-stage Dockerfiles now).
142 lines
4.7 KiB
Go
142 lines
4.7 KiB
Go
package metrics
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
const namespace = "muyu"
|
|
const subsystem = "inventory"
|
|
|
|
// ---- Business Counters ----
|
|
|
|
var ProductCreatedTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
|
|
Namespace: namespace, Subsystem: subsystem,
|
|
Name: "product_created_total",
|
|
Help: "Total number of products created.",
|
|
}, []string{"tenant_id", "product_name", "spec", "color"})
|
|
|
|
var ProductDeletedTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
|
|
Namespace: namespace, Subsystem: subsystem,
|
|
Name: "product_deleted_total",
|
|
Help: "Total number of products deleted (soft delete).",
|
|
}, []string{"tenant_id"})
|
|
|
|
var ImportOperationsTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
|
|
Namespace: namespace, Subsystem: subsystem,
|
|
Name: "import_operations_total",
|
|
Help: "Total number of import operations executed.",
|
|
}, []string{"tenant_id", "operator"})
|
|
|
|
var ImportProductsTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
|
|
Namespace: namespace, Subsystem: subsystem,
|
|
Name: "import_products_total",
|
|
Help: "Total number of products processed during imports.",
|
|
}, []string{"tenant_id", "result"})
|
|
|
|
var InboundLengthMetersTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
|
|
Namespace: namespace, Subsystem: subsystem,
|
|
Name: "inbound_length_meters_total",
|
|
Help: "Total length (meters) of fabric added to inventory.",
|
|
}, []string{"tenant_id", "product_name", "color", "operation"})
|
|
|
|
var InboundBoltsTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
|
|
Namespace: namespace, Subsystem: subsystem,
|
|
Name: "inbound_bolts_total",
|
|
Help: "Total number of bolt records added to inventory.",
|
|
}, []string{"tenant_id", "operation"})
|
|
|
|
var PansSavedTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
|
|
Namespace: namespace, Subsystem: subsystem,
|
|
Name: "pans_saved_total",
|
|
Help: "Total number of pan-save operations.",
|
|
}, []string{"tenant_id", "product_id"})
|
|
|
|
var StockCheckCreatedTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
|
|
Namespace: namespace, Subsystem: subsystem,
|
|
Name: "stock_check_created_total",
|
|
Help: "Total number of stock check records created.",
|
|
}, []string{"tenant_id"})
|
|
|
|
var StockCheckConfirmedTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
|
|
Namespace: namespace, Subsystem: subsystem,
|
|
Name: "stock_check_confirmed_total",
|
|
Help: "Total number of stock checks confirmed.",
|
|
}, []string{"tenant_id"})
|
|
|
|
var StockAdjustCreatedTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
|
|
Namespace: namespace, Subsystem: subsystem,
|
|
Name: "stock_adjust_created_total",
|
|
Help: "Total number of stock adjustments created.",
|
|
}, []string{"tenant_id", "reason"})
|
|
|
|
var StockAdjustApprovedTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
|
|
Namespace: namespace, Subsystem: subsystem,
|
|
Name: "stock_adjust_approved_total",
|
|
Help: "Total number of stock adjustments approved/rejected.",
|
|
}, []string{"tenant_id", "action"})
|
|
|
|
var StockAdjustQuantityMeters = prometheus.NewCounterVec(prometheus.CounterOpts{
|
|
Namespace: namespace, Subsystem: subsystem,
|
|
Name: "stock_adjust_quantity_meters_total",
|
|
Help: "Total adjusted quantity in meters (absolute value, split by direction).",
|
|
}, []string{"tenant_id", "direction"})
|
|
|
|
// ---- Histograms ----
|
|
|
|
var ImportBatchSize = prometheus.NewHistogramVec(prometheus.HistogramOpts{
|
|
Namespace: namespace, Subsystem: subsystem,
|
|
Name: "import_batch_size",
|
|
Help: "Number of products per import operation.",
|
|
Buckets: []float64{1, 5, 10, 50, 100, 200, 500, 1000},
|
|
}, []string{"tenant_id"})
|
|
|
|
// ---- RPC Operational Metrics ----
|
|
|
|
var RPCDurationSeconds = prometheus.NewHistogramVec(prometheus.HistogramOpts{
|
|
Namespace: namespace, Subsystem: subsystem,
|
|
Name: "rpc_duration_seconds",
|
|
Help: "Duration of RPC calls in seconds.",
|
|
Buckets: prometheus.DefBuckets,
|
|
}, []string{"method"})
|
|
|
|
var RPCRequestsTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
|
|
Namespace: namespace, Subsystem: subsystem,
|
|
Name: "rpc_requests_total",
|
|
Help: "Total number of RPC requests.",
|
|
}, []string{"method", "code"})
|
|
|
|
func init() {
|
|
prometheus.MustRegister(
|
|
ProductCreatedTotal,
|
|
ProductDeletedTotal,
|
|
ImportOperationsTotal,
|
|
ImportProductsTotal,
|
|
InboundLengthMetersTotal,
|
|
InboundBoltsTotal,
|
|
PansSavedTotal,
|
|
StockCheckCreatedTotal,
|
|
StockCheckConfirmedTotal,
|
|
StockAdjustCreatedTotal,
|
|
StockAdjustApprovedTotal,
|
|
StockAdjustQuantityMeters,
|
|
ImportBatchSize,
|
|
RPCDurationSeconds,
|
|
RPCRequestsTotal,
|
|
)
|
|
}
|
|
|
|
func StartHTTP(addr string) {
|
|
mux := http.NewServeMux()
|
|
mux.Handle("/metrics", promhttp.Handler())
|
|
go func() {
|
|
logx.Infof("prometheus metrics listening on %s", addr)
|
|
if err := http.ListenAndServe(addr, mux); err != nil {
|
|
logx.Errorf("prometheus http server error: %v", err)
|
|
}
|
|
}()
|
|
}
|